关于应广单片机读取数字传感器(PIR)

//本次试验用到的是(PMS150G)光敏检测(高电平触发)
#include "extern.h" #define pir pa.7 #define cds_io pa.6 #define LED3 pa.5 #define LED2 pa.4 #define LED1 pa.3 #define key_io pa.0 void UserFunction1(void); void main0(void); void main1(void); void main2(void); void pir_fun(void); void cds_fun(void); void delay_1s(void); //************************************************** //************************************************** // Define General Register .RAMADR 0 word fpp0_point; //延时 word tm16c; .RAMADR SYSTEM //用户变量添加 //位变量 //bit bitTest; word tim_num,tim_xs_num,tim_set_num,rember2,rember1; word delay_1s_num; byte mode_flag; //字节变量 //byte bTest; bit cds_flag,pir_flag,pir_off_flag,tim_lock,repeat_lock; //********************************************************************************************************** //===================================== IO口,定时器、中断配置 //********************************************************************************************************** void system_initial (void) { //************************ 定时器设置 ****************************** $ T16M ILRC /1 , BIT8; //时钟源IHRC,16分频,10位定时器 tm16c = 253; //1ms中断初值 此数值为计数器重装值,溢出值1024 stt16 tm16c; tm2ct=0x00; tm2b=0x00; tm2s=0b1_00_00001; tm2c=0b0100_10_0_0; //************************** IO_设置 ****************************** misc = 00000000b; pa = 00000000b; pac = 00111000b; paph = 00000001b; pa = 00000000b; PADIER = 0xC1; //************************** 中断 设置 ****************************** integs = 00000010b; intrq = 00000000b; inten = 01000100b; //开启中断 :T16,ADC LED2= 0; tim_set_num=20; // tim_xs_num=10; mode_flag=0; } void FPPA0 (void) { //--------------------------------------//power on delay 70ms // WatchDog Disable, RAM 0 ~ 0xF temporary be used // You can add the follow code : // CLKMD.En_WatchDog = 1; // WatchDog Enable .ADJUST_IC SYSCLK=ILRC , IHRC=16MHz, VDD=5V; fpp0_point = 10000; while(fpp0_point) { fpp0_point--; } //--------------------------------------//clear all of general register fpp0_point = _SYS(RAM_SIZE) - 1; do { *fpp0_point = 0; }while(-- fpp0_point$0); //--------------------------------------//if has more FPPAs than 2 #if _SYS(FPPA) != 1 pmode Program_Mode; #endif fpp0_point = 10000; while(fpp0_point) { fpp0_point--; } system_initial(); engint; //开启总中断 //.disgint;//关闭总中断 //==========================================================================================================// while (1) { NOP; UserFunction1(); } } void Interrupt (void) { pushaf; if (Intrq.T16) { tim_num++; if(tim_num==72) //1s定时 { tim_num=0; if(tim_lock==0) tim_xs_num++; } Intrq.T16 = 0; } popaf; } //==========================================================================================================// //=++++++++++++++++++++++++++++++++++++++++++++++use fun+++++++++++++++++++++++++++++++++++++++++++++++++++// //==========================================================================================================// void UserFunction1(void) { main1(); } //==========================================================================================================// //=++++++++++++++++++++++++++++++++++++++++++++++光敏 fun+++++++++++++++++++++++++++++++++++++++++++++++++++// //==========================================================================================================// void cds_fun() { if(cds_io==1) cds_flag=1; else { cds_flag=0; pir_flag=0; LED2=0; tm2c.3=0; tm2b=0xab; tim_xs_num=0; } } void pir_fun() { cds_fun(); if(pir_off_flag==0) { if((cds_flag==1)&&(pir==1)) { pir_flag=1; tim_xs_num=0; } else pir_flag=0; } } void repeat_fun() { if(pir==1) { tim_xs_num=0; } } //=++++++++++++++++++++++++++++++++++++++++++++++1 fun+++++++++++++++++++++++++++++++++++++++++++++++++++// void main1() { pir_fun(); if(pir_flag==1) { if(tim_xs_num<tim_set_num) { pir_off_flag=1; tim_lock=0; repeat_fun(); LED2=1; tm2c.3=1; tm2b=0x40; } elseif(tim_lock==0) { pir_off_flag=0; pir_flag=0; tim_xs_num=0; tim_lock=1; LED2=0; tm2c.3=0; tm2b=0x00; delay_1s(); } } else { LED2=0; tm2c.3=0; tm2b=0x00; LED1=0; pir_off_flag=0; pir_flag=0; tim_xs_num=0; } } //=++++++++++++++++++++++++++++++++++++++++++++++delay_1s fun+++++++++++++++++++++++++++++++++++++++++++++++++++// void delay_1s() { delay_1s_num=250; while(delay_1s_num--){NOP;} }

 

posted @ 2020-12-02 16:19  飘扬的狗尾草  阅读(714)  评论(0)    收藏  举报