运用C51单片机制作一个极简易的防盗系统。用到的硬件有:振动传感器模块 、继电器模块 、高功率喇叭 、433M无线发射接收模块 、还有杜邦线。
#include "reg52.h"
#include "intrins.h"
#define Alarm_ON 1
#define Alarm_OFF 0
sbit switcher = P1^0;
sbit D0_ON = P1^1;
sbit D1_OFF = P1^2;
sbit vibrate = P1^3;void Delay1000ms() //@11.0592MHz
{unsigned char i, j, k;_nop_();i = 8;j = 1;k = 243;do{do{while (--k);} while (--j);} while (--i);
}void Delay500ms() //@11.0592MHz
{unsigned char i, j, k;_nop_();i = 4;j = 129;k = 119;do{do{while (--k);} while (--j);} while (--i);
}void Delay50ms() //@11.0592MHz
{unsigned char i, j;i = 90;j = 163;do{while (--j);} while (--i);
}void main()
{while(1){ int mark;//if(D0_ON == 1)//{switcher = 0;Delay1000ms();switcher = 1;mark=Alarm_ON;}if(D1_OFF == 1){switcher = 0;Delay500ms();switcher = 1;mark=Alarm_OFF;}if(mark == Alarm_ON){if(vibrate == 0){Delay50ms();if(vibrate == 0){switcher = 0;}else{switcher = 1;}}}}
}
烧在单片机的代码如上图所示,剩下的就是实物接线。