int *p, calendar[12][13];
1.先说独立按键,有独立按键三步
unsigned char Trg;
unsigned char Cont;
#define KEYPORT P3
void Key_Read( void )
{
unsigned char ReadData = KEYPORT^0xff;
Trg = ReadData & (ReadData ^ Cont);
Cont = ReadData;
}
while(1)
{ //三行独立按键的实验
Key_Read();
if(Trg & 0x08)//S4
{
P2=0xa0;buzzer=1;P2=0x00;
}
if(Trg & 0x04)//S5
{
P2=0xa0;buzzer=0;P2=0x00;
}
}
trg可以检测单次的
cont可以检测一直的按键
2!!!qiao 重点!
unsigned char ReadData = KEYPORT^0xff;
Trg = ReadData & (ReadData ^ Cont);
Cont = ReadData;
所以按键功能处理程序不会重复执行,省下了一大堆的条件判断,这个可是精粹哦!!Cont代表的是长按键,如果PB0按着不放,那么Cont的值就为 0x01,相对应,PB7按着不放,那么Cont的值应该为0x80,同样很好理解。
#include
#include
#define keypress P3
sbit buzzer=P0^6;
/*silu: 首先我要确认状态
然后消抖后继续确认状态
最后如果真的按下后的反应,按键控制、*/
unsigned char trg,cont,i;
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 43;
j = 6;
k = 203;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
char keyread()
{
unsigned char key1,sreturn,key2;
1) static char keystate=0;
keypress=0x0f;
key1=keypress&0x0f;
keypress=0xf0;
key2=keypress&0xf0;
keypress=key1|key2;
2) switch (keystate)
{
case 0: if(keypress!=0xff) keystate=1;break;//spend 10 ms
case 1:
if(keypress!=0xff)
{
if(keypress==0xde) sreturn =1;//s15
if(keypress==0xdd) sreturn =2;//s14
if(keypress==0xdb) sreturn=3;
keystate=2;
}
else keystate=0;
break;//说明在抖动
case 2:if(keypress==0xff) keystate=0; break;
}
return sreturn;
}
void led()
{
for(i=0;i<8;i++)
{
P2=0x80;
P0=~(0x01<<i);
Delay1000ms();
if(i==8)
i=0;
}
}
main()
{
unsigned char key_v;
while(1)
{
key_v=keyread();
if(key_v==1)//s
{
led();
}
if(key_v==2)
{
P2=0xa0;
buzzer=1;
P2=0x00;
}
if(key_v==3)
{
P2=0xa0;
buzzer=0;
P2=0x00;
}
}
}
解释
1)参数方程中状态不能函数结束就被释放,因而要用static
2)switch检测三种状态时
state=0 检测是否有按键按下,如果没有,则break进行下一次循环
state=1 检测按键是否抖动,检测出对应的按键后,并不着急输出,而是进行下一次的case 2,检验此时是否按键仍然按下,若检测出就是抖动,break重来,如果确实没有抖动!恭喜,这个switch函数圆满结束,输出 state值
然后主函数就可以使用它来对应按键对应功能了。
#include
#include "intrins.h"
#define key P3
void delay2ms()
{
unsigned char i, j;
_nop_();
_nop_();
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
char keyKBD()
{
unsigned char key1,key2,keypress;
static unsigned char state,returnn;
key=0x0f; key1=key&0x0f;
key=0xf0; key2=key&0xf0;
keypress=key1|key2;
switch(state)
{
case 0:
if(keypress!=0xff) state=1; break;
case 1:
if(keypress==0xbe) returnn=0;
if(keypress==0xbd) returnn=1;
if(keypress==0xbb) returnn=2;
if(keypress==0xb7) returnn=3;
if(keypress==0xde) returnn=4;
if(keypress==0xdd) returnn=5;
if(keypress==0xdb) returnn=6;
if(keypress==0xd7) returnn=7;
state=2; break;
case 2:
if(keypress!=0xff)
state=0;
}
return returnn;
}
//思路:按键最主要就是消抖嘛,记得住的其实状态机还是记得住,
//case 0 是否按键 case 1 检测按得哪个键 case 2,
//利用定时器中断两ms然后while(1)使它一直停留在那样的状态!
unsigned char duansmg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char weismg[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
main()
{
while(1)
{
P2=0xe0; P0=~duansmg[keyKBD()];delay2ms(); P2=0x00;
P2=0xc0; P0=weismg[keyKBD()];delay2ms(); P2=0x00;
delay2ms();
}
}
#include
#include "intrins.h"
void delay2ms()
{
unsigned char i, j;
_nop_();
_nop_();
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
//思路:利用定时器中断两ms然后while(1)使它一直停留在那样的状态!
unsigned char duansmg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char weismg[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
main()
{
static unsigned char i;
while(1)
{
P2=0xe0; P0=~duansmg[i]; P2=0x00;//有一层逻辑关系,先有显示的数据,然后才有哪个数码管显示什么
P2=0xc0; P0=weismg[i]; P2=0x00;
i++;
if((i==8)!=0) i=0;
delay2ms();
}
}
#include
#include "intrins.h"
static unsigned char i;
typedef unsigned char BYTE;
typedef unsigned int WORD;
unsigned char duansmg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char weismg[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
//-----------------------------------------------
/* define constants */
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
/* define SFR */
sbit TEST_LED = P1^0; //work LED, flash once per second
/* define variables */
WORD count; //1000 times counter
//-----------------------------------------------
/* Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8;
P2=0xe0; P0=~duansmg[i]; P2=0x00;//有一层逻辑关系,先有显示的数据,然后才有哪个数码管显示什么
P2=0xc0; P0=weismg[i]; P2=0x00;
i++;
if(i==8) i=0;
//reload timer0 high byte
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 2; //reset counter
TEST_LED = ! TEST_LED; //work LED flash
}
}
//-----------------------------------------------
/* main program */
void initial()
{
TMOD = 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1; //timer0 start running
ET0 = 1; //enable timer0 interrupt
EA = 1; //open global interrupt switch
count = 0; //initial counter
while (1); //loop
}
//思路:利用定时器中断两ms然后while(1)使它一直停留在那样的状态!
main()
{
initial();
while(1);
}
#include "reg52.h"
#include
//thought:目的是要自动调节的温度感应显示器。首先时序文件开初已有,现在其实就是利用数码管动态显示来辨别温度大小。
//然后,要注意中断可能会对DS18B20时序有影响,所以在进行BS18B20初始化读写程序中要关闭中断.
#include "onewire.h"
void Delay2ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 4;
j = 146;
do
{
while (--j);
} while (--i);
}
unsigned char temp;
unsigned char duansmg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x80};
unsigned char weismg[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
typedef unsigned char BYTE;
typedef unsigned int WORD;
//-----------------------------------------------
/* define constants */
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
/* define SFR */
sbit TEST_LED = P1^0; //work LED, flash once per second
/* define variables */
WORD count; //1000 times counter
//-----------------------------------------------
/* Timer0 interrupt routine */
unsigned char temptable[2];
void tm0_isr() interrupt 1 using 1
{
unsigned char i;
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8; //reload timer0 high byte
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1;
P2=0xe0; P0=~duansmg[temptable[i]];P2=0x00;
P2=0xc0; P0=weismg[i]; P2=0x00;
i++;
if(i==2) i=0;
//reset counter
TEST_LED = ! TEST_LED; //work LED flash
}
}
//-----------------------------------------------
/* main program */
void main()
{
TMOD = 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1;
EA=1; //timer0 start running
ET0 = 1; //enable timer0interrupt
//open globalinterrupt switch
count = 0; //initial counter
P2=0xa0;P0=0x00;P2=0x00;
while (1)
{
EA=0;
temp=(unsigned char)rd_temperature_f();
EA=1;
temptable[0]=temp/10;
temptable[1]=temp%10;
Delay2ms();
}
}
我吐了,就是不对!!!!哪天对了一定要过来写笔记!
与.
或+,v
非
异或
https://www.imooc.com/article/16813?block_id=tuijian_wz //原作者保留权利
1.反码可以计算出一个整数加一个负数