Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

I2C LCD PCF8574 Adapter Library
Goto page 1, 2  Next
 
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: 257
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: 257
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: 5298
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: 257
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: 257
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: 5298
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
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 257
Location: Japan

japan.gif
PostPosted: Fri Jul 02, 2021 9:09 am    Post subject: Reply with quote

We were able to confirm operation with XTINY (ATmega4809).
Please use Ver.002 of "lcd_i2c_PCF8574.LIB".

Code:
$programmer = 26                                            'MCS UPDI programmer

   '
   '  **********************************
   '  *  PCF8574 I2C LCD Adapter test  *
   '  *         (For XMEGA and XTINY)  *
   '  *                  2021/ 7/ 1    *
   '  **********************************
   '

$regfile = "mx4809.dat"                                     'Set the AVR to use.
$crystal = 20000000                                         '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.

   Config Sysclock = 20mhz , Prescale = 1                   'Set the system clock.
   Config Com1 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8

   '
   '  * PCF8574 I2C LCD Adapter settings *
   '
   Const I2c_select = 0                                     '0:Software I2C , 1:TWI
   '
   Dim Twi_start As Byte                                    'Variables used in the TWI library.
   Const Twi_adr = Twi0_ctrla : Const Twi_ch = 1            'Set the TWI interface and TWI channel constants to be used.
   '
#if I2c_select = 1
   '------[For software I2C]------
   $forcesofti2c                                            'Force the use of the software I2C/TWI library.
   $lib "i2c.lbx"                                           'Overwrite the library for software I2C.
   Config I2cdelay = 10                                     'SCL clock frequency = approx. 42KHz. (At AVR clock 16MHz) (* Maximum 100KHz)
   Config Scl = Porta.5                                     'Set the port pin to connect the SCL line of the I2C bus.
   Config Sda = Porta.4                                     '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]------------------
   Config Twi0 = 100000                                     '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 *
   '  ****************
   '
   Do
      Cls                                                   'Clear all LCD displays.
      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
      Wait 3
      '
      Locate 2 , 1
      Lcd " For XMEGA&XTINY"
      Wait 3
   Loop

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

Bascom Member



Joined: 24 Sep 2007
Posts: 79
Location: Aachen

germany.gif
PostPosted: Sat Nov 06, 2021 11:51 am    Post subject: Reply with quote

Thx for the library.
I started compilation of the library Ver.002 dated 2021/05/13 with the LIB Manager tool and got following errors:
Code:

Error : 222   Line :   139   Illegal character [expected (, got ')' [LBYTE(TWI_ADR)]]  , in File :
Error : 222   Line :   140   Illegal character [expected (, got ')' [HBYTE(TWI_ADR)]]  , in File :
Error : 222   Line :   152   Illegal character [expected (, got ')' [LBYTE(TWI_ADR)]]  , in File :
Error : 222   Line :   153   Illegal character [expected (, got ')' [HBYTE(TWI_ADR)]]  , in File :
Error : 222   Line :   164   Illegal character [expected (, got ')' [LBYTE(TWI_ADR)]]  , in File :
Error : 222   Line :   165   Illegal character [expected (, got ')' [HBYTE(TWI_ADR)]]  , in File
 

It is my first time to compile a library file. So it may be that I did something wrong.

Please be so kind and advise.
Christian
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 257
Location: Japan

japan.gif
PostPosted: Sun Nov 07, 2021 2:47 am    Post subject: Reply with quote

I don't know the hardware environment you have selected, so please attach a source that displays simple characters.
Back to top
View user's profile Visit poster's website
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 79
Location: Aachen

germany.gif
PostPosted: Sun Nov 07, 2021 11:15 am    Post subject: Reply with quote

I did not specify any hardware. I just did the compilation with the LIB-Tool.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5298
Location: Holland

blank.gif
PostPosted: Mon Nov 08, 2021 2:35 pm    Post subject: Reply with quote

not all libs need to be compiled. in this case there is not need to compile it either. just include it with $LIB and the .LIB extension


if you actually want to use the lbx, the error is about : TWI_ADR it is unknown in the lib.
to solve that, put a * in front so it will be compiled when the app is compiled.

_________________
Mark
Back to top
View user's profile Visit poster's website
hawkfire

Bascom Member



Joined: 15 Oct 2006
Posts: 10
Location: N.Wales U.K.

uk.gif
PostPosted: Fri Nov 12, 2021 11:14 pm    Post subject: Reply with quote

I have been trying to get this working but unfortunately it seems to elude me.I am using Microchip's mcp23008 i2c port chip, instead of the pcf8574 with a few library modifications, although I have verified the port chip to be working with correct output, I still get blank on my screen. I would also like to point out that the 'backlight' arrangement circuit appears to have a full 5Volts across the input of the transistor base emitter junction, pulled up with a 4k7 resistor.??, how the bjt is suppose to survive this is beyond my comprehension. Rolling Eyes
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 257
Location: Japan

japan.gif
PostPosted: Sat Nov 13, 2021 4:31 am    Post subject: Reply with quote

MCP23008 does not work with this library because the commands to I2C are completely different from PCF8574.
Did you change your library for the MCP23008?

The backlight is turned off when the output of the port is set to [L] level.
Back to top
View user's profile Visit poster's website
hawkfire

Bascom Member



Joined: 15 Oct 2006
Posts: 10
Location: N.Wales U.K.

uk.gif
PostPosted: Sat Nov 13, 2021 11:43 am    Post subject: Reply with quote

O-Family wrote:
MCP23008 does not work with this library because the commands to I2C are completely different from PCF8574.
Did you change your library for the MCP23008?


Yes, I modified the pcf8574 library to mcp23008 and the port is working but nothing on the display.

O-Family wrote:
The backlight is turned off when the output of the port is set to [L] level.


Yes, but when it is turned on, your circuit shows full 5 Volt output across the base emitter junction of the transistor? the mcp23008 can happily source 25mA
Back to top
View user's profile
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
Goto page 1, 2  Next
Page 1 of 2

 
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