PIC RTC and 7 Segment code
/*
* Project name:
PIC Development Board RTC and 7 Segment code
* Copyright
(c) Researchdesignlab.com
* Test configuration:
MCU: PIC16F877A
Dev.Board: PIC
Oscillator: 20.0 MHz
Software: mikroC PRO for PIC v 4.6
Developer: KK
*/
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;
unsigned int portb_index,portb_array[4],digit, portd_index=0,shifter;
void delay()
{
unsigned char z;
for(z=250;z>0;z--)
;
}
unsigned short mask(unsigned short num) {
switch (num) {
case '0' : return 0xC0;
case '1' : return 0xF9;
case '2' : return 0xA4;
case '3' : return 0xB0;
case '4' : return 0x99;
case '5' : return 0x92;
case '6' : return 0x82;
case '7' : return 0xF8;
case '8' : return 0x80;
case '9' : return 0x90;
} //case end
}
void interrupt() {
PORTD = 0XFF; // Turn off all 7seg displays
PORTB= portb_array[portb_index]; // bring appropriate value to PORTB
if(portb_index==0)
PORTD = 0XF7;
else if(portb_index==1) // turn on appropriate 7seg. display
PORTD = 0XFB;
else if(portb_index==2) // turn on appropriate 7seg. display
PORTD = 0XFD;
else if(portb_index==3) // turn on appropriate 7seg. display
PORTD = 0XFE;
// increment portd_index
portb_index++ ;
if (portb_index > 4u)
portb_index = 0; // turn on 1st, turn off 2nd 7seg.
TMR0 = 0; // reset TIMER0 value
INTCON = 0x20; // Clear T0IF
}
void rtc_init()
{
unsigned char a[]={0x00,0x00,0x01,0x07,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 display1(unsigned char a1)
{
unsigned char b1;
b1=(a1 & 0x0f0);
b1=(b1>>4)+0x30;
portb_array[3] = mask(b1);
b1=(a1 & 0x0f);
b1=b1+0x30;
portb_array[2] = mask(b1);
}
void display2(unsigned char a1)
{
unsigned char b1;
b1=(a1 & 0x0f0);
b1=(b1>>4)+0x30;
portb_array[1] = mask(b1);
b1=(a1 & 0x0f);
b1=b1+0x30;
portb_array[0] = mask(b1);
}
void main()
{
unsigned char i,ch;//,hr,min;
TRISD=0X00;
TRISB=0X00;
PORTB=0X00;
PORTD=0XFF;
Delay_ms(2000);
PORTD=0X00;
OPTION_REG = 0x80; // Timer0 settings
digit = 0;
portb_index = 0;
TMR0 = 0;
INTCON = 0xA0; // Enable GIE, T0IE
Delay_ms(200);
display1(0x12);
display2(0X14);
delay();
I2C1_Init(100000);
delay();
rtc_init();
delay();
while(1)
{
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();
display1(ch);
delay();
delay();
delay();
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(1);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
delay();
I2C1_Stop();
delay();
display2(ch);
delay();
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(0);
Delay_100ms();
I2C1_Start();
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
ch=I2C1_Rd(0u);
I2C1_Stop();
delay();
delay();
delay();
}
}