#include <STC15F2K60S2.H>
typedef unsigned char u8;
typedef unsigned int u16;
typedef char s8;
typedef int s16;
typedef unsigned long u32;
typedef struct
{
u8 b0 : 1;
u8 b1 : 1;
u8 b2 : 1;
u8 b3 : 1;
u8 b4 : 1;
u8 b5 : 1;
u8 b6 : 1;
u8 b7 : 1;
}bits;
typedef union
{
u8 hex;
bits b;
}HexToBin;
HexToBin led_ctrl,buzzer_ctrl;
void Device_Ctrl(u8 position,u8 dat)
{
P0 = dat;
P2 = position;
P2 = 0;
}
void System_Init()
{
led_ctrl.hex = 0xff;
Device_Ctrl(0x80,led_ctrl.hex);
buzzer_ctrl.hex = 0x00;
Device_Ctrl(0xa0,buzzer_ctrl.hex);
}
void Timer2Init(void) //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2 |= 0x04;
EA = 1;
}
u8 code SMG_duanxuan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
u8 SMG_buf[8];
u8 num;
void SMG_Process()
{
SMG_buf[0] = SMG_duanxuan[num/100];
SMG_buf[1] = SMG_duanxuan[num/10%10];
SMG_buf[2] = SMG_duanxuan[num%10];
SMG_buf[3] = 0;
SMG_buf[4] = 0;
SMG_buf[5] = 0;
SMG_buf[6] = 0;
SMG_buf[7] = 0;
}
void SMG_Display()
{
static u8 i;
Device_Ctrl(0xc0,0x00);
Device_Ctrl(0xe0,~SMG_buf[i]);
Device_Ctrl(0xc0,0x01<<i);
i = (i+1) % 8;
}
u8 cnt_BTN;
u8 Trig;
u8 Cont;
void BTN_Read_ThreeLine()
{
u8 ReadData = P3 ^ 0xff;
Trig = ReadData & (ReadData^Cont);
Cont = ReadData;
}
void BTN_ThreeLine_Process()
{
if(cnt_BTN >= 10)
{
cnt_BTN = 0;
BTN_Read_ThreeLine();
if(Trig & 0x01)//S7
{
num++;
}
if(Trig & 0x02)//S6
{
num--;
}
if(Trig & 0x04)//S5
{
num = num+10;
}
if(Trig & 0x08)//S4
{
num = num-10;
}
}
}
void main()
{
System_Init();
Timer2Init();
while(1)
{
SMG_Process();
}
}
void timer2_ISR() interrupt 12 //1毫秒中断
{
cnt_BTN++;
SMG_Display();
BTN_ThreeLine_Process();
}
蓝桥杯_独立按键(三行代码)
最新推荐文章于 2024-03-01 21:11:42 发布