// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
volatile int //VOLATILE statement used to change
//value after the optimisation aslo
change=0,
count=0,
sec=0, //declaring the variable
minu=0,
hr=0;
void timer_init() //initialisation of timer1 module
{
TMR1H=TMR1L=0; //clearing 16 bit register to start from first itself
T1CON.T1CKPS1=1; //choosing the 1:4 prescalr
T1CON.T1CKPS0=0;
T1CON.T1OSCEN=0; //Don"t use LP oscillator
T1CON.TMR1CS=0; //Use microcontroller internal clock for counting the pulse
PIE1.TMR1IE=1; //Interrupt enable pin of the timer1 module
T1CON.TMR1ON=1; //Enable timer1 ON
INTCON.PEIE=1; //Enable peripheral interrupt enable bit
INTCON.GIE=1; //Enable Global interrupt enable bit
}
//Interrupt Service routine(ISR)
void Interrupt_ISR()
{
if(PIR1.TMR1IF==1) //check really interrupt is fired by the timer1 module
{
count++; //increase count by one(count++=(count=count+1))
PIR1.TMR1IF=0; //clear the interrupt flag
}
}
//display the clock on the LCD
void displayclock()
{
//HOURS
if(hr<10)
{
LCD_out(1,5,"0"); //move the cursor to 1st row 5th column
_LCD_MOVE_CURSOR_RIGHT;
Lcd_Out(1,6,hr);
_LCD_MOVE_CURSOR_RIGHT;
}
else
{
LCD_out(1,5,hr);
_LCD_MOVE_CURSOR_RIGHT;
}
LCD_out(1,7,":");
_LCD_MOVE_CURSOR_RIGHT;
//MINUTE
if(minu<10)
{
LCD_out(1,8,"0"); //move the cursor to 1st row 8th column
_LCD_MOVE_CURSOR_RIGHT;
Lcd_Out(1,9,minu);
_LCD_MOVE_CURSOR_RIGHT;
}
else
{
LCD_out(1,8,minu);
_LCD_MOVE_CURSOR_RIGHT;
}
LCD_out(1,10,":");
_LCD_MOVE_CURSOR_RIGHT;
//SECONDS
if(sec<10)
{
LCD_out(1,11,"0"); //move the cursor to 1st row 11th column
_LCD_MOVE_CURSOR_RIGHT;
Lcd_Out(1,12,sec);
_LCD_MOVE_CURSOR_RIGHT;
}
else
{
LCD_out(1,11,sec);
_LCD_MOVE_CURSOR_RIGHT;
}
}
int main()
{
LCD_init(); //Initialise the LCD display
//Set the initial time
hr=18;
minu=34;
sec=10;
timer_init(); //initialise the timer1
displayclock();
while(1)
{
if(count==19)
{
sec++;
count=0;
change=1;
}
if(sec==60)
{
minu++;
sec=0;
change=1;
}
if(minu==60)
{
hr++;
minu=0;
change=1;
}
if(hr==24)
{
hr=0;
minu=0;
sec=0;
}
// if current time has changed, update the clock
if(change)
{
displayClock();
change = 0;
}
}
}