论文下载:
基于51单片机的电子万年历毕业论文设计.doc
(1.49 MB, 下载次数: 11)
电路原理图如下:
- 设计主程序:
- /*
- * 万年历
- */
- #include "main.h"
- #include "LCD.h"
- #include "DS1302.h"
- #include "word.h"
- #include "lunar_calendar.h"
- #include "buzz.h"
- TIME time, tmp_time;
- ALARM alarm;
- bit Alarm_flag=0;
- bit Clock_flag=0;
- bit flag=0;
- sbit DQ=P2^6; //DS18B20 pin
- //-----------------18B20-----------------------
- unsigned char L_18B20,H_18B20,zhengshu,shangwen,xiawen;
- unsigned int fg=0,xiaoshu_a;
- //-----------------18B20----------------------
- //-------------音乐-----------------------------
- uint8 code SONG_TONE[]=
- {
- 212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,129,169,190,119,119,126,159,142,159,0
- };
- uint8 code SONG_LONG[]=
- {
- 9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0
- };
- //------------------------------------------------------------
- void delay(uint16 n)
- {
- while (n--);
- }
- //************************************************************************/
- // 函数: LCD_Delay()
- // 描述: 延时t ms函数
- // 参数: t
- // 返回: 无
- // 备注: 11.0592MHZ t=1延时时间约1ms
- // 版本: 2011/01/01 First version
- //************************************************************************/
- void Delay_nms(unsigned int t)
- {
- unsigned int i,j;
- for(i=0;i<t;i++)
- for(j=0;j<113;j++)
- ;
- }
- /////////////////////////////////////////////////
- //-----------播放音乐----------------------------
- void PlayMusic()
- {
- uint16 i =0,j,k;
- while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
- {
- for(j=0;j<SONG_LONG[i]*20;j++)
- {
- BEEP = ~BEEP;
- for(k=0;k<SONG_TONE[i]/3;k++);
- }
- Delay_nms(10);
- i++;
- }
- BEEP =1;//关闭蜂鸣器
- }
- /////////////////////////////////////////////////
- /*------DS18B20------*/
- void delay_18B20(unsigned int i)
- {
- while(i--);
- }
- /*DS18B20的复位脉冲 主机通过拉低单总线至少480us以产生复位脉冲
- 然后主机释放单总线并进入接收模式 此时单总线电平被拉高
- DS18B20检测到上升沿后 延时15~60us,拉低总线60~240us产生应答脉冲 */
- void Init_DS18B20(void)
- {
- unsigned char x=0;
- DQ = 1; //DQ复位
- delay_18B20(8); //稍做延时
- DQ = 0; //单片机将DQ拉低
- delay_18B20(80); //精确延时 大于 480us
- DQ = 1; //拉高总线
- delay_18B20(14);
- x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
- delay_18B20(20);
- }
- /*写时隙 主机在写1时隙向DS18B20写入1,在写0时隙向DS18B20写入0
- 所有写时隙至少需要60us,且在两次写时隙之间至少需要1us的恢复时间
- 两种写时隙均以主机拉低总线开始
- 产生写1时隙:主机拉低总线后,必须在15us内释放总线,由上拉电阻拉回至高电平
- 产生写0时隙:主机拉低总线后,必须整个时隙保持低电平 */
- void WriteOneChar(unsigned char dat)
- {
- unsigned char i=0;
- for (i=8; i>0; i--)
- {
- DQ = 0;
- DQ = dat&0x01;
- delay_18B20(5);
- DQ = 1;
- dat>>=1;
- }
- }
-
- /*所有读时隙至少60us 且两次独立的读时隙之间至少需要1us的恢复时间
- 每次读时隙由主机发起,拉低总线至少1us。
- 若传1,则保持总线高电平;若发送0,则拉低总线
- 传0时DS18B20在该时隙结束时释放总线,再拉回高电平状态,主机必须在读时隙开始后的15us内释放总线,并保持采样总线状态 */
- unsigned char ReadOneChar(void)
- {
- unsigned char i=0;
- unsigned char dat = 0;
- for (i=8;i>0;i--)
- {
- DQ = 0; // 给脉冲信号
- dat>>=1;
- DQ = 1; // 给脉冲信号
- if(DQ)
- dat|=0x80;
- delay_18B20(4);
- }
- return(dat);
- }
-
- void read_18B20(void)
- {
- Init_DS18B20();
- WriteOneChar(0xCC); // 跳过读序号列号的操作
- WriteOneChar(0x44); // 启动温度转换
- delay_18B20(100); // this message is wery important
- Init_DS18B20();
- WriteOneChar(0xCC); //跳过读序号列号的操作
- WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
- delay_18B20(100);
- L_18B20=ReadOneChar(); //读取低八位数据
- H_18B20=ReadOneChar(); //读取高八位数据
- zhengshu=L_18B20/16+H_18B20*16; //整数部分
- xiaoshu_a=(L_18B20&0x0f)*10/16; //小数第一位
- }
- //------------------DS18B20---------------------
- /////////////////////////////////////////////////
- /*
- * 按键扫描
- */
- int8 scan_key(void)
- {
- int8 val=-1;
- if (KeyIn1 == 0)
- {
- val = 1;
- while (KeyIn1 == 0);
- }
- else if (KeyIn2 == 0)
- {
- val = 2;
- while (KeyIn2 == 0);
- }
- else if (KeyIn3 == 0)
- {
- val = 3;
- while (KeyIn3 == 0);
- }
- //if (val > 0)
- //buzzer_sound();
- return val;
- }
- /*
- * 主界面框架
- */
- void main_frame(void)
- {
- play32(80, 2, 10);
- play32(32, 2, 10);
- play8(16, 0, S_xie);
- play8(40, 0, S_xie);
- // play8(96, 0, RH);
- // play8(120, 0, S_percent);
- play8(120, 6, S_du);
- }
- /*
- * 主界面
- */
- void main_show(bit refresh)
- {
- uint8 lunar[2];
- if (refresh)
- read_time((uint8 *)&time);
- // 时间
- if (refresh || (time.sec != tmp_time.sec)) // 秒更新
- {
- tmp_time.sec = time.sec;
- // 温湿度
- play8_num(104, 6,zhengshu); //温度显示
- play32_num(96, 2, time.sec);
- }
- if (refresh)
- main_frame();
- if (refresh || (time.min != tmp_time.min)) // 分更新
- {
- if (!refresh)
- flag = 0;
- tmp_time.min = time.min;
- play32_num(48, 2, time.min);
- }
- if (refresh || (time.hour != tmp_time.hour)) // 时更新
- {
- if ((!refresh)&&(Clock_flag))
- alarm_sound();
- tmp_time.hour = time.hour;
- play32_num(0, 2, time.hour);
- }
- if (refresh || (time.day != tmp_time.day)) // 日更新
- {
- tmp_time.day = time.day;
- play8_num(48, 0, time.day);
- // 农历
- turn_lunar_calendar(&time, lunar);
- play_lunar_calendar(0, 6, lunar[0], lunar[1]);
- }
- if (refresh || (time.week != tmp_time.week)) // 周更新
- {
- tmp_time.week = time.week;
- play_week(68, 0, time.week);
- }
- if (refresh || (time.mon != tmp_time.mon)) // 月更新
- {
- tmp_time.mon = time.mon;
- play8_num(24, 0, time.mon);
- // 农历
- turn_lunar_calendar(&time, lunar);
- play_lunar_calendar(0, 6, lunar[0], lunar[1]);
- }
- if (refresh || (time.year != tmp_time.year)) // 年更新
- {
- tmp_time.year = time.year;
- play8_num(0, 0, time.year);
- // 农历
- turn_lunar_calendar(&time, lunar);
- play_lunar_calendar(0, 6, lunar[0], lunar[1]);
- }
-
- }
- /*
- * 主机界面设置
- */
- void main_set(void)
- {
- int8 key_val, state=1;
- play32_num(96, 2|0x80, time.sec);
-
- while (1)
- {
- key_val = scan_key();
-
- if (key_val == 1) // 设置
- {
- if (state >= 7)
- state = 0;
- else
- state++;
- set_time((uint8 *)&time);
- main_show(1);
- switch (state)
- {
- case 0: set_time((uint8 *)&time); break;
- case 1: play32_num(96, 2|0x80, time.sec); break;
- case 2: play32_num(48, 2|0x80, time.min); break;
- case 3: play32_num(0, 2|0x80, time.hour); break;
- case 4: play_week(68, 0|0x80, time.week); break;
- case 5: play8_num(48, 0|0x80, time.day); break;
- case 6: play8_num(24, 0|0x80, time.mon); break;
- case 7: play8_num(0, 0|0x80, time.year); break;
- default: break;
- }
- }
- else if (key_val > 1)
- {
- if (state == 1)
- {
- if (key_val == 3)
- time.sec++;
- else
- time.sec--;
- if (time.sec >= 60)
- time.sec = 0;
- else if (time.sec < 0)
- time.sec = 59;
- play32_num(96, 2|0x80, time.sec);
- }
- else if (state == 2)
- {
- if (key_val == 3)
- time.min++;
- else
- time.min--;
- if (time.min >= 60)
- time.min = 0;
- else if (time.min < 0)
- time.min = 59;
- play32_num(48, 2|0x80, time.min);
- }
- else if (state == 3)
- {
- if (key_val == 3)
- time.hour++;
- else
- time.hour--;
- if (time.hour >= 24)
- time.hour = 0;
- else if (time.hour < 0)
- time.hour = 23;
- play32_num(0, 2|0x80, time.hour);
- }
- else if (state == 4)
- {
- if (key_val == 3)
- time.week++;
- else
- time.week--;
- if (time.week >= 8)
- time.week = 1;
- else if (time.week < 1)
- time.week = 7;
- play_week(68, 0|0x80, time.week);
- }
- else if (state == 5)
- {
- if (key_val == 3)
- time.day++;
- else
- time.day--;
- if (time.day >= 32)
- time.day = 1;
- else if (time.day < 1)
- time.day = 31;
- play8_num(48, 0|0x80, time.day);
- }
- else if (state == 6)
- {
- if (key_val == 3)
- time.mon++;
- else
- time.mon--;
- if (time.mon >= 13)
- time.mon = 1;
- else if (time.mon < 1)
- time.mon = 12;
- play8_num(24, 0|0x80, time.mon);
- }
- else if (state == 7)
- {
- if (key_val == 3)
- time.year++;
- else
- time.year--;
- if (time.year >= 100)
- time.year = 0;
- else if (time.year < 0)
- time.year = 99;
- play8_num(0, 0|0x80, time.year);
- }
- else
- {
- break;
- }
- }
- if (state == 0)
- break;
- }
- }
- /*
- * 闹钟界面显示
- */
- void alarm_show(void)
- {
- int8 key_val, state=1;
- uint32 t=0;
- play16(0, 0, nao);
- play16(16, 0, zhong);
- play16(32, 0, maohao);
- if (Alarm_flag)
- play16(48, 0, kai);
- else
- play16(48, 0, guan);
- play32_num(32, 2, alarm.hour);
- play32(64, 2, 10);
- play32_num(80, 2, alarm.min);
-
- play16(0, 6, zheng);
- play16(16, 6, dian);
- play16(32, 6, bao);
- play16(48, 6, shi);
- play16(64, 6, maohao);
- if (Clock_flag)
- play16(80, 6, kai);
- else
- play16(80, 6, guan);
-
- for (t=0; t<30000; t++)
- {
- key_val = scan_key();
- if (key_val > 1)
- break;
- else if (key_val == 1)
- {
- if (Alarm_flag)
- play16(48, 0|0x80, kai);
- else
- play16(48, 0|0x80, guan);
-
- while (1)
- {
- key_val = scan_key();
-
- if (key_val == 1) // 完成设置
- {
- if (state >= 4)
- state = 0;
- else
- state++;
- if (Alarm_flag)
- play16(48, 0, kai);
- else
- play16(48, 0, guan);
- play32_num(32, 2, alarm.hour);
- play32_num(80, 2, alarm.min);
- if (Clock_flag)
- play16(80, 6, kai);
- else
- play16(80, 6, guan);
- switch (state)
- {
- case 1:
- if (Alarm_flag)
- play16(48, 0|0x80, kai);
- else
- play16(48, 0|0x80, guan);
- break;
- case 2:
- play32_num(80, 2|0x80, alarm.min);
- break;
- case 3:
- play32_num(32, 2|0x80, alarm.hour);
- break;
- case 4:
- if (Clock_flag)
- play16(80, 6|0x80, kai);
- else
- play16(80, 6|0x80, guan);
- break;
- default: break;
- }
- }
- else if (key_val > 1)
- {
- if (state == 1)
- {
- Alarm_flag = ~Alarm_flag;
- if (Alarm_flag)
- play16(48, 0|0x80, kai);
- else
- play16(48, 0|0x80, guan);
- }
- else if (state == 2)
- {
- if (key_val == 3)
- alarm.min++;
- else
- alarm.min--;
- if (alarm.min >= 60)
- alarm.min = 0;
- else if (alarm.min < 0)
- alarm.min = 59;
- play32_num(80, 2|0x80, alarm.min);
- }
- else if (state == 3)
- {
- if (key_val == 3)
- alarm.hour++;
- else
- alarm.hour--;
- if (alarm.hour >= 24)
- alarm.hour = 0;
- else if (alarm.hour < 0)
- alarm.hour = 23;
- play32_num(32, 2|0x80, alarm.hour);
- }
- else if (state == 4)
- {
- Clock_flag = ~Clock_flag;
- if (Clock_flag)
- play16(80, 6|0x80, kai);
- else
- play16(80, 6|0x80, guan);
- }
- else
- {
- break;
- }
- }
- if (state == 0)
- break;
- }
- if (state == 0)
- break;
- }
- }
- }
- main()
- {
- uint8 key_val;
- read_18B20(); //读温度
- Delay_nms(1000);//延时1S,等待18B20工作正常
- LCD_init(); //初始化液晶
- clear12864(); //清屏幕
- main_frame(); //显示主界面框架
- main_show(1); //刷新1次
- read_18B20(); //读温度
- play8_num(104, 6,zhengshu); //显示温度
- while(1)
- {
- key_val = scan_key();
- if (key_val == 1) //K1?
- {
- main_set();
- }
- else if (key_val == 2) //K2?
- {
- clear12864(); //清屏幕
- alarm_show();
- clear12864(); //清屏幕
- main_show(1);
- }
- else if (key_val == 3) //K3?
- {
- clear12864(); //清屏幕
- alarm_show();
- clear12864(); //清屏幕
- main_show(1);
- }
- else
- {
- read_time((uint8 *)&time);
- main_show(0);
- if((time.sec%2)==0){read_18B20();} //每隔2S采集一次
- }
-
- // 闹钟
- if (Alarm_flag)
- {
- if ((flag == 0) && (alarm.hour == time.hour) && (alarm.min == time.min))
- {
- flag = 1;
- clear12864(); //清屏幕
- alarm_show(); //闹钟
- PlayMusic(); //播放音乐
- PlayMusic(); //播放音乐
- clear12864(); //清屏幕
- main_show(1); //
- }
- }
- }
- }
复制代码
|