新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)的數(shù)字時(shí)鐘程序

單片機(jī)的數(shù)字時(shí)鐘程序

作者: 時(shí)間:2016-11-18 來(lái)源:網(wǎng)絡(luò) 收藏
#include

#include

本文引用地址:http://m.butianyuan.cn/article/201611/315948.htm

#define unit unsigned int

#define uchar unsigned char

/*定義字型字位口*/

#define DIGPORT XBYTE [0x8000]

#define WORDPORT XBYTE [0x4000]

/*定義P1口各管腳*/

sbit K0=P1^0;

sbit K1=P1^1;

sbit K2=P1^2;

sbit BEEP=P1^3;

sbit L0=P1^4;

sbit L1=P1^5;

sbit L2=P1^6;

sbit L3=P1^7;

uchar data BUFFER[2]={0,0}; /*顯示緩沖區(qū)(依次為低位,高位)*/

uchar data CLOCK[4]={0,0,0,0}; /*存放時(shí)鐘時(shí)間(依次為百分秒,秒,分和時(shí)位)*/

uchar data SECOND[2]={0,0}; /*存放秒表時(shí)間(依次為百分秒,秒)*/

uchar data REVERSE[4]={0,0,0,0}; /*存放倒計(jì)時(shí)時(shí)間(依次為百分秒,秒,分和時(shí)位)*/

/*定義運(yùn)行狀態(tài)*/

uchar data STATE=0;

/*STATE=0,秒表*/

/*STATE=1,倒計(jì)時(shí)分調(diào)整*/

/*STATE=2,倒計(jì)時(shí)小時(shí)調(diào)整*/

/*STATE=3,倒計(jì)時(shí)運(yùn)行*/

/*STATE=4 or 7,時(shí)鐘運(yùn)行*/

/*STATE=5,時(shí)鐘分調(diào)整*/

/*STATE=6,時(shí)鐘時(shí)調(diào)整*/

bit bdata SND=0; /*秒表START*/

bit bdata RST=0; /*秒表RESET*/

bit bdata STP=0; /*倒計(jì)時(shí)STOP*/

/*數(shù)碼管顯示編碼"0"-"9","A","-"*/

uchar code TABLE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x40};

void display(void)

{

uchar i; /*顯示緩沖區(qū)首址*/

uchar delay; /*顯示延時(shí)*/

uchar disp; /*顯示內(nèi)容*/

uchar digit; /*定義數(shù)碼管顯示位*/

digit=0x08;

for (i=0;i<2;i++) /*秒表,顯示百分秒和秒*/

{

if (BUFFER[i]>100) disp=BUFFER[i]%10+10;

else disp=BUFFER[i]%10;

DIGPORT=digit;

WORDPORT=TABLE[disp];

for (delay=0;delay<=200;delay++);

digit=digit>>1;

disp=BUFFER[i]/10;

DIGPORT=digit;

WORDPORT=TABLE[disp];

for (delay=0;delay<=200;delay++);

digit=digit>>1;

}

}

void timer0(void) interrupt 1 using 1

{

/*重置定時(shí)初值*/

TH0=-5000/256;

TL0=-5000%256;

/*時(shí)鐘處理*/

CLOCK[0]=CLOCK[0]+1;

if ((CLOCK[0]==50) && (STATE>=4))

{

L0=!L0;

}

if (CLOCK[0]==100)

{

if (STATE>=4) L0=!L0;

CLOCK[0]=0;

CLOCK[1]=CLOCK[1]+1;

if (CLOCK[1]==60)

{

CLOCK[1]=0;

CLOCK[2]=CLOCK[2]+1;

if (CLOCK[2]==60)

{

CLOCK[2]=0;

CLOCK[3]=CLOCK[3]+1;

if (CLOCK[3]==24)

{

CLOCK[3]=0;

}

}

}

}

}

void timer2(void) interrupt 5 using 3

{

TF2=0;

TH2=-5000/256;

TL2=-5000%256;

/*秒表處理*/

if (STATE==0)

{

SECOND[0]=SECOND[0]+1;

if (SECOND[0]==50)

{

L1=!L1;

}

if (SECOND[0]==100)

{

L1=!L1;

SECOND[0]=0;

SECOND[1]=SECOND[1]+1;

if (SECOND[1]==100)

{

SECOND[1]=0;

}

}

}

/*倒計(jì)時(shí)處理*/

if (STATE==3)

{

REVERSE[0]=REVERSE[0]+1;

if (REVERSE[0]==50)

{

L2=!L2;

}

if (REVERSE[0]==100)

{

L2=!L2;

REVERSE[0]=0;

REVERSE[1]=REVERSE[1]+1;

if (REVERSE[1]==60)

{

REVERSE[1]=00;

REVERSE[2]=REVERSE[2]-1;

if ((REVERSE[2]==0) && (REVERSE[3]==0))

{

TR2=0;

STP=1;

BEEP=0;

}

else if (REVERSE[2]==0xff)

{

REVERSE[2]=59;

REVERSE[3]=REVERSE[3]-1;

}

}

}

}

}

