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;

}

This should really be posted in the assembly section.

I don't think so. There's enough problems with C in this code.

See comments inline.

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!

void Lcd_Write(unsigned char Data);
unsigned int sec, min, hour;

void main (void)
 {

	Lcd_Command(0xc0);						 // print at 2nd line
	while(1)
	{
		Lcd_Write(hour);		   
// Lcd_Write is declared as Lcd_Write(unsigned char. hour is 
// declared as unsigned int.
		Lcd_Write(':');
		Lcd_Write(min);
		Lcd_Write(':');
		Lcd_Write(sec);
// Don't you need some command to get back to the 
// beginning of the 2nd line?			

}

I don't think so. There's enough problems with C in this code.

See comments inline.

hey there, sorry my code is a bit messy :X anyone can help me in this? I'm kinda confuse in how it works.

anyone can help me in this?

Did you read my comments?

As I said, you need at least two things:
1. Check the speck and find out how to move cursor to an initial position at the second line.
2. Write a function to print int into an array of chars (maybe kiel libraries already have it), apply it to hours and minutes and display the result accordingly.

I'm kinda confuse in how it works.

I have no idea how your LCD works, and I am not familiar with the current set of kiel libraries. You have to do it yourself.

This article has been dead for over six months. Start a new discussion instead.