int2bcd
Before, i was entering alarm and time values by keypad.To be sure that RTC is working, i wrote a simple program.
In this program first, i wrote 23:59:55:00 Monday (i suppose the 000 is Monday, 001 Tuesday..110 Sunday) to RTC.
Then, i wrote 00:00:05:00 Tuesday as alarm time. After, i powered up the PIC it produced an interrupt.
Instead of using external interrupt, you can also use reading alarm flag which is the 1. bit of control register.
Do not forget clearing alarm flag after interrupt produced and connecting a pull-up resistor to the interrupt output of RTC.
Because, it is an open collector output.
#include <18F452.h>
#include <string.h>
#include <stdlib.h>
#fuses HS, PROTECT, NOWDT, NOBROWNOUT, PUT, NOLVP
#use delay (clock=20000000)
#use I2C(MASTER, sda=PIN_C4, scl=PIN_C3,SLOW)
#include<lcd.c>
short int ex_interrupt=0;
#INT_EXT
void EXT_ISR(){
ex_interrupt=1;
}
void main(){
byte temp=0;
enable_interrupts(GLOBAL);
ext_int_edge(H_TO_L);
set_tris_b(0x03);
set_tris_c(0xC0);
set_tris_d(0x08);
i2c_start();
i2c_write(0xA2); //send device address
i2c_write(0x00);//send address byte
//Write to RTC 23:59:55:00 Monday
//alarm time is 00:00:05:00 Tuesday
i2c_write(0x80);//Control Register,Stop counting
i2c_write(0x00);//hund. of seconds
i2c_write(0x55);//Seconds
i2c_write(0x59);//Minutes
i2c_write(0x23);//Hours
i2c_write(0x01);//Year_Date
i2c_write(0x01);//Weekday_Month
i2c_write(0x00);//Timer
i2c_write(0xA0);//Alarm Control Register
i2c_write(0x00);//Alarm hund. of seconds
i2c_write(0x05);//Alarm Seconds
i2c_write(0x00);//Alarm Minutes
i2c_write(0x00);//Alarm Hours
i2c_write(0x00);//Alarm Date has no effect in weekday alarm
i2c_write(0x02);//Alarm Weekday
i2c_stop();
delay_ms(5);
i2c_start();//start counting again
i2c_write(0xA2); //send device address
i2c_write(0x00);//send address byte
i2c_write(0x04);
i2c_stop();
delay_ms(5);
//You can load new alarm values to alarm registers without stop counting
i2c_start();
i2c_write(0xA2); //send device address
i2c_write(0x08);//send address byte
i2c_write(0xA0);//Alarm Control Register
i2c_write(0x00);//Alarm hund. of seconds
i2c_write(0x05);//Alarm Seconds
i2c_write(0x00);//Alarm Minutes
i2c_write(0x00);//Alarm Hours
i2c_write(0x00);//Alarm Date has no effect in weekday alarm
i2c_write(0x02);//Alarm Weekday
i2c_stop();
lcd_init();
lcd_gotoxy(1,1);
delay_ms(10);
enable_interrupts(INT_EXT);//External interrupt must be enable after RTC is set.
while(1){
i2c_start();//Reading alarm flag
i2c_write (0xA2); //send device address
i2c_write(0x00); //send RTC address byte
i2c_start();//again start transmission for reading data
i2c_write(0xA3);//reading address
temp=i2c_read(0);
i2c_stop();
if(bit_test(temp,1)){
lcd_putc("Flag\n");
}
if(ex_interrupt){
lcd_putc("Interrupt\n");
}
if(bit_test(temp,1)||ex_interrupt){
harici_interrupt=0;
lcd_putc("Alarm Ringing");
i2c_start();//After interrupt you must clear alarm flag
i2c_write(0xA2);//device address
i2c_write(0x00);//adress byte
i2c_write(0x04);//control register
i2c_stop();
delay_ms(60000);
}
}
}