Single chip infrared pyroelectric alarm

Publisher:MagicGardenLatest update time:2020-07-29 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include //Call the microcontroller header file
#define uchar unsigned char //Unsigned character type macro definition variable range 0~255
#define uint unsigned int //Unsigned integer type macro definition variable range 0~65535

//Infrared pyroelectric sensor is usually 0 and has an output of 1

sbit beep = P2^5; //Buzzer definition
sbit red = P0^1; //Red LED definition
sbit green = P0^4; //Green LED definition
sbit yellow = P0^7; //Yellow LED definition
sbit hw = P0^0; //Infrared pyroelectric sensor definition
bit flag_500ms = 0;
uchar flag_alarm ; //Alarm flag uchar
flag_bufang ; //Arm
flag uchar flag_bufang_en ; //Arm flag enable
uint flag_value; //Variable used as timer

/**************************1ms delay function********************************/
void delay_1ms(uint q)
{
        uint i,j;
        for(i=0;i                for(j=0;j<120;j++);
}

/********************Independent key program********************/
uchar key_can; //Key value

void key() //Independent key program
{
        static uchar key_new;
        key_can = 20; //Key value restoration
        P1 |= 0xff;
        if((P1 & 0xff) != 0xff) //Key pressed
        {
                delay_1ms(1); //Key debounce
                if(((P1 & 0xff) != 0xff) && (key_new == 1))
                { //Confirm that the key is pressed
                        key_new = 0;
                        switch(P1 & 0xff)
                        {
                                case 0xfb: key_can = 1; break; //Get key value
                                case 0xf7: key_can = 2; break; //Get key value
                                case 0x7f: key_can = 3; break; //Get the key value
                        }
                }                        
        }
        else //Release
                the keykey_new = 1;        
}

/******************Corresponding to different key processing**********************/           
void key_with()
{
        if(key_can == 1) //Key emergency alarm
        {
                flag_alarm = 1; //Alarm flag;                 
        }
        if(key_can == 2) //Arm button
        {
                flag_bufang_en = 1;        
        }
        if(key_can == 3) //Cancel alarm and clear variables
        {
                flag_alarm = 0;   
                flag_bufang = 0;
                flag_bufang_en = 0;        
                flag_value = 0;        
                beep = 1;
                red = 1; //Turn off the red light
                green = 1; //Turn off the green light
                yellow = 1; //Turn off the yellow light      

        }                        
}

/*************Timer 0 initialization program*******************/
void time_init()         
{
        EA = 1; //Open the total interrupt
        TMOD = 0X01; //Timer 0 working mode 1
        ET0 = 1; //Open timer 0 interrupt
        TR0 = 1; //Allow timer 0 timing
}

/******************Infrared alarm processing**********************/           
void hongwai_dis()
{
        if(flag_bufang_en == 1) //Prepare to start arming
        {
                green = ~green; //Green light flashes                        
        }  
        if(flag_bufang == 1) //Confirm arming
        {
                green = 0; //If the delayed arming is successful, the green light will be on
                if(hw == 1) //Infrared has output
                {
                        flag_alarm = 1;        
                }
        }
        if(flag_alarm == 1) //Alarm
        {
                red = ~red; //Red light alarm
                beep = ~beep; //Buzzer alarm
        }        
}


/******************Main program**********************/           
void main()
{
        time_init(); //Timer initialization program
        beep = 0; //Beep once when starting up  
        delay_1ms(200);
        beep = 1; //Buzzer off
        delay_1ms(200);
        beep = 0; //Beep again  
        delay_1ms(200);
        P0 = P1 = P2 = P3 = 0xff; //Initialize the MCU IO port to a high levelwhile
        (1)
        {
                key();                  
                yellow = ~hw; //The infrared pyroelectric indicator light turns yellow
                if there is outputif(key_can < 10)
                {
                        key_with(); //Key setting function
                }
                if(flag_500ms == 1)
                {
                        flag_500ms = 0;
                        hongwai_dis(); //Infrared alarm function
                }        
        }
}


/*************Timer 0 interrupt service program***************/
void time0_int() interrupt 1
{        
        static uint value;
        TH0 = 0x3c;
        TL0 = 0xb0; // 50ms         
        value ++;
        if(value % 10 == 0)
        {
                flag_500ms = 1;
        }
        if(flag_bufang_en == 1)
        {
                flag_value ++; // 400 * 50ms = 20000ms = 20 secondsif
                (flag_value >= 1200) //20 seconds
                {
                        flag_bufang = 1;
                        flag_bufang_en = 0;
                        flag_value = 0;        
                }
        }
}

Reference address:Single chip infrared pyroelectric alarm

Previous article:MCU drives EC11 encoder source program
Next article:AT89S52 MCU + LCD12864 Snake game (independent buttons)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号