void intsvr1(void) interrupt 2 using 2

{

if (STATE==5)

{

CLOCK[2]=CLOCK[2]+1;

if (CLOCK[2]==60) CLOCK[2]=0;

}

if (STATE==6)

{

CLOCK[3]=CLOCK[3]+1;

if (CLOCK[3]==24) CLOCK[3]=0;

}

}

void timer1(void) interrupt 3 using 3

{

if (STATE==0)

{

if (RST)

{

SECOND[0]=0;

SECOND[1]=0;

L1=1;

RST=0;

}

else

{

if (SND) TR2=1;

else

{

L1=1;

TR2=0;

RST=1;

}

SND=!SND;

}

}

else if (STATE==1)

{

REVERSE[2]=REVERSE[2]+1;

if (REVERSE[2]==60) REVERSE[2]=0;

}

else if (STATE==2)

{

REVERSE[3]=REVERSE[3]+1;

if (REVERSE[3]==24) REVERSE[3]=0;

}

else

{

if (STP)

{

BEEP=1;

L2=1;

STP=0;

}

else TR2=0;

}

}

void main(void)

{

uchar stabak; /*上一次程序運(yùn)行的狀態(tài)*/

P1=0x0f;

EA=1; IT1=1; ET0=1;

TMOD=0x61; /*T1方式2計(jì)數(shù),T0方式1計(jì)時(shí)*/

TH0=-5000/256; TL0=-5000%256;

TH1=0xff; TL1=0xff;

T2CON=0; TH2=-5000/256; TL2=-5000%256;

K0=1; K1=1; K2=1;

STATE=P1&0x07;

stabak=STATE;

for (;;)

{

switch (STATE)

{

case 0: /*STATE=0,秒表*/

{

EX1=0; ET1=1; ET2=1; TR1=1; SND=1;

L1=1; L0=0; L2=0;

SECOND[0]=0;

SECOND[1]=0;

} break;

case 1: /*STATE=1,倒計(jì)時(shí)分調(diào)整*/

{

EX1=0; ET1=1; ET2=1; TR1=1; TR2=0;

L2=1; L0=0; L1=0;

REVERSE[0]=0;

REVERSE[1]=0;

} break;

case 2: /*STATE=2,倒計(jì)時(shí)小時(shí)調(diào)整*/

{

EX1=0; ET1=1; ET2=1; TR1=1; TR2=0;

L2=1; L0=0; L1=0;

REVERSE[0]=0;

REVERSE[1]=0;

} break;

case 3: /*STATE=3,倒計(jì)時(shí)運(yùn)行*/

{

EX1=0; ET1=1; ET2=1; TR1=1;

if ((REVERSE[2]==0)&&(REVERSE[3]==0)) L2=1;

else TR2=1;

L0=0; L1=0;

} break;

case 5: /*STATE=5,時(shí)鐘分調(diào)整*/

{

IE1=0; EX1=1; ET1=0; ET2=1; TR0=0;

L0=1; L1=0; L2=0;

} break;

case 6:/*STATE=6,時(shí)鐘時(shí)調(diào)整*/

{

IE1=0; EX1=1; ET1=0; ET2=1; TR0=0;

L0=1; L1=0; L2=0;

} break;

default: /*STATE=4 or 7,時(shí)鐘運(yùn)行*/

{

EX1=0; ET1=0; ET2=1; TR0=1;

L1=0; L2=0;

}

}

while (STATE==stabak)

{

switch (STATE)

{

case 0: /*STATE=0,秒表*/

{

BUFFER[0]=SECOND[0];

BUFFER[1]=SECOND[1];

} break;

case 1: /*STATE=1,倒計(jì)時(shí)分調(diào)整*/

{

BUFFER[0]=REVERSE[2];

BUFFER[1]=101;/*A-*/

} break;

case 2: /*STATE=2,倒計(jì)時(shí)小時(shí)調(diào)整*/

{

BUFFER[0]=110;/*-A*/

BUFFER[1]=REVERSE[3];

} break;

case 3: /*STATE=3,倒計(jì)時(shí)運(yùn)行*/

{

BUFFER[0]=REVERSE[2];

BUFFER[1]=REVERSE[3];

} break;

case 5: /*STATE=5,時(shí)鐘分調(diào)整*/

{

BUFFER[0]=CLOCK[2];

BUFFER[1]=101;/*A-*/

} break;

case 6: /*STATE=6,時(shí)鐘時(shí)調(diào)整*/

{

BUFFER[0]=110;/*-A*/

BUFFER[1]=CLOCK[3];

} break;

default: /*STATE=4 or 7,時(shí)鐘運(yùn)行*/

{

BUFFER[0]=CLOCK[2];

BUFFER[1]=CLOCK[3];

}

}

display();

K0=1; K1=1; K2=1;

STATE=P1&0x07;

}

stabak=STATE;

}

}

