Menu

I2C Problem

Help
2015-08-25
2020-03-04
  • Fábio Almeida

    Fábio Almeida - 2015-08-25

    When it comes to I2CStop the program crashes. I can not see where is the problem.

    ~~~~~

    chip Mega328p, 16

    'Configurações I2C

    define I2C_MODE Master

    define I2C_DATA PORTB.1

    define I2C_CLOCK PORTB.2

    define MAD 130

    define VolumeLEFT 0

    define VolumeRIGHT 1

    define BASS 2

    define TREBLE 3

    define SFunctions 8

    define stereoCH1 198

    define stereoCH2 199

    define soundA1 194

    define soundA2 195

    define soundB1 196

    define soundB2 197

    define spatial 216

    define linear 208

    define pseudo 200

    define mono 192

    define Mute 224

    'Configurações Controle Remoto

    define IR_PINO PORTD.7

    define tecla_vol_up 18

    define tecla_vol_down 19

    'Variáveis
    Dim Tecla as Byte
    Dim Volume as word
    Dim Valor_BASS as Byte
    Dim Valor_TREBLE as Byte
    Dim IR_rem_width(12) as byte

    'Valores Iniciais
    Volume = 192
    Valor_BASS = 247
    Valor_TREBLE = 247

    'Inicio do Programa Principal

    EPRead 0, Volume

    Do
    IF IR_PINO = on then Leitura_controle
    Wait 100 ms
    Volume1 = volume
    If tecla = tecla_vol_up then volume = volume + 2
    If tecla = tecla_vol_down then volume = volume - 2
    If Volume =< 191 then volume = 192
    If Volume => 255 then volume = 254
    If Volume1 <> Volume then GoSub Enviar_Volume
    EPWrite 0, Volume
    tecla = 0
    Loop

    The program stop here

    Sub Enviar_Volume
    Pulseout PORTB.5, 10 ms
    wait 10 ms
    I2CStart
    I2CSend MAD
    I2CSend VolumeLEFT
    I2CSend Volume
    I2CStop
    Return

    ;-----------Program remote control OK
    Sub Leitura_controle (out IR_rem_dev as byte, out TECLA as byte)
    Do
    IR_rem_count = 0
    Do while IR_PINO = 0
    Wait 100 uS
    IR_rem_count++
    Loop
    Loop while IR_rem_count < 20
    For IR_rem_i = 1 to 12
    do
    IR_rem_count = 0
    Do while IR_PINO = 0
    Wait 100 uS
    IR_rem_count++
    Loop
    Loop while IR_rem_count < 4
    IR_rem_width(IR_rem_i) = IR_rem_count
    Next IR_rem_i
    TECLA = 0
    For IR_rem_i = 1 to 7
    TECLA = TECLA / 2
    If IR_rem_width(IR_rem_i) > 10 then
    TECLA = TECLA + 64
    End if
    Next
    IR_rem_dev = 0
    for IR_rem_i = 8 to 12
    IR_rem_dev = IR_rem_dev / 2
    if IR_rem_width( IR_rem_i ) > 10 then
    IR_rem_dev = IR_rem_dev + 16
    end if
    next
    end sub

     

    Last edit: Anobium 2015-08-26
  • Anobium

    Anobium - 2015-08-26

    Good day.

    When you say crash. What do you mean? During operation? Compiling?

     
  • Anobium

    Anobium - 2015-08-26

    I have assumed that this is a compilation issue as I found a few issues in the code.

    I have added interrupt handling, repaired the errors in the sub calls and removed the deprecated GOSUB.

    Try the attached.

     

    Last edit: Anobium 2015-08-26
  • Anobium

    Anobium - 2015-08-26

    Sorry, I spotted another error.

    No DIR was set for IR_PINO.

     
  • unkotarenagasi

    unkotarenagasi - 2020-01-29

    I2C [I2CSend] Bug
    AT-Tiny16A
    There is a bug in the i2c transmission command output by the compiler.
    Correction is required as follows.
    ;***********
    I2CTX:
    wait until I2C_CLOCK = OFF 'and just keep waiting for response
    ;I2C_DATA_LOW 'begin with SDA=0 as a baseline
    ' sbi DDRB,0
    *No required

    ' cbi PORTB,0 No required
    repeat 8 '8 data bits
    if I2CBYTE.7 = ON then 'put most significant bit on SDA line
    ;I2C_DATA_HIGH 'idle the SDA line again
    cbi DDRB,0
    else
    ;I2C_DATA_LOW
    sbi DDRB,0
    cbi PORTB,0
    end if
    rotate I2CBYTE Left ; Required [LEFT]
    wait until I2C_CLOCK = ON 'wait for SCL=1
    wait until I2C_CLOCK = OFF
    end repeat 'all 8 bits sent now
    ;I2C_DATA_HIGH 'idle the SDA line again
    cbi DDRB,0
    wait until I2C_CLOCK = ON 'wait for the 9th bit
    if I2C_DATA then 'read 9th bit in from Master
    I2CAck = FALSE 'return a NAK to program
    else
    I2CAck = TRUE 'else, return ACK if all okay
    end if
    wait until I2C_CLOCK = OFF 'let SCL go low
    Return
    ;**************
    Ex.
    I2CBYTE = BYTEDATA
    Gosub I2CTX

     
  • Anobium

    Anobium - 2020-01-29

    Hello,

    Have you tried the published public methods? Iike i2cstart, i2csend, i2cstop etc? You are down a level and writing ASM.

    What are you trying to achieve? This will help us guide you

     
  • unkotarenagasi

    unkotarenagasi - 2020-02-28

    Hello Anobium Thank you for your Help.
    I'm useing GCB v0.98.06
    I found I2C.h bug.
    C:\GCB@Syn\GreatCowBasic\Include\i2c.h

    Line 412 [rotate I2CByte]
    Fix it [rotate I2CByte Left]

    I found I2C-DATA noise by digital-osiloscope.
    Line 403 [I2C_DATA_LOW]
    Recomend fix it comment out or delete.

    that's all.
    Got at the highest speed I2C communication .

     
    • Anobium

      Anobium - 2020-02-28

      Thank you.

      Thanks for the update to the .h. I am thinking this was not spotted as the rotate is implied and you chip may not have LEFT implied.

      I have updated.

       
      • unkotarenagasi

        unkotarenagasi - 2020-03-03

        Hello Anobium Thank you for your Help.
        I'm useing GCB v0.98.06
        Terget chip is 16F57(Baseline)

        I2C-clock remains driven on the slave side.

         

        Last edit: unkotarenagasi 2020-03-04
        • Anobium

          Anobium - 2020-03-03

          Most odd.

          the software I2C is pretty robust code. Do you have the pull-up resistors fitted to the two lines?

           
          • unkotarenagasi

            unkotarenagasi - 2020-03-04

            Hello Anobium Thank you for your Help.

            As a result of further verification,
            Running out of working memory and running out of variable space
            It seems that the registers were broken.

             
  • stan cartwright

    stan cartwright - 2020-02-28

    I thought sub used end sub not return.

    Sub Enviar_Volume
    Pulseout PORTB.5, 10 ms
    wait 10 ms
    I2CStart
    I2CSend MAD
    I2CSend VolumeLEFT
    I2CSend Volume
    I2CStop
    Return

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.