|
|
View previous topic :: View next topic |
Author |
Message |
ofarcis Guest
|
Use DS1307 with CCS compiler |
Posted: Thu Oct 24, 2002 2:21 am |
|
|
Hi,
I have problems to use a DS1307 RTC with CCS compiler. I development the next code, but don't answer correctly de byte write into ram memory. The same case to get and put in BCD format de time and date. Can you help me?
#include <16F873.h>
#use delay(clock=4000000)
#fuses XT,NOWDT,PUT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#define DAL_SCL PIN_C3
#define DAL_SDA PIN_C4
#use i2c(master,sda=DAL_SDA, scl=DAL_SCL)
#define I2C_SELECT_DS1307_READ 0xD1 // 1101 0001
#define I2C_SELECT_DS1307_WRITE 0xD0 // 1101 0000
void set_ram(byte bAddress, byte bData)
{
i2c_start();
i2c_write(0xD0);
i2c_write(bAddress);
i2c_write(bData);
i2c_stop();
}
byte get_ram(byte bAddress)
{
byte bRet;
i2c_start();
i2c_write(0xD0);
i2c_write(bAddress);
//i2c_stop();
i2c_start();
i2c_write(0xD1);
bRet=i2c_read(0);
i2c_stop();
return bRet;
}
void init_ds1307()
{
// Set control register
output_high(DAL_SDA);
output_high(DAL_SCL);
// Enable oscillator
i2c_start();
i2c_write(I2C_SELECT_DS1307_WRITE);
i2c_write(0x00);
i2c_write(0x00);
i2c_stop();
// Enabels the Square Wave Output at 1KHz
i2c_start();
i2c_write(I2C_SELECT_DS1307_WRITE);
i2c_write(0x07);
i2c_write(0x8);
i2c_stop();
}
void main()
{
int i;
// Access to all ram memory of DS1307
for(i=0; i< 56; i++){
set_ram((0x08+i), 0xA1);
delay_ms(20);
bTmp=get_ram((0x08+i));
delay_ms(20);
printf("\r\nByte write=\%2d, byte read=\%2X", i, bTmp);
}
}
Ever return 0xFF when i write 0xA1 data.
Thanks, Oscar.
___________________________
This message was ported from CCS's old forum
Original Post ID: 8137 |
|
|
Pete Smith Guest
|
Re: Use DS1307 with CCS compiler |
Posted: Thu Oct 24, 2002 9:35 am |
|
|
:=Hi,
:=
:=I have problems to use a DS1307 RTC with CCS compiler. I development the next code, but don't answer correctly de byte write into ram memory. The same case to get and put in BCD format de time and date. Can you help me?
:=void set_ram(byte bAddress, byte bData)
:={
:= i2c_start();
:= i2c_write(0xD0);
:= i2c_write(bAddress);
:= i2c_write(bData);
In my code, I allow a 11msec delay here, to allow the device time to write the data, before shutting down the port.
This is actually a hang over from the code, because it's based on the code I did for the DS1624, which required a 10msec delay after telling it to write to the e2prom, otherwise, a new i2c_start would cancel the write.
:= i2c_stop();
:=}
:=
:=byte get_ram(byte bAddress)
:={
:= byte bRet;
:=
:= i2c_start();
:= i2c_write(0xD0);
:= i2c_write(bAddress);
:= //i2c_stop();
:=
:= i2c_start();
:= i2c_write(0xD1);
:= bRet=i2c_read(0);
And here, I personally would use
bRet=i2c_read();
dummy=i2c_read(0);
FWIW, we use (or used to use) the DS1307 in all our products, and our code is the same as yours, bar the changes I've suggested above.
Hope this helps,
Pete.
___________________________
This message was ported from CCS's old forum
Original Post ID: 8155 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Use DS1307 with CCS compiler |
Posted: Thu Oct 24, 2002 12:09 pm |
|
|
:=Hi,
:=
:=I have problems to use a DS1307 RTC with CCS compiler. I development the next code, but don't answer correctly de byte write into ram memory. The same case to get and put in BCD format de time and date. Can you help me?
:=#fuses XT,NOWDT,PUT
:=void init_ds1307()
:={
:=
:= // Set control register
:= output_high(DAL_SDA);
:= output_high(DAL_SCL);
----------------------------------------
1. Do you have a 3v lithium battery connected to the Vbat pin ?
If not, do so.
2. Do you have pull-up resistors on SDA and SCL ?
You could use 4.7K resistors.
3. Are you using Low Voltage programming mode ?
If not, you should add NOLVP to the end of the #fuses
statement. Otherwise, the PIC could appear to randomly
lock-up.
4. In my init code for i2c chips, I set the SCL and SDA lines
to be inputs, instead of setting them high, as you do.
I do this:
output_float(DAL_SDA);
output_float(DAL_SCL);
___________________________
This message was ported from CCS's old forum
Original Post ID: 8160 |
|
|
ofarcis Guest
|
Re: Use DS1307 with CCS compiler |
Posted: Thu Oct 24, 2002 11:38 pm |
|
|
Hi,
Finaly is a problem with the backup batery. When i conect a 3v. to a Vbat pin works correctly. Thanks for you help.
Regards, Oscar.
:=:=Hi,
:=:=
:=:=I have problems to use a DS1307 RTC with CCS compiler. I development the next code, but don't answer correctly de byte write into ram memory. The same case to get and put in BCD format de time and date. Can you help me?
:=
:=:=void set_ram(byte bAddress, byte bData)
:=:={
:=:= i2c_start();
:=:= i2c_write(0xD0);
:=:= i2c_write(bAddress);
:=:= i2c_write(bData);
:=
:=In my code, I allow a 11msec delay here, to allow the device time to write the data, before shutting down the port.
:=
:=This is actually a hang over from the code, because it's based on the code I did for the DS1624, which required a 10msec delay after telling it to write to the e2prom, otherwise, a new i2c_start would cancel the write.
:=
:=
:=:= i2c_stop();
:=:=}
:=:=
:=:=byte get_ram(byte bAddress)
:=:={
:=:= byte bRet;
:=:=
:=:= i2c_start();
:=:= i2c_write(0xD0);
:=:= i2c_write(bAddress);
:=:= //i2c_stop();
:=:=
:=:= i2c_start();
:=:= i2c_write(0xD1);
:=:= bRet=i2c_read(0);
:=
:=And here, I personally would use
:=
:=bRet=i2c_read();
:=dummy=i2c_read(0);
:=
:=FWIW, we use (or used to use) the DS1307 in all our products, and our code is the same as yours, bar the changes I've suggested above.
:=
:=Hope this helps,
:=
:=Pete.
___________________________
This message was ported from CCS's old forum
Original Post ID: 8173 |
|
|
ofarcis Guest
|
Re: Use DS1307 with CCS compiler |
Posted: Thu Oct 24, 2002 11:43 pm |
|
|
Hi,
Finaly is a problem with the backup batery. When i conect a 3v. to a Vbat pin works correctly. Thanks for you help.
Regards, Oscar.
:=:=Hi,
:=:=
:=:=I have problems to use a DS1307 RTC with CCS compiler. I development the next code, but don't answer correctly de byte write into ram memory. The same case to get and put in BCD format de time and date. Can you help me?
:=
:=:=#fuses XT,NOWDT,PUT
:=
:=:=void init_ds1307()
:=:={
:=:=
:=:= // Set control register
:=:= output_high(DAL_SDA);
:=:= output_high(DAL_SCL);
:=----------------------------------------
:=
:=
:=1. Do you have a 3v lithium battery connected to the Vbat pin ?
:= If not, do so.
Yes, this is the problem.
:=
:=2. Do you have pull-up resistors on SDA and SCL ?
:= You could use 4.7K resistors.
I used a 10K resistors and works.
:=
:=3. Are you using Low Voltage programming mode ?
:= If not, you should add NOLVP to the end of the #fuses
:= statement. Otherwise, the PIC could appear to randomly
:= lock-up.
:=
I don't use.
:=4. In my init code for i2c chips, I set the SCL and SDA lines
:= to be inputs, instead of setting them high, as you do.
:= I do this:
:=
:= output_float(DAL_SDA);
:= output_float(DAL_SCL);
:=
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 8174 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|