我是新手,不要嘲笑我,希望这些例程对正在受“天祥”大叔的按键子程序残害的人有所帮助吧
希望大家都能用上状态机。
谢谢马老师的例程。
==================================================================================
独立按键(简单的状态机,无连_发)
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit led=P0^0;
void delay()
{
uint i,j;
for(i=100;i>0;i--)
for(j=118;j>0;j--);
}
uchar readkey()
{
static keystate=0,keytime=0;
uchar keypress,keyreturn=0;
keypress=P1;
switch(keystate)
{
case 0:
if (keypress!=0xff)
{
keystate=1;
}
break;
case 1:
if (keypress!=0xff)
{
keystate=2;
keytime=0;
}
else
keystate=0;
break;
case 2:
if (keypress==0xff)
{
keystate=0;
keyreturn=1;
}
else if (++keytime>=20000)
{
keystate=3;
keytime=0;
keyreturn=2;
}
break;
case 3:
if (keypress==0xff)
{
keystate=0;
}
else
{
if (++keytime>=10000)
{
keytime=0;
keyreturn=2;
}
}
break;
}
return keyreturn;
}
void main()
{
uchar k;
while(1)
{
switch(readkey())
{
case 1:
led=!led;
break;
case 2:
P0=0xfe;
for(k=0;k<8;k++)
{
delay();
P0=P0<<1|0X01;
}
break;
}
}
}
注:该子程序最好在中断设置标志位,10ms进入检测一次即可。本例程没有,为了方便。
=================================================================================================
矩阵键盘状态机(4×4,无连_发)
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define nokey 255
#define keymask 0x0f
sbit cs1=P1^0;
sbit cs2=P1^1;
sbit clk=P1^2;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar num,timecount=0;
bit timeok;
void delay_us(uint us)
{
while(--us);
}
//==============================延时子程序==================================
void delay_ms(uint ms)
{
uint i,j;
for(i=ms;i>0;i--)
for(j=118;j>0;j--);
}
//==============================显示子程序=================================
void display(void)
{
P2=tab[num];
cs1=0;
clk=0;clk=1;
cs1=1;
P2=0xfe;
cs2=0;
clk=0;clk=1;
cs2=1;
delay_ms(1);
}
//============================矩阵键盘按键状态机========================
uchar readkeyboard()
{
static uchar keystate=0,keyvalue,keyline;
uchar keyreturn=nokey,i;
switch(keystate)
{
case 0:
keyline=0x10;
for (i=1;i<=4;i++)
{
P0=~keyline;
P0=~keyline;
keyvalue=keymask&P0;
if (keyvalue==keymask)
{
keyline<<=1;
}
else
{
keystate++;
break;
}
}
break;
case 1:
if (keyvalue==(keymask&P0))
{
switch(keyline|keyvalue)
{
case 0x87:keyreturn=0;break;
case 0x8b:keyreturn=1;break;
case 0x8d:keyreturn=2;break;
case 0x8e:keyreturn=3;break;
case 0x47:keyreturn=4;break;
case 0x4b:keyreturn=5;break;
case 0x4d:keyreturn=6;break;
case 0x4e:keyreturn=7;break;
case 0x27:keyreturn=8;break;
case 0x2b:keyreturn=9;break;
case 0x2d:keyreturn=10;break;
case 0x2e:keyreturn=11;break;
case 0x17:keyreturn=12;break;
case 0x1b:keyreturn=13;break;
case 0x1d:keyreturn=14;break;
case 0x1e:keyreturn=15;break;
}
keystate++;
}
else
keystate--;
break;
case 2:
P0=0x0f;
P0=0x0f;
if ((keymask&P0)==keymask)
{
keystate=0;
}
break;
}
return keyreturn;
}
//==============================主程序==========================
void main()
{
uchar keytemp;
TMOD=0x01;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
if (timeok)
{ keytemp=readkeyboard();
if (keytemp!=nokey)
{
num=keytemp;
}
}
}
}
//=================================中断服务子程序====================
void timer0() interrupt 1
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
display();
if (++timecount>=5)
{
timecount=0;
timeok=1;
}
}
================================================================================
以上都是自己写的程序,希望不要喷我,我就是想让初学者的朋友们使用上状态机~
完
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
http://www.amobbs.com/thread-3985870-1-1.html 看看这个贴!认真看后一定会有收获的!
一周热门 更多>