PDA

View Full Version : help with pic16f887



lerameur
- 30th November 2010, 03:59
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

Darrel Taylor
- 30th November 2010, 04:58
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).


ANSELH = 0

lerameur
- 30th November 2010, 11:56
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

lerameur
- 30th November 2010, 12:56
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

mackrackit
- 30th November 2010, 13:52
@ device pic16F887, HS_OSC
Is not correct for MPASAM.
Try

@ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
OSCCON = %01110000 '8 Mhz
DEFINE OSC 8

lerameur
- 30th November 2010, 14:15
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

mackrackit
- 30th November 2010, 14:19
Why doesn't it work in MPlab ???
Because @ DEVICE is for PM
The inc file tells you that.

lerameur
- 30th November 2010, 14:26
Ooo yes in the comment on top :) I will try this . I would rather learn MPlab then installing XP on my Win 7 machine.

mackrackit
- 30th November 2010, 14:46
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.

lerameur
- 30th November 2010, 15:42
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

mackrackit
- 30th November 2010, 18:50
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.


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

lerameur
- 30th November 2010, 23:29
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:

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

@ __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
OSCCON = %01110000 '8 Mhz
DEFINE OSC 8
ANSELH = 0

mackrackit
- 1st December 2010, 00:14
Up in post #3 you were using mpasm, now you are using PM with MPLAB...
I do not understand.

lerameur
- 1st December 2010, 00:53
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

mackrackit
- 1st December 2010, 01:01
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.

lerameur
- 1st December 2010, 02:26
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....

lerameur
- 1st December 2010, 02:30
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

mackrackit
- 1st December 2010, 03:28
If I remove the "e"

lcdout $FE,1, " Appuyé sur Start "
And remove the space at
RTCYear

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

lerameur
- 2nd December 2010, 04:03
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 ???



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

lerameur
- 2nd December 2010, 04:31
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 :)

lerameur
- 2nd December 2010, 12:45
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

lerameur
- 2nd December 2010, 22:59
Hi I copied this program from a thread. It is suppose to be working, but I keep getting zeros in on my LCD:

anyone ?


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

lerameur
- 2nd December 2010, 23:10
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......

mackrackit
- 2nd December 2010, 23:27
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?

lerameur
- 2nd December 2010, 23:50
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,

mackrackit
- 3rd December 2010, 07:12
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/content.php?r=165-Serial-EEPROM-Part-1

Dave
- 3rd December 2010, 11:55
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

lerameur
- 3rd December 2010, 14:03
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.

lerameur
- 3rd December 2010, 19:34
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

lerameur
- 4th December 2010, 23:05
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 ??



'/////////////////////////
'// 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,RTCY ear,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

mackrackit
- 4th December 2010, 23:17
Is it a typo or are you trying to use the RTC and EEPROM on the same PIC pins?

lerameur
- 5th December 2010, 14:37
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.

mackrackit
- 5th December 2010, 15:16
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.

lerameur
- 5th December 2010, 16:50
Are you saying that SDA and SCL can be used on almost any ports?? I thought they where dedicated to a specific port each..

mackrackit
- 5th December 2010, 16:57
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.

lerameur
- 5th December 2010, 17:04
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..

cncmachineguy
- 5th December 2010, 17:18
Hmmm, I'm confused myself. I thought I2C allowed any number of devices, same or different since it is an addressable bus.

mackrackit
- 5th December 2010, 17:19
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.

lerameur
- 5th December 2010, 18:15
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:

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:

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

mackrackit
- 5th December 2010, 18:48
I stand corrected... again...:o
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.


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

lerameur
- 5th December 2010, 18:52
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

mackrackit
- 5th December 2010, 19:00
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.

lerameur
- 5th December 2010, 19:22
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

mackrackit
- 5th December 2010, 20:22
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]

lerameur
- 5th December 2010, 21:44
Hi
thanks you for the hand. I still do not what is wrong with my code. I will work on it tonight. Your code does work on my chip with both RTC and EEPROM chip. thank you again. I corrected a few things with my code, but I cannot getthe full thing working. What is odd is that if I run my program, first thing I do is press PORTC.7 to LCDout, I see the time I put in the RTC, but if I do it again it shows me only 255 255 255 ...

K

lerameur
- 6th December 2010, 04:06
Ouff, ok I got it narrowed down now to one function, and I really really would like to know why it is not working. Here is the mainloop that is not working, the red part is where the differences are, The output is always FF.

Mainloop:
lcdout $FE,1, "Appuye sur Record"
lcdout $FE,$C0, "ou sur Show"
pause 100

if PORTC.6 =0 then

