Pic Rtc Code
/*
* Project name:
PIC Development Board
* Copyright
(c) Researchdesignlab.com
* Test configuration:
MCU: PIC16F877A
Dev.Board: PIC
Oscillator: 20.0 MHz
Software: mikroC PRO for PIC v 4.6
*/
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit I2C_Scl at RC3_bit;
sbit I2C_Sda at RC4_bit;
sbit I2C_Scl_Direction at TRISC3_bit;
sbit I2C_Sda_Direction at TRISC4_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
void delay()
{
unsigned char z;
for(z=250;z>0;z--)
;
}
void rtc_init()
{
unsigned char a[]={0x00,0x00,0x01,0x06,0x02,0x06,0x03,0x05,0x04,0x17,0x05,0x09,0x06,0x11};
unsigned char x=0;
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(a[0]);
I2C1_Wr(a[1]);
I2C1_Stop();
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(a[2]);
I2C1_Wr(a[3]);
I2C1_Stop();
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(a[4]);
I2C1_Wr(a[5]);
I2C1_Stop();
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(a[8]);
I2C1_Wr(a[9]);
I2C1_Stop();
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(a[10]);
I2C1_Wr(a[11]);
I2C1_Stop();
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(a[12]);
I2C1_Wr(a[13]);
I2C1_Stop();
}
void display(unsigned char a1)
{
unsigned char b1;
b1=(a1 & 0x0f0);
b1=(b1>>4)+0x30;
Lcd_Chr_Cp(b1);
b1=(a1 & 0x0f);
b1=b1+0x30;
Lcd_Chr_Cp(b1);
}
void main()
{
unsigned char i,temp,ch,hr,min,STORE_VAL=0,FLAG1=0,FLAG2=0;
unsigned int LDR_VAL1,LDR_VAL2,LDR_VAL3;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
delay();
delay();
I2C1_Init(100000);
delay();
rtc_init();
while(1)
{
Lcd_Out(1, 1, "TIME ");
I2C1_Start();
I2C1_Wr(0xD0);
delay();
I2C1_Wr(2);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
I2C1_Stop();
hr=ch;
display(ch);
delay();
delay();
delay();
Lcd_Chr_Cp(':');
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(1);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
min=ch;
delay();
I2C1_Stop();
delay();
display(ch);
delay();
delay();
delay();
Lcd_Chr_Cp(':');
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(0);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
I2C1_Stop();
display(ch);
delay();
delay();
if(hr==0x12||hr==0x01||hr==0x02||hr==0x03||hr==0x04||hr==0x05||hr==0x06)
{
Lcd_Chr_Cp(' ');
Lcd_Chr_Cp('A');
Lcd_Chr_Cp('M');
}
else
{
Lcd_Chr_Cp(' ');
Lcd_Chr_Cp('P');
Lcd_Chr_Cp('M');
}
i=0;
Lcd_Out(2, 1, "DATE ");
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(4);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
delay();
I2C1_Stop();
display(ch);
delay();
delay();
Lcd_Chr_Cp('-');
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(5);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
delay();
I2C1_Stop();
display(ch);
delay();
delay();
Lcd_Chr_Cp('-');
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(6);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
I2C1_Stop();
display(ch);
delay();
delay();
}
}
