Program for LCD1602 to display adjustable clock

Publisher:runaway2000Latest update time:2015-08-11 Source: dzsc Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//Hope it helps beginners//

#include
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^7;
sbit lcdrs=P2^6;
sbit W=P2^5;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
uchar num,count,k1num;
char miao,fen,shi;
uchar code table[]=" 2015-01-5 SAT" ;
uchar code table1[]=" 00:00:00";

void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void write_com(uchar com) // write command
{
W=0;
lcdrs=0; //RS selects write command or write data, write command
P0=com; //send the written command to the data bus
delay(5); //delay a little to wait for the data to stabilize
lcden=1; //enable the end to give a high pulse, because the initialization function has set lcden to 0
delay(5); //delay a little
lcden=0; //set the enable end to 0 to complete the high pulse
}

void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

void write_sfm(uchar add,uchar date) //Write data to a certain address
{
uchar shi,ge;
shi=date/10;
ge=date;
write_com(0x80+0x40+add); //The address pointer points to the add+1th in the second row
write_data(0x30+shi); //Send data, send the tens digit of data
write_data(0x30+ge); //Send data, send the ones digit of data
}

void keyscan()
{
if(k1==0)
{
delay(5);
if(k1==0)//debounce
{
k1num++; //Count, calculate how many times k1 is pressed
while(!k1); //Release detection
if(k1num==1) //When pressed once
{
TR0=0; //Timer stops, time stops
write_com(0x0f); //Turn on display, display cursor, cursor flashes
write_com(0x80+0x49); //The data pointer points to the units of seconds in the second line
}

if(k1num==2) //When k1 is pressed twice
{

// write_com(0x0f);
write_com(0x80+0x46); //The data pointer points to the units of minutes in the second line

}
if(k1num==3) //When k1 is pressed three times
{
// write_com(0x0f);
write_com(0x80+0x43); //The data pointer points to the unit digit of the second line
}

if(k1num==4) //If it is pressed four times
{
TR0=1; //Start timerk1num
=0; //
Clearwrite_com(0x0c); //Close cursor
}
// if(k1num==5)
// {
// write_com(0x80+0x14)
// }
}
}
if(k1num!=0) //If k1 has been pressed
{
if(k2==0) //If k2 is pressed
{
delay(5);
if(k2==0)
{
while(!k2); //Release detectionif
(k1num==1) //When k1 is pressed once, adjust the seconds
{
miao++; //Add one second each time k2 is pressedif
(miao==60)
{
miao=0;
}
write_sfm(8,miao); //Data is displayed in the 9th and 10th positions of the second line
write_com(0x80+0x40+9); //Cursor has been set in k1, data pointer points to 0x48

}
if(k1num==2) //If k1 is pressed twice
{
fen++;
if(fen==60)
fen=0;
write_sfm(5,fen); //Data is sent to the sixth position++
write_com(0x80+0x40+6); //Data pointer points to the seventh position } if(k1num==3) //If k1 is pressed 3 times { shi++; if(shi==24) shi=0; write_sfm(2,shi); //Data is sent to the third position++ write_com(0x80+0x40+3); //Address pointer points to the fourth position } } } if(k3==0) //After k1 is pressed, if k3 is pressed { delay(5); if(k3==0) { while(!k3); if(k1num==1) //If k1 is pressed once { miao--; if(miao==-1) { miao=59; } write_sfm(8,miao); //Data is sent to the ninth position of the second line++ write_com(0x80+0x40+9); //The pointer points to the tenth position } if(k1num==2) { fen--; if(fen==-1) fen=59; write_sfm(5,fen); write_com(0x80+0x40+6); } if(k1num==3) { shi--; if(shi==-1) shi=23; write_sfm(2,shi); write_com(0x80+0x40+3); } } } } }

















































void init() //initialization function
{
// dula=0;
// wela=0;
lcden=0;
write_com(0x38); //Set 16*2 display, 5*7 dot matrix, 8-bit data interface
write_com(0x0c ); //Open display, do not display cursor
write_com(0x06); //When writing a data, the address pointer increases by 1, and the cursor increases by 1. When displaying a data, the whole screen does not move
write_com(0x01); //Data pointer clears to 0, display clears to 0
write_com(0x80); //Display on the screen
TMOD=0x01;
TH0=(655536-50000)/256;
TL0=(655536-50000)%6;
EA=1;
ET0=1;
TR0=1;
}


void main()
{
init(); //初始化
for(num=0;num<15;num++)
{
write_data(table[num]);
delay(10);
}

write_com(0x80+0x40); //The second line is displayed at the beginning
for(num=0;num<10;num++)
{
write_data(table1[num]);
delay(5);
}
while(1)
{
keyscan();

}
}

void timer0() interrupt 1
{
TH0=(655536-50000)/256;
TL0=(655536-50000)%6;
count++;
if(count==18) //一秒时间到
{
count=0;
miao++;
if(miao==60)
{ miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(2,shi);
}
write_sfm(5,fen);
}
write_sfm(8,miao);
}
}

Reference address:Program for LCD1602 to display adjustable clock

Previous article:Stepper Motor Driver
Next article:51 MCU operation LCD1602

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号