News:

Let's find out together what makes a PIC Tick!

Main Menu

I2C lcd driver

Started by shantanu@india, Jan 30, 2022, 08:26 AM

Previous topic - Next topic

shantanu@india

Hi,
I've purchased this I2C backpack for 16X2 HD44780 based alphanumeric display.
I'm confused about slave address. The PCF8574 says that to write to the LCD I need to use 0x4E which is reasonable since A0/A1/A2 are all pulled up internally. But the seller says to use 0x3F.
I also need some assistance in writing code for this module.
Can anyone help please?
Regards
Shantanu

rick.curl

Hi Shantanu-
Not my area of expertise, but I did remember THIS THREAD that may be what you're looking for.

-Rick

shantanu@india

Thank you so much Rick.
Need to study and then write my own code.  both in Proton and Python
Regards
Shantanu

midali

Hi Shantanu,

This code may be help you . This code was posted in this forum  :

example:

Device = 18F46K20
Declare Xtal = 8
Declare All_Digital = true
Declare Watchdog = On
Declare Slow_Bus On
Clear

Include "I2CPrintHardCommand.inc"
TRISA=%11111111
TRISB=%00000000
TRISC=%10011111
TRISD=%00000000
TRISE=%00000011
PORTA=0
PORTB=0
PORTC=0
PORTD=0
PORTE=0
Dim lights As Byte
lights = 0
DelayMS 500 'Short delay to stabilize the controller
Main:
Cls
If lights = 0 Then
Print $FE, 3 'Backlight on
EndIf

DelayMS 500

Print At 1,1, "  PCF8574T DISPLAY  "
DelayMS 5
Print At 2,1, "TEST TEST  TEST TEST"
DelayMS 5
Print At 3,1, "20 x 4 lines display"
DelayMS 5
Print At 4,1, "  PROTON BASIC    "
DelayMS 500

lights = 1
If lights = 1 Then
Print $FE, 4 'Backlight off
EndIf
DelayMS 500
lights = 0
GoTo Main

Config_Start
FOSC = HS ;HS oscillator
FCMEN = On ;Fail-Safe Clock Monitor enabled
IESO = OFF ;Oscillator Switchover mode disabled
PWRT = On ;PWRT enabled
BOREN = SBORDIS ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
BORV = 18 ;VBOR set to 1.8 V nominal
WDTEN = On ;WDT is controlled by SWDTEN bit of the WDTCON register
WDTPS = 8192 ;1:8192 işlemci çekirdeği 8 saniye çalışmazsa sistem resetlenir.
CCP2MX = PORTC ;CCP2 input/output is multiplexed with RC1
PBADEN = OFF ;PORTB<4:0> pins are configured as analog input channels on Reset
LPT1OSC = OFF ;Timer1 configured for higher power operation
HFOFST = OFF ;HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.
MCLRE = On ;MCLR pin enabled; RE3 input pin disabled
STVREN = On ;Stack full/underflow will cause Reset
LVP = OFF ;Single-Supply ICSP enabled
XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
Debug = OFF ;Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
Cp0 = OFF ;Block 0 (000800-003FFFh) not code-protected
CP1 = OFF ;Block 1 (004000-007FFFh) not code-protected
CP2 = OFF ;Block 2 (008000-00BFFFh) not code-protected
CP3 = OFF ;Block 3 (00C000-00FFFFh) not code-protected
CPB = OFF ;Boot block (000000-0007FFh) not code-protected
CPD = OFF ;Data EEPROM not code-protected
WRT0 = OFF ;Block 0 (000800-003FFFh) not write-protected
WRT1 = OFF ;Block 1 (004000-007FFFh) not write-protected
WRT2 = OFF ;Block 2 (008000-00BFFFh) not write-protected
WRT3 = OFF ;Block 3 (00C000h-00FFFFh) not write-protected
WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
WRTD = OFF ;Data EEPROM not write-protected
EBTR0 = OFF ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
EBTR1 = OFF ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
EBTR2 = OFF ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
EBTR3 = OFF ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
End



