PDA

View Full Version : I2C LCD 16x2 Pic16f628 Code



DaveBig
- 3rd January 2026, 02:58
I2C LCD 16x2 Pic16f628 Code

I have I2C attached to LCD 16x2 using Pic16f628, internal OSC 4Mhz, DSA on B.1, SLC on B.2, LED on B.5 for Blink at the end, there are 4.7k pull up resistors on the expansion board. I am not getting anything readable. The hardware verified with the Arduino uno "Nice display". I am looking for some simple code that gives something readable to be able to change around to study changes.

richard
- 3rd January 2026, 07:58
I am looking for some simple code

its a frequently asked question
a quick forum search yields
https://www.picbasic.co.uk/forum/showthread.php/24684-I2C-LCD-help-needed
https://www.picbasic.co.uk/forum/showthread.php/17302-Interfacing-with-Arduino-I2C-LCD
www.picbasic.co.uk/forum/showthread.php/19222-I2C-lcd-(-arduino-)-with-PICBASIC-help

DaveBig
- 4th January 2026, 00:53
I have gone through all those posts that I thought were remotely pertinent. I have tried all the code available in those posts. I have changed every variable possible that I know to try. I don’t understand the details of insertion of the code and variables but somewhat I think the concept. Do I need to learn C first or something else to figure this out? I have not posted any of my code because it has no viability. I have spent countless hours on this. Any information would be much appreciated.

richard
- 4th January 2026, 03:48
There a a few variants of backpack lcd's there are pcf8574 ones and pcf8574a ones. they have different i2c address' ranges.
there are at least two differently wired versions also.
there are also dfrobot lcds that look similar but are completely different.


The hardware verified with the Arduino uno "Nice display"
so what settings from arduino were used to get that result ?



Do I need to learn C first or something else to figure this out?
C has nothing to do with it , first step is know what the actual hardware is and its i2c address and how its configured



I have not posted any of my code because it has no viability.
so nobody has any idea of what you are trying and can not possibly see where you have gone wrong
do post your best effort

DaveBig
- 10th January 2026, 18:57
''This is as far as I have got. I know it is not much. I am having great dificulty in breaking down
'' the elements of the I2CWrite. I get a lit up display and that is all. Any help would be greatly ''appreciated.

'************************************************* **********************
'* Pic16f628 LCD I2C 01 260110 *
'* Display output to LCD of Something Readable *
'* Display lights Nothing else *
'* *
'* Internal Oscillator *
'* *
'************************************************* **********************
#CONFIG
cfg = _FOSC_INTOSCCLK ; INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
; _FOSC_ERCLK _ER_OSC_CLKOUT _FOSC_ERIO
; _ER_OSC_NOCLKOUT _FOSC_INTOSCCLK _INTRC_OSC_CLKOUT
; _FOSC_INTOSCIO _INTRC_OSC_NOCLKOUT _FOSC_ECIO
; _EXTCLK_OSC _FOSC_HS _HS_OSC _FOSC_XT
; _XT_OSC _FOSC_LP _LP_OSC
cfg&= _WDT_ON ; WDT enabled (MUST USE CAPS)
cfg&= _WDTE_OFF ; WDT disabled
cfg&= _PWRTE_ON ; PWRT disabled (MUST USE CAPS)
cfg&= _MCLRE_OFF ; RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD
cfg&= _LVP_OFF ; RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming
cfg&= _BOREN_OFF ; BOD Reset disabled
cfg&= _CPD_OFF ; Data memory code protection off
cfg&= _BODEN_OFF ; BOD Reset disabled
cfg&= _CP_OFF ; Code protection off
__CONFIG cfg
#ENDCONFIG
DEFINE OSC 4
define I2C_SLOW 1
DEFINE I2C_INTERNAL 1
'#define I2C_SLOW 1 ' Not sure if it gets #
'#DEFINE I2C_INTERNAL 1 ' Not sure if it gets #

TrisB = 0
SDA VAR PORTB.1 'SDA Pin#7 on Pic16f628
SCL VAR PORTB.2 'SCL Pin#8 on Pic16f628
Five var PORTB.5 'Pin#11 LED Output to indicate program is running
Tim var byte ' Variable for Time
Tim =500
Tim2 var byte ' Variable for Time
Tim2 =40

Control Var byte
Control = %10100000 'Page 157 PBP Control byte would be %10100000 or $A0
Address var byte 'LCD address:With Arduino I2C Scan Program 39 Dec or 0x27 Hex
Address = 39 'LCD address:With Arduino I2C Scan Program 39 Dec or 0x27 Hex

Main:

