//----------------------------------------------------------------------------------------
// File Name: KeyToLed.c
// Author : Great Tiger
// Function : Drive a LED flash
// 说明 : 这里只是一个51MCU按键控制LED灯的程序。可能很多人很不解,一个再简单不过的功能何必搞得那么复杂,是在炫吗。
//: 如果你这么想可以绕道了。这里浓缩很多技巧,是我把一个算是蛮大的前后台程序的一些常用而又不能丢的技巧用在了
//: 这个按键控制LED灯的程序上(呵呵,源程序是台湾人写的多媒体播放器的程序)。程序的可读性,可移植性都在这里
//: 很好的表现了,这里只是开始,还有很多就等初学都慢慢体会吧。如果你只会 if(P1^0 == 0) P1^1 = 0; 你是做不了好的产品的。
// Update : 2012-03-25
//----------------------------------------------------------------------------------------
#include "STC12C2052AD.h"
// 常用数据类型名重定义
typedef bit bool;
typedef unsigned charuint8;
typedef unsigned int uint16;
typedef unsigned longuint32;
#define TRUE(bool)1
#define FALSE(bool)0
#define HEIGH(bool)1
#define LOW(bool)0
// 自定义开关常量
#define ledONLOW
#define ledOFFHEIGH
// Timer Conversion (unit: us)
#define _1S_PERIOD 1000000UL // 1S = 1000000US
#define _1MS_PERIOD1000
#define CPU_F12000000UL // 晶振频率
#define _CPU_PERIOD((float)_1S_PERIOD / (CPU_F / 12)) // CPU 指令周期
//--------------------------------------------------
// Timer1 Period (unit: ns)
//--------------------------------------------------
#define _TIMER1_PERIOD_1MS_PERIOD // T1 定时周期
#define _TIMER1_COUNTER(uint32)(0xFFFF - (_TIMER1_PERIOD / _CPU_PERIOD))
typedef struct
{
uint16 mSecond;
uint8 Second;
uint8 Minute;
}TimeType;
TimeType tMyTime;
uint16 Timer1Count; // 空间换时间,避免重复计算
sbit bLED = P3^5; // LED
sbit bKey = P3^4; // 按键
#define KeyStableCounter30 // 防抖延时时间
#define KeyEvent2
uint8 ucKeyStableCountBuff[KeyEvent]; // 防抖延时计数器
// 一个按键动作的两个状态
bool bKeyDown = FALSE;
bool bKeyUp = FALSE;
//----------------------------------------------------------
void IO_Init(void)
{
bLED = HEIGH; // 假设LOW亮
bKey = HEIGH; // 假设LOW有效
}
void ClearRTC(void)
{
tMyTime.mSecond = 0;
tMyTime.Second = 0;
tMyTime.Minute = 0;
}
void Timer1_Init(void)
{
Timer1Count = _TIMER1_COUNTER;
ET1= 1;
TMOD = 0x10;
TH1 = Timer1Count >> 8;
TL1 = (uint8)Timer1Count;
EA = 1;
TR1 = 1;
}
void SystemInit(void)
{
IO_Init();
ClearRTC();
Timer1_Init();
}
void KeyStableValueInit(void)
{
uint8 i;
for(i=0; i!=KeyEvent; i++)
ucKeyStableCountBuff[i] = 0;
}
void ScanKey(void)
{
static bool bVKeyPre = HEI