Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

I2C LCD PCF8574 Adapter Library

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 237
Location: Japan

japan.gif
PostPosted: Thu Mar 25, 2021 9:44 am    Post subject: I2C LCD PCF8574 Adapter Library Reply with quote

'lcd_i2c.LIB' and 'bl_lcd_i2c.LIB' have been re-edited for the commercially available PCF8574 I2C LCD adapter board.

You can control multiple LCDs [up to 8] by changing the slave address of PCF8574.
Software I2C and TWI can be changed by [I2c_select].

Circuit diagram
https://drive.google.com/file/d/16oR1toL40Z3vEJHSHsyoLEHTDxihpRLn/view?usp=sharing

Adapter board
https://drive.google.com/file/d/1abm7OaR63MSxJyDIUDMqgCTbxSpUu5zW/view?usp=sharing
https://drive.google.com/file/d/1iT7vKscLrOTqLwyzkuPIuUcTJFoQGDim/view?usp=sharing
https://drive.google.com/file/d/1Wj1bvnkUgL8jYgW72P1cbYl9sbrN6bTz/view?usp=sharing
Code:
$programmer = 22                                            'ARDUINO (using stk500v1 protocol)

   '
   '  **********************************
   '  *  PCF8574 I2C LCD Adapter test  *
   '  *                  2021/ 3/24    *
   '  **********************************
   '

$regfile = "m328pdef.dat"                                   'Set the AVR to use.
$crystal = 16000000                                         'Set the AVR clock.
   '
$hwstack = 64                                               'Set the capacity of the hardware stack.
$swstack = 10                                               'Set the capacity of the software stack.
$framesize = 24                                             'Set the capacity of the frame area.

   '
   '  * PCF8574 I2C LCD Adapter settings *
   '
   Const I2c_select = 1                                     '0:Software I2C , 1:TWI
#if I2c_select = 0
   '------[For software I2C]------
   Config I2cdelay = 10                                     'SCL clock frequency = approx. 42KHz. (At AVR clock 16MHz) (* Maximum 100KHz)
   Config Scl = Portd.2                                     'Set the port pin to connect the SCL line of the I2C bus.
   Config Sda = Portd.3                                     'Set the port pin to connect the SDA line of the I2C bus.
   I2cinit                                                  'Initialize the SCL and SDA lines of the I2C bus.
   '-------------------------------
#else
   '------[For TWI]------------------
   $lib "i2c_twi.lib"                                       'Incorporate the hardware I2C/TWI library.
   Config Twi = 100000                                      'I2C bus clock = 100KHz
   Config Scl = Portc.5                                     'You must specify the SCL pin name.
   Config Sda = Portc.4                                     'You must specify the SDA pin name.
   I2cinit                                                  'Initialize the SCL and SDA lines of the I2C bus.
   '-------------------------------
#endif

   Dim Pcf8574_lcd As Byte : Pcf8574_lcd = &H4E             'PCF8574 slave address. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Dim Backlight As Byte : Backlight = 1                    'LCD backlight control. (0: off, 1: on)
   $lib "lcd_i2c_PCF8574.LIB"                               'Incorporate the library of I2C LCD PCF8574 Adapter.
   Config Lcd = 20x4                                        'Set the LCD to 20 characters and 4 lines.
   Initlcd                                                  'Initialize the LCD.


   '
   '  ****************
   '  * Display test *
   '  ****************
   '
   Locate 1 , 1                                             'Display of title.
   Lcd "PCF8574"
   '
   Locate 2 , 2
   Lcd "I2C LCD Adapter"
   '
   Deflcdchar 2 , &H02 , &H04 , &H0C , &H1E , &H0F , &H06 , &H04 , &H08       'Write the custom character [Lightning] to the LCD.
   Locate 1 , 15                                            'Display custom characters.
   Lcd Chr(2) ; "1"
   '
   Locate 1 , 9                                             'Display the slave address of PCF8574.
   Lcd "[" ; Hex(pcf8574_lcd) ; "]"
   Wait 3
   '
   Backlight = 0                                            'Turn off the LCD backlight.
   Locate 1 , 1                                             'Send a dummy command to control the backlight.
   Wait 2
   Backlight = 1                                            'Turn on the LCD backlight.
   Locate 1 , 1                                             'Send a dummy command to control the backlight.
   '
   Locate 2 , 16                                            'Display the cursor.
   Cursor On , Blink

   End


Last edited by O-Family on Tue May 04, 2021 2:15 am; edited 2 times in total
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 237
Location: Japan

japan.gif
PostPosted: Thu Mar 25, 2021 9:52 am    Post subject: Reply with quote

Multiple LCD codes.
Code:

$programmer = 22                                            'ARDUINO (using stk500v1 protocol)

   '
   '  *************************************
   '  *  PCF8574 I2C LCD Adapter test     *
   '  *    For multiple LCDs   2021/ 3/24 *
   '  *************************************
   '