GOSUB READ_RTC
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec]
Write_Address = Write_Address +1
PAUSE 10
I2CWRITE DPIN,CPIN,$A0,Write_Address,[mins]
Write_Address = Write_Address +1
PAUSE 10
I2CWRITE DPIN,CPIN,$A0,Write_Address,[hr]
Write_Address = Write_Address +1
PAUSE 10
Battery_Charge_Counter = Battery_Charge_Counter + 1
Endif

if PORTC.7 =0 then

bat =0
For i=0 to 2
I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr]
PAUSE 10
LCDOUT $FE,1," Time ",DEC3 i+1
LCDOUT $FE,$C0,"AD",DEC Write_Address,":", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec
PAUSE 1000
bat = bat+3
next i
Endif

GOTO Mainloop
End

and is is the working good, looks similar, but in the none working code I need a loop.

Mainloop:
lcdout $FE,1, "Appuye sur Record"
lcdout $FE,$C0, "ou sur Show"
pause 100

if PORTC.6 =0 then

GOSUB READ_RTC
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec]
Write_Address = Write_Address +1
PAUSE 10
I2CWRITE DPIN,CPIN,$A0,Write_Address,[mins]
Write_Address = Write_Address +1
PAUSE 10
I2CWRITE DPIN,CPIN,$A0,Write_Address,[hr]
Write_Address = Write_Address +1
PAUSE 10
Battery_Charge_Counter = Battery_Charge_Counter + 1
Endif

if PORTC.7 =0 then
Write_Address = 0
I2CREAD DPIN,CPIN,$A0,Write_Address,[sec,mins,hr]
PAUSE 10
LCDOUT $FE,1," Time ",DEC3 1
LCDOUT $FE,$C0,"AD",DEC Write_Address," T:", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec
PAUSE 1000
Endif

GOTO Mainloop
End

mackrackit
- 6th December 2010, 04:29
If this is for reading the RTC
DPIN,CPIN,$A0,bat,[sec,mins,hr]
then $A0 is the problem.

lerameur
- 6th December 2010, 13:12
NO uts reading the EEPROM.
When PORTC.6 =0 I record from RTC to EEPROM
And when PORTC.7=0 I show on LCD what has been recorded to EEPROM

The only time I read RTC is in the first option C.6 using your routine GOSUB READ_RTC, that works fine.
The program works, but if I add an IF loop it do not work ( red code above)
I think this has been the problem since the beginning !
K

lerameur
- 6th December 2010, 13:40
IS it possible to write a byte at a time on the EEPROM. From the document, 2 byte ( a word is written for teh address, but I guess this is at the same place..) and the internal clock is incremented. Maybe after writting, the internal clock did not reset and when reading it keeps incrementing where it left off !?!....
Just a thought

.... I think the address has to be a word, not a byte. will try it tonight.

K

lerameur
- 6th December 2010, 23:36
OK it is now working.. somewhat, Needed to put a word instaed of a byte for the address. Now its more of a programming issue which I am stuck with. The lcd is showing recoded time, but the time does not seem to follow a normal clocking. I record at 5 sec interval, and the first three times are identical, then the next two are 15sec later but are the same...


Mainloop:
lcdout $FE,1, "Appuye sur Record"
lcdout $FE,$C0, "ou sur Show"
pause 100

if PORTC.6 =0 then

GOSUB READ_RTC
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr]
Write_Address = Write_Address +3

Battery_Charge_Counter = Battery_Charge_Counter + 1
Endif

if PORTC.7 =0 then
bat = 0
j=1
for i =0 to Battery_Charge_Counter/3
I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr]
PAUSE 10
LCDOUT $FE,1," Time ",DEC3 j
LCDOUT $FE,$C0,"WA",DEC Write_Address," T:", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec
PAUSE 2500
bat = bat +3
j = j+1
next i
Endif

GOTO Mainloop
End

READ_RTC:
I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
pause 10
RETURN

SET_RTC: ' Do once at startup
yr = $3
mon = $5
date = $05
sec = $09
mins = $05
hr = $03
I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
RETURN

lerameur
- 7th December 2010, 02:37
Ok,
The chips are working but my outputs are erronous. I just want to record time, store it, and then replay it. I am getting all kinds of numbers on the LCD.
here is my latest code:

'/////////////////////////
'// DS1337 and test program
'// Using the PIC16F887
'// From Dave Mackarit FUNCTIONAL
'/////////////////////////

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

