基于51单片机的秒表程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

{ if (StopwatchRefresh) { StopwatchRefresh=0; StopwatchDisplay(); } KeyDriver();
} } void ConfigTimer0(unsigned int ms) {
unsigned long tmp;
tmp=11059200/12; tmp=(tmp*ms)/1000; tmp=65536-tmp; tmp=tmp+18; T0RH=(unsigned char)(tmp>>8); T0RL=(unsigned char)tmp; TMOD&=0XF0; TMOD|=0X01; TH0=T0RH; TL0=T0RL; ET0=1; TR0=1; } void StopwatchDisplay() { signed char i; unsigned char buf[4];
TH0=T0RH; TL0=T0RL; LedScan(); KeyScan(); tmr10ms++; if(tmr10ms>=5) {
tmr10ms=0; StopwatchCount(); } }
StopwatchRunning=0; else
StopwatchRunning=1; } //秒表复位函数 void StopwatchReset() {
StopwatchRunning=0; DecimalPart=0; IntegerPart=0; StopwatchRefresh=1; } //检测按键是否有动作,主函数中调用此函数 void KeyDriver() { unsigned char i; static unsigned char backup [4]= {1,1,1,1};
基于 51 单片机的秒表程序
#include<reg52.h>
sbit ADDR3=P1^3; sbit ENLED=P1^4; sbit KEY1 = P2^4; sbit KEY2 = P2^5; sbit KEY3 = P2^6; sbit KEY4 = P2^7;
//单片机 I/O 口
unsigned char code LedChar[] = { //数码管显示字符转换表 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
for(i=0; i<4; i++) { if(backup[i] != KeySta[i]) { if(backup[i] != 0) { if(i==1) StopwatchReset(); else if(i==2) StopwatchAction(); } backup[i] = KeySta[i]; } }
if(buf[i]==0) LedBuff[i+2]=0XFF;
else break;
//高位消零
} for(;i>=0;i--) {
LedBuff[i+2]=LedChar[buf[i]]; } LedBuff[2]&=0X7F;//第三个数码管显示小数点 } //秒表开始函数 void StopwatchAction() { if(StopwatchRunning)
void ConfigTimer0(unsigned int ms); void StopwatchDisplay(); void KeyDriver();
void main()
{
EA = 1;
//使能总中断
ENLED=0;
ADDR3=1;
P2=0XFE;
ConfigTimer0(2);
while (1)
}; unsigned char LedBuff[6] = { //数码管显示缓冲区
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; unsigned char KeySta[4] = {
1,1,1,1};
bit StopwatchRefresh=1; bit StopwatchRunning=0; unsigned char DecimalPart=0; unsigned int IntegerPart=0; unsigned char T0RH=0; unsigned char T0RL=0;
}
//按键扫描函数,需在定时中断中调用,推荐调用间隔 1ms void KeyScan() {
unsigned char i; static unsigned char keybuf[4] = {0xFF, 0xFF, 0xFF, 0xFF};
//将一行的 4 个按键值移入缓冲区 keybuf[0] = (keybuf[0] << 1) | KEY1; keybuf[1] = (keybuf[1] << 1) | KEY2; keybuf[2] = (keybuf[2] << 1) | KEY3; keybuf[3] = (keybuf[3] << 1) | KEY4; //消抖后更新按键状态 for (i=0; i<4; i++) //每行 4 个ห้องสมุดไป่ตู้键,所以循环 4 次 {
if(DecimalPart>=10) {
DecimalPart=0; IntegerPart++; if(IntegerPart>=10000) {
IntegerPart=0; } } StopwatchRefresh=1; } } /* T0 中断服务函数,用于数码管显示扫描与按键扫描 */ void InterruptTimer0() interrupt 1 { static unsigned char tmr10ms=0;
if (keybuf[i]== 0x00) { //连续 4 次扫描值为 0,即 4*4ms 内都是按下状态时,可认为按键已稳定的按 下
KeySta[i] = 0; } else if (keybuf[i]==0xFF) { //连续 4 次扫描值为 1,即 4*4ms 内都是弹起状态时,可认为按键已稳定的弹 起
LedBuff[0]=LedChar[DecimalPart%10]; LedBuff[1]=LedChar[(DecimalPart/10)%10];
buf[0]=IntegerPart%10; buf[1]=(IntegerPart/10)%10; buf[2]=(IntegerPart/100)%10; buf[3]=(IntegerPart/1000)%10; for(i=3;i>=1;i--) {
KeySta[i] = 1; } } } //数码管显示函数,中断函数中调用 void LedScan() { static unsigned char i=0; P0=0XFF; P1=(P1&0XF8)|i; P0=LedBuff[i]; if(i<5) i++; else i=0; } //秒表计数加函数 void StopwatchCount() { if(StopwatchRunning) { DecimalPart++;
相关文档
最新文档