大家帮忙看看吧,主程序要怎么改啊。。谢谢了~
1.当按下按钮时,LCD显示为"time=0",再按一下时"time=1",依次递加到"time=24"当按24次时
2.当确认某个时间时,按住按钮保持2秒以上,便可确认设置成功
3.LCD是8x2的,第一排可以显示"Set Time",第二排显示时间"time=0"
4.用的是单片机8051f226
//#include <reg52.h>
#include <c8051f200.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_DATA P3 //data port for LCD interfac
sbit LCD_RS=P2^6; //LCD control pins
sbit LCD_RW=P0^5;
sbit LCD_EN=P2^0;
sbit key=P1^3;
int a=0;
void lcd_com(uchar com); //LCD command code
void lcd_data(uchar da);
void PORT_Init (void); //ASCII character to be display on LCD
void init();
void lcd_msg1(char*msg)
{
char*pstr;
pstr=msg;
while(*pstr)
{
lcd_data(*pstr++);
}
}
uchar delay(uint z) //delay for z ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
return a;
}
void lcd_com(uchar com) //8 bits interface,to LCD command
{
LCD_DATA=com;
LCD_RW=0;
LCD_RS=0;
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void lcd_data(uchar da) //send a byte to LCD data port
{
LCD_DATA=da;
LCD_RW=0; //data byte sent to the data bus
LCD_RS=1;
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void init()
{
LCD_DATA=0;
lcd_com(0x38); //LCD initialized 8-bit interface
delay(10);
lcd_com(0x38); //LCD initialized 8-bit interface
delay(10);
lcd_com(0x38); //LCD initialized 8-bit interface
delay(10);
lcd_com(0x0c); delay(5); //display on, cursor off
lcd_com(0x06); delay(5); //cursor moving direction
lcd_com(0x01); delay(5); //clear display
lcd_com(0x02); delay(5); //cursor home
}
void main()
{
LCD_RS=0;
LCD_EN=0;
init();
PORT_Init();
while(1)
{
if(key!=1) //button pressed
{
while(key!=1);
{ //wait for button to be released
a++; //count the times of button pressed
if(a==1) //button pressed in first time
{
lcd_com(0x80);
delay(100);
lcd_msg1("Time=0");
delay(100);
}
a=1;
}
}
if(key!=1)
{
while(key!=1);
{
if(a==2) //button pressed in second time
{
lcd_com(0x80);
delay(100);
lcd_msg1("Time=1");
delay(100);
}
a=2;
}
}
if(key!=1)
{
while(key!=1);
{
if(a==3) //button pressed in third time
{
lcd_com(0x80);
delay(100);
lcd_msg1("Time=2");
delay(100);
}
a=3;
}
}
}
}
void PORT_Init (void)
{
PRT1CF |= 0x08;
}