$regfile = "m328pdef.dat"                                   'Set the AVR to use.
$crystal = 16000000                                         'Set the AVR clock.
   '
$hwstack = 64                                               'Set the capacity of the hardware stack.
$swstack = 10                                               'Set the capacity of the software stack.
$framesize = 24                                             'Set the capacity of the frame area.

   '
   '  * PCF8574 I2C LCD Adapter settings *
   '
   Const I2c_select = 1                                     '0:Software I2C , 1:TWI
#if I2c_select = 0
   '------[For software I2C]------
   Config I2cdelay = 10                                     'SCL clock frequency = approx. 42KHz. (At AVR clock 16MHz) (* Maximum 100KHz)
   Config Scl = Portd.2                                     'Set the port pin to connect the SCL line of the I2C bus.
   Config Sda = Portd.3                                     'Set the port pin to connect the SDA line of the I2C bus.
   I2cinit                                                  'Initialize the SCL and SDA lines of the I2C bus.
   '-------------------------------
#else
   '------[For TWI]------------------
   $lib "i2c_twi.lib"                                       'Incorporate the hardware I2C/TWI library.
   Config Twi = 100000                                      'I2C bus clock = 100KHz
   Config Scl = Portc.5                                     'You must specify the SCL pin name.
   Config Sda = Portc.4                                     'You must specify the SDA pin name.
   I2cinit                                                  'Initialize the SCL and SDA lines of the I2C bus.
   '-------------------------------
#endif

   Dim Pcf8574_lcd As Byte : Pcf8574_lcd = &H4E             'PCF8574 slave address. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Dim Backlight As Byte : Backlight = 1                    'LCD backlight control. (0: off, 1: on)
   $lib "lcd_i2c_PCF8574.LIB"                               'Incorporate the library of I2C LCD PCF8574 Adapter.
   Config Lcd = 20x4                                        'Set the LCD to 20 characters and 4 lines.
   Initlcd                                                  'Initialize the LCD.

   '
   '  * When installing the second and subsequent LCDs  *
   '
   Pcf8574_lcd = &H4C                                       'The slave address of the second PCF8574. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Initlcd                                                  'Initialize the second LCD.
   '
   Pcf8574_lcd = &H4A                                       'The slave address of the third PCF8574. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Initlcd                                                  'Initialize the third LCD.


   '
   '  ****************
   '  * Display test *
   '  ****************
   '
   Pcf8574_lcd = &H4E                                       'Specify the first LCD.
   '
   Locate 1 , 1                                             'Display of title.
   Lcd "PCF8574"
   '
   Locate 2 , 2
   Lcd "I2C LCD Adapter"
   '
   Deflcdchar 2 , &H02 , &H04 , &H0C , &H1E , &H0F , &H06 , &H04 , &H08       'Write the custom character [Lightning] to the LCD.
   Locate 1 , 15                                            'Display custom characters.
   Lcd Chr(2) ; "1"
   '
   Locate 1 , 9                                             'Display the slave address of PCF8574.
   Lcd "[" ; Hex(pcf8574_lcd) ; "]"

   '
   '  * Second LCD *
   '
   Pcf8574_lcd = &H4C                                       'Specify the second LCD.
   '
   Locate 1 , 1                                             'Display of title.
   Lcd "PCF8574"
   '
   Locate 2 , 2
   Lcd "I2C LCD Adapter"
   '
   Deflcdchar 3 , &H02 , &H04 , &H0C , &H1E , &H0F , &H06 , &H04 , &H08       'Write the custom character [Lightning] to the LCD.
   Locate 1 , 15                                            'Display custom characters.
   Lcd Chr(3) ; "2"
   '
   Locate 1 , 9                                             'Display the slave address of PCF8574.
   Lcd "[" ; Hex(pcf8574_lcd) ; "]"

   '
   '  * Third LCD *
   '
   Pcf8574_lcd = &H4A                                       'Specify the third LCD.
   '
   Locate 1 , 1                                             'Display of title.
   Lcd "PCF8574"
   '
   Locate 2 , 4
   Lcd "I2C LCD Adapter"
   '
   Deflcdchar 4 , &H02 , &H04 , &H0C , &H1E , &H0F , &H06 , &H04 , &H08       'Write the custom character [Lightning] to the LCD.
   Locate 1 , 19                                            'Display custom characters.
   Lcd Chr(4) ; "3"
   '
   Locate 1 , 9                                             'Display the slave address of PCF8574.
   Lcd "[" ; Hex(pcf8574_lcd) ; "]"
   '
   Locate 3 , 3
   Lcd "-- 3rd Line --"
   '
   Locate 4 , 4
   Lcd "20x4 Display "
   '
   Locate 4 , 20                                            'Display the cursor.
   Cursor On , Blink

   End
 
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5199
Location: Holland

