51单片机超级闹钟程序设计(有图)
时间:10-24
来源:互联网
点击:
/*课程设计终于弄完了,总想共享一下源代码,*/
/*遂在51hei上贴出来,大家看有什么问题吧~如果哪里写得不好还请众亲们指出哦~*/
/*---附:实物图--*/
/*===Chip:STC12C5A32S2======================*//*===Software:Keil 4 C51========================*//*===Author:梁鹏===================================*//*===Organization:广西民族大学07自动化===========*//*===Date:2010年05月26日======================*//*===Version:1.0===============================*//*===声明:本程序属原创作品,仅供学习交流,不得用于商业用途,如需转载,请注明出处,谢谢合作!======*//*--------文件名:Supper_clock.C-----------*/#include#include #include #include #include #include#include #define KEY_PORT P3 //按键端口sbit MODE_KEY=KEY_PORT^3; //模式选择键sbit HOU_KEY=KEY_PORT^4; //时增一键sbit MIN_KEY=KEY_PORT^5; //分增一键sbit CLKON_KEY=KEY_PORT^6; //闹钟ON/OFF控制键sbit BUZ_OUT=P2^3; //蜂鸣器输出口sbit DS18B20_PORT=P2^2; //DS18B20数据接口sbit LAMP_PORT=P2^1; //继电器控制口 /*=========子函数的声明==============*/void init(); //系统初始化void display_time(); //显示时间的子函数void display_clk(); //显示闹钟的子函数void judge(); //判断秒增量是否大于限定值void clk_judge(); //检测是否到达闹铃时间void beep(); //蜂鸣器响子函数void buzzer(); //闹铃响子函数void get_adc_data(); //AD采样数据及处理子函数void serial_judge(); //串口数据处理子函数//void delay_ms(uint); //MS级延时子程序/*==========变量的声明===============*/bit pause_flags=0,clk_flags=0,clk_on=0,clk_on_or_off=1,flash_flags=0;bit clk1_off=0,clk2_off=0,clk3_off=1; //各闹钟是否开启的变量bit serial_finish=0; //串口接收结束标志位uchar com_send,com_get; //串口通信相关变量uchar hour,min,sec; //时间变量uchar dis_hour_clk,dis_min_clk; //用于显示闹钟时间的变量uchar hour_clk1,min_clk1; //闹钟1有关变量uchar hour_clk2,min_clk2; //闹钟2有关变量uchar hour_clk3,min_clk3; //闹钟3有关变量uchar mode_flags; //状态标志位uint ovf_cnt=0; //定时中断次数纪录uchar first_on_flags=0; //掉电保存变量uint ad_data_channal7; //ADC采样数据1uint ad_data_channal6; //ADC采样数据2float vol_data,vol_data1; //采样电压值转换uchar send_temp[10],send_i_cnt; //发送缓冲/*===================================*//*************************************//*************************************/void main(){ init(); //程序初始化LCD_init(); //1602初始化 init_24c02(); //24C02掉电保存初始化serial_port_init(); //串行通信初始化first_on_flags=read_add_24c02(2);beep();if(first_on_flags!=33){ //首次上电,写首次上电标记write_add_24c02(2,33); }else{hour =read_add_24c02(3);min =read_add_24c02(4);hour_clk1 =read_add_24c02(5);min_clk1 =read_add_24c02(6);hour_clk2 =read_add_24c02(7);min_clk2 =read_add_24c02(8);hour_clk3 =read_add_24c02(9);min_clk3 =read_add_24c02(10);clk1_off =read_add_24c02(11);clk2_off =read_add_24c02(12);clk3_off =read_add_24c02(13);}while(1){get_adc_data(); //获取AD值display_time(); //时间显示judge();}}/*************************************//*************************************/void init(){ hour=6;min=29;sec=55; //初始化系统时间hour_clk1=6;min_clk1=30; //初始化系统时钟hour_clk2=6;min_clk2=31;hour_clk3=6;min_clk3=32;AURX1|=0x04; //AD转换结果存储格式控制 P1ASF=17; //模拟通道选择P1ASF=16;BUZ_OUT=1; //蜂鸣器不响LAMP_PORT=0; //继电器不吸合P0=255; //LCD数据口状态初始化TCON|=0x01; //INT0下降沿触发EX0=1; //外部中断0中断允许 INT0=1; //P3.2置位TMOD|=0x02; //定时器0工作在方式1TH0=5; //250us计数初值高八位TL0=5; //250us计数初值低八位ET0=1; //定时器0中断允许EA=1; //全局中断允许TR0=1; //定时器0开启}/**************************/void display_time(){Locate(1,1);if((mode_flags==1)flash_flags) LCD_str_(" ");else LCD_str_("Time: "); //调整模式下,“Time:”闪烁LCD_2char(hour); //显示时if(flash_flags(!pause_flags)) LCD_char(' ');else LCD_char(':'); //非暂停模式下,“:”闪烁LCD_2char(min); //显示分if(flash_flags(!pause_flags)) LCD_char(' ');else LCD_char(':');LCD_2char(sec); //显示秒LCD_str_(" ");if(mode_flags>0){display_clk();} //暂停时,显示闹钟 else{Locate(2,1);LCD_float_(vol_data,2);LCD_str_("V ");LCD_4char(ad_data_channal7);LCD_str_(" ");Locate(2,13);if(!clk_on_or_off)LCD_str_(" ON"); //全局显示闹钟的ON/OFFelse LCD_str_(" OFF");} }void display_clk(){Locate(2,1);if(flash_flags(mode_flags>1))LCD_str_(" ");//调整模式下,“Clkx:”闪烁else{ if(mode_flags>0mode_flags3){LCD_str_("Clk1: ");dis_hour_clk=hour_clk1;dis_min_clk=min_clk1;}if(mode_flags==3){LCD_str_("Clk2: ");dis_hour_clk=hour_clk2;dis_min_clk=min_clk2;}if(mode_flags==4){LCD_str_("Clk3: ");dis_hour_clk=hour_clk3;dis_min_clk=min_clk3;}}//根据不同的状态,显示不同的闹钟时间LCD_2char(dis_hour_clk);LCD_char(':');LCD_2char(dis_min_clk);LCD_str_(" ");Locate(2,13);if(mode_flags2){ //调整时间的模式下,全局显示闹钟的开或者关if(!clk_on_or_off)LCD_str_(" ON");else LCD_str_(" OFF");}/*根据不同调闹和开关状态显示指定闹钟的开关*/ if( (mode_flags==2!clk1_off)||(mode_flags==3!clk2_off)||(mode_flags==4!clk3_off)) LCD_str_(" ON"); else LCD_str_(" OFF");}/**************************/void judge(){if(sec>=60){ //秒增量是否超过限定的判断sec=0;if(++min>=60){min=0;if(++hour>=24)hour=0;}}if(mode_flags1) {pause_flags=0;} //是否暂停的判断else {pause_flags=1;}/*以下为时限判断*/if(hour>23)hour=0; //调整时增量是否超限的判断if(min>59)min=0;if(hour_clk1>23) hour_clk1=0;if(min_clk1>59) min_clk1=0;if(hour_clk2>23) hour_clk2=0;if(min_clk2>59) min_clk2=0;if(hour_clk3>23) hour_clk3=0;if(min_clk3>59) min_clk3=0;/*以上为时限判断*/ /*闹铃是否开启判断*/if(!clk1_off||!clk2_off||!clk3_off) clk_on_or_off=0;else clk_on_or_off=1;/*蜂鸣器是否响判断*/if(!pause_flags!clk_on_or_off){clk_judge();}
//非暂停模式下,闹钟打开情况下,检测闹钟if (!clk_flags) //使用clk_flags和clk_on检测是否闹铃{ if(((hour!=hour_clk1)||(min!=min_clk1))((hour!=hour_clk2)||(min!=min_clk2))((hour!=hour_clk3)||(min!=min_clk3)))clk_flags=1;clk_on=0;BUZ_OUT=1;}else{ if(clk_on){buzzer();BUZ_OUT=1;}} //满足闹铃响所需的所有条件,发出闹铃声/*以下为EEPROM操作*/if(sec==5){//一分钟存一次write_add_24c02(3,hour); LCD_delay(100); write_add_24c02(4,min); LCD_delay(100);write_add_24c02(5,hour_clk1);LCD_delay(100);write_add_24c02(6,min_clk1);LCD_delay(100);write_add_24c02(7,hour_clk2);LCD_delay(100);write_add_24c02(8,min_clk2);LCD_delay(100);write_add_24c02(9,hour_clk3);LCD_delay(100);write_add_24c02(10,min_clk3);LCD_delay(100);write_add_24c02(11,clk1_off);LCD_delay(100);write_add_24c02(12,clk2_off);LCD_delay(100);write_add_24c02(13,clk3_off);LCD_delay(100);}/*以下为串口接收完成处理*/if(serial_finish){serial_judge(); //串口数据处理serial_finish=0; //清0接收结束标志}}void clk_judge() {if(((hour==hour_clk1)(min==min_clk1)!clk1_off)||((hour==hour_clk2)(min==min_clk2)!clk2_off)||((hour==hour_clk3)(min==min_clk3)!clk3_off))clk_on=1;else{clk_on=0;}}void beep(){uchar beep_cnt;for(beep_cnt=0;beep_cnt60;beep_cnt++){BUZ_OUT=0; LCD_delay(2);BUZ_OUT=1; //确保每次闹铃结束后蜂鸣器不响LCD_delay(1); }}void buzzer(){uchar i;for(i=0;i2;i++){beep();LCD_delay(500);}}/**************************/void INT0_isr() interrupt 0{LCD_delay(30);if(!INT0){beep();if(!MODE_KEY){ /*MODE_KEY*/if(++mode_flags>=5){pause_flags=0;mode_flags=0;flash_flags=0; }} if(!HOU_KEY){ /*HOU_KEY*/if(mode_flags==1){++hour;} if(mode_flags==2){++hour_clk1;} if(mode_flags==3){++hour_clk2;}if(mode_flags==4){++hour_clk3;}}if(!MIN_KEY){ /*MIN_KEY*/if(mode_flags==1){++min;} if(mode_flags==2){++min_clk1;} if(mode_flags==3){++min_clk2;}if(mode_flags==4){++min_clk3;}}if(!CLKON_KEY){if(mode_flags==2)clk1_off=~clk1_off;if(mode_flags==3)clk2_off=~clk2_off;if(mode_flags==4)clk3_off=~clk3_off;}clk_flags=0; /*闹钟发生时,按下任意键停止*/}INT0=1;}//================================void timer0_ovf_isr() interrupt 1{if(ovf_cnt==1846)flash_flags=~flash_flags;if(++ovf_cnt==3691){ //12MHZ自动重载4000次为1s,11.071692HZ下为3691ovf_cnt=0;if(!pause_flags)sec++; //非暂停模式下,秒增一flash_flags=~flash_flags;} //每间隔半秒flash_flags取反一次 }//================================void receive_uart() interrupt 4{if(RI){RI=0;com_get=SBUF;serial_finish=1;}else{TI=0;}}//================================ void serial_judge(){switch(com_get){case 0x31:Uart_send_int(hour,2);Uart_send_(':');Uart_send_int(min,2);Uart_send_(':');Uart_send_int(sec,2);Uart_send(" ");break;case 0x32:Uart_send("The brightness:");Uart_send_flt(vol_data1,3);Uart_send("V ");break;case 0x33:Uart_send("The voltage:");Uart_send_flt(vol_data,3);Uart_send("V ");break;case 0x34:LAMP_PORT=!LAMP_PORT;break;default :Uart_send("Command Error! ");Uart_send("1->To get the system time. ");Uart_send("2->To get the brightness. ");Uart_send("3->To get the voltage. ");Uart_send("4->To control the 220V light ON/OFF. ");}com_get=0;}//================================void get_adc_data(){ad_data_channal7=ADC_GET(7);LCD_delay(1000);ad_data_channal6=ADC_GET(6);LCD_delay(1000);vol_data=(float)ad_data_channal6/1024;vol_data*=5.0;vol_data1=(float)ad_data_channal7/1024;vol_data1*=5.0;}//================================/*void delay_ms(uint xms){uint i,j;for(i=0;i xms;i++)for(j=0;j 990;j++);}*///================================
- 关于RTX51 TINY的分析与探讨(05-30)
- 浅析8051模块化编程技巧(05-28)
- 基于DSP和单片机通信的液晶显示设计方案(07-20)
- 锁相环控制及初始化简析(08-27)
- 基于MSP430自动胀管控制器的研究(09-07)
- 嵌入式C实现延时程序的不同变量的区别(03-01)