DPIN VAR PORTC.4 'I2C DATA PIN
CPIN VAR PORTC.3 'I2C CLOCK PIN
Write_Address VAR WORD
i var byte
j var byte
bat var byte
Battery_Charge_Counter var byte
' -------------- RTC definitions -----------------
RTC CON %11010000
SecReg CON $00 ' seconds Write_Addressess (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 : mins VAR BYTE : hr VAR BYTE : day VAR BYTE :date VAR BYTE : mon VAR BYTE : yr VAR BYTE
Battery_Charge_Counter = 0

PAUSE 1000
GOSUB SET_RTC
Write_Address = 0
Mainloop:

lcdout $FE,1, "Appuye sur Record"
lcdout $FE,$C0, "ou sur Show"
pause 100

if PORTC.6 =0 then
GOSUB READ_RTC
Write_Address =0
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr]
PAUSE 10
Write_Address = Write_Address +7
Battery_Charge_Counter = Battery_Charge_Counter + 1
Endif

if PORTC.7 =0 then
bat = 0
j=1
for i=0 to Battery_Charge_Counter
I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr]
PAUSE 10
LCDOUT $FE,1," Time ",DEC3 j
LCDOUT $FE,$C0,"time ",DEC Battery_Charge_Counter,":", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec
PAUSE 2000
bat = bat +7
j= j + 1
next i


Endif

GOTO Mainloop
End

READ_RTC:
I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
RETURN

SET_RTC: ' Do once at startup
yr = $3
mon = $5
date = $05
sec = $09
mins = $04
hr = $02
I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
RETURN

mackrackit
- 7th December 2010, 03:02
Here you are always writing starting at Address 0. The ADD 7 is always being reset.


if PORTC.6 =0 THEN
GOSUB READ_RTC
Write_Address =0
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr]
PAUSE 10
Write_Address = Write_Address +7
Battery_Charge_Counter = Battery_Charge_Counter + 1
Endif

Unless I am missing something.

lerameur
- 7th December 2010, 04:09
Ok that was not my latest one ...sorrryy !!
here it is:
Gining me very eronnous time (ie FF)


'/////////////////////////
'// DS1337 and test program
'// Using the PIC16F887
'// From Dave Mackarit FUNCTIONAL
'/////////////////////////

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

DPIN VAR PORTC.4 'I2C DATA PIN
CPIN VAR PORTC.3 'I2C CLOCK PIN
Write_Address VAR WORD
Battery_Charge_Counter var byte
bat var byte
i var byte
j var byte
' -------------- RTC definitions -----------------
RTC CON %11010000
SecReg CON $00 ' seconds Write_Addressess (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 : mins VAR BYTE : hr VAR BYTE : day VAR BYTE :date VAR BYTE : mon VAR BYTE : yr VAR BYTE
Battery_Charge_Counter = 0

PAUSE 1000
GOSUB SET_RTC
Write_Address = 0
Mainloop:
lcdout $FE,1, "Appuye sur Record"
lcdout $FE,$C0, "ou sur Show"
pause 100

if PORTC.6 =0 then

GOSUB READ_RTC
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr]
PAUSE 10
Write_Address = Write_Address +7
Battery_Charge_Counter = Battery_Charge_Counter + 1
Endif

if PORTC.7 =0 then
bat = 0
j = 1
for i=0 to Battery_Charge_Counter
I2CREAD DPIN,CPIN,$A0,bat,[sec,mins,hr]
PAUSE 10
LCDOUT $FE,1," Time ",DEC3 j
LCDOUT $FE,$C0,"AD",DEC Write_Address," T:", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec
PAUSE 1000
bat = bat + 7
j = j + 1
next i
Endif

GOTO Mainloop
End

READ_RTC:
I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
RETURN

SET_RTC: ' Do once at startup
yr = $3
mon = $5
date = $05
sec = $09
mins = $04
hr = $02
I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
RETURN

mackrackit
- 7th December 2010, 08:47
I could not see the problem with your code so I made a test myself.
When the EEPROM address reaches 255 the data read back for Min and Sec is FF.

Must be crossing the boundary of rolling over because the EEPROM is full?
It has been a long day and I am getting my bits and bytes and K and k mixed up.
Anyone wants to clear me up we are using
http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en024639

Here is my test


'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
Esec VAR BYTE ' seconds
Emins VAR BYTE ' minutes
Ehr VAR BYTE ' hours
I VAR BYTE
PAUSE 1000
'GOSUB SET_RTC

