单片机秒表程序.doc
#include<reg52.h> #include<intrins.h> #define uchar unsigned char sbit RS = P24; /定义端口 sbit RW = P25;sbit EN = P26;sbit s0=P20; / 暂停开始键sbit s1=P21; /清零键uchar ge,shi,bai;uchar time=0;uchar count;uchar press0_num=0; /s0按键次数uchar press1_num=0; /s1按键次数uchar code table="liu hui sb"void delayus(uchar t) /延时um级while(-t);void delay(uchar z) /延时函数ms 级 while(z-) delayus(245); delayus(245); bit lcd_check_busy() /判忙函数 P0=0xff; RS=0; RW=1; EN=0; _nop_(); EN=1; return (bit)(P0&0x80); /忙返回值是一,不忙是零void lcd_write_com(uchar com) /写命令 while(lcd_check_busy(); RS=0; RW=0; EN=1; P0=com; _nop_(); EN=0;void lcd_write_date(uchar date) /写数据 while(lcd_check_busy(); RS=1; RW=0; EN=1; P0=date; _nop_(); EN=0;void lcd_clear() lcd_write_com(0x01); delay(5); void LCD_Write_String(uchar x,uchar y,uchar *s) /液晶写入一个字符串 if (y=0) lcd_write_com(0x80+x); /表示第一行 else lcd_write_com(0xC0+x); /表示第二行 while (*s) lcd_write_date( *s); s+; void lcd_init()/初始化函数 lcd_write_com(0x38); /*显示模式设置*/ delay(5); lcd_write_com(0x38); delay(5); lcd_write_com(0x0c); /设置开显示,不显示光标 lcd_write_com(0x06); /写第一个字符后指针加一 lcd_write_com(0x01); /显示清零,数据指针指零 lcd_write_com(0x80); /显示第一行 void keyscan()if(!s0) /按键按下 /延时 delay(20); if(!s0) /确定按下 while(!s0); /松手有效press0_num+;if(press0_num=1) /按一下TR0=0; /d定时器关闭if(press0_num=2) /按两下TR0=1; /定时器开始press0_num=0; /按键次数清零 if(!s1) delay(20); if(!s1) while(!s1);press1_num+;if(press1_num=1)TR0=0;time=0; /计时从零开始if(press1_num=2)TR0=1;press1_num=0; void main() TMOD=0x11; /选择中断方式3 TH0=(66536-47040)/256; /覆初值 TL0=(66536-47040)%256; EA=1; ET0=1; TR0=1; lcd_init(); while(1) keyscan();s0=1; s1=1; if(count=20)count=0;time+;if(time>100)time=0;lcd_clear();ge=time%10;shi=time/10%10;bai=time/100;lcd_write_com(0x80); /选地址lcd_write_date(0x30+bai); /十位地址 lcdinit中有0x06有写第一个字符后指针加一lcd_write_date(0x30+shi); /十位地址 lcd_write_date(0x30+ge); /个位地址LCD_Write_String(1,1,table); void timer0() interrupt 1 TH0=(66536-47040)/256; TL0=(66536-47040)%256; count+;