I2CHardPrintCommand:


#Disable Print                          ;Disable the Proton Print command   

  ;***************************************************************************************************************
  ; Configure items to suit your LCD and I2C hardware, I2CPrint_DataOnHighBits remmed if data is on low bits    *
  ;                                                                                                              *
  Symbol I2CPrint_address        = %01001110  ' PCF8574 Slave Address 0,1,1,1,A2,A1,A0,RW  where W=0          *
  Symbol I2CPrint_InitForLCDLines = $28        ' $28 for two lines or 4 lines                                              *
  Symbol I2CPrint_RS              = %00000001  '                                                                *
  Symbol I2CPrint_RW              = %00000010  '                                                                *
  Symbol I2CPrint_E              = %00000100  '                                                                *
  Symbol I2CPrint_BackLight      = %00001000  '                                                                *
  $define I2CPrint_DataOnHighBits              '                                                                *
  ;***************************************************************************************************************
 
  Symbol I2CPrint_RS_E          = I2CPrint_RS | I2CPrint_E
 
  $ifdef I2CPrint_DataOnHighBits
    Symbol I2CPrint_Init1          = %00110000
    Symbol I2CPrint_Init1_E        = %00110000 | I2CPrint_E
    Symbol I2CPrint_Init2          = %00110000
    Symbol I2CPrint_Init2_E        = %00110000 | I2CPrint_E
    Symbol I2CPrint_Init3          = %00100000
    Symbol I2CPrint_Init3_E        = %00100000 | I2CPrint_E
  $else
    Symbol I2CPrint_Init1          = %00000011
    Symbol I2CPrint_Init1_E        = %00000011 | I2CPrint_E
    Symbol I2CPrint_Init2          = %00000011
    Symbol I2CPrint_Init2_E        = %00000011 | I2CPrint_E
    Symbol I2CPrint_Init3          = %00000010
    Symbol I2CPrint_Init3_E        = %00000010 | I2CPrint_E
  $endif

  Dim BPF As Byte System
  Symbol I2CPrint_tComOrData = BPF.0
  Symbol I2CPrint_tInitialised = BPF.1

  Dim I2CPrint_PrintStore As Byte
  Dim I2CPrint_PrintData As Byte
  Dim I2CPrint_HighData As Byte
  Dim I2CPrint_LowData As Byte
  Dim I2CPrint_dummy As Byte
  Dim I2CPrint_RSFlag As Byte
  Dim Backlight_State As Byte

  GoTo I2CPrintCommand_End

  @Print                                              ; assembler Print redirect
  Wreg_Byte I2CPrint_PrintStore                      ; save the W Register

  If I2CPrint_tInitialised = 0 Then                  ; Has LCD been inititalised?
    DelayMS 100                                      ; Power-on delay for slow display types
    HBusOut I2CPrint_address, [I2CPrint_Init1_E]      ; E hi and send command nibble 0011 (3)   
    HBusOut I2CPrint_address, [I2CPrint_Init1]        ; E lo   
    DelayMS 20
    HBusOut I2CPrint_address, [I2CPrint_Init2_E]      ; E hi and send command nibble 0011 (3)   
    HBusOut I2CPrint_address, [I2CPrint_Init2]        ; E lo   
    DelayMS 20
    HBusOut I2CPrint_address, [I2CPrint_Init3_E]      ; E hi and send command nibble 0010 (2)   
    HBusOut I2CPrint_address, [I2CPrint_Init3]        ; E lo   
    DelayMS 20
    I2CPrint_PrintData = I2CPrint_InitForLCDLines    ; 4 bit mode and n lines
    GoSub I2CPrint_SendControl
    I2CPrint_PrintData = $0C                          ; enable display, cursor and blink off
    GoSub I2CPrint_SendControl
    I2CPrint_PrintData = $06                          ; move cursor after each write
    GoSub I2CPrint_SendControl                   
    I2CPrint_PrintData = $80                          ; goto column 1 line 1
    GoSub I2CPrint_SendControl
    Set I2CPrint_tInitialised                        ; Indicate that the LCD is initialised
  EndIf

  I2CPrint_PrintData = I2CPrint_PrintStore
  If I2CPrint_PrintData = 0xFE Then                  ; it is a command header
    I2CPrint_RSFlag = 0                              ; clear a flag so we know the next byte will be a command
  Else
    If I2CPrint_RSFlag = 0 Then                      ; this is a command byte
      If I2CPrint_PrintData = 03 Then
        Backlight_State = I2CPrint_BackLight
      ElseIf I2CPrint_PrintData = 04 Then
        Backlight_State = 0
      Else
        GoSub I2CPrint_SendControl
      End If
    Else
      GoSub I2CPrint_SendData
    EndIf
    I2CPrint_RSFlag = 1                              ; set the flag so the next byte will be send as Data
  EndIf                                   
  Byte_Wreg I2CPrint_PrintStore                      ; restore the W register
  Return

