一、原理部分
和定时扫描矩阵按键差不多的原理,只不过跳线帽连接2和3。不明白的可以看这篇博客。
定时扫描矩阵
二、代码部分
实验平台:CT107D
实验芯片:stc15f2k60s2
实验现象:S7打开蜂鸣器,S6关闭蜂鸣器,S5打开继电器,S4关闭继电器
代码如下(这里使用了标志位控制蜂鸣器和继电器)
#include<stc15f2k60s2.h>
#define uchar unsigned char
#define uint unsigned int
sbit buzz = P0^6;
sbit relay = P0^4;
bit buzz_flag = 0;
bit relay_flag = 0;
sbit key_in1 = P3^0;
sbit key_in2 = P3^1;
sbit key_in3 = P3^2;
sbit key_in4 = P3^3;
uchar keysta[] = {1,1,1,1};
uchar kaymap[] = {1,2,3,4};
//uchar code duan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};//定义段码数组
//uchar disbuff[8];//定义显示数字数字
void set_buzz()
{
if(buzz_flag == 0)
{
P2 = P2&0x1f|0xa0;
buzz = 0;
P2 = P2&0x1f;
}
if(buzz_flag == 1)
{
P2 = P2&0x1f|0xa0;
buzz = 1;
P2 = P2&0x1f;
}
}
void set_relay()
{
if(relay_flag == 0)
{
P2 = P2&0x1f|0xa0;
relay = 0;
P2 = P2&0x1f;
}
if(relay_flag == 1)
{
P2 = P2&0x1f|0xa0;
relay = 1;
P2 = P2&0x1f;
}
}
void keyscan()
{
static uchar keybuff[]={0xff,0xff,0xff,0xff};
uchar j;
keybuff[0] = keybuff[0]<<1|key_in1;
keybuff[1] = keybuff[1]<<1|key_in2;
keybuff[2] = keybuff[2]<<1|key_in3;
keybuff[3] = keybuff[3]<<1|key_in4;
for(j=0;j<4;j++)
{
if((keybuff[j]&0x0f) == 0x00)
{
keysta[j]=0;
}
else if((keybuff[j]&0x0f) == 0x0f)
{
keysta[j]=1;
}
}
}
void keyfun(uchar key_value)
{
switch(key_value)
{
case 1:buzz_flag=1;break;
case 2:buzz_flag=0;break;
case 3:relay_flag=1;break;
case 4:relay_flag=0;break;
}
}
void keydrive()
{
static uchar keyback[]={1,1,1,1};
uchar i;
for(i=0;i<4;i++)
{
if(keysta[i]!=keyback[i])
{
if(keysta[i] != 0)
{
keyfun(kaymap[i]);
}
keyback[i]= keysta[i];
}
}
}
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1 ;
}
void time0() interrupt 1
{
keyscan();
}
void main()
{
Timer0Init();
while(1)
{
set_buzz();
set_relay();
keydrive();
}
}