RUN:
ADDR = 0
'SAVE DATA
FOR I = 0 TO 100
ADDR = ADDR + 3
I2CREAD DPIN, CPIN, RTC, SecReg, [sec,mins,hr,day,date,mon,yr]
PAUSE 100
I2CWRITE DPIN,CPIN,$A0,ADDR,[hr,mins,sec]
PAUSE 100
I2CREAD DPIN,CPIN,$A0,ADDR,[Ehr,Emins,Esec]
PAUSE 100
LCDOUT $FE,1,"RTC ", HEX2 hr, ":", HEX2 mins, ":", HEX2 sec
LCDOUT $FE,$C0,"EEP ",HEX2 Ehr, ":", HEX2 Emins, ":", HEX2 Esec
LCDOUT $FE,$90,"ADDR ", DEC ADDR
PAUSE 1000
NEXT I
'READ SAVED DATA
ADDR = 0
FOR I = 0 TO 100
ADDR = ADDR + 3
I2CREAD DPIN,CPIN,$A0,ADDR,[Ehr,Emins,Esec]
PAUSE 100
LCDOUT $FE,1,"READ SAVED "
LCDOUT $FE,$C0,"EEP ",HEX2 Ehr, ":", HEX2 Emins, ":", HEX2 Esec
LCDOUT $FE,$90,"ADDR ", DEC ADDR
NEXT I
GOTO RUN

lerameur
- 7th December 2010, 12:28
Yes thats the chip I am using... 24LC1025 and the DS1337.
The memory is net getting full already, it shows screwed up time even in the beginning !!!

Ken

lerameur
- 7th December 2010, 13:37
I mean the memory cannot be full. I am starting over each time the chip is power on and I just record a few few 'times' maybe 6 or 7. I must or ran 40 versions of this program. I cant find the problem :( I will use a different approach tonight

mackrackit
- 7th December 2010, 16:10
Just a thought...
Maybe being you are jumping 7 locations but writing to 3, when you go to read it is reading from locations that are empty.
Write_Address and bat are not starting from the same place?

Write_Address = Write_Address +7
bat = bat + 7

lerameur
- 7th December 2010, 17:00
Write_address is for recording, bat is for reading.
Lets say there are 3 reading:
first reading:
Read RTC then write at eeprom at 0
second reading:
Read RTC then write at eeprom at 7
third reading:
Read RTC then write at eeprom at 14
....
Then a show is made by putting C.7 to 0
Bat reads at 0
then bat reads at 7
then reads at 14.
...
A write is made again it is written at Write_address = 21
...

I am only using the first three anyway.
This is not the problem, the seconds for the first three reading are the same !!!!
eXAMPLE:
If I take 8 readings at 5 seconds interval, My results are that the first 3 are have the same time ie: 02:04:11 , the following 3 are: 02:04:27. last two are 02:04:38

lerameur
- 11th December 2010, 15:28
Hola Dave,
I finally got the program to work like I wanted to, well almost. Its showing the same value twice. If I hit the record button. Then I hit the show button, then it will loop twice the show with the same time.. weird... it seems its doing the loop in red twice.. always.


Mainloop:

if PORTC.6 =0 then
GOSUB READ_RTC
I2CWRITE DPIN,CPIN,$A0,Write_Address,[sec,mins,hr]
PAUSE 10
Write_Address = Write_Address +3
Endif

if PORTC.7 =0 then
if Write_Address = 0 then goto Mainloop
j = 0
For i=0 to (Write_Address / 3) -1
I2CREAD DPIN,CPIN,$A0,j,[sec,mins,hr]
PAUSE 10
LCDOUT $FE,1," Time ",DEC2 i
LCDOUT $FE,$C0,"WA ",DEC j," T:", dec2 hr, ":", dec2 mins, ":", dec2 sec
PAUSE 1000
j =j + 3
next i
Endif

GOTO Mainloop
End

READ_RTC:
I2CREAD DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
pause 10
TempVal=hr
GoSub BCD_TO_BIN
hr=TempVal

TempVal=sec
GoSub BCD_TO_BIN
sec=TempVal

TempVal=mins
GoSub BCD_TO_BIN
mins=TempVal
RETURN

SET_RTC: ' Do once at startup
yr = $3
mon = $5
date = $05
sec = $09
mins = $04
hr = $02
I2CWRITE DPIN, CPIN, $D0, SecReg, [sec,mins,hr,day,date,mon,yr]
RETURN

BCD_TO_BIN: ' Convert the BCD values into BIN

Temp1 = $0F & TempVal ' Clear off the top four bits
Temp1 = Temp1 Dig 0
Temp2 = TempVal >> 4 ' Shift down four to read 2 BCD value
Temp2 = Temp2 Dig 0
TempVal = Temp2 * 10 + Temp1

Return

lerameur
- 11th December 2010, 19:01
OK, I found it, changed the if then for BUTTON action with a 255 debounce cycle. works great now :))
Thanks Dave for all the time you spent with me on this.

mackrackit
- 11th December 2010, 21:38
I am glad to help. Good to see you got it.