' ****************************************************************************
' **                            S U B R O U T I N E S                      **
' ****************************************************************************

I2CPrint_SendData:
  $ifdef I2CPrint_DataOnHighBits
    I2CPrint_HighData = I2CPrint_PrintData & %11110000            ; mask LSBs
    I2CPrint_LowData = I2CPrint_PrintData << 4                    ; shift left
  $else
    I2CPrint_LowData = I2CPrint_PrintData & %00001111            ; mask MSBs
    I2CPrint_HighData = I2CPrint_PrintData >> 4                  ; shift Right
  $endif

  I2CPrint_dummy = I2CPrint_RS_E | I2CPrint_HighData              ; data and e hi
  I2CPrint_dummy = I2CPrint_dummy | Backlight_State              ; data and e hi and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]           
  I2CPrint_dummy = I2CPrint_RS | I2CPrint_HighData                ; data and e lo
  I2CPrint_dummy = I2CPrint_dummy | Backlight_State              ; data and e lo and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]             
  I2CPrint_dummy = I2CPrint_RS | Backlight_State                  ; data and e hi
  HBusOut I2CPrint_address, [I2CPrint_dummy]                      ; Delay, RS on throughout
  I2CPrint_dummy = I2CPrint_RS_E | I2CPrint_LowData              ; data and e hi
  I2CPrint_dummy = I2CPrint_dummy | Backlight_State              ; data and e hi and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]             
  I2CPrint_dummy = I2CPrint_RS | I2CPrint_LowData                ; data and e lo
  I2CPrint_dummy = I2CPrint_dummy | Backlight_State              ; data and e lo and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]
  Return
 
I2CPrint_SendControl:
  $ifdef I2CPrint_DataOnHighBits
    I2CPrint_HighData = I2CPrint_PrintData & %11110000            ; mask LSBs
    I2CPrint_LowData = I2CPrint_PrintData << 4                    ; shift left
  $else
    I2CPrint_LowData = I2CPrint_PrintData & %00001111            ; mask MSBs
    I2CPrint_HighData = I2CPrint_PrintData >> 4                  ; shift Right
  $endif

  I2CPrint_dummy = I2CPrint_E | I2CPrint_HighData                ; data and e hi
  I2CPrint_dummy = I2CPrint_dummy | Backlight_State              ; data and e hi and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]             
  I2CPrint_dummy = I2CPrint_HighData | Backlight_State            ; data and e lo and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]             
  I2CPrint_dummy = Backlight_State                                ;
  HBusOut I2CPrint_address, [I2CPrint_dummy]                      ; Delay
  I2CPrint_dummy = I2CPrint_E | I2CPrint_LowData                  ; data and e hi
  I2CPrint_dummy = I2CPrint_dummy | Backlight_State              ; data and e hi and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]             
  I2CPrint_dummy = I2CPrint_LowData | Backlight_State            ; data and e lo and bls
  HBusOut I2CPrint_address, [I2CPrint_dummy]       
  Return

I2CPrintCommand_End:


shantanu@india

Regards
Shantanu