分享網淘的時鐘套件資料
興向榮時鐘套件配套資料.rar
(2.41 MB, 下載次數: 16)
2019-10-13 20:41 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
興向榮時鐘模塊電路圖.JPG (300.98 KB, 下載次數: 56)
下載附件
2019-10-13 20:41 上傳
- /* 忠興那個寶店編輯
- */
- /*====================================================================
- 調試要求:
- 1.MCU:AT89S52芯片或AT89C52
- 2.晶振:12MHz
- 功能:多功能時鐘+溫度計
- ====================================================================*/
- #include <reg52.h>
- #include <intrins.h>
- [size=14px]
- [/size]
- #define uchar unsigned char
- #define uint unsigned int
- [size=14px]
- [/size]
- sbit dis_bit1=P2^7;//定義數碼管控制口
- sbit dis_bit2=P2^6;//定義數碼管控制口
- sbit dis_bit3=P2^4;//定義數碼管控制口
- sbit dis_bit4=P2^3;//定義數碼管控制口
- sbit dis_bit5=P2^1;//定義數碼管控制口
- sbit dis_bit6=P2^0;//定義數碼管控制口
- sbit led1_bit=P2^2;//定時LED的控制口
- sbit led2_bit=P2^5;//定時LED的控制口
- sbit s1_bit=P1^0; //定義S1控制口
- sbit s2_bit=P1^1; //定義S2控制口
- sbit s3_bit=P1^2; //定義S3控制口
- sbit dq_ds18b20=P3^3;//定義控制DS18B20
- sbit speak=P3^7; //定義蜂鳴器控制口
- sbit clk_ds1302=P3^6;//定義控制DS1302的時鐘線
- sbit io_ds1302=P3^5;//定義控制DS1302的串行數據
- sbit rest_ds1302=P3^4;
- #define smg_data P0//定義數碼管數據口
- [size=14px]
- [/size]
- void delay_3us();//3US的延時程序
- void delay_8us(uint t);//8US延時基準程序
- void delay_50us(uint t);//延時50*T微妙函數的聲明
- void display1(uchar dis_data);//數碼管1顯示子程序
- void display2(uchar dis_data);//數碼管2顯示子程序
- void display3(uchar dis_data);//數碼管3顯示子程序
- void display4(uchar dis_data);//數碼管4顯示子程序
- void display5(uchar dis_data);//數碼管5顯示子程序
- void display6(uchar dis_data);//數碼管6顯示子程序
- void init_t0();//定時器0初始化函數
- void dis_led();//LED處理函數
- void judge_s1();//S1按鍵處理函數
- void judge_s2();//S2按鍵處理函數
- void judge_s3();//S3按鍵處理函數
- void dis(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1);//顯示子程序
- void dis_san(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1,uchar san);//閃爍顯示子程序
- void judge_dis();//顯示處理函數
- void judge_clock();//顯示處理函數
- void set_ds1302();//設置時間
- void get_ds1302();//讀取當前時間
- [size=14px]
- [/size]
- void w_1byte_ds1302(uchar t);//向DS1302寫一個字節的數據
- uchar r_1byte_ds1302();//從DS1302讀一個字節的數據
- //***********************************************************************
- //DS18B20測溫函數定義
- void w_1byte_ds18b20(uchar value);//向DS18B20寫一個字節
- uchar r_1byte_ds18b20(void);//從DS18B20讀取一個字節的數據
- void rest_ds18b20(void);//DS18B20復位程序
- void readtemp_ds18b20(void);//讀取溫度
- void dis_temp();//溫度顯示函數
- [size=14px]
- [/size]
- [size=14px]
- [/size]
- [size=14px]
- [/size]
- [size=14px]
- [/size]
- //共陽數碼管斷碼表
- const uchar tabl1[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
- // 0 1 2 3 4 5
- 0x82,0xf8,0x80,0x90,0x86,0x87,0xFF,
- //6 7 8 9 E T B
- 0xc6,0xbf,0xff};
- // C -
- const uchar tabl3[]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
- uchar t0_crycle;
- uchar hour_count,minute_count,second_count,msecond_count;
- uchar clock_hour,clock_minute;
- uchar countdown_second;
- uchar countdown_hour,countdown_minute;
- uchar clock_en;//鬧鐘關閉和開啟的標志,1開啟,0關閉
- uchar flag1,second_flag,zancun1,zancun2,zancun3;
- uchar zancun4,zancun5,zancun6,zancun7;
- uchar clock_flag,countdown_flag;
- uchar msecond_minute,msecond_second,msecond_msecond,msecond_flag;//秒表相關參數
- uint speak_count;
- uchar templ,temph,temp_flag;
- uchar t_b,t_s,t_g,t_x,temp_flag2;//從左到右分別存儲溫度百位,十位,個位,小數位
- uchar tab23[3];//={0x40,0x59,0x23,0x28,0x11,0x06,0x09};//上電時默認的時間
- //主程序
- void main()
- {
- P3=0x00;
- flag1=0;
- zancun3=0;
- msecond_minute=0;//置秒表相關參數為0
- msecond_second=0;
- msecond_msecond=0;
- speak=1;//關閉蜂鳴器
- speak_count=0;
- clock_hour=0;
- clock_minute=0;
- clock_flag=0;
- countdown_flag=0;//倒計時標志位為0
- clock_en=0;//開機時默認關閉鬧鐘
- init_t0();
- TR0=1;//
- [size=14px]
- [/size]
- // set_ds1302();//設置DS1302的初始時間
- //接下來開始編寫讓數碼管顯示的程序
- while(1)
- {
- get_ds1302();
- judge_dis();//顯示處理
- judge_s1();
- judge_s2();
- judge_s3();
- judge_clock();//鬧鐘處理程序
- }
- }
- void timer0() interrupt 1
- {
- [size=14px] TH0=(65536-50000)/256;[/size]
- [size=14px] TL0=(65536-50000)%256;[/size]
- [size=14px] t0_crycle++;[/size]
- [size=14px] if(t0_crycle==2)// 0.1秒[/size]
- [size=14px] {[/size]
- [size=14px] t0_crycle=0;[/size]
- msecond_flag=1;
- [size=14px] msecond_count++;[/size]
- if(msecond_count==10)//1秒
- {
- msecond_count=0;
- second_flag=1;
- }
- [size=14px] }[/size]
- }
- //**************************************************
- //顯示處理函數
- void judge_dis()
- {
- if(flag1==0)
- {
- if(second_flag==1)
- {
- zancun7++;
- second_flag=0;
- }
- if(zancun7<1)
- {
- if(temp_flag2==1)
- {
- readtemp_ds18b20();//讀取溫度
- temp_flag2=0;
- }
- dis_temp();//溫度顯示函數
- }
- if(zancun7>=1)
- {
- temp_flag2=1;
- zancun4=hour_count&0xf0;
- zancun4>>=4;
- zancun5=minute_count&0xf0;
- zancun5>>=4;
- zancun6=second_count&0xf0;
- zancun6>>=4;
- dis(zancun4,hour_count&0x0f,zancun5,minute_count&0x0f,zancun6,second_count&0x0f);
- dis_led();
- if(zancun7==5)zancun7=0;
- }
- }
- if(flag1!=0)
- {
- switch(flag1)
- {
- case 1:
- dis(5,10,11,1,12,12);//顯示SET1
- led1_bit=1;
- led2_bit=1;
- break;
- case 2:
- dis(5,10,11,2,12,12);//顯示SET2
- break;
- case 3:
- dis(5,10,11,3,12,12);//顯示SET3
- break;
- case 4:
- dis(5,10,11,4,12,12);//顯示SET4
- break;
- case 5:
- dis(5,10,11,5,12,12);//顯示SET5
- break;
- case 6:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,1);
- break;
- case 7:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,2);
- break;
- case 8:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,3);
- break;
- case 9://進入修改時間,時間分位個位閃爍
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,4);
- break;
- case 10://進入修改鬧鐘,鬧鐘小時十位閃爍
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,1);
- break;
- case 11://進入修改鬧鐘,鬧鐘小時個位閃爍
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,2);
- break;
- case 12://進入修改鬧鐘,鬧鐘小時十位閃爍
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,3);
- break;
- case 13://進入修改鬧鐘,鬧鐘小時個位閃爍
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,4);
- break;
- case 14://進入修改鬧鐘的開關
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,6);
- break;
- case 15:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,1);
- break;
- case 16:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,2);
- break;
- case 17:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,3);
- break;
- case 18:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,4);
- break;
- case 19:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,5);
- break;
- case 20:
- dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,6);
- break;
- case 21:
- if(second_flag==1)
- {
- second_flag=0;
- countdown_second--;
- if(countdown_second==255)
- {
- countdown_second=59;
- countdown_minute--;
- if(countdown_minute==255)
- {
- countdown_minute=59;
- countdown_hour--;
- if(countdown_hour==255)
- {
- flag1=22;
- countdown_minute=0;
- countdown_hour=0;
- countdown_second=0;
- countdown_flag=1;
- }
- }
- }
- }
- dis(countdown_hour/10,countdown_hour%10,countdown_minute/10,countdown_minute%10,countdown_second/10,countdown_second%10);//
- break;
- case 22:
- if(countdown_flag>0 && countdown_flag<7)
- {
- speak=0;
- if(second_flag==1)
- {
- second_flag=0;
- countdown_flag++;
- }
- }
- else
- {
- speak=1;
- }
- dis(countdown_hour/10,countdown_hour%10,countdown_minute/10,countdown_minute%10,countdown_second/10,countdown_second%10);//
- break;
- case 23:
- dis(msecond_minute/10,msecond_minute%10,msecond_second/10,msecond_second%10,msecond_msecond%10,12);
- break;
- case 24:
- if(msecond_flag==1)
- {
- msecond_flag=0;
- msecond_msecond++;
- if(msecond_msecond==10)
- {
- msecond_msecond=0;
- msecond_second++;
- if(msecond_second==60)
- {
- msecond_second=0;
- msecond_minute++;
- if(msecond_minute==100)
- {
- msecond_minute=99;
- flag1=23;
- }
- }
- }
- }
- dis(msecond_minute/10,msecond_minute%10,msecond_second/10,msecond_second%10,msecond_msecond%10,12);
- break;
- case 25:
- dis(zancun3/10,zancun3%10,zancun2/10,zancun2%10,zancun1/10,zancun1%10);
- break;
- default:
- break;
- }
- }
- }
- //**************************************************
- //S1按鍵處理函數
- void judge_s1()
- {
- s1_bit=1;//置IO為1,準備讀入收據
- if(s1_bit==0)//判斷是否有按鍵按下
- {
- delay_50us(1);// 延時,去除機械抖動
- if(s1_bit==0)
- {
- switch(flag1)
- {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 6:
- case 7:
- case 8:
- case 10:
- case 11:
- case 12:
- case 13:
- case 15:
- case 16:
- case 17:
- case 18:
- case 19:
- flag1++;
- break;
- case 9:
- flag1=6;
- break;
- case 14:
- flag1=10;
- break;
- case 20:
- flag1=15;
- break;
- case 5:
- case 21:
- case 22:
- case 23://系統從秒表狀態復位
- case 24://系統從秒表狀態復位
- case 25://系統從計數器復位
- flag1=0;
- break;
- default:
- break;
- }
- while(s1_bit==0)
- {
- judge_dis();
- }//等待按鍵釋放
- }
- }
- }
- //**************************************************
- //S2按鍵處理函數
- void judge_s2()
- {
- s2_bit=1;//置IO為1,準備讀入收據
- if(s2_bit==0)//判斷是否有按鍵按下
- {
- delay_50us(1);// 延時,去除機械抖動
- if(s2_bit==0)
- {
- switch (flag1)
- {
- case 1: //在顯示SET1狀態下按S2件,進入修改時間
- flag1=6;
- zancun4=hour_count&0xf0;
- zancun4>>=4;
- zancun6=hour_count&0x0f;
- zancun1=zancun4*10+zancun6;
- //zancun1=hour_count;
- zancun5=minute_count&0xf0;
- zancun5>>=4;
- zancun6=minute_count&0x0f;
- zancun2=zancun5*10+zancun6;
- // zancun2=minute_count;
- break;
- case 2://在顯示SET2狀態下按S2,進入設置鬧鐘
- zancun1=clock_hour;
- zancun2=clock_minute;
- flag1=10;
- break;
- case 6://修改時鐘小時十位狀態下按S2件
- case 7://修改時鐘小時個位狀態下按S2件
- case 8://修改時鐘分鐘十位狀態下按S2件
- case 9://修改時鐘分鐘個位狀態下按S2件
- //zancun4=zancun1/10;
- [size=14px]
- [/size]
- tab23[2]=zancun1/10*16+zancun1%10;
- //zancun5=zancun2&0xf0;
- //zancun5>>=4;
- tab23[1]=zancun2/10*16+zancun2%10;
- hour_count=tab23[2];
- minute_count=tab23[1];
- second_count=0;
- tab23[0]=0;
- set_ds1302();//設置DS1302的初始時間
- flag1=0;
- break;
- case 10://修改鬧鐘小時十位狀態下按S2
- case 11://修改鬧鐘小時個位狀態下按S2
- case 12://修改鬧鐘分鐘十位狀態下按S2
- case 13://修改鬧鐘分鐘個位狀態下按S2
- case 14://修改鬧鐘使能狀態下按S2
- clock_hour=zancun1;
- clock_minute=zancun2;
- clock_en=zancun3;
- flag1=0;
- break;
- case 3:
- flag1=15;
- zancun1=countdown_hour;
- zancun2=countdown_minute;
- zancun3=countdown_second;
- break;
- case 15:
- case 16:
- case 17:
- case 18:
- case 19:
- case 20:
- countdown_hour=zancun1;
- countdown_minute=zancun2;
- countdown_second=zancun3;
- flag1=21;
- countdown_flag=0;
- break;
- case 22:
- flag1=21;
- break;
- case 21:
- flag1=22;
- break;
- case 4:
- flag1=23;//秒表暫停
- msecond_minute=0;
- msecond_second=0;
- msecond_msecond=0;
- break;
- case 23:
- flag1=24;
- break;
- case 24:
- flag1=23;
- break;
- case 5:
- flag1=25;//進入計數器模式
- zancun1=0;
- zancun2=0;
- zancun3=0;
- break;
- default:
- break;
- }
- while(s2_bit==0)
- {
- judge_dis();
- }//等待按鍵釋放
- }
- }
- }
- //**************************************************
- //S3按鍵處理函數
- void judge_s3()
- {
- s3_bit=1;//置IO為1,準備讀入收據
- if(s3_bit==0)//判斷是否有按鍵按下
- {
- delay_50us(1);// 延時,去除機械抖動
- if(s3_bit==0)
- {
- switch (flag1)
- {
- case 6://修改時間小時的十位數
- zancun1+=10;
- if(zancun1>=24)zancun1=zancun1%10;
- break;
- case 7://修改時間小時的個位數
- zancun1=zancun1/10*10+(zancun1%10+1)%10;
- if(zancun1>=24)zancun1=20;
- break;
- case 8://修改時間分鐘的十位數
- zancun2+=10;
- if(zancun2>=60)zancun2-=60;
- break;
- case 9://修改時間分鐘的個位數
- zancun2=zancun2/10*10+(zancun2%10+1)%10;
- break;
- case 10://修改鬧鐘小時的十位數
- zancun1+=10;
- if(zancun1>=24)zancun1=zancun1%10;
- break;
- case 11://修改鬧鐘小時的個位數
- zancun1=zancun1/10*10+(zancun1%10+1)%10;
- if(zancun1>=24)zancun1=20;
- break;
- case 12://修改鬧鐘分鐘的十位數
- zancun2+=10;
- if(zancun2>=60)zancun2-=60;
- break;
- case 13://修改鬧鐘分鐘的個位數
- zancun2=zancun2/10*10+(zancun2%10+1)%10;
- break;
- case 14:
- zancun3^=1;
- break;
- case 15://修改倒計時小時的十位數
- zancun1+=10;
- if(zancun1>=100)zancun1-=100;
- break;
- case 16: //修改倒計時小時的個位數
- zancun1=zancun1/10*10+(zancun1%10+1)%10;
- break;
- case 17://修改倒計時分鐘的十位數
- zancun2+=10;
- if(zancun2>=60)zancun2-=60;
- break;
- case 18: //修改倒計時分鐘的個位數
- zancun2=zancun2/10*10+(zancun2%10+1)%10;
- break;
- case 19://修改倒計時秒的十位數
- zancun3+=10;
- if(zancun3>=60)zancun3-=60;
- break;
- case 20: //修改倒計時秒的個位數
- zancun3=zancun3/10*10+(zancun3%10+1)%10;
- break;
- case 21:
- case 22://
- countdown_hour=zancun1;
- countdown_minute=zancun2;
- countdown_second=zancun3;
- flag1=21;
- break;
- case 23:
- case 24://秒表復位
- flag1=24;
- msecond_minute=0;
- msecond_second=0;
- msecond_msecond=0;
- break;
- case 25:
- zancun1++;
- if(zancun1==100)
- {
- zancun1=0;
- zancun2++;
- if(zancun2==100)
- {
- zancun2=0;
- zancun3++;
- }
- }
- break;
- default:
- break;
- }
- while(s3_bit==0)
- {
- judge_dis();
- }//等待按鍵釋放
- }
- }
- }
- //****************************************
- //顯示處理函數
- void judge_clock()
- {
- zancun4=hour_count&0xf0;
- zancun4>>=4;
- zancun6=hour_count&0x0f;
- zancun4*=10;
- zancun4+=zancun6;
- [size=14px]
- [/size]
- zancun5=minute_count&0xf0;
- zancun5>>=4;
- zancun6=minute_count&0x0f;
- zancun5*=10;
- zancun5+=zancun6;
- [size=14px]
- [/size]
- if(clock_hour==zancun4 && clock_minute==zancun5)
- {
- if(clock_en==1 && clock_flag==0)
- {
- speak_count=0;//開啟蜂鳴器
- clock_flag=1;
- speak_count=0;
- }
- }
- else
- {
- clock_flag=0;
- }
- if(clock_flag==1 && speak_count<400)
- {
- if(msecond_count<=5)
- {
- speak=0;
- speak_count++;
- }
- else
- {
- speak=1;
- }
- }
- else
- {
- speak=1;
- }
- }
- //****************************************
- ////閃爍顯示子程序
- void dis_san(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1,uchar san)
- {
- if(san==1)
- {
- if(msecond_count<5)
- {
- display1(s6);
- }
- }
- else
- {
- display1(s6);
- }
- if(san==2)
- {
- if(msecond_count<5)
- {
- display2(s5);
- }
- }
- else
- {
- display2(s5);
- }
- if(san==3)
- {
- if(msecond_count<5)
- {
- display3(s4);
- }
- }
- else
- {
- display3(s4);
- }
- if(san==4)
- {
- if(msecond_count<5)
- {
- display4(s3);
- }
- }
- else
- {
- display4(s3);
- }
- if(san==5)
- {
- if(msecond_count<5)
- {
- display5(s2);
- }
- }
- else
- {
- display5(s2);
- }
- if(san==6)
- {
- if(msecond_count<5)
- {
- display6(s1);
- }
- }
- else
- {
- display6(s1);
- }
- }
- //****************************************
- //時鐘顯示程序
- void dis(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1)
- {
- display1(s6);
- display2(s5);
- display3(s4);
- display4(s3);
- display5(s2);
- display6(s1);
- }
- //********************************************************************************************
- void init_t0()
- {
- TMOD=0x01;//設定定時器工作方式1,定時器定時50毫秒
- [size=14px] TH0=(65536-50000)/256;[/size]
- [size=14px] TL0=(65536-50000)%256;[/size]
- [size=14px] EA=1;//開總中斷[/size]
- [size=14px] ET0=1;//允許定時器0中斷[/size]
- [size=14px] t0_crycle=0;//定時器中斷次數計數單元[/size]
- }
- //**************************************************
- //LED處理函數
- void dis_led()
- {
- if(msecond_count<5)
- {
- led1_bit=1;
- led2_bit=1;
- }
- else
- {
- led1_bit=0;
- led2_bit=0;
- }
- }
- //***************************************************************
- //功能:把數據1顯示在數碼管1上
- void display6(uchar dis_data)
- {
- smg_data=tabl1[dis_data];//送顯示斷碼
- dis_bit6=0;//鎖存數據
- delay_50us(40);
- dis_bit6=1;
- }
- //***************************************************************
- //功能:把數據1顯示在數碼管1上
- void display5(uchar dis_data)
- {
- smg_data=tabl1[dis_data];//送顯示斷碼
- dis_bit5=0;//鎖存數據
- delay_50us(40);
- dis_bit5=1;
- }
- //***************************************************************
- //功能:把數據1顯示在數碼管1上
- void display4(uchar dis_data)
- {
- smg_data=tabl1[dis_data];//送顯示斷碼
- dis_bit4=0;//鎖存數據
- delay_50us(40);
- dis_bit4=1;
- }//***************************************************************
- //功能:把數據1顯示在數碼管1上
- void display3(uchar dis_data)
- {
- smg_data=tabl1[dis_data];//送顯示斷碼
- dis_bit3=0;//鎖存數據
- delay_50us(40);
- dis_bit3=1;
- }
- //***************************************************************
- //功能:把數據1顯示在數碼管1上
- void display1(uchar dis_data)
- {
- smg_data=tabl1[dis_data];//送顯示斷碼
- dis_bit1=0;//鎖存數據
- delay_50us(40);
- dis_bit1=1;
- }
- //***************************************************************
- //功能:把數據1顯示在數碼管1上
- void display2(uchar dis_data)
- {
- smg_data=tabl1[dis_data];//送顯示斷碼
- dis_bit2=0;//鎖存數據
- delay_50us(40);
- dis_bit2=1;
- }
- //**************************************************************************************************
- //函數名稱:void delay_50US(unsigned int t)
- //功能: 延時50*t(us)
- void delay_50us(uint t)
- {
- unsigned char j;
- for(;t>0;t--)
- {
- for(j=19;j>0;j--);
- }
- }
- //*******************************************************************************
- //8微秒延時基準程序
- void delay_8us(uint t)
- {
- while(--t);
- }
- //*******************************************************************************
- //3微秒延時程序
- void delay_3us()
- {
- ;
- ;
- }
- //*******************************************************************************
- //子程序功能:向DS18B20寫一字節的數據
- void w_1byte_ds18b20(uchar value)
- {
- uchar i=0;
- for(i=0;i<8;i++)
- {
- dq_ds18b20=0;
- delay_3us();
- if (value & 0x01) dq_ds18b20=1; //DQ = 1
- delay_50us(1); //延時50us 以上
- value>>=1;
- dq_ds18b20=1; //DQ = 1
- }
- delay_50us(1);
- }
- //讀一個字節
- uchar r_1byte_ds18b20(void)
- {
- uchar i=0;
- uchar value = 0;
- for (i=0;i<8;i++)
- {
- value>>=1;
- dq_ds18b20=0;// DQ_L;
- delay_3us();
- dq_ds18b20=1;
- delay_3us();
- delay_3us();
- if(dq_ds18b20==1) value|=0x80;
- delay_50us(1); //延時40us
- }
- return value;
- }
- //;**************************************************
- //ds18b20復位子程序
- void rest_ds18b20(void)
- {
- rest:delay_3us(); //稍做延時
- delay_3us();
- dq_ds18b20=1;
- delay_3us();
- dq_ds18b20=0;// DQ_L;
- delay_8us(75);//480us<T<960us
- dq_ds18b20=1;//拉高總線
- delay_8us(8);
- if(dq_ds18b20==1)
- {
- return;
- }
- delay_8us(11); //延時90us
- if(dq_ds18b20==1)
- {
- return;
- }
- else
- {
- goto rest;
- }
- }
- //****************************************************
- //讀取溫度
- void readtemp_ds18b20(void)
- {
- uchar temp32;
- rest_ds18b20();
- w_1byte_ds18b20(0xcc); //跳過讀序列號的操作
- w_1byte_ds18b20(0x44); //啟動溫度轉換
- delay_8us(2);
- rest_ds18b20();
- w_1byte_ds18b20(0xcc); //跳過讀序列號的操作
- w_1byte_ds18b20(0xbe); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
- templ=r_1byte_ds18b20();
- temph=r_1byte_ds18b20();
- if((temph&0xf0))//判斷溫度的正負性
- {
- temp_flag=0;//溫度為負數標志
- temph=-temph;
- templ=-templ;
- t_x=tabl3[templ & 0x0f];//計算溫度的小數
- temp32=temph & 0x0f;
- temp32<<=4;
- templ>>=4;
- temp32=temp32 | templ;
- t_b=temp32/100%10;//計算溫度的百位數據
- t_s=temp32/10%10;//計算溫度的十位數據
- t_g=temp32%10;//計算溫度的個位數據
- }
- else//為正數
- {
- t_x=tabl3[templ & 0x0f];//計算溫度的小數
- temp32=temph & 0x0f;
- temp32<<=4;
- templ>>=4;
- temp32=temp32 | templ;
- t_b=temp32/100%10;//計算溫度的百位數據
- t_s=temp32/10%10;//計算溫度的十位數據
- t_g=temp32%10;//計算溫度的個位數據
- temp_flag=1;
- }
- }
- void dis_temp()//溫度顯示函數
- {
- if(temp_flag=1)
- {
- if(t_b==0)
- {
- dis(12,12,t_s,t_g,13,12);
- }
- else
- {
- dis(12,t_b,t_s,t_g,13,12);
- }
- }
- else
- {
- dis(14,t_b,t_s,t_g,13,12);
- }
- }
- //;##############################################################################
- //;子程序名:w_1byte_ds1302
- //;功能: 向DS1302寫一個字節的數據
- void w_1byte_ds1302(uchar t)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- if(t & 0x01)
- {io_ds1302=1;}
- else
- {io_ds1302=0;}
- clk_ds1302=1;
- delay_3us();
- delay_3us();
- clk_ds1302=0;
- delay_3us();
- delay_3us();
- t>>=1;
- }
- }
- //;########################################################################
- //;子程序名:r_1byte_ds1302()
- //;功能: 從DS1302讀一個字節的數據
- uchar r_1byte_ds1302()
- {
- uchar i,temp11=0;
- io_ds1302=1;//置IO為1,準備讀入數據
- for(i=0;i<8;i++)
- {
- temp11>>=1;
- if(io_ds1302) temp11 |= 0x80;
- clk_ds1302=1;
- delay_3us();
- delay_3us();
- clk_ds1302=0;
- delay_3us();
- }
- return(temp11);
- }
- //;#################################################################################
- //;子程序名:setbds1302
- //;功能: 設置DS1302初始時間,并啟動計時
- void set_ds1302()
- {
- uchar i,j;
- rest_ds1302=0;
- delay_3us();
- clk_ds1302=0;
- delay_3us();
- rest_ds1302=1;
- delay_3us();
- w_1byte_ds1302(0x8e);//寫控制命令字
- delay_3us();
- w_1byte_ds1302(0x00);//寫保護關閉
- clk_ds1302=1;
- delay_3us();
- rest_ds1302=0;
- for(i=0,j=0x80;i<7;i++,j+=2)
- {
- rest_ds1302=0;
- delay_3us();
- clk_ds1302=0;
- delay_3us();
- rest_ds1302=1;
- delay_3us();
- w_1byte_ds1302(j);
- delay_3us();
- w_1byte_ds1302(tab23[i]);
- delay_3us();
- delay_3us();
- clk_ds1302=1;
- delay_3us();
- rest_ds1302=0;
- delay_3us();
- delay_3us();
- }
- rest_ds1302=0;
- delay_3us();
- clk_ds1302=0;
- delay_3us();
- rest_ds1302=1;
- delay_3us();
- w_1byte_ds1302(0x8e);
- delay_3us();
- w_1byte_ds1302(0x80);
- clk_ds1302=1;
- delay_3us();
- rest_ds1302=0;
- delay_3us();
- }
- //;#################################################################-------
- //;子程序名:get1302
- void get_ds1302()
- {
- uchar temp11[7],i,j;
- for(i=0;i<7;i++)
- {temp11[i]=0;}
- for(i=0,j=0x81;i<7;i++,j+=2)
- {
- rest_ds1302=0;
- delay_3us();
- clk_ds1302=0;
- delay_3us();
- rest_ds1302=1;
- delay_3us();
- w_1byte_ds1302(j);
- temp11[i]=r_1byte_ds1302();
- delay_3us();
- clk_ds1302=1;
- delay_3us();
- rest_ds1302=0;
- delay_3us();
- }
- if(temp11[0]!=0xff)
- {second_count=temp11[0];}
- if(temp11[1]!=0xff)// 數據驗證
- {minute_count=temp11[1];}
- if(temp11[2]!=0xff)//數據驗證
- {hour_count=temp11[2];}
- // date=temp[3];
- //month=temp[4];
- // week=temp[5];
- //year=temp[6];
- }
復制代碼
|