I2C lcd ( arduino ) with PICBASIC, help


Closed Thread
Results 1 to 40 of 93

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: I2C lcd ( arduino ) with PICBASIC, help

    Ambrogio,

    Any feedback?

    If you have no questions then I will post the next improvement which includes the command in the LCD_Write subroutine. I am now looking at using arrays as the next step.

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: I2C lcd ( arduino ) with PICBASIC, help

    Quote Originally Posted by EarlyBird2 View Post
    Ambrogio,

    Any feedback?

    If you have no questions then I will post the next improvement which includes the command in the LCD_Write subroutine. I am now looking at using arrays as the next step.
    Here it is! including LOOKUP and ARRAYs.

    Code:
    '****************************************************************
    '*  Name    : I2CLCD.BAS                                        *
    '*  Author  : Steve Earl                                        *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 17/07/2014                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : Serial I2C LCD using PBP 2.5                      *
    '*          : PCF8574T I2C port expander 20x4 LCD               *
    '****************************************************************
    
    define OSC 40     ' set the correct speed here!!
    'DEFINE I2C_SLOW 1
    SDA         VAR PORTD.2       ; I2C Data pin
    SCL         VAR PORTD.3       ; I2C Clock Pin
    LCD_Addr CON 78               ; PCF8574T address
    LCD_Line_1 con $80
    LCD_Line_2 con $A8
    LCD_Line_3 con $94
    LCD_Line_4 con $D4
    LCD_BuffSize    CON 22
    
    LCD_Buff        VAR BYTE[LCD_BuffSize]
    'LCD_BuffAddr    CON EXT : @LCD_BuffAddr = _LCD_Buff
    LCD_BuffLen     VAR byte        ; 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 1=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
    
    
    seconds var byte
    minutes var byte
    hours   var byte
    
    Init:
    ;----[Initialize the LCD]-------------------------------------------------------
    PAUSE 250                           ; LCD Power-on delay
    LCD_Backlight = 1                   ; Backlight On
    LCD_RW = 0                          ; Write to LCD
    LCD_RS = 0                          ; Command Register
    LCD_WriteMode = 1                   ;-- Low Nibbles only
    LCD_Data = 3                        ; Software 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_buff[0] = $28                   ; Function Set, 4-bit, 2-line
    LCD_buff[1] = $0C                   ; Display ON
    LCD_buff[2] = $FE 
    LCD_buff[3] = $06                   ; Entry Mode
    LCD_buff[4] = $FE 
    LCD_buff[5] = $01                   ; Clear Screen
    lcd_bufflen = 6
    gosub LCD_Buff_Write
    
    ;Print Static Background to LCD
    for lcd_idx = 0 to 85
                   ;|                    | 20 characters
    lookup lcd_idx,["     Steve Earl     ",_            ;20
     $FE,lcd_line_2,"   Datageo Limited  ",_            ;+22 = 42
     $FE,lcd_line_3,"     **********     ",_            ;+22 = 64
     $FE,lcd_line_4,"      00:00:00      "],lcd_data    ;+22 = 86  (0-85)
     gosub lcd_write
    next
    
    ;Set up fixed buffer characters
    lcd_bufflen = 10                     ;8 characters in clock + $FE + LCD_Line
    LCD_buff[0] = $FE                   ;Command is next
    LCD_buff[1] = lcd_line_4 + 6        ;Move to line 4 col 7
    LCD_buff[4] = ":"                   ;Fixed characters
    LCD_buff[7] = ":"                   ;       "
    main:                               ;Count seconds and refresh the clock
    for hours = 0 to 23
        for minutes = 0 to 59
            for seconds = 0 to 59  
                LCD_buff[2] = (hours dig 1)+48  
                LCD_buff[3] = (hours dig 0)+48 
                LCD_buff[5] = (minutes dig 1)+48 
                LCD_buff[6] = (minutes dig 0)+48 
                LCD_buff[8] = (seconds dig 1)+48 
                LCD_buff[9]= (seconds dig 0)+48 
                gosub lcd_buff_write
                pause 1000
            next    ;second
        next    ;minute
    next    ;hour
    goto main
    
    LCD_WRITE:
    if lcd_data = $FE then
        lcd_commandMode = 1                 ;Next byte Command
        lcd_rs = 0                          ;Switch to Command Register
    else                                              
        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
        if lcd_commandmode then
         lcd_commandmode = 0              ;Reset to data
         lcd_rs = 1                       ;Switch to Data Register
         pause 2
        endif
    endif
    return
    
    Lcd_Buff_Write:
     for lcd_idx = 0 to lcd_bufflen - 1
      lcd_data=lcd_buff[LCD_idx]
      gosub LCD_Write
     next
    return

  3. #3
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: I2C lcd ( arduino ) with PICBASIC, help

    Steve,
    I am sorry for the delayed reply : I am not at home on those days.
    I had a look to your code : it is a good work and I am waiting to check it soon when the hardware is arrived .
    Thanks a lot for the assistance.
    I think your work will help a lot of people .
    Regards,
    Ambrogio

  4. #4
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: I2C lcd ( arduino ) with PICBASIC, help

    Quote Originally Posted by iw2fvo View Post
    Steve,
    I am sorry for the delayed reply : I am not at home on those days.
    I had a look to your code : it is a good work and I am waiting to check it soon when the hardware is arrived .
    Thanks a lot for the assistance.
    I think your work will help a lot of people .
    Regards,
    Ambrogio
    You are welcome. More is on the way as I am now investigating embedded strings.

  5. #5
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: I2C lcd ( arduino ) with PICBASIC, help

    I tried using the on board EEPROM by using DATA and READ.

    DATA "This is a string",0

    but it stores only alternate characters. I was surprised it worked at all but the alternate characters? There has to be a reason for it to work this way.

  6. #6
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: I2C lcd ( arduino ) with PICBASIC, help

    Quote Originally Posted by EarlyBird2 View Post
    I tried using the on board EEPROM by using DATA and READ.

    DATA "This is a string",0

    but it stores only alternate characters. I was surprised it worked at all but the alternate characters? There has to be a reason for it to work this way.
    It looks like this was fixed back in 2010.

    http://www.picbasic.co.uk/forum/showthread.php?t=12775

    My PBP2.46 is from 2005.

  7. #7
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: I2C lcd ( arduino ) with PICBASIC, help

    Quote Originally Posted by EarlyBird2 View Post
    It looks like this was fixed back in 2010.

    http://www.picbasic.co.uk/forum/showthread.php?t=12775

    My PBP2.46 is from 2005.
    The plot thickens! Here is an extract from this thread http://www.picbasic.co.uk/forum/show...7143#post97143 using DATA with strings and it works.

    Quote Originally Posted by dan-tron View Post
    EUREKA!
    Thanks guys! You have been a huge help. I have finally figured it out.
    My problem was more like 3 problems...

    1. I have never done EEPROM stuff before so I was lost to begin with.
    2. As Bruce pointed out, the mathematical order of operations in one line was not correct. I guess the BS2 thinks a little differently from PBP in that respect.
    3. My PicBasic Pro compiler was Version 2.30 form 2000. It seems that v2.30 supports the PIC 16F628 but the PIC 16F628A was implemented in a later version. I have upgraded my PBP to version 2.47 and now the EEPROM features work correctly on the 16F628A. When I successfully tested a 16F84A using the same code, that lead me to question the compiler.

    So for future generations and the benefit of other people like me, here's my WORKING code that is tested successfully on a PIC 16F628A with a 20MHz resonator compiled using PBP v2.47. It reads data from the Parallax RFID Reader Module, compares it against known values stored in EEPROM and allows or denies access accordingly. Enjoy.

    Code:
    CMCON = 7   
    DEFINE OSC 20	'Set oscillator in MHz
    
    ' -----[ Variables ]-------------------------------------------------------
    
    buf	VAR	Byte(10)	' RFID bytes buffer
    tagNum	VAR	Byte	' from EEPROM table
    idx	VAR	Byte	' tag byte index
    char	VAR	Byte	' character from table
    
    ' -----[ EEPROM Data ]-----------------------------------------------------
    
    Tag1	DATA	"100050A4B7"
    Tag2	DATA	"1000508E0A"
    Tag3	DATA	"10005091DC"
    Tag4	DATA	"100050203A"
    Tag5	DATA	"100050DA36"
    
    ' -----[ Initialization ]--------------------------------------------------
    
    HIGH portb.3	' turn off RFID reader
    LOW portb.6	' lock the door!
    Low portb.4	'Turn off LED
    
    ' -----[ Program Code ]----------------------------------------------------
    
    Main:
    
    LOW portb.3				' activate the reader
    SERIN2 portb.2, 396, [WAIT($0A), STR buf\10]	' wait for hdr + ID
    HIGH portb.3				' deactivate reader
    
    Check_List:
      FOR tagNum = 1 to 5			' scan through known tags
        FOR idx = 0 TO 9				' scan bytes in tag
        READ (((tagNum-1) * 10) + idx), char		' get tag data from table
        IF (char <> buf(idx)) THEN Bad_Char		' compare tag to table
        NEXT
        GOTO Tag_Found				' all bytes match!
    Bad_Char:					' try next tag
      NEXT
    
    Bad_Tag:
      tagNum = 0
      FREQOUT portb.5, 1000 */ $100, 115 */ $100	' groan
      PAUSE 1000
      GOTO Main
    
    Tag_Found:
      HIGH portb.6				' remove latch
      High portb.4				' Light LED
      FREQOUT portb.5, 2000 */ $100, 880 */$100	' beep
      LOW portb.6				' restore latch
      Low portb.4				' LED OFF
      GOTO Main

Similar Threads

  1. Interfacing with Arduino I2C LCD
    By norohs in forum Documentation
    Replies: 47
    Last Post: - 30th May 2017, 18:53
  2. I2c ext eeprom picbasic
    By iw2fvo in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th July 2011, 22:00
  3. How do I operate an Arduino I2C LCD
    By menta in forum General
    Replies: 8
    Last Post: - 13th July 2011, 02:28
  4. Still new to PicBasic - i2c questions
    By cometboy in forum mel PIC BASIC
    Replies: 4
    Last Post: - 13th November 2006, 18:27
  5. A little help with I2C and PICBASIC Pro
    By Qacer in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th January 2006, 16:13

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