單片機(jī)多功能電子鐘程序編程參考_第1頁(yè)
單片機(jī)多功能電子鐘程序編程參考_第2頁(yè)
單片機(jī)多功能電子鐘程序編程參考_第3頁(yè)
單片機(jī)多功能電子鐘程序編程參考_第4頁(yè)
單片機(jī)多功能電子鐘程序編程參考_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、附錄A 程序程序A.1 主程序/主程序的功能是對(duì)子程序進(jìn)行調(diào)用,并設(shè)定顯示延時(shí)時(shí)間#include reg51.h /頭文件;#include typedef.h#include lcd.h#include key.h#include alarm_clock.h#include ds1302.hsbit DAT=P10; /74LS164的A、B腳接單片機(jī)P1.0;sbit CLK=P11; /74LS164的CLOCK腳接單片機(jī)P1.1;sbit HYPERLINK app:calendarCalendar=P27; /定義日歷顯示按鍵K7接單片機(jī)P2.7;sbit WDZ=P26; /定義

2、溫度顯示按鍵K6接單片機(jī)P2.6;sbit FUN=P25; /定義功能選擇鍵K5接單片機(jī)P2.5;sbit UP=P24; /定義加1鍵K4接單片機(jī)P2.4;sbit DOWN=P23; /定義減1鍵K3接單片機(jī)P2.3;sbit Ente_Snooze=P22; /定義確認(rèn)/貪睡鍵K2接單片機(jī)P2.2;sbit Alarm=P21; /定義鬧鈴開(kāi)關(guān)鍵K1接單片機(jī)P2.1;sbit beeper=P20; /定義鬧鈴接口P2.0;#define uint unsigned int#define uchar unsigned char#define true 1 /定義true=1;#defi

3、ne false 0 /定義false=0;#define FUNCTION 0 xDF /定義FUN鍵值為DFH;#define UP 0 xEF /定義UP鍵值為EFH;#define DOWN 0 xF7 /定義DOWN鍵值為F7H;#define ALARM 0 xFB /定義ALARM鍵值為FBH;#define Ente_Snooze 0 xFB /定義E/S鍵值為BFH;void key_task(void);void process(uchar current_key);extern bit flash_flag; /定義全局變量(標(biāo)志位);extern uchar funct

4、ion_count; extern bit alarm_flag;extern bit key_enable;void dis(); void sendbyte();voidreset_3w();voidwbyte_3w(uchar);ucharrbyte_3w();voidwrite_byte(uchar Clock_Add,uchar Clock_Data);uchar read_byte(uchar);void write_clock_burst();void ds1302_init();void ds1302_task();void lcd_disp_time1();void dis_

5、WD();void ds18b20();void alarm_clock(void);void delay(unsigned int time) /10ms延時(shí) unsigned char a,b,c; for(a=0;atime;a+) for(b=0;b10;b+) for(c=0;c120;c+); void main() /主程序; ds1302_init(); /初始化DS1302; beeper=1; /初始化鬧鈴管腳;while(1) /循環(huán); key_task(); /掃描鍵盤子程序; ds1302_task(); /DS1302子程序;ds18b20(); /DS18B20子

6、程序; alarm_clock(); /鬧鈴子程序; disp_time(); /時(shí)間顯示子程序; delay(55); /延時(shí); beeper=1; /鬧鈴管腳置1; 程序A.2 電子時(shí)鐘程序/主要是對(duì)時(shí)鐘芯片DS1302初始化;#include reg51.h#include typedef.h#include lcd.h#include ds1302.h#include key.hvoidreset_3w();voidwbyte_3w(uchar);ucharrbyte_3w();voidwrite_byte(uchar Clock_Add,uchar Clock_Data);uchar

7、 read_byte(uchar);void write_clock_burst();voidds1302_init();void ds1302_task();#define uint unsigned int#define uchar unsigned char/*-定義初始化時(shí)間-*/uchar sec=00; uchar min=25;uchar hour=14;uchar date=12; uchar month=5; uchar year=8;uchar day_of_week; uchar clk_hour=14; uchar clk_min=28; /-14:28- /*-定義寄

8、存器地址-*/ #define READ_SEC_ADD 0 x81 /讀秒寄存器#define READ_MIN_ADD 0 x83 /讀分寄存器#define READ_HOUR_ADD 0 x85 /讀時(shí)寄存器#define READ_DATE_ADD 0 x87 /讀日寄存器#define READ_MONTH_ADD 0 x89 /讀月寄存器#define READ_DOW_ADD 0 x8B /讀周寄存器#define READ_YEAR_ADD 0 x8D /年寄存器#define WRITE_SEC_ADD 0 x80 /寫秒寄存器#define WRITE_MIN_ADD 0

