Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

MCP23017 I2C Graphic LCD 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: 320
Location: Japan

japan.gif
PostPosted: Thu Apr 29, 2021 2:44 am    Post subject: MCP23017 I2C Graphic LCD Adapter Library Reply with quote

A library for controlling the KS0108 (128*64) Graphic LCD with the I2C(TWI) bus using MCP23017(I2C 16bit I/O Expander).
The drawing speed will be considerably slower, but the graphic LCD can be used even with an AVR with a small number of pins.
Software I2C and TWI can be changed by [I2c_select].

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

Adapter board
https://drive.google.com/file/d/1lrUTJfMFxfbY0iDsDJZTdgOYR7FQsMge/view?usp=sharing
https://drive.google.com/file/d/1IgIOVwhkwYfg9cR2egt9cxa2BEY5nFDl/view?usp=sharing
https://drive.google.com/file/d/1s7FgZluO28kK52dYGn9FHJT4gH67nTt8/view?usp=sharing
https://drive.google.com/file/d/13oUfkIJdOqiHLqiXOr7AkNZUGDcZgOs2/view?usp=sharing

Code:
$programmer = 22                                            'ARDUINO (using stk500v1 protocol)

   '
   '  *******************************************
   '  *  MCP23017 I2C Graphic LCD Adapter test  *
   '  *                       2021. 4.21        *
   '  *******************************************
   '

$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.

   '
   '  * MCP23017 I2C Graphic LCD Adapter settings *
   '
   Const I2c_select = 1                                     '0:Software I2C , 1:TWI
#if I2c_select = 0
   '------[For software I2C]------
'   $lib "i2cV2.LBX"                                         'Incorporate a modified version of the I2C library.
   Config I2cdelay = 1                                      'SCL clock frequency.
   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 = 400000                                      'I2C bus clock = 400KHz
   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

   Const Glcd_address = &H40                                'MCP23017 slave address. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Dim Backlight As Byte : Backlight = 1                    'LCD backlight control. (0: off, 1: on)
   Dim Dummy As Byte : I2creceive &H00 , Dummy              'Dummy instruction to include the [_I2C_READ] routine.
$lib "glcdKS108_I2C_MCP23017.lib"                           'Incorporates the I2C Graphic LCD Adapter library for the KS0108 chip.
   Config Graphlcd = 128 * 64sed                            'Graphic LCD screen settings.


   '
   '  * Drawing test  *
   '
   Dim Y As Byte
   '
   Do
      Cls                                                   'Erase the full screen of the graphic LCD.
      Setfont Font8x8                                       'Set the font.
      Lcdat 1 , 1 , "MCP23017 I2C"                          'Display a character string.
      Lcdat 2 , 20 , "G-LCD adapter" , 1                    'Black and white reverse display of character strings.
      '
      For Y = 23 To 63 Step 5
         Line(3 , 23) -(35 , Y) , 1                         'Draw a straight line.
      Next Y
      '
      Box(45 , 30) -(80 , 55) , 1                           'Draw a box.
      Cls 6 , 53 , 73 , &HCA                                'Block erase test.
      '
      Circle(105 , 40) , 16 , 1                             'Draw a circle.
      Pset 105 , 40 , 1                                     'Draw a point in the center of the circle.
      '
      Wait 3
      '
      '  * Backlight control test  *
      '
      Backlight = 0 : Glcdcmd &H3E , 1 : Glcdcmd &H3E , 2   'Turn off the LCD screen and backlight.
      Wait 2
      Backlight = 1 : Glcdcmd &H3E , 1 : Glcdcmd &H3E , 2   'Turn on the backlight.
      Wait 2
      Backlight = 1 : Glcdcmd &H3F , 1 : Glcdcmd &H3F , 2   'Turn on the LCD screen and backlight.
      Wait 2
      '
      '  * SHOWPIC instruction test  *
      '
      Cls                                                   'Erase the full screen of the graphic LCD.
      Showpic 0 , 0 , Plaatje                               'Draw a BGF file.
      '
      Wait 3
      '
   Loop
   '
   End                                                      'The end of the program.

   '
$include "font8x8.font"                                     'Include font files.
   '
Plaatje:                                                    'The label name of the BGF file.
$bgf "KS108.bgf"


Last edited by O-Family on Sun May 02, 2021 1:52 am; edited 3 times in total
Back to top
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Thu Apr 29, 2021 2:53 am    Post subject: Reply with quote

By writing the [Const Mcp23017_multi = 1] line, multiple [1-8] LCDs can be controlled on the same bus.

Code:
$programmer = 22                                            'ARDUINO (using stk500v1 protocol)

   '
   '  *******************************************
   '  *  MCP23017 I2C Graphic LCD Adapter test  *
   '  *     (For multiple LCDs)   2021. 4.21    *
   '  *******************************************
   '

$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.

   '
   '  * MCP23017 I2C Graphic LCD Adapter settings *
   '
   Const I2c_select = 1                                     '0:Software I2C , 1:TWI
#if I2c_select = 0
   '------[For software I2C]------
'   $lib "i2cV2.LBX"                                         'Incorporate a modified version of the I2C library.
   Config I2cdelay = 1                                      'SCL clock frequency.
   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 = 400000                                      'I2C bus clock = 400KHz
   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

   Const Mcp23017_multi = 1                                 'Switch to multiple graphic LCD mode.
   Dim Glcd_address As Byte : Glcd_address = &H40           'MCP23017 slave address. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Dim Backlight As Byte : Backlight = 1                    'LCD backlight control. (0: off, 1: on)
   Dim Dummy As Byte : I2creceive &H00 , Dummy              'Dummy instruction to include the [_I2C_READ] routine.
