help with pic16f887


Closed Thread
Results 1 to 40 of 61

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Are you saying that SDA and SCL can be used on almost any ports?? I thought they where dedicated to a specific port each..
    Yup, I2C is a bit-banging solution, sorta like SHIFTIN/SHIFTOUT. Remember the wiki post I linked you to.

    Just be sure to have the analog turned off on the PIN you choose. MCLR does not work either. Other than that you should be good.
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I read in one of Melanie's post that this was possible because each chip uses a different code to identify itself $D0 for RTC and $A0 for EEPROM..

  3. #3
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    Hmmm, I'm confused myself. I thought I2C allowed any number of devices, same or different since it is an addressable bus.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    I read in one of Melanie's post that this was possible because each chip uses a different code to identify itself $D0 for RTC and $A0 for EEPROM..
    That makes sense, would seem to be the purpose of an I2C bus. I will have to try it...

    But, you can make the RTC and EEPROM work separately?
    Maybe I have missed something else.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Ok I am backing up a bit now, I am trying to make things work seperatly ( just your post mackackit)
    First: I can make the DS1337 work ONLY with SDA and CLK port of the pichip 16F887 (40 pins). I tried using D.0 and D.3 which are digital ports but nothing.
    Here is the code for that. other ports did not work:
    Code:
    OSCCON = %01110000 '8 Mhz
    DEFINE OSC 8
    ANSELH = 0
    
    
    '/////////////////////////
    '// LCD configuration //
    '/////////////////////////
    
    DEFINE LCD_DREG PORTB 	' Set LCD Data port
    DEFINE LCD_DBIT 4 		' Set starting Data bit (0 or 4) if 4-bit bus  RB.4, RB.5, RB.6, RB.7
    DEFINE LCD_RSREG PORTB 	' Set LCD Register Select port
    DEFINE LCD_RSBIT 1 		' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB	' Set LCD Enable port
    DEFINE LCD_EBIT 0 		' Set LCD Enable bit
    DEFINE LCD_BITS 4 		' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
    DEFINE LCD_LINES 2 		' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000
    pause 1000
    
    
    SDA var PORTC.4                     ' RTC data 
    SCL var PORTC.3                     ' RTC clock
    RTCSec var byte :RTCMin var byte :RTCHour var byte :RTCWDay var byte :RTCDay var byte 
    
    
    TRISB= %11111111
    TRISC= %00000000
    TRISD= %11000000 
    
    pause 1000
    I2CWRITE SDA,SCL,$D0,$00,[$08,$03,$08,$7,$07,$2,$10,$80] ' Write to DS1307
    
    	     pause 50
    		
    read_ds:                          ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
    			I2CRead SDA,SCL,$D1,$00,[RTCSec,RTCMin,RTCHour,RTCDay]
    			lcdout $FE,1, "  Time ", dec 1
    			lcdout $FE,$C0, "Day:",dec RTCDay,"  ", dec RTCHour,":", dec RTCMin,":", dec RTCSec
     pause 100
    goto read_ds
    end

    Second:
    I cannot store or read anything from the 24LC1025 EEPROM chip.. Here si the code for that:
    Code:
    OSCCON = %01110000 '8 Mhz
    DEFINE OSC 8
    ANSELH = 0
    
    
    '/////////////////////////
    '// LCD configuration //
    '/////////////////////////
    
    DEFINE LCD_DREG PORTB 	' Set LCD Data port
    DEFINE LCD_DBIT 4 		' Set starting Data bit (0 or 4) if 4-bit bus  RB.4, RB.5, RB.6, RB.7
    DEFINE LCD_RSREG PORTB 	' Set LCD Register Select port
    DEFINE LCD_RSBIT 1 		' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB	' Set LCD Enable port
    DEFINE LCD_EBIT 0 		' Set LCD Enable bit
    DEFINE LCD_BITS 4 		' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
    DEFINE LCD_LINES 2 		' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000
    pause 1000
    
    
    SDA var PORTC.4                     ' RTC data 
    SCL var PORTC.3                     ' RTC clock
    RTCSec var byte :RTCMin var byte :RTCHour var byte :RTCWDay var byte :RTCDay var byte 
    
    
    TRISB= %11111111
    TRISC= %00000000
    TRISD= %11000000 
    
    pause 1000
    I2CWRITE SDA,SCL,$A0,$00,[$08,$03,$08,$7,$07,$2,$10,$80] ' Write to eeprom
    
    	     pause 50
    		
    read_eeprom:                          ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
    			I2CRead SDA,SCL,$A1,$00,[RTCSec,RTCMin,RTCHour,RTCDay]
    			lcdout $FE,1, "  Time ", dec 1
    			lcdout $FE,$C0, "Day:",dec RTCDay,"  ", dec RTCHour,":", dec RTCMin,":", dec RTCSec
     pause 100
    goto read_eeprom
    end
    Last edited by lerameur; - 5th December 2010 at 19:22.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I stand corrected... again...
    Below is working code with an EEPROM and RTC on the same bus!! COOL!! I love it when I learn something new.
    It writes and reads a series of values to different addresses and reads the time from the RTC.
    Code:
    'FL PIC18F6680
    '  18F6680(PIC)  24FC1025(EEPROM)  DS1337C(RTC)
        DEFINE OSC 20
        @ __CONFIG    _CONFIG1H, _OSC_HS_1H
        @ __CONFIG    _CONFIG2H, _WDT_OFF_2H & _WDTPS_128_2H
        @ __CONFIG    _CONFIG4L, _LVP_OFF_4L
        DEFINE LCD_DREG     PORTG
        DEFINE LCD_DBIT     0
        DEFINE LCD_RSREG    PORTE
        DEFINE LCD_RSBIT    0
        DEFINE LCD_EREG     PORTE
        DEFINE LCD_EBIT     1
        DEFINE LCD_BITS     4
        DEFINE LCD_LINES    4
        DEFINE LCD_COMMANDUS    3000
        DEFINE LCD_DATAUS   150
    
        DPIN    VAR PORTB.2    'I2C DATA PIN
        CPIN    VAR PORTB.1    'I2C CLOCK PIN
        ADDR    VAR WORD
        DATI    VAR BYTE
        DATO    VAR BYTE
    
        ' -------------- RTC definitions -----------------
    RTC CON     %11010000
    SecReg CON $00 ' seconds address (00 - 59) ' MSB of SecReg must be set to a 0 to enable RTC
    ContReg CON $0E ' control register
    cntrl CON %00000000 ' sets the SQW/OUT to 1Hz pulse, logic level low
    I2CWRITE DPIN, CPIN, RTC, ContReg, [cntrl]
    ' The variable below holds the values entered:
    ' entered by the user
    sec VAR BYTE ' seconds
    mins VAR BYTE ' minutes
    hr VAR BYTE ' hours
    day VAR BYTE ' day
    date VAR BYTE ' date
    mon VAR BYTE ' month
    yr VAR BYTE ' year
    
        PAUSE 1000
    GOSUB SET_RTC
        START2:  ADDR = 0 : DATO = 0
        FOR ADDR = 0 TO 15
        DATO = DATO + 2
        I2CWRITE DPIN,CPIN,$A0,ADDR,[DATO]
        PAUSE 10
        NEXT ADDR
        PAUSE 100
        GOSUB READ_RTC
        ADDR = 0
        FOR ADDR = 0 TO 15
        I2CREAD DPIN,CPIN,$A0,ADDR,[DATI]
        PAUSE 100
        LCDOUT $FE,1," DATI  ",DEC3 DATI
        LCDOUT $FE,$C0,"ADDR ",DEC ADDR
        LCDOUT $FE,$90,"TIME ", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec
        PAUSE 1000
        NEXT ADDR
        GOTO START2
    
    READ_RTC:
    I2CREAD DPIN, CPIN, RTC, SecReg, [sec,mins,hr,day,date,mon,yr]
    RETURN
    
    '#################################################
    SET_RTC:
    yr = $10
    mon = $11
    date = $05
    sec = $00
    mins = $41
    hr = $11
    I2CWRITE DPIN, CPIN, RTC, SecReg, [sec,mins,hr,day,date,mon,yr]
    RETURN
    Now to look over your code.
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi dave, does that code work?? you have $A0 for both read and write cycle .. I thought it was write $A0 and Read $A1
    K

  8. #8
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    It works, I have it running on a bread board right now.
    I thought it was write $A0 and Read $A1
    PBP takes care of flipping that bit for you.
    Dave
    Always wear safety glasses while programming.

  9. #9
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I am looking over you pic pin configuration for the SDA and CLK pins but and you have them on DPIN VAR PORTB.2 'I2C DATA PIN
    CPIN VAR PORTB.1 'I2C CLOCK PIN
    What is the purpose of having SDA, CLK pins on the chip if we can use any port. Unless I am reading another chip besides the PIC18F6680.

    I also how do you make the difference between writing to the RTC and writing to the DS1337 then ?
    K

  10. #10
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    What is the purpose of having SDA, CLK pins on the chip if we can use any port. Unless I am reading another chip besides the PIC18F6680.
    The hardware ports are good for hardware interrupts and maybe a little speed. But I have not noticed much speed wise myself.

    I also how do you make the difference between writing to the RTC and writing to the DS1337 then ?
    I think you mean writing to the RTC and EEPROM...
    It IS in the Control-Address part of the command as you mentioned earlier from the post Mel did.
    The EEPROM used $A0 for the Control and a value for the location where the data is stored. In my code it is the VAR ADDR.

    The RTC uses %11010000 for the Control and $00 for the Address part. The big thing is the control byte.
    I2CWRITE DPIN,CPIN,$A0,ADDR,[DATO] 'EEPROM
    I2CREAD DPIN, CPIN, RTC, SecReg, [sec,mins,hr,day,date,mon,yr]
    Dave
    Always wear safety glasses while programming.

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