9、 x82 /寫分寄存器#define WRITE_HOUR_ADD 0 x84 /寫時(shí)寄存器#define WRITE_DATE_ADD 0 x86 /寫日寄存器#define WRITE_MONTH_ADD 0 x88 /寫月寄存器#define WRITE_DOW_ADD 0 x8A /寫周寄存器#define WRITE_YEAR_ADD 0 x8C /寫年寄存器#define CLOCK_BURST_ADD 0 xBE /時(shí)鐘多字節(jié)傳送模式extern void write_clock_burst();extern voidwrite_byte (uchar Clock_Add,uch

10、ar Clock_Data);void sendbyte();voidreset_3w();voidwbyte_3w(uchar);ucharrbyte_3w();voidwrite_byte(uchar Clock_Add,uchar Clock_Data);uchar read_byte(uchar);void write_clock_burst();voidds1302_init();void ds1302_task();sbit SCLK = P35; /定義管腳SCLKsbit IO = P36; /定義管腳I/Osbit RST = P37; /定義管腳RSTuchar sec,m

11、in,hour,date,month,year;uchar code hex2bcd = 0 x00, 0 x01, 0 x02, 0 x03, 0 x04, 0 x05, 0 x06, 0 x07, 0 x08, 0 x09,/* 00-09 */ 0 x10, 0 x11, 0 x12, 0 x13, 0 x14, 0 x15, 0 x16, 0 x17, 0 x18, 0 x19,/* 10-19 */ 0 x20, 0 x21, 0 x22, 0 x23, 0 x24, 0 x25, 0 x26, 0 x27, 0 x28, 0 x29,/* 20-29 */ 0 x30, 0 x31

12、, 0 x32, 0 x33, 0 x34, 0 x35, 0 x36, 0 x37, 0 x38, 0 x39,/* 30-39 */ 0 x40, 0 x41, 0 x42, 0 x43, 0 x44, 0 x45, 0 x46, 0 x47, 0 x48, 0 x49,/* 40-49 */ 0 x50, 0 x51, 0 x52, 0 x53, 0 x54, 0 x55, 0 x56, 0 x57, 0 x58, 0 x59,/* 50-59 */ 0 x60, 0 x61, 0 x62, 0 x63, 0 x64, 0 x65, 0 x66, 0 x67, 0 x68, 0 x69,

13、/* 60-69 */ 0 x70, 0 x71, 0 x72, 0 x73, 0 x74, 0 x75, 0 x76, 0 x77, 0 x78, 0 x79,/* 70-79 */ 0 x80, 0 x81, 0 x82, 0 x83, 0 x84, 0 x85, 0 x86, 0 x87, 0 x88, 0 x89,/* 80-89 */ 0 x90, 0 x91, 0 x92, 0 x93, 0 x94, 0 x95, 0 x96, 0 x97, 0 x98, 0 x99,/* 90-99 */;uchar code bcd2hex = 0, 1, 2, 3, 4, 5, 6, 7,

14、8, 9, 0, 0, 0, 0, 0, 0,/* 00-09 */ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0,/* 10-19 */ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 0, 0, 0, 0, 0, 0,/* 20-29 */ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 0, 0, 0, 0, 0, 0,/* 30-39 */ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 0, 0, 0, 0, 0, 0,/

15、* 40-49 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0, 0, 0, 0, 0, 0,/* 50-59 */ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 0, 0, 0, 0, 0, 0,/* 60-69 */ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 0, 0, 0, 0, 0, 0,/* 70-79 */ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 0, 0, 0, 0, 0, 0,/* 80-89 */ 90, 91, 92, 9