DS1820溫度測(cè)量程序

#include

#include

#include

#include

#include

#define uchar unsigned char

#define uint unsigned int

#define dsyawh XBYTE[0x0600] // 定義雙端口RAM中的存儲(chǔ)單元地址;

#define dsyawl XBYTE[0x0100]

#define dspitchh XBYTE[0x0200]

#define dspitchl XBYTE[0x0300]

#define dsrollh XBYTE[0x0400]

#define dsrolll XBYTE[0x0500]

sbit P1_0=P1^0;

sbit P1_2=P1^2;

uchar idata stringyaw[6];

uchar idata stringpitch[6];

uchar idata stringroll[6];

uint k, i,j;

uint yaw0,yaw0flag;

uint record,number;

uint iyaw, ipitch, iroll;

uint numbercoma;

uint len1,len2,len3;

uint numyaw,numroll,numpitch;

//主函數(shù),初始化串口和變量

main()

{

TMOD=0x20;

TL1=0xfd;

TH1=0xfd;

SCON=0x50;

PCON=0x00;

IE=0x90;

TR1=1;

P1_0=0;

k=2;

dsyawh=0;

dsyawl=0;

dspitchh=0;

dspitchl=0;

dsrollh=0;

dsrolll=0;

yaw0flag=0;

i=0;

P1_2=1;

while(1);

}

// 由于采用的是專用的通信單片機(jī),可以使用中斷方式進(jìn)行串行通信,提高通信質(zhì)量。

serial () interrupt 4 using 1

{

RI=0; //軟件清除中斷標(biāo)志位

//判斷是否收到HPR格式語(yǔ)句的第一個(gè)字符"$",其數(shù)值為0x24,如果收到,則開始記錄數(shù)據(jù),

//并設(shè)置記錄標(biāo)志record=1。*/

if (SBUF==0x24)

{

record=1;

i=0;

iyaw=0; //記錄航向數(shù)據(jù)字符數(shù)量的變量

ipitch=0; //記錄俯仰數(shù)據(jù)字符數(shù)量的變量

iroll=0; //記錄橫滾數(shù)據(jù)字符數(shù)量的變量

numbercoma=0; // 記錄逗號(hào)數(shù)量的變量

}

if (record==1) // 開始處理HPR中的數(shù)據(jù)信息

{

//利用HPR數(shù)據(jù)中的逗號(hào)間隔,區(qū)分接收到的字符處于何種姿態(tài)數(shù)據(jù)中。

if(SBUF==0x2c) {numbercoma++;}

// 第一個(gè)逗號(hào)之后的字符屬于航向數(shù)據(jù),參見HRP語(yǔ)句

if (numbercoma==1)

{

stringyaw[iyaw]=SBUF; // 將字符放入航向字符串"stringyaw[ ]"中

iyaw++;

}

// 第三個(gè)逗號(hào)之后的字符屬于俯仰數(shù)據(jù)

if (numbercoma==3)

{

stringpitch[ipitch]=SBUF; // 將字符放入俯仰字符串"stringpitch[ ]"中

ipitch++;

}

// 第五個(gè)逗號(hào)之后的字符屬于滾轉(zhuǎn)數(shù)據(jù)

if (numbercoma==5)

{

stringroll[iroll]=SBUF; // 將字符放入橫滾字符串"stringroll[ ]"中

iroll++;

}

//判斷是否收到HPR格式語(yǔ)句的字符"*",如果收到,則結(jié)束記錄,開始處理。

if (SBUF==*)

{

stringyaw[iyaw]=