刚学会用Proteus,做了个秒表,请高手指点
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#define T0HIGH 0xec
#define T0LOWW 0x8a
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
typedef bit BOOL;
sbit KEY_PIN = P2^0;
#define true 1
#define false 0
#define KEY_NULL 0
#define KEY_DOWN 0x80
#define KEY_LONG 0x40
#define KEY_UP 0x20
#define KEY_STATE_INIT 0
#define KEY_STATE_WOBBLE 1
#define KEY_STATE_PRESS 2
#define KEY_STATE_LONG 3
#define KEY_STATE_RELEASE 4
#define COUNT_INIT 0
#define COUNT_START 1
#define COUNT_STOP 2
code u8 DisCode[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0xff};
u8 CouNum[4] = {0x00, 0x00, 0x00, 0x00};
u8 DisNum[4] = {0x0a, 0x0a, 0x00, 0x00};
u8 DisPosition = 0;
u8 SecCount = 0;
BOOL b10msEvent = false;
BOOL b100msEvent = false;
BOOL CountStart = false;
void Key_Prosess(void);
void LCD_Display(void);
void Counter(void);
main()
{
TMOD = 0x01;
TH0 = T0HIGH;
TL0 = T0LOWW;
TR0 = 1;
ET0 = 1;
EA = 1;
P1 = 0x00;
while(1)
{
Key_Prosess(); //按键扫描
if(b10msEvent == true)
{
b10msEvent = false;
LCD_Display(); //动态显示
}
if(b100msEvent == true)
{
b100msEvent = false;
if(CountStart == true)
Counter(); //计时
}
}
}
void Num_Clear(void)
{
u8 i;
for(i=0; i<4; i++)
{
CouNum = 0;
}
DisNum[0] = DisNum[1] = 0x0a;
DisNum[2] = DisNum[3] = 0x00;
}
u8 Key_Scan(void)
{
if(KEY_PIN == false)
return (1);
return KEY_NULL;
}
u8 GetKey(void)
{
static u8 KeyState = KEY_STATE_INIT;
static u8 KeyTimeCount = 0;
static u8 LastKey = KEY_NULL; //保存按键释放时候的键值
u8 KeyTemp = KEY_NULL ;
KeyTemp = Key_Scan();
switch(KeyState)
{
case KEY_STATE_INIT: //初始状态
if(KEY_NULL != KeyTemp) //有键按下
{
KeyState = KEY_STATE_WOBBLE; //进入消抖
}
break;
case KEY_STATE_WOBBLE: //消抖
KeyState = KEY_STATE_PRESS;
break;
case KEY_STATE_PRESS:
if(KEY_NULL != KeyTemp)
{
LastKey = KeyTemp; //保存键值,以便在释放按键状态返回键值
KeyTemp |= KEY_DOWN; //按键按下
KeyState = KEY_STATE_LONG;
}
else
{
KeyState = KEY_STATE_INIT;
}
break;
case KEY_STATE_LONG:
if(KEY_NULL != KeyTemp)
{
if(++KeyTimeCount >= 100)
{
KeyTimeCount = 0;
LastKey |= KEY_LONG;
}
}
else
{
KeyState = KEY_STATE_RELEASE;
}
break;
case KEY_STATE_RELEASE:
LastKey |= KEY_UP; //按键释放
KeyTemp = LastKey;
KeyState = KEY_STATE_INIT;
break;
default:
break;
}
return KeyTemp;
}
void Key_Prosess(void)
{
u8 KeyData;
static u8 CountState = COUNT_INIT;
KeyData = GetKey();
if(KeyData == 0x81)
{
switch(CountState)
{
case COUNT_INIT: //开始计时
SecCount = 0;
TH0 = T0HIGH;
TL0 = T0LOWW;
CountStart = true;
CountState = COUNT_START;
break;
case COUNT_START: //停止计时
CountStart = false;
CountState = COUNT_STOP;
break;
case COUNT_STOP: //清零
Num_Clear();
CountState = COUNT_INIT;
break;
default:
break;
}
}
}
void Counter(void)
{
u8 i;
if(++CouNum[3] == 10) //0.1秒
{
CouNum[3] = 0;
CouNum[2] += 1;
if(CouNum[2] == 10) //1秒
{
CouNum[2] = 0;
CouNum[1] += 1;
if(CouNum[1] == 10) //10秒
{
CouNum[1] = 0;
CouNum[0] += 1;
if(CouNum[0] == 10) //100秒
CouNum[0] = 0;
}
}
}
for(i=0; i<2; i++)
{
if(CouNum != 0)
break;
}
for(; i<4; i++)
{
DisNum = CouNum;
}
}
void LCD_Display(void)
{
static u8 DisPinOn = 0x01;
DisPinOn <<= 1;
if(DisPinOn == 0x10)
{
DisPosition = 0;
DisPinOn = 0x01;
}
P0 = 0xff;
P1 = DisPinOn;
P0 = DisCode[DisNum[DisPosition++]];
if(DisPinOn == 0x04)
P0 &= 0x7f;
}
void timer0(void) interrupt 1
{
TH0 = T0HIGH;
TL0 = T0LOWW;
b10msEvent = true;
if(++SecCount == 10)
{
SecCount = 0;
b100msEvent = true;
}
}
#define T0HIGH 0xec
#define T0LOWW 0x8a
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
typedef bit BOOL;
sbit KEY_PIN = P2^0;
#define true 1
#define false 0
#define KEY_NULL 0
#define KEY_DOWN 0x80
#define KEY_LONG 0x40
#define KEY_UP 0x20
#define KEY_STATE_INIT 0
#define KEY_STATE_WOBBLE 1
#define KEY_STATE_PRESS 2
#define KEY_STATE_LONG 3
#define KEY_STATE_RELEASE 4
#define COUNT_INIT 0
#define COUNT_START 1
#define COUNT_STOP 2
code u8 DisCode[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0xff};
u8 CouNum[4] = {0x00, 0x00, 0x00, 0x00};
u8 DisNum[4] = {0x0a, 0x0a, 0x00, 0x00};
u8 DisPosition = 0;
u8 SecCount = 0;
BOOL b10msEvent = false;
BOOL b100msEvent = false;
BOOL CountStart = false;
void Key_Prosess(void);
void LCD_Display(void);
void Counter(void);
main()
{
TMOD = 0x01;
TH0 = T0HIGH;
TL0 = T0LOWW;
TR0 = 1;
ET0 = 1;
EA = 1;
P1 = 0x00;
while(1)
{
Key_Prosess(); //按键扫描
if(b10msEvent == true)
{
b10msEvent = false;
LCD_Display(); //动态显示
}
if(b100msEvent == true)
{
b100msEvent = false;
if(CountStart == true)
Counter(); //计时
}
}
}
void Num_Clear(void)
{
u8 i;
for(i=0; i<4; i++)
{
CouNum = 0;
}
DisNum[0] = DisNum[1] = 0x0a;
DisNum[2] = DisNum[3] = 0x00;
}
u8 Key_Scan(void)
{
if(KEY_PIN == false)
return (1);
return KEY_NULL;
}
u8 GetKey(void)
{
static u8 KeyState = KEY_STATE_INIT;
static u8 KeyTimeCount = 0;
static u8 LastKey = KEY_NULL; //保存按键释放时候的键值
u8 KeyTemp = KEY_NULL ;
KeyTemp = Key_Scan();
switch(KeyState)
{
case KEY_STATE_INIT: //初始状态
if(KEY_NULL != KeyTemp) //有键按下
{
KeyState = KEY_STATE_WOBBLE; //进入消抖
}
break;
case KEY_STATE_WOBBLE: //消抖
KeyState = KEY_STATE_PRESS;
break;
case KEY_STATE_PRESS:
if(KEY_NULL != KeyTemp)
{
LastKey = KeyTemp; //保存键值,以便在释放按键状态返回键值
KeyTemp |= KEY_DOWN; //按键按下
KeyState = KEY_STATE_LONG;
}
else
{
KeyState = KEY_STATE_INIT;
}
break;
case KEY_STATE_LONG:
if(KEY_NULL != KeyTemp)
{
if(++KeyTimeCount >= 100)
{
KeyTimeCount = 0;
LastKey |= KEY_LONG;
}
}
else
{
KeyState = KEY_STATE_RELEASE;
}
break;
case KEY_STATE_RELEASE:
LastKey |= KEY_UP; //按键释放
KeyTemp = LastKey;
KeyState = KEY_STATE_INIT;
break;
default:
break;
}
return KeyTemp;
}
void Key_Prosess(void)
{
u8 KeyData;
static u8 CountState = COUNT_INIT;
KeyData = GetKey();
if(KeyData == 0x81)
{
switch(CountState)
{
case COUNT_INIT: //开始计时
SecCount = 0;
TH0 = T0HIGH;
TL0 = T0LOWW;
CountStart = true;
CountState = COUNT_START;
break;
case COUNT_START: //停止计时
CountStart = false;
CountState = COUNT_STOP;
break;
case COUNT_STOP: //清零
Num_Clear();
CountState = COUNT_INIT;
break;
default:
break;
}
}
}
void Counter(void)
{
u8 i;
if(++CouNum[3] == 10) //0.1秒
{
CouNum[3] = 0;
CouNum[2] += 1;
if(CouNum[2] == 10) //1秒
{
CouNum[2] = 0;
CouNum[1] += 1;
if(CouNum[1] == 10) //10秒
{
CouNum[1] = 0;
CouNum[0] += 1;
if(CouNum[0] == 10) //100秒
CouNum[0] = 0;
}
}
}
for(i=0; i<2; i++)
{
if(CouNum != 0)
break;
}
for(; i<4; i++)
{
DisNum = CouNum;
}
}
void LCD_Display(void)
{
static u8 DisPinOn = 0x01;
DisPinOn <<= 1;
if(DisPinOn == 0x10)
{
DisPosition = 0;
DisPinOn = 0x01;
}
P0 = 0xff;
P1 = DisPinOn;
P0 = DisCode[DisNum[DisPosition++]];
if(DisPinOn == 0x04)
P0 &= 0x7f;
}
void timer0(void) interrupt 1
{
TH0 = T0HIGH;
TL0 = T0LOWW;
b10msEvent = true;
if(++SecCount == 10)
{
SecCount = 0;
b100msEvent = true;
}
}
做的真不错
谢谢!
不错 刚学会就能做这么好 不容易
3d8光立方diy套件8X8X8 PCB板散件/上位机/音乐频谱送资料包邮
1、4种超炫模式 2、超长动画 3、多种音频效果 4、支持3d8上位机 5、三个精美铜柱独立按键控制各种功能 7、搭配完美的C语言源码和强大的硬件功能,初学者接触单片机也是一次不错的选择哦8、精美PCB,军工级板材18*18cm,选择它没有错! qq:3久7八4六8九一