观焦点:Protues教程第三讲:“挑战10秒”网红按钮的实现
Protues教程第三讲:“挑战10秒”网红按钮的实现
2023-06-14 11:02:04
(相关资料图)
视频教程:https://www.bilibili.com/video/BV1m741187ga
什么是网红按钮?
51内核单片机定时器的计算(以12M晶振为例); 晶振 12MHz =12000000Hz 时钟周期 1/12000000 s(晶振的倒数) 机器周期是12/12000000 s (标准框架下51单片机一个机器是12个时钟周期) 因为定时器是每个机器周期加1所以定时时间为 N*时钟机器周期时间=所定时间T 所以要经过的机器周期是N*(12/12000000 s)=T N=t/(12/12000000 ) 初值等于 65536-N(因为16位的定时器65535再加1才溢出) 例如: 需要定时1ms=0.001s =时间(s)*(晶振/12) 0.001/(12/12000000 )=0.001*(12000000 /12)=0.001*1000000=1000 初值y=65536-1000=64536=0XFC18
#include "reg52.h"#define uchar unsigned char#define uint unsigned int#define SEG P0sbit SCAN1 = P2^0;sbit SCAN2 = P2^1;sbit SCAN3 = P2^2;sbit SCAN4 = P2^3;sbit key = P3^0;sbit Bepp = P3^7;uchar number;uchar flag;uchar distab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};int display=0;//数码管刷新int The_Time;//显示时间 单位10msint dis_value;//计数频率 10msvoid delay(uint j) //1ms{ uchar i=250; for(;j>0;j--){ while(--i); i=249; while(--i); i=250; }}void Key_Scan(){if(key==0){delay(20);if(key==0){while(key==0);if(flag==0){flag=1;The_Time = 0;}else{flag=0;}}}}void Key_INT( ) interrupt 0 // 这里0对应下表{Bepp = ~ Bepp; if(flag==0){flag=1;The_Time = 0;}else{flag=0;}}void main(){TMOD |= 0x01; //开启定时器0 0x10时使用定时器1 0x11时启动两个TH0=0XFC;//给定时器赋初值,定时1ms TH1TL0=0X18; //TL1EA=1; //总中断打开ET0=1; //定时器0中断打开 ET1TR0=1; //定时器0开关打开 TR1EX0=1; //外部中断0开() EX1 为外部中断1 (P3^3)IT0=1; //低电平触发 IT1为中断1while(1){}}void Timer0() interrupt 1 //参考上图 定时器1时为3{static uint i; //定义一个自变形变量TH0=0XFC;//给定时器赋初值,定时1ms TH1TL0=0X18; //TL1i++;dis_value++;if(i==2){i=0;SEG = 0XFF;switch(number){ case 0:SCAN4=1;SCAN3=0;SCAN2=0;SCAN1=0;SEG=distab[The_Time%10];break; case 1:SCAN4=0;SCAN3=1;SCAN2=0;SCAN1=0;;SEG=distab[The_Time/10%10];break; case 2:SCAN4=0;SCAN3=0;SCAN2=1;SCAN1=0;SEG=distab[The_Time/100%10]& 0x7f;break; case 3:SCAN4=0;SCAN3=0;SCAN2=0;SCAN1=1;;SEG=distab[The_Time/1000];break; default:break; }number++; if(number>3)number=0;}if(dis_value==10){dis_value = 0;if(The_Time<9999 && flag == 1){The_Time++;}}}
审核编辑:汤梓红
标签: