秒表(定时器扫描按键、数码管)——学习记录

delay函数检测按键状态,按下按键不松开函数会卡在那(delay函数里利用了死循环),只有等按键松开的时候才会继续执行。由于按键抖动的时间大概在20ms,所以在20ms一定能检测到按键的变化,那么我们就可以设置定时器在每20ms检测一次按键状态。
按照江科的代码实验出来会有很大误差,应该是新版晶振频率和旧版不同导致的,我在他代码的基础上改了定时器的初值,但是仍有误差(快了),可能还需要改一下最开始keil选择的单片机型号吧,不过之前做实验只能选c52,就习惯性选了,这里没改。

代码部分
main.c

#include <REGX52.H>
#include "timer.h"
#include "key.h"
#include "smgshow.h"
#include "I2C.h"
#include "AT24C02.h"
#include "delay.h"
unsigned char keynum;
unsigned char min,sec,ms;
unsigned char runflag;//开始标志
void main()
{
	Timer0_Init();//初始化定时器

	while(1)
	{
		keynum=keynumber();
		if(keynum==1)//按键1控制开始
		{
			runflag=!runflag;
		}
		if(keynum==2)//按键2实现清0
		{
			min=0;
			sec=0;
			ms=0;
		}
		if(keynum==3)//按键3实现数据存储
		{
			AT24C02_writebyte(0,min);
			delay(5);
			AT24C02_writebyte(1,sec);
			delay(5);
			AT24C02_writebyte(2,ms);
			delay(5);
		}
		if(keynum==4)//按键4实现数据读出
		{
			min=AT24C02_readbyte(0);
			sec=AT24C02_readbyte(1);
			ms=AT24C02_readbyte(2);
		}
		  smg_setbuf(1,min/10);//显示数据
			smg_setbuf(2,min%10);
			smg_setbuf(3,11);
			smg_setbuf(4,sec/10);
			smg_setbuf(5,sec%10);
			smg_setbuf(6,11);
			smg_setbuf(7,ms/10);
			smg_setbuf(8,ms%10);
	}
	
}

void sec_loop()
{
	if(runflag)
	{
		ms++;
	  if(ms>=100)//ms100进1位
	{
	  ms=0;
		sec++;
		if(sec>=60)
		{
			sec=0;
			min++;
			if(min>=60)
			{
				min=0;
			}
		}
	}
	}

}
void timer0_routine() interrupt 1
{
	static unsigned int T0count1,count2,count3;
	TL0=0x80;
	TH0=0xF0;
	T0count1++;
	if(T0count1>=20)//每隔20ms检测一次
	{
		T0count1=0;
		key_loop();//每20ms跳转一次获取按键状态
	}
	count2++;
	if(count2>=2)
	{
		count2=0;
		smgshow_loop();
	}
	count3++;
	if(count3>=10)//每隔10ms调用一次
	{
		sec_loop();
		count3=0;
	}
}

key.c

#include <REGF51RC.H>
#include "delay.h"

unsigned char key;//全局变量

unsigned char keynumber()//专门负责返回键(key)值的函数
{
	unsigned char temp;
	temp=key; 
	key=0;//每返回一次键值将key清零
	return temp;
}

unsigned char key_getstate()//获取按键状态
{
	unsigned char keynum=0;
	if(P3_1==0){keynum=1;}
	if(P3_0==0){keynum=2;}
	if(P3_2==0){keynum=3;}
	if(P3_3==0){keynum=4;}
	return keynum;
}

void key_loop()
{
	static unsigned char nowstate,laststate;
	laststate=nowstate;//更新按键状态
	nowstate=key_getstate();//获取按键值
	if(laststate==1&& nowstate==0)//获取按键状态之后赋值给key
	{
		key=1;
	}
	if(laststate==2&& nowstate==0) 
	{
		key=2;
	}
	if(laststate==3&& nowstate==0) 
	{
		key=3;
	}
	if(laststate==4&& nowstate==0) 
	{
		key=4;
	}
}

smgshow.c

#include <REGX52.H>
#include "delay.h"

unsigned char smg_buf[9]={0,10,10,10,10,10,10,10,10};//显示缓存区
char num[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};