I2CWRITE SDA,SCL,Control,[$00,$01]'clear display
pause Tim2
I2CWRITE SDA,SCL,Control,[$00,$0C]'display on/off
pause Tim2
' Don't know where the below $FE,$50,$04,$0A,$15,$0E,$04,$0A,$0A,$00]
' are from they were copied from another program.
I2CWRITE SDA,SCL,Control,[Address,$FE,$50,$04,$0A,$15,$0E,$04,$0A,$0A,$00]' Hello
pause Tim2

low Five 'This is to see that the program is running It does Blink
pause Tim
high Five
pause Tim
low Five
Goto Main
END

richard
- 11th January 2026, 01:12
there are a number of issues here



#CONFIG
;cfg = _FOSC_INTOSCCLK ; INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN do you really need clockout on RA6
cfg = _INTRC_OSC_NOCLKOUT ;INTRC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
; _FOSC_ERCLK _ER_OSC_CLKOUT _FOSC_ERIO
; _ER_OSC_NOCLKOUT _FOSC_INTOSCCLK _INTRC_OSC_CLKOUT
; _FOSC_INTOSCIO _INTRC_OSC_NOCLKOUT _FOSC_ECIO
; _EXTCLK_OSC _FOSC_HS _HS_OSC _FOSC_XT
; _XT_OSC _FOSC_LP _LP_OSC
cfg&= _WDT_ON ; WDT enabled (MUST USE CAPS)
cfg&= _WDTE_OFF ; WDT disabled
cfg&= _PWRTE_ON ; PWRT disabled (MUST USE CAPS)
cfg&= _MCLRE_OFF ; RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD
cfg&= _LVP_OFF ; RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming
cfg&= _BOREN_OFF ; BOD Reset disabled
cfg&= _CPD_OFF ; Data memory code protection off
cfg&= _BODEN_OFF ; BOD Reset disabled
cfg&= _CP_OFF ; Code protection off
;__CONFIG cfg ;error not correct or needed
#ENDCONFIG




DEFINE OSC 4
;define I2C_SLOW 1 not relevant
;DEFINE I2C_INTERNAL 1 incorrect
'#define I2C_SLOW 1 ' Not sure if it gets #
'#DEFINE I2C_INTERNAL 1 ' Not sure if it gets #


TrisB = 0 ; i2c pins should not be set as outputs
SDA VAR PORTB.1 'SDA Pin#7 on Pic16f628
SCL VAR PORTB.2 'SCL Pin#8 on Pic16f628
Five var PORTB.5 'Pin#11 LED Output to indicate program is running
;Tim var byte ' Variable for Time a byte cannot hold such a large number byte range 0 to 255
Tim var word ' Variable for Time
Tim =500
Tim2 var byte ' Variable for Time
Tim2 =40

;Control Var byte ; the example in manual is for an eeprom , there is no eeprom here
;Control = 100000 'Page 157 PBP Control byte would be 100000 or $A0

Address var byte 'LCD address:With Arduino I2C Scan Program 39 Dec or 0x27 Hex
Address = 39 'LCD address:With Arduino I2C Scan Program 39 Dec or 0x27 Hex
; arduino i2c address is a 7 bit address pbp uses an 8 bit address


Address = Address <<1 ; convert to 8 bit



I2CWRITE SDA,SCL,Address ,[$00,$01]'clear display
pause Tim2
I2CWRITE SDA,SCL,Address ,[$00,$0C]'display on/off
pause Tim2
' Don't know where the below $FE,$50,$04,$0A,$15,$0E,$04,$0A,$0A,$00]
' are from they were copied from another program. they come from the Darrel Taylor LCD_PCF8574A.pbp i2c lcd include file and are pointless here
I2CWRITE SDA,SCL,Address ,[$FE,$50,$04,$0A,$15,$0E,$04,$0A,$0A,$00]' Hello
pause Tim2

low Five 'This is to see that the program is running It does Blink at twice the expected rate due to incorrect var type
pause Tim
high Five
pause Tim
low Five
Goto Main





the lcd has not been initialized
nor have you sent the lcd data in a nibblised format with correct strobe pulses being generated
its much simpler to use the Darrel Taylor LCD_PCF8574A.pbp i2c lcd include

there are plenty of example in the links given

DaveBig
- 11th January 2026, 15:20
Where can the Darrel Taylor LCD_PCF8574A.pbp i2c lcd include be found?
This is the only forum I have ever been on, I am trying to learn how to navigate around. Thank you very much for pointing out the each of the code errors.

richard
- 11th January 2026, 23:13
Where can the Darrel Taylor LCD_PCF8574A.pbp i2c lcd include be found?

[open in an incognito window in chrome]
support.melabs.com/forum/picbasic-pro-compiler-3-0-and-later/pbp3-picbasic-pro-3-0/773-i2c-pcf8574-20x4-lcd

original ver in post #3
alternative ver in post #6