Ⅰ 求單片機數碼管倒計時程序
#include<reg52.h>
#include <intrins.h> //內部包含延時函數 _nop_();
#define uchar unsigned char
#define uint unsigned int sbit d1=P2^0; sbit key1=P0^4;
sbit SDATA_595=P0^0; //串列數據輸入 ----接板卡上的SPI 數據信號輸入端
sbit SCLK_595=P0^1; //移位時鍾脈沖(輸入口) ---接板卡上的SPO----串列時鍾線----SHcp移位時鍾信號輸入端
sbit RCK_595=P0^2; //輸出鎖存器控制脈沖 ----接板卡上SPK STcp鎖存信號輸入端
uchar code an[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar num=60;
void delay(uint z);
void aa595_in(uchar Data) {
uchar i;
for(i=0;i<8;i++)
{SCLK_595=0; Data<<=1; SDATA_595=CY;
SCLK_595=1; SCLK_595=0;
}
}
void a595_in(uchar Data) {
uchar i;
for(i=0;i<8;i++) {
SCLK_595=0; Data<<=1; SDATA_595=CY;
SCLK_595=1; SCLK_595=0;
}
}
void aa595_out(void) {
RCK_595=0;
_nop_(); _nop_();
RCK_595=1;
_nop_(); _nop_();
RCK_595=0;
}
void main() {
uchar ge,shi;
while(1) {
if(key1==0) {
delay(10);
if(key1==0)
{d1=0; num--;
if(num==0)
num=60;}
while(!key1);//鬆手檢測
delay(10);//延時10ms
while(!key1);//再鬆手檢測
}
else d1=1;
// aa595_in(an[num]);
// aa595_in(wei[3]);
shi=num/10;
ge=num%10;
a595_in(an[shi]);
aa595_in(wei[3]);
aa595_out();
delay(10);
aa595_in(an[ge]);
aa595_in(wei[2]);
aa595_out();
delay(10);}
}
void delay(uint z) {
uint x,y;
for(x=100;x>0;x--) for(y=z;y>0;y--);
}//延時子程序,延時Zms
Ⅱ 如何用單片機做一個簡單的倒計時器
我已經完成了,下面是電路圖和實際效果,你也試試,程序已經調試完成了!!!是使用秒錶程序改編完成的,喜歡的話,點一個贊吧!希望能幫到你!!!
#include "reg52.h" //此文件中定義了單片機的一些特殊功能寄存器
typedef unsigned int u16; //對數據類型進行聲明定義
typedef unsigned char u8;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 code smgan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f};//顯示0~F的值
u8 ssec=100,sec=60,min=04;
u8 DisplayData[8];
sbit beep=P1^5;
unsigned long counter=0;
unsigned int c=0;
sbit p15=P1^5;
bit p;
/*******************************************************************************
* 函 數 名 : delay
* 函數功能 : 延時函數,i=1時,大約延時10us
*******************************************************************************/
void delay(u16 i)
{
while(i--);
}
/*******************************************************************************
* 函 數 名 : Timer0Init
* 函數功能 : 定時器0初始化
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void Timer0Init()
{
TMOD=0X11;//選擇為定時器0模式,工作方式1,僅用TR0打開啟動。
TH0=0Xd8; //給定時器賦初值,定時10ms
TL0=0Xf0;
ET0=1;//打開定時器0中斷允許
EA=1;//打開總中斷
TR0=1;//打開定時器 -->啟動定時器中斷!
ET1=1;
TR1=1;
TH1=(65536-250)/256; //FC
TL1=(65536-250)%256;
}
/*******************************************************************************
* 函 數 名 : DigDisplay
* 函數功能 : 數碼管動態掃描函數,循環掃描8個數碼管顯示
*******************************************************************************/
void DigDisplay()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i) //位選,選擇點亮的數碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//顯示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//顯示第7位
}
P0=DisplayData[i];//發送段碼
delay(100); //間隔一段時間掃描
P0=0x00;//消隱-->撤銷選中;
}
}
void datapros()
{
DisplayData[0]=smgan[ssec%10];
DisplayData[1]=smgan[ssec/10];
DisplayData[2]=0x40;
DisplayData[3]=smgan[sec%10];
DisplayData[4]=smgan[sec/10];
DisplayData[5]=0x40;
DisplayData[6]=smgan[min%10];
DisplayData[7]=smgan[min/10];
}
/*******************************************************************************
* 函 數 名 : main
* 函數功能 : 主函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void main()
{
Timer0Init(); //定時器0初始化
while(1)
{
datapros();
DigDisplay();
}
}
/*******************************************************************************
* 函 數 名 : void Timer0() interrupt 1
* 函數功能 : 定時器0中斷函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void Timer0() interrupt 1
{
TH0=0Xd8; //給定時器賦初值,定時10ms
TL0=0Xf0;
ssec--;
if(ssec==00) //1s
{
ssec=100;
sec--;
if(sec==00)
{
sec=60;
min--;
if(min==-1)
{
P1=1;
ssec=0;sec=0;min=0;
TR0=0;
}
}
}
}
void Timer1() interrupt 3
{
TH1=(65536-250)/256; //FC
TL1=(65536-250)%256;
if(p!=1) p15=~p15;
if(p==1) p15=0;
c++;
if(c==250)
{
//c=0;
p=~p;
}
if(c>=250&&1695);
if(c==1696) c=0;
}
Ⅲ 單片機c語言編計時器
#include<AT89X51.H>
unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00};
unsignedcharsecond;
unsignedchartcount;
voidmain(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
tcount=0;
second=0;
P0=dispcode[second/10];
P2=dispcode[second%10];
while(1)
{
if(TF0==1)
{
tcount--;
if(tcount==0)
{
tcount=15;
second--;
if(second==0)
{
second=15;
}
P0=dispcode[second/10];
P2=dispcode[second%10];
}
TF0=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
}
}
Ⅳ 單片機倒計時工作原理
單片機倒計時工作原理。在程序中設置一個時間然後不斷的減少時間,時間減少為0時就是倒計時時間到了。
Ⅳ 單片機倒計時器
這個設計有一點實用價值,批准!
Ⅵ 單片機控制倒計時定時器
哥們
我做的定時器 的程序
這個是正的 你該一下吧
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code []={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uchar code da[]={0x40,0x79,0x24,0x30,0x19,0x12,0x2,0x78,0x0,0x10};
display(uchar,uchar,uchar,uchar);
uchar x=0;
uchar n=0;
uchar flag=0;
uchar second=0,minute=0;
sbit K1=P3^0;
sbit K2=P3^1;
uchar temp=0;
delay(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=25;b>0;b--)
;
}
display(uchar m1,uchar m,uchar s1,uchar s)
{
P0=[m1];
P2=0xfe;
delay(10);
P0=da[m];
P2=0xfd;
delay(10);
P0=[s1];
P2=0xfb;
delay(10);
P0=[s];
P2=0xf7;
delay(10);
}
void main()
{
EA=1;
ET1=1;
TMOD=0x10;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
while(1)
{
P3=0XFF;
if(K1==0)
delay(10);
if(K1==0)
TR1=1;
if(K2==0)
delay(10);
if(K2==0)
TR1=0;
if(flag==20)
{
flag=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
}
}
}
display(minute/10,minute%10,second/10,second%10);
}
}
void tim1() interrupt 3
{
TR1=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR1=1;
flag++;
}
Ⅶ 如何用單片機做一個簡單的倒計時器
51單片機實現數碼管99秒倒計時,其實很簡單,就是使用定時器中斷來實現。目的就是學習怎樣用單片機實現倒計時,從而實現一些延時控制類的東西,99秒只是一個例子,你完全可以做出任意倒計時如10秒倒計時程序。定時器定時時間計算公式:初值X=M(最大計時)-計數值。
初值,換算成十六進制,高位給TH0,低位給TL0,如果用定時器0的話。
M(最大計時)如果是16位的,就是2的16次方,最大定時,65535 微秒,實現1秒定時,可以通過定時10毫秒,然後100次改變一次秒值即可。10*100毫秒=1S
計數值:你要定時多長時間,如果定時1毫秒,就是1000微秒,(單位為微秒),如果定時10毫秒,就是10000(微秒),當然,最大定時被定時器本身位數限制了,最大2的16次方(16位定時計數器),只能定時65.535毫秒。定時1S當然不可能1S定時器中斷。
下面為實現99秒倒計時C語言源程序
/*了解定時器,這樣的話,就可以做一些基本的實驗了,如定時炸彈~~,10秒後打開關閉繼電器*/
/*數碼管,12M晶振*/
#include <reg52.h>
#define uchar unsigned char
sbit p11=P1^1; //連的是繼電器。。
code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar shiwei;
uchar gewei;
void delay(unsigned int cnt)
{
while(--cnt);
}
void main()
{
TMOD|=0x01; /*定時器0 16位定時器 X=65535-10000(10毫秒)=55535=D8F0(十六進制)定時10ms
*/
TH0=0xd8;
TL0=0xf0;
IE=0x82; //這里是中斷優先順序控制EA=1(開總中斷),ET0=1(定時器0允許中斷),這里用定時器0來定時
TR0=1; //開定時器0
while(1)
{
P0=shiwei; //99的十位
P2=0; //99的個位,
delay(300); //動態掃描數碼管延時
P0=gewei;
P2=1;
delay(300);
}
}
void tim(void) interrupt 1 using 1 //定時器0中斷
{
static uchar second=99,count; //99隻是一個數,可以任意改,因為這里只學習怎樣實現倒計時
TH0=0xd8; //定時10毫秒
TL0=0xf0;
count++;
if(count==100) //10毫秒定時,10*100=1000(毫秒)=1秒
{
count=0;
second--;
if(second==0)
{
p11=0; //這里讓繼電器動作,當然動作之後,要復位才能等下次倒定時再動作。
second=99; //回到99再循環來,當然,可以做其他的控制,
}
shiwei=tab[second/10]; //數碼管10位
gewei=tab[second%10]; //數碼管個位
}
Ⅷ 用單片機製作倒計時秒錶的發現
顯示子程序中,有些語句邏輯不順暢,改了一些,如下:
;顯示倒計時器:
;入口:30H單元BCD碼十位數
; 31H單元BCD碼個位數
DIS:
MOV DPTR,#TAB
MOV A, 30H
MOVC A, @A+DPTR
CLR P3.0
CLR P3.1
MOV P1, A ;顯示十位
SETB P3.0
ACALL DELAY10
MOV A, 31H
MOVC A, @A+DPTR
CLR P3.0
MOV P1, A ;顯示個位
SETB P3.1
ACALL DELAY10
CLR P3.0
CLR P3.1
RET
Ⅸ 單片機的倒計時程序
//工程名稱:99秒倒計時//功能描述:程序開始顯示99秒等待開始倒計時,當再次按下開關後計時//停止,當再按下開關後計時//復位實際上是單片機復位,重新開始!//通過本程序新的認識: 1.BCD數碼管的使用 2.十進制到BCD碼轉化 //3.C51的等待按鍵作用//2. temp=99;i=temp/10;j=temp%10;i=(i<<4);dis=i+j;P0=dis;//3.while(sw==1){;}// 判按下,下則出 while(sw==0){;}//判松開// ,開則出#includesbit sw=P3^5; //定義開關main(){ unsigned char temp,dis;unsigned int i,j,k,t;// P0=0x88; //初試時檢測數碼管是否正常// for(k=0;k<40000;k++); temp=99;i=temp/10;j=temp%10;i=(i<<4);dis=i+j;P0=dis;//啟動是顯示99while(sw==1){;}// 判按下,下則出while(sw==0){;}//判松開 ,開則出 第一次按開關for(t=0;t<5;t++)//循環來減{for(k=0;k<30000;k++);temp--;i=temp/10;j=temp%10;i=(i<<4);dis=i+j;P0=dis;for(k=0;k<30000;k++);if(sw==0) break;// while(sw==1){;}// while(sw==1){;}// break;}while(sw==1){;}// 判按下,下則出while(sw==0){;}//判松開 ,開則出 第一次按開關while(sw==1){;}// 判按下,下則出while(sw==0){;}//判松開 ,開則出 第一次按開關
Ⅹ 單片機倒計時的編程
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP T0ISR
ORG 0030H
MAIN:
MOV TMOD,#01H
MOV TH0,#HIGH(65536-10000)
MOV TL0,#LOW(65536-10000)
SETB TR0
SETB ET0
SETB EA
MOV R0,#99
MOV R1,#0
LOOP:
JB P1.0,LOOP1
JNB P1.0,$
INC R0
CJNE R0,#100,LOOP
MOV R0,#99
SJMP LOOP
LOOP1:
JB P1.1,LOOP
JNB P1.1,$
CJNE R0,#0,LOOP11
SJMP LOOP
LOOP11:
DEC R0
SJMP LOOP
T0ISR:
CLR TR0
MOV TH0,#HIGH(65536-10000)
MOV TL0,#LOW(65536-10000)
SETB TR0
INC R1
CJNE R1,#100,T0E
DEC R0
MOV R1,#0
DEC R0
CJNE R0,#0,T00
CLR P3.0
SJMP T0E
T00:
SETB P3.0
MOV A,R0
MOV B,#10
DIV AB
MOV DPTR,#TABLE
JB 00H,T001
MOV P2,#0FEH
MOVC A,@A+DPTR
MOV P0,A
SJMP T0E
T001:
MOV P2,#0FDH
MOV A,B
MOVC A,@A+DPTR
MOV P0,A
T0E:
RETI
;---------------------------------------
TABLE: ; 共陰極數碼管顯示代碼表
DB 3FH,06H,5BH,4FH,66H ;01234
DB 6DH,7DH,07H,7FH,6fh ;56789
END