本文最后更新于96 天前,如有错误请联系本人VX: g1489269494
各模块初始化函数(上电、LED、矩阵键盘、数码管)
/*----------------------------------上电初始化-----------------------------------*/
void System_Init()
{
P0 = 0xFF;
P2 = P2 & 0x1F | 0x80;
P2 = P2 & 0x1F;
P0 = 0x00;
P2 = P2 & 0x1F | 0xA0;
P2 = P2 & 0x1F;
}
/*---------------------------------LED初始化函数--------------------------------*/
void LED_Set(unsigned char arr , unsigned char status)
{
static unsigned char temp = 0x00;
static unsigned char temp_old = 0xFF;
if(status){
temp |= 0x01 << arr;
}
else{
temp &= ~(0x01 << arr);
}
if(temp != temp_old){
P0 = ~temp;
P2 = P2 & 0x1F | 0x80;
P2 &= 0x1F;
temp_old = temp;
}
}
/*-------------------------------矩阵键盘初始化函数----------------------------*/
unsigned char Key_Get()
{
unsigned char temp = 0;
P44 = 0; P42=1;P35=1;P34=1;
if(P33==0)temp =4;
if(P32==0)temp =5;
if(P31==0)temp =6;
if(P30==0)temp =7;
P44 = 1; P42=0;P35=1;P34=1;
if(P33==0)temp =8;
if(P32==0)temp =9;
if(P31==0)temp =10;
if(P30==0)temp =11;
P44 = 1; P42=1;P35=0;P34=1;
if(P33==0)temp =12;
if(P32==0)temp =13;
if(P31==0)temp =14;
if(P30==0)temp =15;
P44 = 0; P42=1;P35=1;P34=0;
if(P33==0)temp =16;
if(P32==0)temp =17;
if(P31==0)temp =18;
if(P30==0)temp =19;
return temp;
}
/*-------------------------------数码管初始化函数----------------------------*/
unsigned char code smg_duanxuan[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xFF};//10- 11灭
unsigned char code smg_weixuan[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void Smg_Set(unsigned char wei,duan,point)
{
P0 = 0xFF;
P2 = P2 & 0x1F | 0xE0;
P2 &= 0x1F;
P0 = smg_weixuan[wei];
P2 = P2 & 0x1F | 0xC0;
P2 &= 0x1F;
if(point){
P0 = smg_duanxuan[duan] & 0x7F;// 0111 1111
P2 = P2 & 0x1F | 0xE0;
P2 &= 0x1F;
}
else{
P0 = smg_duanxuan[duan];
P2 = P2 & 0x1F | 0xE0;
P2 &= 0x1F;
}
}
/*--------------------------------------------------------------------------*/
主函数部分
#include <STC15F2K60S2.H>
#include "INit.h"
#include "LED.h"
#include "smg.h"
#include "Key.h"
unsigned char key_slow_down;
unsigned char key_old,key_val,key_down,key_up;
unsigned int smg_slow_down;
unsigned char smg_pos;
unsigned char smg_buf[8] = {1,2,3,4,5,6,7,8};
unsigned char smg_point[8] = {0,0,0,0,0,0,0,0};
unsigned char led_buf[8] = {0,0,0,0,0,0,0,0};
void Key_Function()
{
if(key_slow_down)return;
key_slow_down = 1;
key_val = Key_Get();
key_down = key_val & (key_val ^ key_old);
key_up = ~key_val & (key_val ^ key_old);
key_old = key_val;
}
void smg_Function()
{
if(smg_slow_down)return;
smg_slow_down = 1;
}
void LED_Function()
{
}
void Timer0_Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初始值
TH0 = 0xFC; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA=1;
}
void Timer0_Function() interrupt 1
{
if(++key_slow_down==10)key_slow_down=0;
if(++smg_slow_down==50)smg_slow_down=0;
if(++smg_pos==8)smg_pos = 0;
Smg_Set(smg_pos,smg_buf[smg_pos],smg_point[smg_pos]);
LED_Set(smg_pos,led_buf[smg_pos]);
}
void main()
{
System_Init();
Timer0_Init();
while(1)
{
Key_Function();
smg_Function();
LED_Function();
}
}