I want to create a clock using a timer (TMR2) to do the calculation and display current time.
I have a problem dealing with the switch
statement and couldn’t identify where the problem is. When I run the code in Proteus to test and write the variable sec0
on the screen, I expect it to display 1 to 9 but it keeps on displaying “9” on the LCD? Looks like the switch
function is not working. Below is the code I am using and the circuit diagram.
#pragma config FOSC = XT
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
#include <xc.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#define rs RA1
#define rw RA2
#define e RA3
int Count = 0;
int sec = 0;
int sec0 = 0;
int sec1 = 0;
int min = 0;
int min0 = 0;
int min1 = 1;
int hr = 0;
int hr0 = 0;
int hr1 = 1;
char name[] = { ' ', 'h', 'r', ' ', 'm', 'i', 'n', ' ', 's', 'e', 'c' };
void initialize();
void lcd_int();
void write();
void LCD_set();
void main()
{
ADCON1 = 0x00;
TRISA = 0x00;
TRISD = 0x00;
T2CON = 0b01111101;
PR2 = 0x7F;
TMR2 = 2;
GIE = 1;
PEIE = 1;
while (1)
{
TMR2 = 2;
while (!TMR2IF);
TMR2IF = 0;
Count++;
if (Count == 125)
{
initialize();
Count = 0;
sec += 1;
sec0 = sec % 10;
sec1 = sec / 10;
if (sec == 60)
{
sec = 0;
sec0 = sec % 10;
sec1 = sec / 10;
min += 1;
min0 = min % 10;
min1 = min / 10;
if (min >= 60)
{
min = 0;
min0 = min % 10;
min1 = min / 10;
hr += 1;
hr0 = hr % 10;
hr1 = hr / 10;
if (hr >= 24)
{
hr = 0;
hr0 = hr % 10;
hr1 = hr / 10;
}
}
}
write(sec0);
}
PORTD = 0xC0;
}
}
void initialize()
{
PORTD = 0x01;
LCD_set();
PORTD = 0x00;
LCD_set();
PORTD = 0x0F;
LCD_set();
PORTD = 0x06;
LCD_set();
PORTD = 0x80;
LCD_set();
}
void LCD_set()
{
rs = 0;
rw = 0;
e = 0;
__delay_ms(100);
e = 1;
}
void write(int i)
{
switch (i)
{
case 0:
PORTD = 0b00110000;
case 1:
PORTD = 0b00110001;
case 2:
PORTD = 0b00110010;
case 3:
PORTD = 0b00110011;
case 4:
PORTD = 0b00110100;
case 5:
PORTD = 0b00110101;
case 6:
PORTD = 0b00110110;
case 7:
PORTD = 0b00110111;
case 8:
PORTD = 0b00111000;
case 9:
PORTD = 0b00111001;
}
rs = 1;
rw = 0;
e = 0;
__delay_ms(1000);
e = 1;
}
PORTD = 0b00110000 + i;
\$\endgroup\$