Pcf8574 i2c lcd


Closed Thread
Results 1 to 3 of 3

Thread: Pcf8574 i2c lcd

  1. #1
    Join Date
    Aug 2008
    Posts
    81

    Default Pcf8574 i2c lcd

    Hello anyone to help me 12c
    PCF8574 Does Not Work on Test Board
    Only works on proteus
    Here are all the files
    Thank you
    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 8/20/2016                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    ;----[16F873AHardware Configuration]-------------------------------------------
    
    #CONFIG
        __config _HS_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF & _PWRTE_OFF & _WDTE_OFF
    #ENDCONFIG
    
    ;----[Oscillator Setup]---------------------------------------------------------
    DEFINE OSC 12
    TRISA = %11111100:PORTA=0
    TRISB = %11111111:PORTB=0
    TRISC = %11100111:PORTC=0
    ;----[Aliases]------------------------------------------------------------------
    SDA         VAR PORTC.4      ;I2C Data pin
    SCL         VAR PORTC.3      ;I2C Clock Pin
    ;----[Initialize Hardware]------------------------------------------------------
     ADCON1 = 7
    ;----[Setup LCD using PCF8574 Port Expander]------------------------------------
    LCD_Addr    CON $40           ;A0,A1,A2 tied to ground
    INCLUDE "LCD_PCF8574.pbp"     ;Include LCD module for PCF8574
    
    ;----[Variables]----------------------------------------------------------------
    LoopCount       VAR BYTE
    CustChar        VAR BYTE
    
    ;----[Program Start]------------------------------------------------------------
    Backlight = 0                ;Turn on LCD Backlight
    ARRAYWRITE LCD_Buff,["   LCD PCF8574"] : LCD_WriteBuff
    LoopCount= 0
    
    ;----[Custom Characters]-(Jumping Jacks)----------------------------------------
    ARRAYWRITE LCD_Buff,[$FE,$40,$00,$04,$0A,$04,$0E,$15,$0A,$11]:LCD_WriteBuff ' #0  
    ARRAYWRITE LCD_Buff,[$FE,$48,$00,$04,$0A,$04,$1F,$04,$0A,$0A]:LCD_WriteBuff ' #1  
    ARRAYWRITE LCD_Buff,[$FE,$50,$04,$0A,$15,$0E,$04,$0A,$0A,$00]:LCD_WriteBuff ' #2 
    
    ;----[Main Program Loop]--------------------------------------------------------
    Main:
        ARRAYWRITE LCD_Buff,[$FE,$C0,"Count = ",DEC LoopCount,"   "] : LCD_WriteBuff
        PAUSE 200
        LoopCount = LoopCount + 1
    
        CustChar = LoopCount//3   ; Jumping Jacks Sequence
        ARRAYWRITE LCD_Buff,[$FE,$9D,CustChar] : LCD_WriteBuff
    GOTO Main
    Code:
    ; File Name   : LCD_PCF8574.pbp
    ; Author      : Darrel Taylor
    ; Created     : Mon Feb 24 2014
    ; Compiler    : PicBasic Pro 3.0.6.x or higher
    ; Description : Uses PCF8574 Port expander to interface with HD44780 LCD
    ;
    ;-------------------------------------------------------------------------------
    LCD_BuffSize    CON 30
    LCD_Buff        VAR BYTE[LCD_BuffSize]
    LCD_BuffAddr    CON EXT : @LCD_BuffAddr = _LCD_Buff
    LCD_BuffLen     VAR WORD        ; Length of data in buffer
    LCD_Data        VAR BYTE        ; Data to Send to LCD
    LCD_Byte        VAR BYTE        ; Nibbles to Send
    LCD_E           VAR LCD_Byte.4  ; Enable bit
    LCD_RW          VAR LCD_Byte.5  ; Read/Write bit
    LCD_RS          VAR LCD_Byte.6  ; Register Select bit
    BackLight       VAR LCD_Byte.7  ; Backlight 0=ON
    LCD_WriteMode   VAR BYTE        ; 1=LowNibble, 2=HighNibble, 3=Both
    LCD_CommandMode VAR BIT         ; Indicates next byte is a command
    LCD_Byte2       VAR BYTE        ; Same nibble without E bit
    LCD_Idx         VAR BYTE
    
    GOTO Over_LCDPCF8574
    
    ;----[Write 1-byte to LCD]--(Input is LCD_Data)---------------------------------
    USERCOMMAND "LCD_WRITE"
    LCD_WRITE_:
        LCD_E = 1
        IF LCD_WriteMode.1 THEN                                  ; Write High Nibble
            LCD_Byte = (LCD_Byte & $F0) | ((LCD_Data >> 4) & $0F)
            LCD_Byte2 = LCD_Byte & $EF
            I2CWRITE SDA,SCL, LCD_Addr,[LCD_Byte, LCD_Byte2]
        ENDIF
    
        IF LCD_WriteMode.0 THEN                                  ; Write Low Nibble
            LCD_Byte = (LCD_Byte & $F0) | (LCD_Data & $0F)
            LCD_Byte2 = LCD_Byte & $EF
            I2CWRITE SDA,SCL, LCD_Addr,[LCD_Byte, LCD_Byte2]
        ENDIF
    RETURN
    
    ASM
    LCD_WRITE?  macro
        L?CALL _LCD_WRITE_
      endm
    ENDASM
    
    
    ;----[Write contents of buffer to LCD]------------------------------------------
    USERCOMMAND "LCD_WRITEBUFF"
    LCD_WRITEBUFF_:
        LCD_BuffLen = R5 - LCD_BuffAddr -1
        LCD_WriteMode = 3          ; Both Nibbles
        LCD_CommandMode = 0
        FOR LCD_Idx = 0 TO LCD_BuffLen
            IF LCD_Buff(LCD_Idx) = $FE THEN LCD_CommandMode = 1 : GOTO LCD_ByteDone
            IF LCD_CommandMode THEN
                LCD_RS = 0
                LCD_CommandMode = 0
            ELSE
                LCD_RS = 1
            ENDIF
            LCD_Data = LCD_Buff(LCD_Idx)
            LCD_Write
            IF LCD_CommandMode THEN
                PAUSE 2
            ELSE
                PAUSEUS 50
            ENDIF
    
          LCD_ByteDone:
        NEXT LCD_Idx
    
        LCD_CommandMode = 0
    RETURN
    
    ASM
    LCD_WRITEBUFF?  macro
        L?CALL _LCD_WRITEBUFF_
      endm
    ENDASM
    Over_LCDPCF8574:
    
    ;----[Initialize the LCD]-------------------------------------------------------
    PAUSE 250                         ; LCD Power-on delay
    Backlight = 1                     ; Backlight OFF
    LCD_RW = 0                        ; Write to LCD
    LCD_RS = 0                        ; Command Register
    
    LCD_WriteMode = 1                 ;-- Low Nibbles only
    LCD_Data = 3                      ; Reset 3 times
    LCD_Write : PAUSEUS 6000
    LCD_Write : PAUSEUS 1000
    LCD_Write : PAUSEUS 1000
    
    LCD_Data = 2                      ; Start 4-bit mode
    LCD_Write : PAUSEUS 1000
    
    LCD_WriteMode = 3                 ;-- Both Nibbles
    LCD_Data = $28 : LCD_Write        ; Function Set, 4-bit, 2-line, 5x7
    LCD_Data = $0C : LCD_Write        ; Display ON
    LCD_Data = $01 : LCD_Write        ; Clear Screen
    PAUSE 2
    LCD_Data = $06 : LCD_Write        ; Entry Mode
        
    PAUSE 2                           ; Let command finish
    Attached Images Attached Images   

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Pcf8574 i2c lcd

    Perhaps the address is different on the physical hardware? Note that there are 2 versions of 8574 (with different addresses) also there are jumpers on some backpacks.
    Last edited by towlerg; - 24th August 2017 at 14:23.
    George

  3. #3
    Join Date
    Aug 2008
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Pcf8574 i2c lcd

    Hi, thanks for the reply
    I made a correction on the LCD pins now it is working
    More being stays on too long the display stays as mixed characters
    Code:
    LCD_BuffSize    CON 30
    LCD_Buff        VAR BYTE[LCD_BuffSize]
    LCD_BuffAddr    CON EXT : @LCD_BuffAddr = _LCD_Buff
    LCD_BuffLen     VAR WORD        ; Length of data in buffer
    LCD_Data        VAR BYTE        ; Data to Send to LCD
    LCD_Byte        VAR BYTE        ; Nibbles to Send
    LCD_RS          VAR LCD_Byte.0  ; Register Select bit
    LCD_RW          VAR LCD_Byte.1  ; Read/Write bit
    LCD_E           VAR LCD_Byte.2  ; Enable bit
    LCD_BackLight   VAR LCD_Byte.3  ; Backlight 0=ON
    LCD_WriteMode   VAR BYTE        ; 1=LowNibble, 2=HighNibble, 3=Both
    LCD_CommandMode VAR BIT         ; Indicates next byte is a command
    LCD_Byte2       VAR BYTE        ; Same nibble without E bit
    LCD_Idx         VAR BYTE
    testmode        var byte
    
    GOTO Over_LCDPCF8574 
    
    ;----[Write 1-byte to LCD]--(Input is LCD_Data)---------------------------------
    USERCOMMAND "LCD_WRITE"
    LCD_WRITE_:
       LCD_E = 1
       IF LCD_WriteMode.1 = 1 THEN                            'Write High Nibble
         LCD_Byte = (LCD_Byte & $0F) | (LCD_Data  & $F0)
         LCD_Byte2 = LCD_Byte & $FB    
         I2CWRITE SDA,SCL, LCD_Addr,[LCD_Byte, LCD_Byte2]
       ENDIF
      
       IF LCD_WriteMode.0 = 1 THEN                             'Write Low Nibble
         LCD_Byte = (LCD_Byte & $0F) | ((LCD_Data << 4 )& $F0)
         LCD_Byte2 = LCD_Byte & $FB
         I2CWRITE SDA,SCL, LCD_Addr,[LCD_Byte, LCD_Byte2]
       ENDIF
    return
    
    ASM
    LCD_WRITE?  macro
        L?CALL _LCD_WRITE_
      endm
    ENDASM
    
    ;----[Write contents of buffer to LCD]------------------------------------------
    USERCOMMAND "LCD_WRITEBUFF"
    LCD_WRITEBUFF_:
    ' The purpose of this routine is to increment through LCD_Buff and
    ' pass each character to LCD_Data so that it can be written.
    ' This will loop for LCD_BuffLen which is calculated by 
    ' subtracting the starting address of the buffer from the
    ' ending address of the buffer.
    
        LCD_BuffLen = R5 - LCD_BuffAddr -1
        LCD_WriteMode = 3          ; Both Nibbles
        LCD_CommandMode = 0
        FOR LCD_Idx = 0 TO LCD_BuffLen
            IF LCD_Buff(LCD_Idx) = $FE THEN LCD_CommandMode = 1 : GOTO LCD_ByteDone
            IF LCD_CommandMode THEN
                LCD_RS = 0   ' this is a command
                LCD_CommandMode = 0 ' false, next byte will be data
            ELSE
                LCD_RS = 1   ' this is data
            ENDIF
            LCD_Data = LCD_Buff(LCD_Idx)
            LCD_Write
            IF LCD_CommandMode THEN
                PAUSE 2
            ELSE
                PAUSEUS 50
            ENDIF
    
          LCD_ByteDone:
        NEXT LCD_Idx
    
        LCD_CommandMode = 0
    RETURN
    
    ASM
    LCD_WRITEBUFF?  macro
        L?CALL _LCD_WRITEBUFF_
      endm
    ENDASM
    
    Over_LCDPCF8574:
    
    ;----[Initialize the LCD]-------------------------------------------------------
    PAUSE 250             'LCD Power-on delay
    LCD_Backlight = 1     'Backlight OFF
    LCD_RW = 0            'Write to LCD
    LCD_RS = 0            'Command Register
    
    LCD_WriteMode = 1     '-- Low Nibbles only
    LCD_Data = 3          'Reset 3 times
    gosub LCD_Write_
    PAUSEUS 6000
    gosub LCD_Write_
    PAUSEUS 1000
    gosub LCD_Write_
    PAUSEUS 1000
    
    LCD_Data = 2          ' Start 4-bit mode
    gosub LCD_Write_
    PAUSEUS 1000
    
    LCD_WriteMode = 3     '-- Both Nibbles
    LCD_Data = $28
    gosub LCD_Write_      'Function Set, 4-bit, 2-line, 5x7
    LCD_Data = $0C
    gosub LCD_Write_      ;Display ON
    LCD_Data = $01
    gosub LCD_Write_      'Clear Screen
    PAUSE 2
    LCD_Data = $06 
    gosub LCD_Write_      'Entry Mode
                           
    PAUSE 2               ; Let command finish
    Attached Images Attached Images  

Similar Threads

  1. I2C LCD
    By midali in forum General
    Replies: 46
    Last Post: - 8th January 2021, 23:52
  2. Pic12f675 + Pcf8574 + Lcd
    By Bostjan in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 4th November 2016, 18:54
  3. Replies: 3
    Last Post: - 15th October 2012, 09:06
  4. I2C PCF8574 4X4 Keypad routine
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th January 2007, 22:25
  5. I2C I/O Expander PCF8574 hanging?
    By NavMicroSystems in forum Serial
    Replies: 12
    Last Post: - 4th August 2005, 03:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts