|
|
View previous topic :: View next topic |
Author |
Message |
MILMAN Guest
|
Using RTC chip as an alarm to wake up PIC |
Posted: Thu Jul 02, 2009 4:14 am |
|
|
Hello,
I am building a circuit that can be programmed to 'wake up' at certain times through out the day, collect data and then go to sleep mode.
I am having a lot of trouble even setting the RTC chip to tell the correct time, and I don't really know how to deal with different months/leap years etc. I have got hold of the code below from someone who has now left the department I work in (so can't ask for help) and have modified it a bit, but to be honest I don't understand much or any of what it is really doing?
If anyone could provide a brief explanation of this or another approach to implementing the time-based data collection idea I would be very grateful. Sorry this is a bit of a vague request,
any help much appreciated,
Cheers
Code: | byte day,month,year,hours,minutes,seconds;
void init_rtc()
{
i2c_start();
i2c_write(0xa0 | RTC_chip_add<<1);
i2c_write(0x00);
i2c_write(0x00);
i2c_stop();
delay_ms(11);
}
unsigned int get_low_bcd(unsigned int in_char) // char lower nibble to bcd
{
return((in_char&0x7f)-48);
}
separate
void set_time_and_date()
{
byte unitday,tenday,unitmonth,tenmonth,tenyear,unityear;
byte unithours,tenhours,unitminutes;
byte tenminutes,unitseconds,tenseconds;
unitseconds=get_low_bcd(rtc_buffer[16]);
tenseconds=get_low_bcd(rtc_buffer[15]);
unitminutes=get_low_bcd(rtc_buffer[13]);
tenminutes=get_low_bcd(rtc_buffer[12]);
unithours=get_low_bcd(rtc_buffer[10]);
tenhours=get_low_bcd(rtc_buffer[9]);
unityear=get_low_bcd(rtc_buffer[7]);
tenyear=get_low_bcd(rtc_buffer[6]);
unitmonth=get_low_bcd(rtc_buffer[4]);
tenmonth=get_low_bcd(rtc_buffer[3]);
unitday=get_low_bcd(rtc_buffer[1]);
tenday=get_low_bcd(rtc_buffer[0]);
i2c_start();
i2c_write(0xa0 | RTC_chip_add<<1);
i2c_write(0x02);
i2c_write((tenseconds * 16) + unitseconds);
i2c_write((tenminutes * 16) + unitminutes);
i2c_write((tenhours * 16) + unithours);
i2c_write((unityear * 64) + (tenday * 16) + unitday);
i2c_write((tenmonth * 16) + unitmonth);
i2c_stop();
delay_ms(11);
fprintf(PC,"\f RTC written ...");
lcd_init();
printf(lcd_putc, "\n RTC written ...");
}
separate
void read_time_and_date()
{
byte dummy;
fprintf(PC,"reading RTC from hardware...\n\r");
i2c_start();
i2c_write(0xa0 | RTC_chip_add<<1);
i2c_write(0x02);
i2c_start();
i2c_write(0xa1 | RTC_chip_add<<1);
seconds = i2c_read();
minutes = i2c_read();
hours = i2c_read();
day = i2c_read();
month = i2c_read();
for(i=0;i<5;i++)
dummy = i2c_read();
i2c_stop();
delay_ms(11);
delay_ms(200);
year = ((day&0xc0)/64)&0x03;
day = (((day / 16) & 0x03) * 10) + (day & 0x0f);
month = (((month / 16) & 0x01) * 10) + (month & 0x0f);
hours = (((hours / 16) & 0x03) * 10) + (hours & 0x0f);
minutes = (((minutes / 16) & 0x0f) * 10) + (minutes & 0x0f);
seconds = (((seconds / 16) & 0x0f) * 10) + (seconds & 0x0f);
fprintf(PC,"writing RTC to monitor ...\n\r");
fprintf(PC,"year .. %3u\n\r",year);
fprintf(PC,"month .. %3u\n\r",month);
fprintf(PC,"day .. %3u\n\r",day);
fprintf(PC,"hours .. %3u\n\r",hours);
fprintf(PC,"minutes .. %3u\n\r",minutes);
fprintf(PC,"seconds .. %3u\n\r",seconds);
} |
|
|
|
MILMAN Guest
|
|
Posted: Thu Jul 02, 2009 4:22 am |
|
|
Also the RTC chip I am using is PCF8583 and using PIC16F76. Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 02, 2009 10:35 am |
|
|
There is a pcf8583 driver here, though it doesn't include alarm functions:
http://www.ccsinfo.com/forum/viewtopic.php?t=27988
It has a test program, and links to schematics.
If you have any questions about the driver, ask them here in this thread
(not in the driver thread). |
|
|
MILMAN Guest
|
|
Posted: Fri Jul 03, 2009 8:39 am |
|
|
Thank you so much. That has helped me loads!
I stupidly overlooked the fact that I had set the PCF8583 i2c address to 0xA2 as well so thanks for the note. It has also highlighted that it would have been a lot better if I had used the DS1307 chip, but o well, will know in the future!
Thanks again. |
|
|
|
|
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
|