#include
#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;
}
}
}
Previous article:MCU drives EC11 encoder source program
Next article:AT89S52 MCU + LCD12864 Snake game (independent buttons)
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Design of intelligent remote control based on MSP430F413
- GT911 touch screen chip datasheet
- MCU reset types and faults
- Here's a wave of benefits for everyone. Pay 10 yuan for postage and get a brand new power meter that can measure the power consumption of home appliances. While stocks last.
- Qorvo's customizable PMIC simplifies power management system design
- Can the digital tube screen be driven by the same driving method as the segment code LCD screen?
- Creative digital electronic clock using ESP8266
- Hardware Engineer (Shanghai)
- Three Clouds
- MSP430FR5969 Development Data