Hi all, I am doing a school project. I am using a At89C51ED2 microprocessor and it is connect to a 2-Line LCD panel. My teacher need me to write a program of the digital clock using the timer in the microprocessor. He want me to show the word " clock: " on the first line of the LCD and display the time on the second line of the LCD display.
I am stuck when i try to display the time on the Lcd panel. It can't seems to show any numbers and update itself. It will keep printing some wierd thing and eventually cover up the " Clock: " word. I am using keil uVsion 3 complier.
Here is my code, hope someone can give me some guidance as i am new and weak in programming. Thanks in advance!
#include <at89c51xd2.h>
//#include <REG51.H>
#define LCD_data P0
#define RS P1_0
#define RW P1_1
#define EN P1_2
unsigned char Lcd_Data[]= {'C','L','O','C','K',':'};
void Lcd_Initial(void);
void Lcd_Command(unsigned char Command);
void Lcd_Busy();
void delay(unsigned char time);
void Lcd_Write(unsigned char Data);
void T0Delay(void);
int i, d;
unsigned int sec, min, hour;
void main (void)
{
i = 0;
sec = 0;
min = 0;
hour = 0;
Lcd_Initial();
Lcd_Command(0x1c); //move right
Lcd_Command(0x1c); //move right
for(i=0;i<7;i++) //loop to display 12 characters
{
Lcd_Write(Lcd_Data[i]); //print data from unsigned char Lcd_Data
}
Lcd_Command(0xc0); // print at 2nd line
while(1)
{
Lcd_Write(hour);
Lcd_Write(':');
Lcd_Write(min);
Lcd_Write(':');
Lcd_Write(sec);
if(sec>=60)
{
sec=0;
if(++min ==60)
{
min=0;
}
if(++hour==13)
{
hour =1;
}
for(d=0;d<40;d++) // Delay for 1 sec
{
T0Delay();
}
}
}
}
void Lcd_Initial(void)
{
Lcd_Command(0x38); // function set : 2-line display
Lcd_Command(0x0C); // display on , cursor off
Lcd_Command(0x01); // clear screen
}
void Lcd_Command(unsigned char Command)
{
Lcd_Busy();
EN = 0;
RS = 0;
RW = 0;
delay(5);
EN = 1;
LCD_data = Command;
delay(5);
EN = 0;
}
void Lcd_Busy()
{
unsigned char i,j;
for(i=0;i<40;i++) //A loop for delay
for(j=0;j<20;j++);
}
void delay(unsigned char time)
{
unsigned char x;
for(x=0;x<time;x++); //A loop for delay
}
void Lcd_Write(unsigned char Data)
{
Lcd_Busy();
EN = 0;
RS = 1;
RW = 0;
delay(5);
EN = 1;
LCD_data = Data;
delay(5);
EN = 0;
}
void T0Delay() //delay for 25ms
{
TMOD=0x01; // Timer 0, mode1 (16-bit)
TL0= 0xCB;
TH0= 0x7D;
TR0= 1;
while(TF0==0);
TR0=0;
TF0=0;
}