16、3, 94, 95, 96, 97, 98, 99, 0, 0, 0, 0, 0, 0,/* 90-99 */;void reset_3w() / /復(fù)位子程序 SCLK = 0; RST = 0; /復(fù)位DS1302,中止數(shù)據(jù)傳送 RST = 1; /啟動(dòng)數(shù)據(jù)傳送void wbyte_3w(uchar W_Byte) /寫字節(jié)子程序 uchar i; for(i = 0; i = 1; ucharrbyte_3w() /讀字節(jié)子程序 uchar i;uchar R_Byte;uchar TmpByte;R_Byte = 0 x00;IO = 1;for(i = 0; i 8; i+) SCL

17、K = 1;SCLK = 0;TmpByte = (uchar)IO;TmpByte = 1;R_Byte |= TmpByte; return R_Byte; voidwrite_byte(uchar Clock_Add,uchar Clock_Data) reset_3w();wbyte_3w(Clock_Add);wbyte_3w(Clock_Data);reset_3w(); void write_clock_burst() reset_3w(); /復(fù)位wbyte_3w(CLOCK_BURST_ADD); /寫多字節(jié)傳送模式寄存器wbyte_3w(hex2bcdsec); /寫入初始

18、化時(shí)間wbyte_3w(hex2bcdmin);wbyte_3w(hex2bcdhour);wbyte_3w(hex2bcddate);wbyte_3w(hex2bcdmonth);wbyte_3w(hex2bcdday_of_week);wbyte_3w(hex2bcdyear);wbyte_3w(0);/* must write control register in burst mode */reset_3w();ucharread_byte(uchar Clock_Add) / 從DS1302讀一個(gè)字節(jié) uchar Clock_Data;reset_3w();wbyte_3w(Cloc

19、k_Add);Clock_Data = rbyte_3w();reset_3w();return(Clock_Data); voidds1302_init()/* - initialize time & date for default value - */ reset_3w();wbyte_3w(0 x8e);/ 寫保護(hù)寄存器wbyte_3w(0);/去保護(hù)reset_3w();wbyte_3w(0 x90);/ 寫涓流充電寄存器wbyte_3w(0 xab);/開(kāi)涓流充電,在VCC1和VCC2之間串入2個(gè)二極管,8K電阻write_clock_burst(); /寫入初始化時(shí)間void d

20、s1302_task() if(!key_enable) sec = bcd2hexread_byte(READ_SEC_ADD); /讀DS1302 secmin = bcd2hexread_byte(READ_MIN_ADD); /讀DS1302 minhour = bcd2hexread_byte(READ_HOUR_ADD); /讀DS1302 hour date = bcd2hexread_byte(READ_DATE_ADD); day_of_week = bcd2hexread_byte(READ_DOW_ADD);month = bcd2hexread_byte(READ_MO

21、NTH_ADD); /讀DS1302 monthyear = bcd2hexread_byte(READ_YEAR_ADD); /讀DS1302 year 程序A.3 鍵盤子程序/主要是用于對(duì)時(shí)間的設(shè)定與調(diào)整#include reg51.h#include typedef.h#include key.h#include ds1302.huint wait_time;bit key_enable; bit flash_flag;bit alarm_flag; uchar flash_count;uchar function_count=0;uchar key;uchar key_push;uch

22、ar key_l; void key_scan() /掃描鍵盤; uchar l,a,PUSH; PUSH=P2&0 xFF; if( PUSH!=0 xFF) for(l=500;l0;l-) /延時(shí); for(a=50;a 0;a-) ; if( PUSH!=0 xFF) key_push=P2&0 xFF; key=key_push; /key等于鍵值; void process(uchar current_key) /按鍵功能子程序;switch (current_key)case FUNCTION: /功能選擇鍵; key_enable=true; function_count=fu

23、nction_count+1; if(function_count=9) function_count=1; break; case UP: /加1操作鍵; if(function_count=1) /function_count=1,則秒加1操作; sec+; if(sec=60) /秒加到60,則被置0; sec=0; write_byte(WRITE_SEC_ADD,hex2bcdsec); /寫入秒寄存器; if(function_count=2) /function_count=2,則分加1操作; min+; if(min=60) /分加到60,則被置0; min=0; write_

24、byte(WRITE_MIN_ADD,hex2bcdmin); /寫入分寄存器; else if(function_count=3) /function_count=3,則小時(shí)加1操作; hour+; if(hour=24) /小時(shí)加到24,則被置0; hour=0; write_byte(WRITE_HOUR_ADD,hex2bcdhour);/寫入小時(shí)寄存器; else if(function_count=4) /function_count=4,則鬧鈴分鐘加1操作; clk_min+; if(clk_min=60) /分鐘加滿60自動(dòng)置0; clk_min=0; else if(func

25、tion_count=5) /function_count=5,則鬧鈴小時(shí)加1操作; clk_hour+; if(clk_hour=24) /小時(shí)加滿24自動(dòng)置0; clk_hour=0; else if(function_count=6) /function_count=6,則年加1操作; year+; if(year99) year=0; write_byte(WRITE_YEAR_ADD,hex2bcdyear); else if(function_count=7) /function_count=7,則月加1操作; month+; if(month=13) month=1; write

26、_byte(WRITE_MONTH_ADD,hex2bcdmonth); else if(function_count=8) /function_count=8,則日加1操作; date+; if(date=31) date=0; write_byte(WRITE_DATE_ADD,hex2bcddate); break;case DOWN: /鍵盤減1操作功能 if(function_count=1) if(sec=0) sec=60; -sec; write_byte(WRITE_SEC_ADD,hex2bcdsec); if(function_count=2) if(min=0) min

27、=60; -min; write_byte(WRITE_MIN_ADD,hex2bcdmin); else if(function_count=3) if(hour=0) hour=24; -hour; write_byte(WRITE_HOUR_ADD,hex2bcdhour); else if(function_count=4) if(clk_min=0) clk_min=60; -clk_min; else if(function_count=5) if(clk_hour=0) clk_hour=24; -clk_hour; else if(function_count=6) if(ye

28、ar=0) year=100; -year; write_byte(WRITE_YEAR_ADD,hex2bcdyear); else if(function_count=7) if(month=1) month=13; -month; write_byte(WRITE_MONTH_ADD,hex2bcdmonth); else if(function_count=8) if(date=0) date=31; -date; write_byte(WRITE_DATE_ADD,hex2bcddate); break; case ALARM: alarm_flag=(alarm_flag); br

29、eak;case Ente_Snooze Time= true; /確認(rèn)鍵; if(alarm_flag=true)&(clk_hour=hour)&(clk_min=min) /貪睡功能; clk_min = clk_min +5; default: break; void key_task() key_scan(); if(key=0 x29) wait_time=wait_time+1;if(wait_time=3) wait_time=0;key_enable=false; elsewait_time=0; if(function_count!=0) flash_count+; if(

30、flash_count20)&(flash_count=40)flash_count=0; process(key);if( HYPERLINK app:calendarCalendar=0) /如果K7按下,顯示日期;dis_ HYPERLINK app:calendarCalendar (); else if(WDZ=0) /如果K6按下,顯示溫度; void dis_WD();if(Time=true) /如果按下確認(rèn)鍵,直接顯示時(shí)間; disp_time();key=0; 程序A.4 鬧鈴程序 主要是設(shè)定鬧鈴的條件#include reg51.h#include typedef.h#i

31、nclude lcd.h#include alarm_clock.h#include key.huchar freq_count;void alarm_clock() unsigned char a,b,c;if(alarm_flag=true)&(clk_hour=hour)&(clk_min=min) freq_count+;if(freq_count30) beeper=false; for(a=0;a2;a+) for(b=0;b10;b+) for(c=0;c=30) beeper=true; if(freq_count=100) freq_count=0; 程序A.5 顯示程序/當(dāng)

32、符合不同的條件時(shí),該程序使數(shù)碼管顯示不同的數(shù)據(jù);#include reg51.h#include typedef.h#include lcd.h#include key.huchar sec=45;uchar min=0;uchar hour=8;uchar date=1; uchar month=1; uchar year=6;uchar day_of_week; /2006-01-01 Sun 00:00:00uchar clk_hour=8; uchar clk_min=1; /08:00uchar disp_buffer27;unsigned chardis=0 x3f,0 x06,0

33、 x5b,0 x4f,0 x66,0 x6d,0 x7d,0 x07,0 x7f,0 x6f,0 x08,0 x40,0 x01;/低中高void disp_time(void) unsigned char gsb,led,led1,jj; if(function_count=1) if(flash_flag=true)&(function_count=1) /時(shí)顯示 disp_buffer0=dis3; disp_buffer2=dishour%10; /正常顯示 disp_buffer1=dishour/10; if(key_enable=false) function_count=0;

34、disp_buffer0=dis11; else if(alarm_flag=true) /鬧鐘開(kāi)關(guān)顯示 disp_buffer0=dis12; else if(function_count=0) disp_buffer0=0; disp_buffer2=dishour%10; disp_buffer1=dishour/10; if(flash_flag=true)&(function_count=2)/分顯示 disp_buffer0=dis2; disp_buffer4=dismin%10; /正常顯示 disp_buffer3=dismin/10; if(key_enable=false

35、) function_count=0; disp_buffer0=dis11; else disp_buffer4=dismin%10; disp_buffer3=dismin/10; if(flash_flag=true)&(function_count=3)/秒顯示 disp_buffer0=dis3; disp_buffer6=dissec%10; /正常顯示 disp_buffer5=dissec/10; if(key_enable=false) function_count=0; disp_buffer0=dis11; else disp_buffer6=dissec%10; dis

36、p_buffer5=dissec/10; for(gsb=0;gsb7;gsb+)led=disp_buffergsb; for(jj=0;jj1; else if(function_count3) if(flash_flag=true)&(function_count=4) /約定時(shí)間設(shè)置 disp_buffer21=dis11; disp_buffer22=dis11; disp_buffer23=dis11; disp_buffer24=dis11; disp_buffer20=dis4; disp_buffer26=disclk_min%10; /預(yù)約分正常顯示 disp_buffer

37、25=disclk_min/10; if(key_enable=false) function_count=0; disp_buffer0=dis11; if(flash_flag=true)&(function_count=5) disp_buffer21=dis11; disp_buffer22=dis11; disp_buffer25=dis11; disp_buffer26=dis11; disp_buffer20=dis5; disp_buffer24=disclk_hour%10; /預(yù)約時(shí)正常顯示 disp_buffer23=disclk_hour/10; if(key_enab

38、le=false) function_count=0; disp_buffer0=dis11; if(flash_flag=true)&(function_count=8) /日設(shè)置 disp_buffer21=dis11; disp_buffer22=dis11; disp_buffer23=dis11; disp_buffer24=dis11; disp_buffer0=dis6; disp_buffer26=disdate%10; /日正常顯示 disp_buffer25=disdate/10; if(key_enable=false) function_count=0; disp_bu

39、ffer0=dis11; if(flash_flag=true)&(function_count=7) /月設(shè)置 disp_buffer21=dis11; disp_buffer22=dis11; disp_buffer25=dis11; disp_buffer26=dis11; disp_buffer20=dis7; disp_buffer24=dismonth%10; /月正常顯示 disp_buffer23=dismonth/10; if(key_enable=false) function_count=0; disp_buffer0=dis11; if(flash_flag=true)

40、&(function_count=6) /年設(shè)置 disp_buffer25=dis11; disp_buffer26=dis11; disp_buffer23=dis11; disp_buffer24=dis11; disp_buffer20=dis8; disp_buffer22=disyear%10; /年正常顯示 disp_buffer21=disyear/10; if(key_enable=false) function_count=0; disp_buffer0=dis11; for(gsb=0;gsb7;gsb+) led=disp_buffer2gsb; for(jj=0;jj

41、1; void dis_ HYPERLINK app:calendarCalendar () /顯示日歷子程序 unsigned char gsb,led,led1,jj; disp_buffer2=tabhour%10; disp_buffer1=tabhour/10; disp_buffer4=tabmin%10; /正常顯示 disp_buffer3=tabmin/10; disp_buffer6=tabsec%10; /正常顯示 disp_buffer5=tabsec/10;for(gsb=0;gsb7;gsb+) led=disp_buffergsb; for(jj=0;jj1; 程

42、序A.6 環(huán)境溫度采集顯示子程序/精度0.1,四位數(shù)碼管顯示。#include reg51.h#include intrins.h /_nop_();延時(shí)函數(shù)用#define Disdata P0 /段碼輸出口#define discan P2 /掃描口#define uchar unsigned char#define uint unsigned intsbit DQ=P34; /溫度輸入口sbit DAT=P10; /74LS164的A、B腳接單片機(jī)P1.0;sbit CLK=P11; /74LS164的CLOCK腳接單片機(jī)P1.1;sbit music=P20;sbit hold=P26

43、;uint h; uint temp;unsigned char presence,flash=0;uchar code ditab16=0 x00,0 x01,0 x01,0 x02,0 x03,0 x03,0 x04,0 x04,0 x05,0 x06,0 x06,0 x07,0 x08,0 x08,0 x09,0 x09;/溫度小數(shù)部分用查表法uchar code tab=0 xfc,0 x60,0 xda,0 xf2,0 x66,0 xb6,0 xbe,0 xe0,0 xfe,0 xf6,0 x00,0 x02,0 x02;/共陰LED段碼表 0 1 2 3 4 5 6 7 8 9 不亮 - uint data temp_data2=0 x00,0 x00; /讀出溫度暫放uchar display7;uchar gsb,led,led1,jj; /*延時(shí)函數(shù)*/void delay_2(uint t) for (;t0;t-); /* 蜂鳴器驅(qū)動(dòng)函數(shù) beep() unsigned char i; for (i=0;i0;i-) DQ=1;_nop_();_nop_(); /從高拉倒低 DQ=0;_nop_();_nop_();_nop_();_nop_(); /5 us DQ=val&0 x01; /最低位移出 Delay_2(6); /66s val=val/

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論