視頻教程:https://www.bilibili.com/video/BV1m741187ga
什么是網(wǎng)紅按鈕?
51內(nèi)核單片機(jī)定時(shí)器的計(jì)算(以12M晶振為例); 晶振 12MHz =12000000Hz 時(shí)鐘周期 1/12000000 s(晶振的倒數(shù)) 機(jī)器周期是12/12000000 s (標(biāo)準(zhǔn)框架下51單片機(jī)一個(gè)機(jī)器是12個(gè)時(shí)鐘周期) 因?yàn)槎〞r(shí)器是每個(gè)機(jī)器周期加1所以定時(shí)時(shí)間為 N*時(shí)鐘機(jī)器周期時(shí)間=所定時(shí)間T 所以要經(jīng)過的機(jī)器周期是N*(12/12000000 s)=T N=t/(12/12000000 ) 初值等于 65536-N(因?yàn)?6位的定時(shí)器65535再加1才溢出) 例如: 需要定時(shí)1ms=0.001s =時(shí)間(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 P0
sbit 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;//數(shù)碼管刷新
int The_Time;//顯示時(shí)間 單位10ms
int dis_value;//計(jì)數(shù)頻率 10ms
void 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對(duì)應(yīng)下表
{
Bepp = ~ Bepp;
if(flag==0)
{
flag=1;
The_Time = 0;
}else
{
flag=0;
}
}
void main()
{
TMOD |= 0x01; //開啟定時(shí)器0 0x10時(shí)使用定時(shí)器1 0x11時(shí)啟動(dòng)兩個(gè)
TH0=0XFC; //給定時(shí)器賦初值,定時(shí)1ms TH1
TL0=0X18; //TL1
EA=1; //總中斷打開
ET0=1; //定時(shí)器0中斷打開 ET1
TR0=1; //定時(shí)器0開關(guān)打開 TR1
EX0=1; //外部中斷0開() EX1 為外部中斷1 (P3^3)
IT0=1; //低電平觸發(fā) IT1為中斷1
while(1)
{
}
}
void Timer0() interrupt 1 //參考上圖 定時(shí)器1時(shí)為3
{
static uint i; //定義一個(gè)自變形變量
TH0=0XFC; //給定時(shí)器賦初值,定時(shí)1ms TH1
TL0=0X18; //TL1
i++;
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_Time9999 && flag == 1)
{
The_Time++;
}
}
}
審核編輯:湯梓紅
-
單片機(jī)
+關(guān)注
關(guān)注
6035文章
44554瀏覽量
634614 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3246瀏覽量
114715 -
按鈕
+關(guān)注
關(guān)注
0文章
134瀏覽量
12768 -
Protues
+關(guān)注
關(guān)注
41文章
183瀏覽量
70924
發(fā)布評(píng)論請先 登錄
相關(guān)推薦
評(píng)論