#define RTC_chip_select portc.f0
char* text;
char txt[6];
unsigned char Minute,Second,Hour;
void lcd(){
byteToStr(Bcd2Dec(Second), txt);
Lcd_Out(3, 12, txt);
byteToStr(Bcd2Dec(Minute), txt);
Lcd_Out(3, 9, txt);
text=":";
Lcd_Out(3, 12, text);
byteToStr(Bcd2Dec(Hour), txt);
Lcd_Out(3, 6, txt);
Lcd_Out(3, 9, text);
}
//////////////////////////////////////// Set Hour
void Inc_Hour(){
delay_ms(200);
Hour = Bcd2Dec(Hour);
Hour++;
if( Hour> 24)Hour =0;
Hour=Dec2Bcd(Hour);
RTC_chip_select=0;
spi_write(0x83);
spi_write(Hour);
RTC_chip_select=1;
}
//////////////////////////////////////////Set Hour
///////////////////////////////////////// Set Minute
void Inc_Minute(){
delay_ms(200);
Minute = Bcd2Dec(Minute);
Minute++;
if( Minute> 59)Minute=0;
Minute=Dec2Bcd(Minute);
RTC_chip_select=0;
spi_write(0x82);
spi_write(Minute);
RTC_chip_select=1;
}
////////////////////////////////////////Set Minute
///////////////////////////////////////////Set Second
void Inc_Second(){
delay_ms(200);
Second = Bcd2Dec(Second);
Second++;
if( Second> 59)Second=0;
Second=Dec2Bcd(Second);
RTC_chip_select=0;
spi_write(0x81);
spi_write(Second);
RTC_chip_select=1;
}
/////////////////////////////////////////////Set Second
///////////////////////////////////////keypad Function
void Keypad(){
unsigned char kp;
kp = Keypad_Read();
if(kp=!0) {
kp = Keypad_Read();
if(kp==1)Inc_Hour();
if(kp==2)Inc_Minute();
if(kp==3)Inc_Second();
}}
/////////////////////////////////////// keypad Function
//////////////////////////////////////RTC_Refresh Function
void RTC_Refresh(){
RTC_chip_select=0; // enable RTC - CS low
spi_write(0x01); // seconds register read address
Second = spi_read(255); // read seconds register
Minute =spi_read(255); // read minutes
Hour =spi_read(255); // read hours
RTC_chip_select=1; // disable RTC - CS high
}
//////////////////////////////////////RTC_Refresh Function
/////////////////////////////////////RTC_Register Function
void RTC_Register(){
///////////////////////////////control register
RTC_chip_select=0;
spi_write(0x8d); // control register write address
spi_write(0xBD); //10111101//
RTC_chip_select=1;
///////////////////////////////control register
///////////////////////////////////trickle charger
RTC_chip_select=0;
spi_write(0x8f);
spi_write(0xAB); // one diode and one 4k resistor
RTC_chip_select=1;
//////////////////////////////////trickle charger
}
//////////////////////////////////////RTC_Register Function
////////////////////////////////////////////Pins Function
void Pin(){
trisc.f0=0; // chip select
portc.f0=1; // chip select
trisc.f1=1; // int
portc.f1=1; // int
trisc.f2=0; // led
portc.f2=0; // led
}
/////////////////////////////////////////////// Pins Function
//////////////////////////////////////////// main Function
void main() {
ADCON1=0b00000111;
Lcd_Init(&PORTB);
Lcd_Cmd(Lcd_CURSOR_OFF);
Keypad_Init(&PORTA);
Spi_Init_Advanced(MASTER_OSC_DIV64,DATA_SAMPLE_End,CLK_IDLE_HIGH , LOW_2_HIGH) ;
Pin();
delay_ms(2000);
RTC_Register();
while(1){
Keypad();
RTC_Refresh();
lcd();
}
}
//////////////////////////////////////////// main Function