电路原理图如下:
单片机源程序如下:
- #include "reg52.h"
- #include "intrins.h"
- #include "wifi.h"
- #include "uart.h"
- #include "keyboard.h"
- #define FOSC 11059200L
- #define T50MS (65536-FOSC/12/20) //1ms timer calculation method in 12T mode
- #define BAUD 9600 //UART baudrate
- #define pwm(m) CCAP0H = CCAP0L = m;
- #define un "tang"
- #define password "88888888"
- #define dc "DEV-002"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- WORD count;
- unsigned char falg_count;
- uchar xdata tt[200]; //定义空数组用于AD0809取平均值
- uchar FlagStart;
- int scale;
- uchar rsd_sec; //定义占空比比例,热释电计时秒变量
- uchar sec,flag_auto;
- uchar lum_mean;
- uchar appcmd=99;//单片机收到的APP信号
- bit bdata flag_rsd; //位定义自动切换的,闪烁标志,报警位标志,热释电动作标志,接近传感器标志
- bit flag_key;
- bit finish=0;
- sbit TEST_LED = P0^7;
- sbit LED_ALARM = P0^0;
- sbit rsd = P2^6;
- /*Declare SFR associated with the ADC */
- sfr ADC_CONTR = 0xBC; //ADC control register
- sfr ADC_RES = 0xBD; //ADC high 8-bit result register
- sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
- sfr P1ASF = 0x9D; //P1 secondary function control register
- /*Define ADC operation const for ADC_CONTR*/
- #define ADC_POWER 0x80 //ADC power control bit
- #define ADC_FLAG 0x10 //ADC complete flag
- #define ADC_START 0x08 //ADC start control bit
- #define ADC_SPEEDLL 0x00 //420 clocks
- #define ADC_SPEEDL 0x20 //280 clocks
- #define ADC_SPEEDH 0x40 //140 clocks
- #define ADC_SPEEDHH 0x60 //70 clocks
- /*Declare SFR associated with the PCA */
- sfr CCON = 0xD8; //PCA control register
- sbit CCF0 = CCON^0; //PCA module-0 interrupt flag
- sbit CCF1 = CCON^1; //PCA module-1 interrupt flag
- sbit CR = CCON^6; //PCA timer run control bit
- sbit CF = CCON^7; //PCA timer overflow flag
- sfr CMOD = 0xD9; //PCA mode register
- sfr CL = 0xE9; //PCA base timer LOW
- sfr CH = 0xF9; //PCA base timer HIGH
- sfr CCAPM0 = 0xDA; //PCA module-0 mode register
- sfr CCAP0L = 0xEA; //PCA module-0 capture register LOW
- sfr CCAP0H = 0xFA; //PCA module-0 capture register HIGH
- sfr CCAPM1 = 0xDB; //PCA module-1 mode register
- sfr CCAP1L = 0xEB; //PCA module-1 capture register LOW
- sfr CCAP1H = 0xFB; //PCA module-1 capture register HIGH
- sfr PCAPWM0 = 0xf2;
- sfr PCAPWM1 = 0xf3;
- void InitADC()
- {
- P1ASF = 0x04; //Open 8 channels ADC function
- ADC_RES = 0; //Clear previous result
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
- }
- BYTE GetADCResult(BYTE ch)
- {
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
- _nop_(); //Must wait before inquiry
- _nop_();
- _nop_();
- _nop_();
- while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
- ADC_CONTR &= ~ADC_FLAG; //Close ADC
- return ADC_RES; //Return ADC result
- }
- void work()
- {
- uint lum_all;
- uchar b,c;
- for(b=0;b<200;b++) //将空数组tt【】内数值整体左移一位
- {
- tt[b]=tt[b+1]; //将后一数值放到前一位置
- }
- tt[199] =GetADCResult(2); //将读出的ad数值放入tt【49】
- lum_all =0;
- for(c=0;c<200;c++) //将tt【】内数值相加
- {
- lum_all=lum_all+tt[c];
- }
- lum_mean=lum_all/200; //将总数/50取出平均值
- if(qiehuan==0) //自动切换按键按下
- {
- delay(10); //去抖
- if(qiehuan==0) //再次判断按键按下
- {
- LED_ALARM =0;
- flag_auto=!flag_auto; //自动模式标志位取反
- if(flag_auto==1) //当切换到手动模式时 首先将LED发光比例设置在50%
- scale=128;
- }
- while(!qiehuan); //按键释放 松开按键后关闭蜂鸣器
- LED_ALARM =1;
- }
- if(flag_auto==1)
- {
- if(add==0) //加键按下
- {
- delay(5);
- if(add==0)
- {
- // LED_ALARM =0;
- if(scale<=254)
- scale++; //灯光比例++
- if(scale>=255)
- scale=255;
- // if(add)
- // LED_ALARM =1;
- }
- }
- if(dec==0) //减键按下时
- {
- delay(5);
- if(dec==0)
- {
- // LED_ALARM =0;
- if(scale>255) scale=128;
- if(scale>=1)
- scale--; //灯光比例--
- if(scale<=0)
- scale=0;
- // if(dec)
- // LED_ALARM =1;
- }
- }
- }
- else
- {
- if(flag_rsd==1) //有人在范围内时
- {
-
- if(lum_mean<=90) scale=0; //判断取出平均值大小 小于30 发光强度0%
- else if(lum_mean>=190) scale=255; //大于150 发光强度100%
- else scale=(lum_mean-90)*2.55; //其他值时将其计算得到发光强度 (计算目的是为了得到一个1-41之间的数值 控制灯光变化)
- }
- else
- {
- scale=0; //没有人在范围内时 将灯光亮度调至0%
- }
- }
- pwm(scale);
- }
- void main()
- {
- unsigned char sendlum[3];
- unsigned char sendlev[3];
- CCON = 0; //Initial PCA control register
- //PCA timer stop running
- //Clear CF flag
- //Clear all module interrupt flag
- CL = 0; //Reset PCA base timer
- CH = 0;
- CMOD = 0x02; //Set PCA timer clock source as Fosc/2
- //Disable PCA timer overflow interrupt
- CCAP0H = CCAP0L = 0x00; //PWM0 port output 50% duty cycle square wave
- CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode and no PCA interrupt
- CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave
- PCAPWM1 = 0x03;
- CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
- CR = 1; //PCA timer start run
- SCON = 0x50; //8-bit variable UART
-
- TMOD = 0x21; //Set Timer1 as 8-bit auto reload mode
- TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
- TR1 = 1; //Timer1 start run
- ES = 1; //Enable UART interrupt
-
- TL0 = T50MS; //initial timer0 low byte
- TH0 = T50MS >> 8; //initial timer0 high byte
- TR0 = 1; //timer0 start running
- ET0 = 1; //enable timer0 interrupt
- EA = 1; //Open master interrupt switch
-
- count=0;
-
- flag_auto=1;
- scale=128;
- InitADC();
- LED_ALARM =0;
- initwifi();
- LED_ALARM =1;
- SendString("{\"cmd\":\"DR\",\"un\":\"");
- SendString(un);
- SendString("\",\"pwd\":\"");
- SendString(password);
- SendString("\",\"dc\":\"");
- SendString(dc);
- SendString("\"}");
- finish=1;
- while(1)
- {
- if(flag_auto==0)
- LED_ALARM = ~rsd;
- work();
- if(appcmd ==0) //自动
- {
- flag_auto =0;
- appcmd =99;
- }
- else if(appcmd==1) //切换到手动
- {
- flag_auto =1;
- scale =128; //50%
- appcmd =99;
- }
- else if(appcmd>1&&appcmd!=99) //1-7档 对应 2 3 4 5 6 7 8
- {
- scale = (appcmd-2)*36;
- if(appcmd==8)
- scale =255;
- appcmd =99;
- }
- if(FlagStart==1)
- {
- sendlum[0] =lum_mean%1000/100+0x30;
- sendlum[1] =lum_mean%100/10+0x30;
- sendlum[2] =lum_mean%10+0x30;
- sendlev[0] = scale%1000/100+0x30;
- sendlev[1] = scale%100/10+0x30;
- sendlev[2] = scale%10+0x30;
- FlagStart=2;
-
- SendString("{\"cmd\":\"DTP\",\"un\":\"");
- SendString(un);
- SendString("\",\"message\":{\"lum\":\"");
- SendASC(sendlum[0]);
- SendASC(sendlum[1]);
- SendASC(sendlum[2]);
- SendString("\",\"aut\":\"");
- SendASC(flag_auto+0x30);
- SendString("\",\"lev\":\"");
- SendASC(sendlev[0]);
- SendASC(sendlev[1]);
- SendASC(sendlev[2]);
- SendString("\"}}");
- falg_count=0;
- }
- if(FlagStart>=3)
- {
- FlagStart =0;
- SendString("{\"cmd\":\"DR\",\"un\":\"");
- SendString(un);
- SendString("\",\"pwd\":\"");
- SendString(password);
- SendString("\",\"dc\":\"");
- SendString(dc);
- SendString("\"}");
- delay(100);
- }
- }
- }
- void tm0_isr() interrupt 1 using 1
- {
- TL0 = T50MS; //reload timer0 low byte
- TH0 = T50MS >> 8; //reload timer0 high byte
- count++; //50ms m++
- if(count>=20) //到达1s时
- {
-
- falg_count++;
- if(falg_count >= 2)
- {
- falg_count =0;
- FlagStart++;
- }
- count=0; //m=0
- if(rsd==0) //热释电无信号时
- rsd_sec++; //热释电计时秒++
- if(rsd_sec<=15&&rsd==1) //热释电计时秒小于等于30 并且 热释电有信号时
- {
- rsd_sec=0; //将热释电秒清零
- flag_rsd=1; //标志位置1 控制AD采集数值 调节灯光亮度
- }
- else if(rsd_sec>15&&rsd==0) //热释电计时秒大于30 并且 热释电无信号时
- {
- flag_rsd=0; //标志位置0 停止ad转换 关闭灯光
- rsd_sec=0; //热释电计时秒清零
- }
- }
- }
- void Uart_Isr() interrupt 4 using 1
- {
- unsigned char dat;
-
- if (RI)
- {
- RI = 0; //Clear receive interrupt flag
- dat = SBUF;
- if(SBUF == '\r' ||SBUF == '\n')
- {
- RsPoint =0;
- }
- if(SBUF !='\r' && SBUF !='\n' && SBUF !=' ')
- {
- RsBuf[RsPoint]=SBUF;
- RsPoint++;
- }
- if(RsPoint>=60)
- RsPoint = 0;
- if(finish==1)
- {
- if(Flag_rec==1)
- {
- switch (Key_num)
- {
- case 'W': appcmd = dat-0x30; LED_ALARM =~LED_ALARM;FlagStart=1; break;
- case 'T': appcmd = dat-0x30+2 ;LED_ALARM =~LED_ALARM;FlagStart=1; break;
- }
- Flag_rec=0;
- }
- if(dat=='W'||dat=='T')
- {
- LED_ALARM =~LED_ALARM;
- Flag_rec=1;
- Key_num=dat;
- }
- }
- }
- }
复制代码
全部代码51hei下载地址:
代码.zip
(94.22 KB, 下载次数: 185)
|