显示问题如图,为什么?
- #include <reg51.h>
- #include <intrins.h>
- #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 Display_String(uchar *,uchar);
- sbit K1 = P1^0;
- sbit K2 = P1^1;
- sbit BEEP = P3^0;
- sbit LCD_RS = P2^0;
- sbit LCD_RW = P2^1;
- sbit LCD_EN = P2^2;
- uchar KeyCount = 0;
- uchar code msg1[] ={" Miao Biao "};
- uchar code msg2[] ={">>>> "};
- uchar code Prompts[] [16] =
- {
- {":: 1----> "},
- {":: 1----> ::2 "},
- {":: 1->2 ::3--> "},
- {":: 1->2 ::3-->4 "}
- };
- // 计时缓冲与显示缓冲
- uchar Time_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--);
- {
- //将两位整数的1/100s,秒,分,时转换为8位数字字符
- LCD_Display_Buffer[2*i+1] = Time_Buffer[i] / 10 + '0';
- LCD_Display_Buffer[2*i ] = Time_Buffer[i] % 10 + '0';
- //在 = 3,2,1,0时分别显示时,分,秒,1/100s
- LCD_Write_Date(LCD_Display_Buffer[2*i+1]);
- LCD_Write_Date(LCD_Display_Buffer[2*i]);
- LCD_Write_Date(':');
- }
- }
- // Time0中断
- void Time0( ) interrupt 1 using 0
- {
- TH0 = -10000 / 256;
- TL0 = -10000 % 256;
- Time_Buffer[0] ++;
- if(Time_Buffer[0] == 100)
- {
- Time_Buffer[0] = 0; Time_Buffer[1]++;
- }
- if(Time_Buffer[1] == 60) //秒
- {
- Time_Buffer[1] = 0; Time_Buffer[2]++;
- }
- if(Time_Buffer[2] == 60) //分
- {
- Time_Buffer[2] = 0; Time_Buffer[3]++;
- }
- if(Time_Buffer[3] == 24) //时
- Time_Buffer[3] = 0;
- }
- //
- //主函数
- //
- void main( )
- {
- uchar i;
- IE = 0x82;
- TMOD = 0x01;
- TH0 = -10000 / 256;
- TL0 = -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: TR0 = 1;
- Display_String(Prompts[i-1],0);
- break;
- case 2:
- case 4: TR0 = 0;
- Display_String(Prompts[i-1],0);
- break;
- default:TR0 = 0;
- break;
- }
- while (K1 == 0); // 等待释放K1键
- Beep( );
- }
- else
- if(K2 == 0)
- {
- TR0 = 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
- #define uint unsigned int
- #define DelayNOP( ){_nop_( ); _nop_( ); _nop_( ); _nop_( );}
- sbit LCD_RS = P2^0;
- sbit LCD_RW = P2^1;
- sbit LCD_EN = P2^2;
- 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;
- while(ms--) for (t =0;t < 120;t++);
- }
- // LCD忙检查
- bit LCD_Busy_Check( )
- {
- bit Result;
- LCD_RS = 0; LCD_RW = 1; LCD_EN = 1;DelayNOP( );
- Result = (bit)(P0 & 0x80);
- LCD_EN = 0;
- return Result;
- }
- // 向LCD写指令
- void LCD_Write_Command(uchar cmd)
- {
- while(LCD_Busy_Check() );
- LCD_RS = 0; LCD_RW = 0; LCD_EN = 0;
- _nop_( ); _nop_( );
- P0 = cmd; 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;P0 = 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 Position)
- {
- LCD_Write_Command(Position | 0x80);
- }
- //显示函数,在LCD指定行上显示字符串
- void Display_String(uchar *str,uchar LineNO)
- {
- uchar k;
- LCD_Set_POS(LineNO);
- for (k = 0;k < 16;k++) LCD_Write_Date(str[k]);
- }
复制代码
-
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>