怎样用C语言做一个按钮并设置点击反应
4个回答
展开全部
void delay() //延迟子程序
{
unsigned char i,j;
i=5;
while(i--)
for(j=0;j<200;j++);
}
uint key() //按键子程序
{
f(k1==0)// 当按键按下时
{
delay( );// 延迟等待
if(k1==0) // 如果按键还是按下的动作,确定按键已经按下
while(!k1) ; // 当按键有弹起以后,再执行动作
{ /*这里自己/设置按键按下以后要执行的动作*/}
}
}
希望对您有所帮助!
{
unsigned char i,j;
i=5;
while(i--)
for(j=0;j<200;j++);
}
uint key() //按键子程序
{
f(k1==0)// 当按键按下时
{
delay( );// 延迟等待
if(k1==0) // 如果按键还是按下的动作,确定按键已经按下
while(!k1) ; // 当按键有弹起以后,再执行动作
{ /*这里自己/设置按键按下以后要执行的动作*/}
}
}
希望对您有所帮助!
展开全部
// Program to make a digital clock using LCD
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define port P1
#define LCD_DATA P3 // Data port for LCD
sbit LCD_RS=P2^6; //LCD control pins
sbit LCD_RW=P0^5;
sbit LCD_EN=P2^0;
sbit key=port^3;
sbit dig_hr1=port^3;
sbit dig_min1=port^3;
int hr ,hr1=0;
int min,min1=0;
int sec,sec1=0;
uchar delay(uint z) //delay for z ms
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<2000;y++);
return 0;
}
void lcd_com(unsigned char com) // Function to send command on LCD
{
LCD_DATA = com;
LCD_RS= 0;
LCD_RW=0;
LCD_EN=1;
delay(1);
LCD_EN=0;
return;
}
void lcd_data(unsigned char da) // Function to send data on LCD
{
LCD_DATA = da;
LCD_RS= 1;
LCD_RW=0;
LCD_EN=1;
delay(1);
LCD_EN=0;
return;
}
void lcd_msg1(char*msg)
{
char*pstr;
pstr=msg;
while(*pstr)
{
lcd_data(*pstr++);
}
}
lcd_data_int(int time_val) // Function to send number on LCD
{
int int_amt;
int_amt=time_val/10;
lcd_data(int_amt+48);
int_amt=time_val%10;
lcd_data(int_amt+48);
}
void init()
{
lcd_com(0x38); //LCD initialized 8-bit interface
lcd_com(0x0c); //display on, cursor off
lcd_com(0x06); //cursor moving direction
lcd_com(0x01); //clear display
lcd_com(0x02); //cursor home
}
void set_hr1() // Function to set hour
{
hr1++;
if(hr1>11)
hr1=0;
lcd_com(0xc0);
lcd_data_int(hr1);
lcd_data(':');
}
void set_min1() // Function to set minute
{
min1++;
if(min1>59)
min1=0;
lcd_com(0xc0);
lcd_data_int(min1);
}
void main()
{
int k;
key=1;
dig_hr1=1;
dig_min1=1;
lcd_com(0x01);
lcd_com(0x80);
lcd_msg1("SET TIME");
lcd_com(0xc0);
lcd_data_int(hr1);
lcd_data(':');
lcd_data_int(min1);
while(key!=0)
{
delay(10);
if(dig_hr1==1)
set_hr1();
if(dig_min1==1)
set_min1();
}
delay(200);
lcd_com(0x01);
while(1)
{
for(k=0;k<2;k++)
{
for(hr=hr1;hr<12;hr++)
{
for(min=min1;min<60;min++)
{
for(sec=0;sec<60;sec++)
{
lcd_com(0x82);
delay(1);
lcd_data_int(hr);
lcd_data(':');
lcd_data_int(min);
lcd_data(':');
lcd_data_int(sec);
}
}
}
}
}
}
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define port P1
#define LCD_DATA P3 // Data port for LCD
sbit LCD_RS=P2^6; //LCD control pins
sbit LCD_RW=P0^5;
sbit LCD_EN=P2^0;
sbit key=port^3;
sbit dig_hr1=port^3;
sbit dig_min1=port^3;
int hr ,hr1=0;
int min,min1=0;
int sec,sec1=0;
uchar delay(uint z) //delay for z ms
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<2000;y++);
return 0;
}
void lcd_com(unsigned char com) // Function to send command on LCD
{
LCD_DATA = com;
LCD_RS= 0;
LCD_RW=0;
LCD_EN=1;
delay(1);
LCD_EN=0;
return;
}
void lcd_data(unsigned char da) // Function to send data on LCD
{
LCD_DATA = da;
LCD_RS= 1;
LCD_RW=0;
LCD_EN=1;
delay(1);
LCD_EN=0;
return;
}
void lcd_msg1(char*msg)
{
char*pstr;
pstr=msg;
while(*pstr)
{
lcd_data(*pstr++);
}
}
lcd_data_int(int time_val) // Function to send number on LCD
{
int int_amt;
int_amt=time_val/10;
lcd_data(int_amt+48);
int_amt=time_val%10;
lcd_data(int_amt+48);
}
void init()
{
lcd_com(0x38); //LCD initialized 8-bit interface
lcd_com(0x0c); //display on, cursor off
lcd_com(0x06); //cursor moving direction
lcd_com(0x01); //clear display
lcd_com(0x02); //cursor home
}
void set_hr1() // Function to set hour
{
hr1++;
if(hr1>11)
hr1=0;
lcd_com(0xc0);
lcd_data_int(hr1);
lcd_data(':');
}
void set_min1() // Function to set minute
{
min1++;
if(min1>59)
min1=0;
lcd_com(0xc0);
lcd_data_int(min1);
}
void main()
{
int k;
key=1;
dig_hr1=1;
dig_min1=1;
lcd_com(0x01);
lcd_com(0x80);
lcd_msg1("SET TIME");
lcd_com(0xc0);
lcd_data_int(hr1);
lcd_data(':');
lcd_data_int(min1);
while(key!=0)
{
delay(10);
if(dig_hr1==1)
set_hr1();
if(dig_min1==1)
set_min1();
}
delay(200);
lcd_com(0x01);
while(1)
{
for(k=0;k<2;k++)
{
for(hr=hr1;hr<12;hr++)
{
for(min=min1;min<60;min++)
{
for(sec=0;sec<60;sec++)
{
lcd_com(0x82);
delay(1);
lcd_data_int(hr);
lcd_data(':');
lcd_data_int(min);
lcd_data(':');
lcd_data_int(sec);
}
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询