Menu

Unhelpful error message from AVR TWI configuration.

2018-01-28
2018-02-01
  • Tony Bolton

    Tony Bolton - 2018-01-28

    Using GCB Great Cow BASIC (0.98.01 2017-10-27) on Linux Mint 18.3

    When attempting to compile programs with this configuration this unhelpful error is produced..

    /opt/GCBASIC/include/lowlevel/hwi2c.h (508): Error: Cannot store -3 in the byte variable TWBR

    ;----- Configuration
      #chip mega8, 1
    '      Setup Hardware I2C
        HI2CMode Master
        #define HI2C_DATA
        #define HI2C_BAUD_RATE 100 ' or 400khz
    
      'Set up LCD
       #define LCD_IO 10
       #define LCD_I2C_Address_1 0x7E 
    

    The reason for the problem is alluded to in the Atmel data sheet where they suggest that for some TWI modes the CPU clock speed must be at least 16x the TWI baud rate. This change will clear the problem.

      #define HI2C_BAUD_RATE 62 
    

    I suggest you check if the 16x requirement is common in AVR processors and if so include a 16x test in hwi2c.h with an informative error message for unsuitable combinations.

    regards TB

     
    • Anobium

      Anobium - 2018-01-28

      Good spot. I was not of the 16x issue when I wrote the library a long time ago.

      So, I understand. If the clock speed is as used at 1 then this mutliplier is an issue.

      Can you point me to the bit in the datasheet? Should be resolvable.

       
  • Tony Bolton

    Tony Bolton - 2018-01-28

    The mega8 16x speed reference to slave mode is at the top of page 164 (Bit Rate Generator
    Unit) but if the same formula is used in master mode then the limitation is relevant.

    To use the 400khz TWI bus speed then the CPU must be faster than 6.4mhz

     
    • Anobium

      Anobium - 2018-01-28

      Cheers - you explaination is excellent.

      Download and place in the lowlevel directory an updated library.

      https://sourceforge.net/p/gcbasic/code/HEAD/tree/GCBASIC/trunk/include/lowlevel/hwi2c.h

      This library will issue a friendly warning and it will advise how to resolve the warning.

      Cheers,

      Evan

       

Log in to post a comment.