1、源程序代码:/名称:用1602LCD设计的秒表/说明:首先按下K1键时开始计时,自此按下时暂停,第三次按下时继续累积计时,再次按下时停止计时,K2键用于清零秒表。/#include #include #define uchar unsigned char#define uint unsigned int#define delayNOP ( ) _nop_( ); _nop_( ); _nop_( ); _nop_( );/LCD控制函数void LCD_Initialize( );void LCD_Set_POS(uchar);void LCD_Write_Date(uchar);void D
2、isplay_String(uchar,uchar);sbit K1 = P10;sbit K2 = P11;sbit BEEP = P30;sbit LCD_RS = P20;sbit LCD_RW = P21;sbit LCD_EN = P22;uchar KeyCount = 0;uchar code msg1 = Second Watch 0 ;uchar code msg2 = 0 ;uchar code Prompts 16 =: 1- - - - ,: 1- - - - :2 ,: 1 - 2 :3- - ,: 1 - 2 :3- - 4 ;/ 计时缓冲与显示缓冲uchar Ti
3、me_Buffer =0,0,0,0;uchar LCD_Display_Buffer=00: 00: 00:00;/ 蜂鸣器/void Beep ()uchar i,j = 70;for (i = 0;i 180;i+ )while(-j);BEEP = BEEP;BEEP = 0;/ 延时/void DelayX(uint ms) uchar i;while(ms-) for (i= 0;i 120;i+);/ 显示计时/void Show_Second( ) uchar i; LCD_Set_POS(0x45); /设置LCD显示起点for(i = 3;i != 0xff ;i-);/将
4、两位整数的1/100s,秒,分,时转换为8位数字字符LCD_Display_Buffer2i+1 = Time_Bufferi / 10 + 0 ;LCD_Display_Buffer2i = Time_Bufferi % 10 + 0 ;/在 = 3,2,1,0时分别显示时,分,秒,1/100sLCD_Writer_Date(LCD_Display_Buffer2i+1)LCD_Writer_Date(LCD_Display_Buffer2i)LCD_Writer_Date(: )/ Time0中断/Void Time0( ) interrupt 1 using 0THO = -10000
5、/ 256;TLO = -10000 % 256;Time_Buffer0 +if(Time_Buffer0 = 100) Time_Buffer0 = 0; Time_Buffer1+;if(Time_Buffer1 = 60) /秒 Time_Buffer1 = 0; Time_Buffer2+;if(Time_Buffer2 = 60) /分 Time_Buffer2 = 0; Time_Buffer3+;if(Time_Buffer3 = 24) /时 Time_Buffer3 = 0;/主函数/void main( ) uchar i; IE = 0x82; TMOD = 0x01;
6、 THO = -10000 / 256; TLO = -10000 % 256; LCD_Initialize( ); Display_String(msg1,0x00);Display_String(msg2,0x40); While(1) if(k1 = 0) DelayX(100); i = +KeyCount; switch (i) case 1: case 3: TRO = 1; Display_String(Promptsi-1,0); Break;case 2: case 4: TRO = 0; Display_String(Promptsi-1,0); break; defau
7、lt:TRO = 0; break; While (K1 = 0); / 等待释放K1键 Beep( ); else if(K2 = 0) TRO = 0; KeyCount = 0; for(i = 0;i 4; i+)Time_Buffer i = 0; / 清零计数缓冲Display_String(msg1,0);Beep( );DelayX(100);while (K2 = 0); / 等待释放K2键 Show_Second( );/ 1602LCD显示驱动函数/#include reg51.h#include intrins.h#define uchar unsigned char#
8、define uint unsigned int#define DellayNOP( )_nop_( ); _nop_( ); _nop_( ); _nop_( );sbit LCD_RS = P20;sbit LCD_RW = P21;sbit LCD_EN = P22;bit LCD_Busy_Check( );void LCD_Initialize( );void LCD_Set_POS(uchar);void LCD_Write_Command(uchar);void LCD_Write_ Date(uchar);/ 延时/void DelayMS(uint ms) uchar t;
9、while(ms-) for (t =0;t 120;t+);/ LCD忙检查/bit LCD_Busy_Check( );bit Rsult;LCD_RS = 0; LCD_RW = 1; LCD_EN = 1;DelayNOP( );Result = (bit)(p0 0x80);LCD_EN = 0return Result;/ 向LCD写指令/void LCD_Write_Command(uchar cmd);while(LCD_Busy_Check() );LCD_RS = 0; LCD_RW = 0; LCD_EN = 0; _nop_( ); _nop_( );pO = cmd;
10、 DelayNOP( );LCD_EN = 1;DelayNOP( );LCD_EN = 0;/ 向LCD写数据/void LCD_Write_Date(uchar str);while(LCD_Busy_Check() );LCD_RS = 1; LCD_RW = 0; LCD_EN = 0;pO = str; DelayNOP( );LCD_EN = 1;DelayNOP( );LCD_EN = 0;/ 初始化LCD/void LCD_Initialize( );DelayMS(5); LCD_Write_Command(0x38);DelayMS(5); LCD_Write_Command(0x0c);DelayMS(5); LCD_Write_Command(0x06);DelayMS(5); LCD_Write_Command(0x01);DelayMS(5);/ 设置显示位置/void LCD_Set_POS(uchar Poition)LCD_Write_Command(Position 0x80);/显示函数,在LCD指定行上显示字符串/void Display_String(ucharstr,uchar LineNO) uchar k; LCD_Set_POS(LineNO); for (k = 0;k 16;k+) LCD_Write_Date(strk)