$lib "glcdKS108_I2C_MCP23017.lib"                           'Incorporates the I2C Graphic LCD Adapter library for the KS0108 chip.
   Config Graphlcd = 128 * 64sed                            'Graphic LCD screen settings.

   '
   '  * When installing the second and subsequent LCDs  *
   '
   Glcd_address = &H42                                      'Specify the second graphic LCD.
   !CALL _set_display                                       'Initialize the graphic LCD.


   '
   '  * Drawing test  *
   '
   Dim Y As Byte
   '
   Glcd_address = &H40                                      'Specify the first graphic LCD.
   Cls                                                      'Erase the full screen of the graphic LCD.
   Setfont Font8x8                                          'Set the font.
   Lcdat 1 , 1 , "MCP23017 I2C"                             'Display a character string.
   Lcdat 2 , 20 , "G-LCD adapter" , 1                       'Black and white reverse display of character strings.
   '
   For Y = 23 To 63 Step 5
      Line(3 , 23) -(35 , Y) , 1                            'Draw a straight line.
   Next Y
   '
   Box(45 , 30) -(80 , 55) , 1                              'Draw a box.
   Cls 6 , 53 , 73 , &HCA                                   'Block erase test.
   '
   Circle(105 , 40) , 16 , 1                                'Draw a circle.
   Pset 105 , 40 , 1                                        'Draw a point in the center of the circle.


   '
   '  * Second graphic LCD  *
   '
   '
   '  * SHOWPIC instruction test  *
   '
   Glcd_address = &H42                                      'Specify the second graphic LCD.
   Cls                                                      'Erase the full screen of the graphic LCD.
   Showpic 0 , 0 , Plaatje                                  'Draw a BGF file.

   '
   End                                                      'The end of the program.

   '
$include "font8x8.font"                                     'Include font files.
   '
Plaatje:                                                    'The label name of the BGF file.
$bgf "KS108.bgf"


Last edited by O-Family on Fri Apr 30, 2021 2:41 am; edited 1 time in total
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5920
Location: Holland

blank.gif
PostPosted: Thu Apr 29, 2021 10:31 am    Post subject: Reply with quote

ha O-Family, you are the man !
Excellent job. As always i can say Very Happy

Thank you for sharing. with your permission i will include this with bascom too.

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

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Fri Apr 30, 2021 2:35 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)

   '
   '  *******************************************
   '  *  MCP23017 I2C Graphic LCD Adapter test  *
   '  *     (For USI)         2021. 4.30        *
   '  *******************************************
   '

$regfile = "ATtiny85.DAT"                                   'Set the AVR to use.
$crystal = 8000000                                          '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.

   '
   '  * MCP23017 I2C Graphic 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 = 1                                      'SCL clock frequency.
   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 = Fast                     'USI master settings.
   I2cinit                                                  'Initialize the SCL and SDA lines of the I2C bus.
   '-------------------------------
#endif

   Const Glcd_address = &H40                                'MCP23017 slave address. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
   Dim Backlight As Byte : Backlight = 1                    'LCD backlight control. (0: off, 1: on)
   Dim Dummy As Byte : I2creceive &H00 , Dummy              'Dummy instruction to include the [_I2C_READ] routine.
$lib "glcdKS108_I2C_MCP23017.lib"                           'Incorporates the I2C Graphic LCD Adapter library for the KS0108 chip.
   Config Graphlcd = 128 * 64sed                            'Graphic LCD screen settings.


   '
   '  * Drawing test  *
   '
   Dim Y As Byte
   '
   Do
      Cls                                                   'Erase the full screen of the graphic LCD.
      Setfont Font8x8                                       'Set the font.
      Lcdat 1 , 1 , "MCP23017 I2C"                          'Display a character string.
      Lcdat 2 , 20 , "G-LCD adapter" , 1                    'Black and white reverse display of character strings.
      '
      For Y = 23 To 63 Step 5
         Line(3 , 23) -(35 , Y) , 1                         'Draw a straight line.
      Next Y
      '
      Box(45 , 30) -(80 , 55) , 1                           'Draw a box.
      Cls 6 , 53 , 73 , &HCA                                'Block erase test.
      '
      Circle(105 , 40) , 16 , 1                             'Draw a circle.
      Pset 105 , 40 , 1                                     'Draw a point in the center of the circle.
      '
      Wait 3
      '
      '  * Backlight control test  *
      '
      Backlight = 0 : Glcdcmd &H3E , 1 : Glcdcmd &H3E , 2   'Turn off the LCD screen and backlight.
      Wait 2
      Backlight = 1 : Glcdcmd &H3E , 1 : Glcdcmd &H3E , 2   'Turn on the backlight.
      Wait 2
      Backlight = 1 : Glcdcmd &H3F , 1 : Glcdcmd &H3F , 2   'Turn on the LCD screen and backlight.
      Wait 2
      '
      '  * SHOWPIC instruction test  *
      '
      Cls                                                   'Erase the full screen of the graphic LCD.
      Showpic 0 , 0 , Plaatje                               'Draw a BGF file.
      '
      Wait 3
      '
   Loop
   '
   End                                                      'The end of the program.

   '
$include "font8x8.font"                                     'Include font files.
   '
Plaatje:                                                    'The label name of the BGF file.
$bgf "KS108.bgf"
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Sun May 09, 2021 4:26 am    Post subject: Reply with quote

Many thanks Very Happy
This opens the micros with smaller numbers of pins for use with displays that use to
consume nearly all the pins with very little left to be of use.
Again Many thanks
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
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