help with pic16f887


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 61
  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default help with pic16f887

    Hello, this is the first I am using the pic16F887. My program is not showing anything on my LCD. I am sure the wiring is good. I would like to know if the initisialixing of the code is good. I took down an example from Melabs, but I saw there was no init strings as I usually do with my pic16F88.

    http://melabs.com/resources/samples/x1/pbp/serbufx.htm

    Ok here is my code. can somebody tell me if there is a problem with the begining ?? thanks
    K


    '/////////////////////////
    '// Define section //
    '/////////////////////////

    '///////////////////////////
    '// Interrupt section //
    '///////////////////////////



    '/////////////////////////
    '// 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
    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


    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////

    DEFINE CCP1_REG PORTB 'Define output port of pulses out
    DEFINE CCP1_BIT 3 'Define output port of pulses out

    Define ADC_BITS 10 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 ' Set sampling time in uSec

    'CMCON = 7 ' Disable analog comparator
    'ANSEL = %11111111 ' set ANx as analog ?????????????????????????????
    ADCON1 = %10000010 ' Right justified results for 10 bits
    ADCON0 = %11000001 ' Internal OSC, AN0, Configure and turn on A/D Module

    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input
    'TRISC = %00000000


    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////


    Battery_Switch var byte
    i var byte
    j var byte
    Battery_Charge_Counter var byte


    SDApin var PORTB.1 ' RTC data
    SCLpin var PORTB.2 ' RTC clock
    DB0 var byte[8]
    bat var byte

    Total_time var byte
    Write_Address var byte

    RTCSec var byte :RTCMin var byte :RTCHour var byte :RTCWDay var byte :RTCDay var byte :RTCMonth var byte :RTCYear var byte :RTCCtrl var byte

    Write_Address = 0

    lcdout $FE,1, "Bienvenue"
    lcdout $FE,$C0, "Welcome"
    pause 4000

    I2CWRITE SDApin,SCLpin,$D0,$00,[$00,$00,$00,$00,$00,$00,$00,$00] ' Write to DS1307 to start counter at ZERO
    PORTC.2 = Battery_Charge_Counter 'always 5v, push button to gnd

    '////////////////////////////////////////////////////
    '////////////////// PROGRAM /////////////////////////
    '////////////////////////////////////////////////////

    Mainloop:

    lcdout $FE,1, " Appuyé sur Start "
    lcdout $FE,$C0, "Press on Start to Begin"
    pause 200

    if PORTC.3 =0 then 'always 5v, push button to gnd

    I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl] 'read clock

    I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCSec] ' Write to External EEPROM
    Write_Address= Write_Address + 1
    I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCMin] ' Write to External EEPROM
    Write_Address= Write_Address + 1
    I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCHour] ' Write to External EEPROM
    Write_Address= Write_Address + 1
    I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCDay] ' Write to External EEPROM
    Write_Address= Write_Address + 1

    Battery_Charge_Counter = Battery_Charge_Counter + 1
    Endif

    if PORTC.4 =0 then 'always 5v, push button to gnd
    j=1
    bat = 0
    For i=0 to Battery_Charge_Counter
    I2CRead SDApin,SCLpin,$A0,bat,[RTCSec,RTCMin,RTCHour,RTCDay]
    lcdout $FE,1, " Time ", dec j
    lcdout $FE,$C0, dec RTCDay, dec RTCHour, dec RTCMin, dec RTCSec
    pause 3000
    j = j + 1
    i = i + 1
    bat = bat + 4
    next i
    Endif

    goto Mainloop
    End

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    The 887 has analog inputs on PORTB.
    So you need to enable Digital Inputs for the LCD to work on those pins ... or the I2C (RB1,RB2).

    Code:
    ANSELH = 0
    DT

  3. #3
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    HI, thanks for the help, I am getting a little bit further I have change my define section:

    '/////////////////////////
    '// Define section //
    '/////////////////////////

    @ device pic16F887, HS_OSC
    Define OSC 8 ' Set clock speed
    ANSELH = 0


    Here is the error:
    Executing: "C:\PBP2.60\PBPMPLAB.BAT" -ampasmwin -k# -p16F887 "C:\PBP2.60\eeprom.bas"
    Executing: "C:\PBP2.60\PBPW.EXE" -ampasmwin -k# -p16F887 "C:\PBP2.60\eeprom.bas"
    PICBASIC PRO(TM) Compiler 2.60A, (c) 1998, 2010 microEngineering Labs, Inc.
    All Rights Reserved.

    ERROR: Unable to execute mpasmwin.Warning[207] C:\PBP2.60\EEPROM.ASM 170 : Found label after column 1. (device)
    Error[122] C:\PBP2.60\EEPROM.ASM 170 : Illegal opcode (pic16F887)
    Warning[202] C:\PBP2.60\EEPROM.MAC 132 : Argument out of range. Least significant bits used.
    Halting build on first failure as requested.
    BUILD FAILED: Tue Nov 30 06:53:30 2010

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I just tested the same code I posted within Windows XP- Dos window. using the command pbp -p16F887 eeprom.bas.. I worked as it did yesterday. At home I have Windows 7, so I had to use Mplab and the same problem free program under windows XP is not compilable under windows 7...
    any thoughts

    .... the code under XP compiles with or without ANSELH = 0


    K

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


    Did you find this post helpful? Yes | No

    Default

    Code:
    @ device pic16F887, HS_OSC
    Is not correct for MPASAM.
    Try
    Code:
    @ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
    OSCCON = %01110000 '8 Mhz
    DEFINE OSC 8
    Last edited by mackrackit; - 30th November 2010 at 13:58. Reason: Setup for internal OSC
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi,
    thats the same line from the Inc file: 16F887.INC except for the oscillation.
    Why doesn't it work in MPlab ??? does it read the INC files ??? maybe there a configuration that I need to point to that file ??

    K

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


    Did you find this post helpful? Yes | No

    Default

    Why doesn't it work in MPlab ???
    Because @ DEVICE is for PM
    The inc file tells you that.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Ooo yes in the comment on top I will try this . I would rather learn MPlab then installing XP on my Win 7 machine.

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


    Did you find this post helpful? Yes | No

    Default

    You do not have to learn MPLAB. If you are using Micro Code Studio, or whatever for an editor, just tell it to use MPASM for the assembler.
    Dave
    Always wear safety glasses while programming.

  10. #10
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    HUmmm..
    I am using Notepad++ as my editor and just use the command in Dos to complie. Iwould use MPLAB strictly to program my chip.
    Now that I have Win7, I cannot compile in Dos and I found out how in MPLAB.

    Why would I want to use MPASM for the assembler as you mention earlier ??
    What is that going to give me more, any advantage ?
    thanks

    K

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


    Did you find this post helpful? Yes | No

    Default

    You can still use DOS.
    Here is the guts of a *.bat file. Running Win7 64 bit.
    Change the paths and file name as needed.
    Code:
    cd
    c:\pbp\pbpw -e -s -ac:\C:\Program Files (x86)\Microchip\MPASM Suite -p16f676  C:\PICPROGS\T1\T1.bas
    cd C:\Program Files (x86)\Microchip\MPASM Suite
    MPASMWIN.exe /e C:\PICPROGS\T1\T1.ASM
    cd\
    Why would I want to use MPASM for the assembler as you mention earlier ??
    Because you need it for the 18F parts and basically more advanced "stuff".
    Last edited by mackrackit; - 30th November 2010 at 18:55.
    Dave
    Always wear safety glasses while programming.

  12. #12
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Ok I am home now and ready to make it work.
    I tried to compile it and it would not work I went into the the build configuration menu and removed the pasmwin option.
    here is what I am getting now in MPLAB:

    Executing: "C:\PBP2.60\PBPMPLAB.BAT" -p16F887 "C:\PBP2.60\eeprom.bas"
    Executing: "C:\PBP2.60\PBPW.EXE" -p16F887 "C:\PBP2.60\eeprom.bas"
    PICBASIC PRO(TM) Compiler 2.60A, (c) 1998, 2010 microEngineering Labs, Inc.
    All Rights Reserved.

    ERROR: Unable to execute PM.EXE.Halting build on first failure as requested.
    BUILD FAILED: Tue Nov 30 18:26:11 2010


    and my code:
    Code:
    '/////////////////////////
    '// Define section //
    '/////////////////////////
    
    @ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
    OSCCON = %01110000 '8 Mhz
    DEFINE OSC 8
    ANSELH = 0

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


    Did you find this post helpful? Yes | No

    Default

    Up in post #3 you were using mpasm, now you are using PM with MPLAB...
    I do not understand.
    Dave
    Always wear safety glasses while programming.

  14. #14
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default ?????

    owowo I am lost now

    -I was previously using Picbasic pro under dos.
    -Now I installed windows 7 64bit, I can no longer use dos. I followed the instruction from Melabs to choose PBPMPLAB.BAT under MPLAB 8.56. I thought I was using the picbasic pro module and MPLAB and an interface between the two.

    I want to keep it simple... either way , dont know where to go from here

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


    Did you find this post helpful? Yes | No

    Default

    Try installing the free version of Micro Code Studio that came with PBP.
    Then in the toolbar click View - Compiler and Program Options.
    There will be tabs on the dialog box.
    Under Compiler click find automatically.
    Under the Assembler tab check Use MPASAM and click find automatically.

    Then when you want to compile hit the F9 key.

    That is the simplest way.
    Dave
    Always wear safety glasses while programming.

  16. #16
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Gee not compiling giving me one error and one warning:

    -Overwriting previous address content (2007)
    -Argument out of range. Least significant bits used


    and I did select the right chip....

  17. #17
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    ok knocked off this line
    @ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF


    and its only giving the warning :-Argument out of range. Least significant bits used

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


    Did you find this post helpful? Yes | No

    Default

    If I remove the "e"
    Code:
    lcdout $FE,1, " Appuyé sur Start "
    And remove the space at
    RTCYear
    Code:
    I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl] 'read clock
    It compiles.
    Dave
    Always wear safety glasses while programming.

  19. #19
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi I finally got it to compile. Now I have a small problem writing to memory. Its actually not writing. and on the LCD its always showing 0000
    I have A0 and A1 to ground and A2 to 5v. on the eeprom 24LC1025, pins are configued correctly, is there anything in the program that will not allow a writing ???


    Code:
    '/////////////////////////
    '// EEPROM  test program
    '//	Using the PIC16F887
    '//
    '/////////////////////////
    
    '/////////////////////////
    '// Define section //
    '/////////////////////////
    
    '@ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
    '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
    
    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////
    
    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input
    TRISC = %11000000 ' Set PORTC to all output
    
    
    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////
    
    Battery_Switch var byte
    i var byte
    j var byte
    Battery_Charge_Counter var byte
    
    
    SDApin var PORTC.4                     ' RTC data 
    SCLpin var PORTC.3                     ' RTC clock
    bat var byte
    Write_Address var byte
    
    RTCSec var byte :RTCMin var byte :RTCHour var byte :RTCWDay var byte :RTCDay var byte :RTCMonth var byte :RTCYear var byte :RTCCtrl var byte 
    
    
    lcdout $FE,1, "Bienvenue"
    lcdout $FE,$C0, "Welcome" 
    pause 1500
    
    I2CWRITE SDApin,SCLpin,$A0,$00,[$03,$02,$05,$00,$00,$00,$00,$00] ' Write to  memory
       pause 20
       
    
    
    '////////////////////////////////////////////////////
    '////////////////// PROGRAM /////////////////////////
    '////////////////////////////////////////////////////
    
    Mainloop:
    
    	lcdout $FE,1, "Appuye sur Record"
    	lcdout $FE,$C0, "ou sur Show"
    	pause 200
    	
    
     
    if PORTC.7 =0 then      'always 5v, push button to gnd
    			j=1
    
    			I2CRead SDApin,SCLpin,$A0,$00,[RTCSec,RTCMin,RTCHour,RTCDay]
    			lcdout $FE,1, "  Time ", dec j
    			lcdout $FE,$C0, dec RTCDay, dec RTCHour, dec RTCMin, dec RTCSec
    			pause 2000
    
    Endif
    			
    goto Mainloop
    End

  20. #20
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    O by the way, while compiling another program, MicroCode studio does not seem to tell me where the error is unlike compiling in Picbasic pro. It only gives me this error:
    least significant bits used
    wow really helpful.... 600 lines of code

  21. #21
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Good morning.
    Ok a good night sleep was excellent. For the first message last night I posted at 23:03, I believe I may have to play around with the registers.. SSPCON... or perhaps others.
    Also, I may have to use A0 for write and A1 for read on the 24LC1025 EEPROM chip. I will try that tonight.

    For the second message, I still have no idea, the compiler just tell me there is a problem somewhere in the code. There must be a way ton configure MicroCode studio to be more specific, is there ?

    K
    Last edited by lerameur; - 2nd December 2010 at 13:41.

  22. #22
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi I copied this program from a thread. It is suppose to be working, but I keep getting zeros in on my LCD:

    anyone ?

    Code:
    '/////////////////////////
    '// EEPROM and test program
    '//	Using the PIC16F887
    '//
    '/////////////////////////
    
    '/////////////////////////
    '// Define section //
    '/////////////////////////
    
    '@ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
    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
    DB0 var byte[8]
    
    TRISB= %11111111
    TRISC= %00000000
    gosub write_eeprom
    
    read_eeprom:                          ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
    I2CREAD SDA,SCL,$A1,$00,[STR DB0\8] ' Read 8 bytes from EEPROM
    lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1],":",hex2 DB0[0]    'bit 0=sec, bit 1=min, bit 2=hrs
    lcdout $fe,$c0,"Date=",hex2 DB0[4],":",hex2 DB0[5],":",hex2 db0[6]  'bit 4=day, bit 5=month, bit 6=year
     pause 50
    goto read_eeprom
    end
    
    Write_eeprom:                          ' Set time & date to 19:00:00  14th Feb 201
    pause 1000
    I2CWRITE SDA,SCL,$A0,$00,[$00,$00,$19,$7,$14,$2,$10,$80] ' Write to EEPROM
    pause 10
    RETURN

  23. #23
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default What ??

    humm
    ok
    from this thread:http://www.picbasic.co.uk/forum/showthread.php?t=12671
    The huy has a pic16F887 like mine, but his SDA and SCL are on portB2 and B1... My datasheet tells me C4 and C3......

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


    Did you find this post helpful? Yes | No

    Default

    Code:
    read_eeprom:                          ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
    I2CREAD SDA,SCL,$A1,$00,[STR DB0\8] ' Read 8 bytes from EEPROM
    lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1],":",hex2 DB0[0]    'bit 0=sec, bit 1=min, bit 2=hrs
    lcdout $fe,$c0,"Date=",hex2 DB0[4],":",hex2 DB0[5],":",hex2 db0[6]  'bit 4=day, bit 5=month, bit 6=year
    Are you using a RTC or EEPROM?
    Dave
    Always wear safety glasses while programming.

  25. #25
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    originally I was using both, but for trouble shooting purposes i am using only the eeprom module 24LC1025 from microchip. After I get I2C going i will add.

    K
    I should of commented out some information,
    Last edited by lerameur; - 3rd December 2010 at 00:21.

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


    Did you find this post helpful? Yes | No

    Default

    Take a look at this and see if you can get the EEPROM working. It is about as simple as it gets for testing.
    http://www.picbasic.co.uk/forum/cont...-EEPROM-Part-1
    Dave
    Always wear safety glasses while programming.

  27. #27
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    lerameur, I just thought I would ask... Where is the Write Protect line tied? It should not be tied to VCC as this would Write Protect the device. I have used these 24LC1025's over the past few years and have never had a problem with them. I use the I2CWRITE and I2CREAD commands. Just thought I would ask....

    Dave Purola,
    N8NTA

  28. #28
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Humm I will check when I get back home this afternoon. I remember reading this in the pin description from the 20LC1025 pdf manual. Maybe I made a mistake, and at this point I truly hope i did, thanks for the cue.

  29. #29
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi, which picchip are you using ? would you mind posting a short code with the chip you are using. I have a few different chip (F88, F84a, F628.. ) and would like to replicate it at my home.
    The WP port is set gnd... tried both configuration anyway and still would not work, always showing zeros
    Last edited by lerameur; - 3rd December 2010 at 20:05.

  30. #30
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    oH MY !! it is working now, Added pull up resistors on SDA , CLK ports.. .
    I think my memory management is not good.
    It recods the first time. But all the time it shows on the LCD is 255, all for seconds, minutes hours,.....
    Can somebody tell me why so ??


    Code:
    '/////////////////////////
    '// EEPROM and test program
    '//	Using the PIC16F887
    '//
    '/////////////////////////
    
    '/////////////////////////
    '// Define section //
    '/////////////////////////
    
    '@ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
    OSCCON = %01110000 '8 Mhz
    DEFINE OSC 8
    ANSELH = 0
    
    '///////////////////////////
    '// Interrupt section //
    '///////////////////////////
    
    
    '/////////////////////////
    '// 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
    
    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////
    
    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input
    TRISC = %11000000 ' Set PORTC to all output
    
    
    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////
    
    Battery_Switch var byte
    i var byte
    j var byte
    Battery_Charge_Counter var byte
    
    
    SDApin var PORTC.4                     ' RTC data 
    SCLpin var PORTC.3                     ' RTC clock
    bat var byte
    Write_Address var byte
    
    RTCSec var byte :RTCMin var byte :RTCHour var byte :RTCWDay var byte :RTCDay var byte :RTCMonth var byte :RTCYear var byte :RTCCtrl var byte 
    
    
    lcdout $FE,1, "Bienvenue"
    lcdout $FE,$C0, "Welcome" 
    pause 4000
    
    I2CWRITE SDA,SCL,$D0,$00,[$00,$00,$19,$7,$14,$2,$10,$80] ' Write to DS1307
    pause 10
    
    Battery_Charge_Counter = 0 
     Write_Address = 0
    '////////////////////////////////////////////////////
    '////////////////// PROGRAM /////////////////////////
    '////////////////////////////////////////////////////
    
    Mainloop:
    
    	lcdout $FE,1, "Appuye sur Record"
    	lcdout $FE,$C0, "ou sur Show"
    	pause 200
    	
    if PORTC.6 =0 then    'always 5v, push button to gnd
    
    		I2CRead SDApin,SCLpin,$D1,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]  'read clock on DS1337
    
    		I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCSec] ' Write to External EEPROM
    		Write_Address= Write_Address + 1
    		I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCMin] ' Write to External EEPROM
    		Write_Address= Write_Address + 1
    		I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCHour] ' Write to External EEPROM
    		Write_Address= Write_Address + 1
    		I2CWRITE SDApin,SCLpin,$A0,Write_Address,[RTCDay] ' Write to External EEPROM
    		Write_Address= Write_Address + 1	
    		
    		Battery_Charge_Counter = Battery_Charge_Counter + 1
    Endif
     
    if PORTC.7 =0 then      'always 5v, push button to gnd
    			j=1
    			bat = 0
    			For i=0 to Battery_Charge_Counter/4
    			I2CRead SDApin,SCLpin,$A1,bat,[RTCSec,RTCMin,RTCHour,RTCDay]
    			lcdout $FE,1, "  Time ", dec j
    			lcdout $FE,$C0, dec RTCDay,"  ", dec RTCHour,":", dec RTCMin,":", dec RTCSec
    			pause 3000
    			j = j + 1
    			i = i + 1
    			bat = bat + 4
    			next i
    Endif
    			
    goto Mainloop
    End

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


    Did you find this post helpful? Yes | No

    Default

    Is it a typo or are you trying to use the RTC and EEPROM on the same PIC pins?
    Dave
    Always wear safety glasses while programming.

  32. #32
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    HI,

    yes they have to be on the same picchip pins, they are one pissibility for the chip, I will try some pauses in betwwen read and wrire, but I doubt it will do any thing.

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


    Did you find this post helpful? Yes | No

    Default

    yes they have to be on the same picchip pins, they are one pissibility for the chip
    Have you used all of the other pins for something else?
    I do not think you can share a RTC and EEPROM on the same bus. Two EEROMS yes.
    Free up some pins or move to a chip that has more.
    Dave
    Always wear safety glasses while programming.

  34. #34
    Join Date
    Sep 2006
    Posts
    747


    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..

  35. #35
    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.

  36. #36
    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..

  37. #37
    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!

  38. #38
    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.

  39. #39
    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 18:22.

  40. #40
    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.

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