blank.gif
PostPosted: Thu Mar 25, 2021 10:36 am    Post subject: Reply with quote

Hi O-Family,

A fantastic contribution Very Happy
Thank you for sharing this. You did the lib as it should have been done.

I will add the lib to the distribution too if you don't mind.

_________________
Mark
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 237
Location: Japan

japan.gif
PostPosted: Fri Mar 26, 2021 12:45 am    Post subject: Reply with quote

Hi,
Hope it helps many users!
Back to top
View user's profile Visit poster's website
aphawk

Bascom Member



Joined: 23 Jan 2010
Posts: 157
Location: Brazil

brazil.gif
PostPosted: Fri Mar 26, 2021 9:25 pm    Post subject: Reply with quote

Hi,

Only to inform that this new library has ceased all Proteus simulation problem that I have for some years .... now the display works for more than 10 minutes without any problem.
Tried in 3 different projects, all works perfect now !
The old library makes simulation erratic after one or two minutes...

Thanks for all people involved in this actualization !

Paulo
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 237
Location: Japan

japan.gif
PostPosted: Tue May 04, 2021 2:19 am    Post subject: Reply with quote

Fixed a bug when using USI.
Please reload the library.

I don't have the XTINY add-on, so I'm not sure if it works with XTINY.

Code:

$programmer = 16                                            'USBprog Programmer/ AVR ISP mkII (Atmel)

'$prog &HFF , &H62 , &HDF , &HFF                             'Fuse setting. (ATtiny85 factory shipping condition)

   '
   '  **********************************
   '  *  PCF8574 I2C LCD Adapter test  *
   '  *     (For USI)    2021/ 5/ 3    *
   '  **********************************
   '

$regfile = "ATtiny85.DAT"                                   'Set the AVR to use.
$crystal = 1000000                                          'Set the AVR clock.
   '
$hwstack = 40                                               'Set the capacity of the hardware stack.
$swstack = 10                                               'Set the capacity of the software stack.
$framesize = 24                                             'Set the capacity of the frame area.

   '
   '  * PCF8574 I2C LCD Adapter settings *
   '
   Const I2c_select = 1                                     '0:Software I2C , 1:USI
#if I2c_select = 0
   '------[For software I2C]------
'   $lib "i2cV2.LBX"                                         'Incorporate a modified version of the I2C library.
   Config I2cdelay = 10                                     'SCL clock frequency.  (*Up to 100KHz)
   Config Scl = Portb.2                                     'Set the port pin to connect the SCL line of the I2C bus.
   Config Sda = Portb.0                                     'Set the port pin to connect the SDA line of the I2C bus.
   I2cinit                                                  'Initialize the SCL and SDA lines of the I2C bus.
   '-------------------------------
#else
   '------[For USI]------------------
   Config Usi = Twimaster , Mode = Normal                   'USI master settings. (I2C bus clock = 100KHz)
   I2cinit                                                  'Initialize the SCL and SDA lines of the I2C bus.
   '-------------------------------
#endif

   Dim Pcf8574_lcd As Byte : Pcf8574_lcd = &H4E             'PCF8574 slave address. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Dim Backlight As Byte : Backlight = 1                    'LCD backlight control. (0: off, 1: on)
   $lib "lcd_i2c_PCF8574.LIB"                               'Incorporate the library of I2C LCD PCF8574 Adapter.
   Config Lcd = 20x4                                        'Set the LCD to 20 characters and 4 lines.
   Initlcd                                                  'Initialize the LCD.


   '
   '  ****************
   '  * Display test *
   '  ****************
   '
   Locate 1 , 1                                             'Display of title.
   Lcd "PCF8574"
   '
   Locate 2 , 2
   Lcd "I2C LCD Adapter"
   '
   Deflcdchar 2 , &H02 , &H04 , &H0C , &H1E , &H0F , &H06 , &H04 , &H08       'Write the custom character [Lightning] to the LCD.
   Locate 1 , 15                                            'Display custom characters.
   Lcd Chr(2) ; "1"
   '
   Locate 1 , 9                                             'Display the slave address of PCF8574.
   Lcd "[" ; Hex(pcf8574_lcd) ; "]"
   Wait 3
   '
   Backlight = 0                                            'Turn off the LCD backlight.
   Locate 1 , 1                                             'Send a dummy command to control the backlight.
   Wait 2
   Backlight = 1                                            'Turn on the LCD backlight.
   Locate 1 , 1                                             'Send a dummy command to control the backlight.
   '
   Locate 2 , 16                                            'Display the cursor.
   Cursor On , Blink

   End
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5199
Location: Holland

blank.gif
PostPosted: Tue May 04, 2021 9:21 am    Post subject: Reply with quote

thank you for the update. Very Happy
_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum