求基于89C51单片机的秒表设计,用LCD1602显示。程序代码

2025-03-18 16:07:14
推荐回答(1个)
回答1:

#define LCD1602_FLAG

#define LCD1602_PORT P0

#include

#include

#define uchar unsigned char

sbit lcd1602_rs=P2^0;

sbit lcd1602_e=P2^2;

sbit lcd1602_rw=P2^1;

sbit lcd1602_busy=P0^7;

uchar str[]="StopWatch";

uchar time,disdat[4];

uchar dat,sec,key;

void delay(uchar a)

{

uchar i;

while(a--)for(i=0;i<120;i++);

}

/*

************************************

* 函数名称:lcd1602_CheckBusy()

* 函数功能:状态查询

************************************

*/

void lcd1602_CheckBusy()

{

do

{

lcd1602_busy=1;

lcd1602_rs=0;

lcd1602_rw=1;

lcd1602_e=0;

lcd1602_e=1;

}

while(lcd1602_busy);

}

/*

***************************************

* 函数名称: lcd1602_WriteCmd()

* 函数功能:写命令

* 入口参数:命令字

* 出口参数:无

***************************************

*/

void lcd1602_WriteCmd(const uchar cmd)

{

lcd1602_CheckBusy();

lcd1602_rs=0;

lcd1602_rw=0;

lcd1602_e=1;

LCD1602_PORT=cmd;

lcd1602_e=0;

}

/*

*******************************************

* 函数名称:lcd1602_WriteData()

* 函数功能:写数据

* 入口参数:c--待写数据

* 出口参数:无

*********************************************

*/

void lcd1602_WriteData(const uchar c)

{

lcd1602_CheckBusy();

lcd1602_rs=1;

lcd1602_rw=0;

lcd1602_e=1;

LCD1602_PORT=c;

lcd1602_e=0;

}

/*

***********************************************

* 函数名称:lcd1602_Init()

* 函数功能:初始化LCD

* 入口参数:无

* 出口参数:无

***********************************************

*/

void lcd1602_Init()

{

lcd1602_WriteCmd(0x38); //显示模式为8位2行5*7点阵

lcd1602_WriteCmd(0x0c); //display enable,flag enable,flash enable,

lcd1602_WriteCmd(0x06); //flag move to right,screen don't move

lcd1602_WriteCmd(0x01); //clear screen

}

/*

************************************************

* 函数名称:lcd1602_Display()

* 函数功能: 字符显示

* 入口参数:ptr--字符或字符串指针

* 出口参数:无

* 说    明:用户可通过以下方式来调用:

*           1)lcd1602_Display("Hello,world!");

*           2) INT8U 存储类型 txt[]="要显示的字符串";

*              或者 INT8U 存储类型 txt[]={'t','x','t',..,'\0'};

*              INT8U *ptr;

*              ptr=&txt;

*              lcd1602_Display(ptr);

*              或 lcd1602_Display(txt);

*              或 lcd1602_Display(&txt);

************************************************

*/

void lcd1602_Display(const uchar *ptr,uchar line)

{

uchar data i=0;

uchar *data q;

q=ptr;

switch(line)

{

case 0:

lcd1602_WriteCmd(0x80);

while(q!=NULL && (*q!='\0') && i<16)

{

lcd1602_WriteData(*q);

q++;

i++;

}

break;

case 1:

lcd1602_WriteCmd(0xc0);

while(q!=NULL && (*q!='\0') && i<16)

{

lcd1602_WriteData(*q);

q++;

i++;

}

break;

}

}

void dischg()

{

disdat[3]=dat+0x30;

disdat[2]='.';

disdat[1]=sec%10+0x30;

disdat[0]=sec/10+0x30;

lcd1602_Display(disdat,1);

}

void ext0() interrupt 0

{

key++;

key%=3;

}

void t0isr() interrupt 1 //秒计时

{

TH0=0x3c;

TL0=0xb0;

time++;

if(time==2)  

{

time=0;

dat++;

if(dat>=10)

{

dat=0;

sec++;

if(sec>59)sec=0;

}

dischg();

}

}

main()

{

TMOD=0x01;

TH0=0x3c;

TL0=0xb0;

TR0=0;

ET0=1;

EX0=1;

IT0=1;

EA=1;

time=0;

dischg();

lcd1602_Init();

lcd1602_Display(str,0);

lcd1602_Display(disdat,1);

while(1)

{

switch(key)

{

case 0:TR0=0;break;

case 1:TR0=1;break;

case 2:sec=0;dat=0;dischg();break;

}

}

}