电路原理图如下:
WT588D -16语音模块+2个8P圆排座 2
DS1302时钟芯片+SIP8管座 2
32.768Khz晶振 2
11.0592M晶振 1
9*15万用板 5
USB电源线或供电电池盒 2
MQ-2烟雾检测 1
单片机源程序如下:
- #include <reg52.h>
- #include"LCD1602.h"//液晶
- #include" 1302.h" //DS1302时钟芯片
- #include "EepRom.h"
- #include"wt588.c" //语音播报
- #include "ADC0832.h"
- #define uchar unsigned char
- #define uint unsigned int
- //-------------------------------------------硬件定义
- sbit key1=P3^3;//按键
- sbit key2=P3^4;
- sbit key3=P3^5;
- sbit key4=P3^6;
- sbit key5=P3^7;
- //---------------------------------------> Dis variable
- bit s0=0; //闪烁
- //------------------------------》时间
- uchar state=0 ;
- ///----步进电机--------------------------------------------
- unsigned char code gaofz[]= //正转数据 高四位
- {0x10,0x30,0x20,0x60,0x40,0xC0,0x80,0x90};
- unsigned char code gaozz[]= //反转数据 高四位
- {0x90,0x80,0xC0,0x40,0x60,0x20,0x30,0x10};
- #define QuanShu 2050
- #define DelayS 1
- uchar FangXiang = 0; //电机转动方向 = 0表示关但是已经关上了所以不动
- unsigned char loop = 0;
- //---定时------------------------------------------
- uchar ding_shi[4] = {0};//定时小时存储
- uchar ding_fen[4] = {0};//定时分钟存储
- bit kai_guan1= 0; //开关定时标志位
- bit kai_guan2= 0;
- bit kai_guan3= 0;
- bit kai_guan4= 0;
- bit shou_dongF = 0;
- uchar i = 0;
- bit only_flag=0; //只允许播报一次
- //------------------------------------------------------------------------- MQ-2烟雾部分定义
- bit Read_Smok=0; //-- 读取浓度标志
- unsigned char AD_Dat=0;
- unsigned char Smok=0;
- unsigned char Smok_H=0; //-- 烟雾浓度及其阈值
- unsigned char Voice_Time=0; //-- 语音报警时间间隔
- //-------------------------------------------------------串口部分定义
- unsigned char Open=0; //-- 开启关闭状态
- bit Send_flag=0; //-- 串口发送标志
- unsigned char data Uart_Buff[50]=0;// 串口接收数据数组
- unsigned char data Uart_Count=0;
- unsigned char Call_Back=0;
- //---函数初始化------------------------------------------------
- void Timer0Init(void); //50毫秒@11.0592MHz
- void Read_Memory();//读存储
- void Key_Dispose(); //按键处理函数
- void Display(); //显示函数
- void BuJinDianJi() ; //步进电机驱动函数
- void Uart_dat();
- void Uart_datbyte();
- void Uart_Send_dat(); //-- 串口发送数据
- //********************************************************************************************
- void main() //主函数
- {
- Timer0Init();
- EA=1;
- LCD1602_cls(); //定时器初始化以及液晶初始化
- Read_Memory();//读存储
- while(1)
- {
- if(Send_flag)
- {
- Send_flag=0;
- Uart_Send_dat(); //-- 发送数据
- }
- if(Read_Smok)
- {
- Read_Smok = 0;
- AD_Dat= A_D(0);
- // if(AD_Dat<5) AD_Dat=5;
- // if(AD_Dat>250) AD_Dat=250;
- Smok = (AD_Dat-0)/2.55;
- }
- Display(); //显示函数
- if(!state)read_time1(); //读取时间函数
- if(FangXiang==0) only_flag=1;
- else if(FangXiang==1)
- {
- if(only_flag==1)
- {
- only_flag=0; //清零 只允许播报一次
- Send_threelines(0); //语音播报 吃饭了
- }
- }
- BuJinDianJi();
- if(Voice_Time==0 && Smok>Smok_H)
- {
- Voice_Time = 10;
- Send_threelines(1);
- }
- }
- }
- //==========================================延时函数
- void Delay(uint delayi) //短延时函数
- {
- while (delayi)
- {
- delayi--;
- time_data[0]=ds1302read(0x81);
- time_data_1[0]=time_data[0]/16*10+time_data[0]%16;
- time_data[1]=ds1302read(0x83);
- time_data_1[1]=time_data[1]/16*10+time_data[1]%16;
- LCD1602_write(0,0xC0+3);
- LCD1602_write(1,0x30+time_data_1[1]/10);
- LCD1602_write(1,0x30+time_data_1[1]%10);
- LCD1602_write(1,':');
- LCD1602_write(1,0x30+time_data_1[0]/10);
- LCD1602_write(1,0x30+time_data_1[0]%10);
- }
- }
- //=========================================================================================================== 串口发送数据函数
- void Uart_dat(unsigned char dat)//--------------------------发送单个字节
- {
- SBUF = dat;
- while(!TI);
- TI=0;
- }
- void Uart_datbyte(char *byte)//--------------------------发送字符
- {
- while(*byte!='\0')
- {
- Uart_dat(*byte++);
- }
- }
- void Uart_Send_dat()//--------------------------发送数据
- {
- Uart_datbyte("Data,");
- Uart_dat(0x30+Open%10); //-- 投食状态 0关闭 1开启 1
- Uart_datbyte(",");
-
- Uart_dat(0x30+Call_Back%10); //-- app 设置
- Uart_datbyte(",");
- Uart_dat(0x30+ding_shi[0]/10%10); //-- 定时时间 3
- Uart_dat(0x30+ding_shi[0]%10);
- Uart_datbyte(",");
- Uart_dat(0x30+ding_fen[0]/10%10); //4
- Uart_dat(0x30+ding_fen[0]%10);
- if(kai_guan1==0) Uart_datbyte(",0,");
- else Uart_datbyte(",1,"); //5
- Uart_dat(0x30+ding_shi[1]/10%10);
- Uart_dat(0x30+ding_shi[1]%10);
- Uart_datbyte(",");
- Uart_dat(0x30+ding_fen[1]/10%10);
- Uart_dat(0x30+ding_fen[1]%10);
- if(kai_guan2==0) Uart_datbyte(",0,");
- else Uart_datbyte(",1,");
- Uart_dat(0x30+ding_shi[2]/10%10);
- Uart_dat(0x30+ding_shi[2]%10);
- Uart_datbyte(",");
- Uart_dat(0x30+ding_fen[2]/10%10);
- Uart_dat(0x30+ding_fen[2]%10);
- if(kai_guan3==0) Uart_datbyte(",0,");
- else Uart_datbyte(",1,");
-
- Uart_dat(0x30+ding_shi[3]/10%10);
- Uart_dat(0x30+ding_shi[3]%10);
- Uart_datbyte(",");
- Uart_dat(0x30+ding_fen[3]/10%10);
- Uart_dat(0x30+ding_fen[3]%10);
- if(kai_guan4==0) Uart_datbyte(",0,");
- else Uart_datbyte(",1,");
- if(Smok>99) Uart_dat(0x30+Smok/100%10);
- else Uart_datbyte(" ");
- if(Smok>9) Uart_dat(0x30+Smok/10%10);
- else Uart_datbyte(" ");
- Uart_dat(0x30+Smok/1%10);
- Uart_datbyte(",");
- if(Smok_H>99) Uart_dat(0x30+Smok_H/100%10);
- else Uart_datbyte(" ");
- if(Smok_H>9) Uart_dat(0x30+Smok_H/10%10);
- else Uart_datbyte(" ");
- Uart_dat(0x30+Smok_H/1%10);
- Uart_datbyte(",");
- if(Smok>Smok_H) Uart_datbyte("0,");
- else Uart_datbyte("1,");
- Uart_datbyte("\r\n");
-
- }
- //===============================================步进电机
- bit jishi_J = 0;
- void BuJinDianJi() //步进电机驱动函数
- {
- static uint bujin1i = 0 ;
- if(FangXiang==0)
- {
- P1&=0x0F;
- Open = 0;
- }
- else if(FangXiang==1)
- {
- for(; bujin1i<QuanShu; bujin1i++)
- {
- P1&=0x0F; //步进电机用到的四个口清零
- P1|=gaozz[loop]; //给电机的四个口中有1为1,其他口数值不变
- loop=(loop+1)%8; //循环加,Loop变化范围是 0 - 7,这样结合上一句话就可循环将转动数据送给P2
- Delay(DelayS);//; //短延时一下,调节这个短延时,可以控制步进电机的转动速度,越小就越快
- }
- jishi_J = 1;
- Open=1;
- }
- else if(FangXiang==2)
- {
- for( ; bujin1i>0; bujin1i--) //
- {
- P1&=0x0F; //步进电机用到的四个口清零
- P1|=gaofz[loop]; //给电机的四个口中有1为1,其他口数值不变
- loop=(loop+1)%8; //循环加,Loop变化范围是 0 - 7,这样结合上一句话就可循环将转动数据送给P2
- Delay(DelayS);//; //短延时一下,调节这个短延时,可以控制步进电机的转动速度,越小就越快
- }
- FangXiang=0;
- Open = 0;
- }
- }
- //====================================================
- void Timer0Init(void) //50毫秒@11.0592MHz
- {
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0x00; //设置定时初值
- TH0 = 0x4C; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0=1;
- SCON = 0X50;
- T2CON = 0X34;
- RCAP2H=TH2=0XFF;
- RCAP2L=TL2=0XDC;
- TR2 = 1;
- ES = 1;
- }
- //======存储==================================================
- void memory_Money() //存储
- {
- SectorErase(0x2000); //第一扇区擦除
- SectorErase(0x2200);
- for(i = 0; i<4;i++)
- {
- byte_write(0x2000+i,ding_shi[i]);
- byte_write(0x2010+i,ding_fen[i]);
-
- }
- byte_write(0x2200,Smok_H);
- }
- void Read_Memory()//读存储
- {
- Smok_H = byte_read(0x2200);
- if(Smok_H>100) Smok_H = 60;
- for(i = 0; i<4;i++)
- {
- ding_shi[i] = byte_read(0x2000+i);
- ding_fen[i] = byte_read(0x2010+i);
- if(ding_shi[i]>23||ding_fen[i]>59)
- {
- for(i = 0; i<4; i ++)
- {
- ding_shi[i] = 10;
- ding_fen[i] = 30;
- }
-
- }
-
- }
- }
- //=============================================================================================================== 按键处理函数
- bit keyF = 0;
- void Key_Dispose()
- {
- if(!key1) //判断按键按下
- {
- if(keyF)
- {
- keyF = 0;
- state=(state+1)%16; //变量累加,加到 7 的时候会变成 0 ,
- if(!state)
- {
- memory_Money(); //存储
- Write();
- }
- }
- } else
- if(!key2) //按键 2
- {
- if(keyF)
- {
- keyF = 0;
- switch(state)
- {
- case 0:
- break; //在状态 0 ,也就是显示非设置,按键按下没有反应
- case 1:
- if(time_data_1[5]<99) time_data_1[5]++;else time_data_1[5] = 0;
- break; //等于 1 ,也就是此时设置年,数据只要小于99就加一
- case 2:
- if(time_data_1[4]<12) time_data_1[4]++; else time_data_1[4] = 1;
- break; //然后是月
- case 3:
- if(time_data_1[4]==1||time_data_1[4]==3||time_data_1[4]==5||time_data_1[4]==7||time_data_1[4]==8||time_data_1[4]==10||time_data_1[4]==12)
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei附件下载:
资料.7z
(1.25 MB, 下载次数: 136)
|