void smg_setbuf(unsigned char location,number)//设置数码管显示的数字
{
	smg_buf[location]=number;
}
void smgshow(unsigned char location,number)
{
	P0=0x00;
	switch(location)
	{
		case 1:P2_4=1   ;P2_3=1   ;P2_2=1   ;break;
		case 2:P2_4=1   ;P2_3=1   ;P2_2=0   ;break;
		case 3:P2_4=1   ;P2_3=0   ;P2_2=1   ;break;
		case 4:P2_4=1   ;P2_3=0   ;P2_2=0   ;break;
		case 5:P2_4=0   ;P2_3=1   ;P2_2=1   ;break;
		case 6:P2_4=0   ;P2_3=1   ;P2_2=0   ;break;
		case 7:P2_4=0   ;P2_3=0   ;P2_2=1   ;break;
		case 8:P2_4=0   ;P2_3=0   ;P2_2=0   ;break;
	}
	P0=num[number];
}
void smgshow_loop()
{
	static unsigned char i=1;
	smgshow(i,smg_buf[i]);
	i++;
	if(i>8){i=1;}
}
#include #include //自定义头文件,KeyScan用于键盘扫描。 #include //用于I2C总线。 #define uchar unsigned char #define uint unsigned int unsigned char b[7]={50,58,9,1,15,6,15},clock1[2]={0,12},clock2[2]={0,0}; //b[7]的元素含义:秒,分,时,星期,日,月,年 clock的元素含义:分,时。 code unsigned char tab[11]={0x03,0xf3,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0x00}; //数码管0-9的编码列表,0x00点亮全部管。 code unsigned char tab2[2]={0x6d,0xff}; //ox6d: 表示三横图标。 unsigned char time[7]; //时间设置函数的参数。 void delay(unsigned int cnt); //延时函数声明。 unsigned char getkey1(); //unsigned char getkey2(); //读键盘函数声明。 void tim(void); //中断函数声明。 void KeyDelay(unsigned int KeyJsTime); //键盘扫描延时函数声明。 void display(uchar bit7,uchar bit6,uchar bit5,uchar bit4, uchar bit3,uchar bit2,uchar bit1,uchar bit0); //数码管显示函数。 void ReadTime(unsigned char TIME[7]); //DS1307读时间函数。 void SetTime(unsigned char TIME[7]); //设置时间初值函数。 void SwitchRTC(unsigned char SWITCH); //时间修改的使能函数:SWITCH=1禁止修改时间,SWITCH=0可以修改时间。 uchar dis_num_h; //显示记录数据的组别十位 uchar dis_num_l; //显示记录数据的组别个位 uchar dis_min_h; //要显示的分的十位; uchar dis_min_l; //要显示的分的个位; uchar dis_sec_h; //要显示的秒的十位; uchar dis_sec_l; //要显示的秒的个位; uchar dis_dsec; //要显示的0.1秒; uchar min=0x00; //防止意外,赋初值。分······· uchar sec=0x00; //秒 uchar dsec=0x00; //十分之一秒。 uchar en,i,clocken1=0,clocken2=0; main() { unsigned char quzhi,xianshi,symbol=0x55,set,set1=0x55,ce; unsigned char c[30]; unsigned char num,num1,j,k,stop=1; //注意stop必须先赋初值1。 PLLCON&=0xf8; //设置频率为12.582912MHz TMOD=0x01; //选定定时器及其工作模式。 TH0=0xce; //赋初值:52736,则总共的计数次数为:65536-52736=12800,即12.8毫秒。 TL0=0x00; IE=0x82; //中断控制,10000010,EA=1开总中断,ET0=1:定时器0中断允许。0x82:1000 0010 I2CCON=0xE8; //开启I2C总线通信,具体含义参阅ADuC848英文版说明资料。 SetTime(b); //设定初始时间。 SwitchRTC(1); //调用时间保持函数。 while(1) //设置死循环,实现循环执行。 { ReadTime(time); //调用读时间函数,读取当前的时间,并保留在time数组中。 if(((clock1[0]==time[1])&&(clock1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值