I2c & Variable Size


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136

    Default I2c & Variable Size

    SkiMask - thanks for your speedy reply. The code is below. The problem variable is
    called 'LoopCounter' and is set at WORD length. THe loop does not increment.

    If LoopCounter is made a BYTE all works as it should. Help?

    'File...... Test Program
    '
    ' Philips PCF8583 I2C RTC gives the time/date
    ' PIC16F877A on Mikro board is run @ 20 MHz
    ' After amendment to pbppic14.lib - now works at 20 MHz
    '
    ' PIC LCD RS232 RTC DS1820 EEPROM
    ' --- --- ----- --- ------ ------
    ' C3 SCL ' I2C device, TRC clock
    ' C4 SDA ' I2C device, RTC data
    ' D2 RS ' LCD reset
    ' D3 EBIT ' LCD enable
    ' D4 DB4 ' LCD data 4
    ' D5 DB5 ' LCD data 5
    ' D6 DB6 ' LCD data 6
    ' D7 DB7 ' LCD data 7
    '
    ' ----[ Variables ]---------------------------------------------------
    '
    DataPin var PORTC.4 ' RTC data
    ClockPin var PORTC.3 ' RTC clock
    Mins var byte ' Minutes on RTC
    Hours var byte ' Hours on RTC
    Days var byte ' Days on TTC
    Months var byte ' Months on RTC
    Time var byte[3] ' Holds months,days,hours,mins read from RTC
    Dtime var byte[3] ' Holds months,days,hours,mins in HEX format
    Units var byte ' Derived from Time
    Tens var byte ' Derived form Time
    CControl var byte ' Control byte for RTC
    '
    LoopCounter var word ' The 'problem' loop counter
    J var byte ' Loop counter for RTC read/write
    '
    ' ----[ Includes / Defines ]------------------------------------------
    '
    define OSC 20 'Now works at 20 MHz
    DEFINE LCD_DREG PORTD 'Define PIC port used for LCD Data lines
    DEFINE LCD_DBIT 4 'Define first pin of portb connected to LCD DB4
    DEFINE LCD_RSREG PORTD 'Define PIC port used for RS line of LCD
    DEFINE LCD_RSBIT 2 'Define Portb pin used for RS connection
    DEFINE LCD_EREG PORTD 'Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT 3 'Define PortB pin used for E connection
    DEFINE LCD_BITS 4 'Define the 4 bit communication mode to LCD
    DEFINE LCD_LINES 2 'Define using a 2 line LCD
    DEFINE LCD_COMMANDUS 2000 'Define delay time between sending LCD commands
    DEFINE LCD_DATAUS 50 'Define delay time between data
    '
    ' -----[ Initialization ]--------------------------------------------------
    '
    LoopCounter = 0 'EEPROM address counter - when I get it working!!!
    J = 0 'Time[x] loop counter
    CControl = %10100000 'Control word for RTC
    '
    ' -----[ Main Program ]----------------------------------------------------
    Start:
    lcdout $fe,1,"Test Program"
    Pause 1000
    lcdout $fe,1

    ' -----[ Set Time ]-------------------------------------------------------
    TimeSet
    i2cwrite DataPin, ClockPin, CControl, $0, [%00001000]
    'Control register for RTC, sets bit 3 to mask unwanted bits in time/date registers
    i2cwrite DataPin, ClockPin, CControl, $2, [%00000000,%01011001,%00100011,%00110000,%00010001]
    'BCD code, sets time registers to 0 Sec, 59 Min, 23 Hr, 30 Day, Month 11

    ' -----[ Main Program ]-----------------------------------------------------
    Run:

    ' Time Read
    i2cread DataPin, ClockPin, %10100000, $3, [Time[0], Time[1], Time[2], Time[3]]
    Pause 20
    'Time[0] = Min, Time[1] = Hr, Time[2] = Day, Time[3] = Month

    ' Time Show
    for J = 0 to 3 'this works because the 'mask' is set and Time[x] bytes are all in BCD format.
    Tens = Time[J] & %11110000
    Tens = Tens>>4
    Tens = Tens * 10
    Units = Time[J] & %00001111
    DTime[J] = Tens + Units 'Dtime is used to "Display Time"
    next j

    LCDout $FE, 1, "Mth: ", Dec DTime[3], " Day: ", dec DTime[2]
    lcdout $FE, $C0, "Hrs: ", dec Dtime[1], " Min: ", DEC Dtime[0]
    pause 1000

    ' Loop Counter
    lcdout $FE,1,"Loop Ctr: ", DEC LoopCounter
    pause 1000

    LoopCounter = LoopCounter + 1

    goto run

    END


    Sorry - the code has come out in a rather messy layout?

  2. #2
    skimask's Avatar
    skimask Guest

    Default

    DEFINEs before everything else...that's my theory...ALL CAPs for all DEFINEs

    DEFINE OSC 20 'Now works at 20 MHz

    No COLON : after TimeSet

    Don't use the ' or " in the comments after a line, except for the character defining the comment itself. When you get into mixing assembly with PBP, it'll mess you up in the future.

    Maybe somebody else has some good ideas, 'cause I'm out at the moment...
    Last edited by skimask; - 15th February 2008 at 19:48.

  3. #3
    skimask's Avatar
    skimask Guest

    Default

    Does this work? (I know it's not formatted like you had it, just see if it works)
    Code:
    DEFINE OSC 20               'Now works at 20 MHz
    DEFINE LCD_DREG	PORTD	    'Define PIC port used for LCD Data lines
    DEFINE LCD_DBIT	4		    'Define first pin of portb connected to LCD DB4
    DEFINE LCD_RSREG PORTD	    'Define PIC port used for RS line of LCD
    DEFINE LCD_RSBIT 2		    'Define Portb pin used for RS connection
    DEFINE LCD_EREG	PORTD   	'Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT	3		    'Define PortB pin used for E connection
    DEFINE LCD_BITS	4		    'Define the 4 bit communication mode to LCD
    DEFINE LCD_LINES 2		    'Define using a 2 line LCD
    DEFINE LCD_COMMANDUS 2000	'Define delay time between sending LCD commands
    DEFINE LCD_DATAUS 50	    'Define delay time between data
    sda var portc.4:scl var portc.3:min var byte:hour var byte:day var byte:month var byte
    time var byte[3]:dtime var byte[3]:units var byte:tens var byte:control var byte
    loopcounter var word:loopcnt1 var loopcounter.lowbyte:loopcnt2 var loopcounter.highbyte
    j var byte:loopcnt1=0:loopcnt2=0:j=0:control=$a0
    Start:     lcdout $fe,1,"Test Program":Pause 1000:i2cwrite sda,scl,control,0,[8]
    i2cwrite sda,scl,control,2,[0,$59,$23,$30,$11]
    run:     i2cread sda,scl,control,3,[Time[0],Time[1],Time[2],Time[3]]:Pause 20
    for J=0 to 3:tens=((time[J] & $f0)>>4)*10:units=time[j] & $f:dtime[j]=tens+units:next j
    lcdout $fe,1,"Mth: ",dec dtime[3]," Day: ",dec dtime[2]
    lcdout $fe,$c0,"Hrs: ",dec dtime[1]," Min: ",dec dtime[0]:pause 1000
    lcdout $fe,1,"Loop Ctr:",dec5 loopcounter:pause 1000   'might want to knock this down so the program runs a bit faster eh?
    loopcnt1=loopcnt1+1:if loopcnt1 = 0 then loopcnt2=loopcnt2+1
    goto run
    END


    (HCM strikes again!)

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  3. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  4. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  5. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36

Members who have read this thread : 0

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