Optocoupler isolation sampling circuit
Resistor voltage divider sampling circuit
The AC signal obtained is as follows:
The function implemented by this program is that when the switch is turned on for the first time, L1 turns on and L2 turns off. When the switch is turned on for the second time, L1 turns off and L2 turns on. When the switch is turned on for the third time, L1 and L2 turn on. When the switch is turned on for the fourth time, L1 and L2 turn off, and so on. Then, the single-chip microcomputer detects AC power failure and detects the AC signal input port once at a certain interval. If it is a low level, start timing. If it is still a low level after 12MS-15MS, it means that the AC power has been cut off once, and corresponding control actions must be taken at this time. The single-chip microcomputer used is PIC16F676, and the pin on RA5 is used as the AC detection pin. RC2 and RC3 are used as load output control terminals. The program is as follows:
#include
__CONFIG(0X1B4);
#define uchar unsigned char //macro definition, equivalent to uchar=unsigned char
#define uint unsigned int //macro definition, equivalent to uint=unsigned int
[page]
uint key2_time_cnt; //key debounce counter
uchar key2_lock; //key valid flag
uchar key2_num_s; //function transfer variable
uchar key2_touch_s_flag; //AC power off signal valid flag
uint shuzi; //time stamp count variable
uchar SYS1MS; //time stamp switch flag
void PORT_init()
{
ANSEL=0X00;//Ordinary port settings
CMCON=0X07;//Comparator off
TRISA=0b00110100;
TRISC=0b00110000;
PORTC=0X00;
PORTA=0X00;
T0CS=0;//Timer internal clock selection
;
T0IF=0;//Timer interrupt flag clear
T0IE=1;//Timer interrupt enable
GIE=1;//Global interrupt enable
TMR0=9;//Timer initial value is zero
}
void key_scan2()
{
if(RA5==1)//If the button on RA5 is not pressed
{
key2_lock=0;//Key 2 flag is cleared
key2_time_cnt=0;//Debounce counter is cleared
if(key2_touch_s_flag==1)//If the AC power-off signal is valid
{
key2_touch_s_flag=0;//Clear AC signal flag
key2_num_s++;//Function number starts to increase
if(key2_num_s>3) key2_num_s=0;//Total 4 functions
}
}
else if(key2_lock==0)//If a key is pressed for the first time
{
key2_time_cnt++;//Debounce counter starts counting
if(key2_time_cnt>149)//When the counter value is greater than 149, it means that the low level lasts for more than 10MS, and the duration of the high and low levels of the AC power is 8-10MS, which means that the AC power is cut off.
{
key2_touch_s_flag=1;//AC power off is effective, the flag is set to 1.
key2_time_cnt=0;//Clear debounce counter
key2_lock=1;//Self-locking flag is set to 1 to avoid continuous triggering
}
}
}
void key_service2()
{
switch(key2_num_s)
{
case 0:
RC2=1;
RC3=0;
RA0=0;
RA1=0;
//key2_num_s=0;
break;
case 1:
RC2=0;
RC3=1;
RA0=0;
RA1=0 ;
//key2_num_s=0;
break;
case 2:
RC2=1;
RC3=1;
RA0=0;
RA1=0;
// key2_num_s=0;
break;
case 3:
RC2=0;
RC3=0;
RA0=0 ;
RA1=0;
//key2_num_s=0;
break;
}
}[page]
void main()
{
PORT_init();
while(1)
{
if(SYS1MS==1)
{
SYS1MS=0;
key_scan2();
}
key_service2();
}
}
void interrupt isr()//Timer 0 interrupt function
{
if((T0IE&T0IF)==1)//Judge timer interrupt
{
T0IF=0;//Clear timer interrupt flag
T0IE=0;//Turn off timer interrupt
TMR0=9;//The time taken to add from 9 to 256 is 250US
shuzi++;
if(shuzi==4)
{
shuzi=0;
SYS1MS=1;
}
T0IE=1;//Turn on timer interrupt
}
}
This program uses
if()
{
first area
}
else if()
{
second area
} for power failure detection
At the beginning, in the high level stage of the signal, in the first zone, various flags should be cleared, and at the same time, it is necessary to determine whether the power-off flag is valid. The purpose of this is to eliminate the malfunction (flash) of the single chip when the AC power is off. If it is used as a general key detection, a judgment is made here, and the corresponding key is effectively processed. In fact, it is to wait for the key to be released before making effective processing. When a low level arrives, in fact, the statement else if (key2_lock==0) is equivalent to else if ((!RA5) && (!key2_lock)), RA5 is low, and the key flag is not set to 1. It is determined that the above two conditions are met at the same time, indicating that the RA5 port is at least low level, but not necessarily power off. In the second zone, then let the de-bounce counter key2_time_cnt self-increment. When the counter is greater than 149, it is considered that the power is off (because the low level of AC power lasts for about 100 in a cycle, so the low level judgment range should be appropriately widened.) At this time, the power-off valid flag key2_touch_s_flag should be set to 1 immediately.
As for the position of the AC power failure detection function key2_scan() in the entire program, first, it can be directly placed in the timer interrupt and executed every 250US. Second, a timer can be used to set a time scale, such as a 1MS time scale, and the key2_scan() function can be executed in the main program according to the time scale. It is best not to put it directly in the main loop, otherwise a certain gear of the key detection will be missed.
The above is the basic control program of the digital segmenter. The single-chip AC detection is the difficulty of this program.
Previous article:STM8S input capture
Next article:BCD code adjustment skills in single chip microcomputer development
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Two-input AND gate engineering files and test files
- Help others and achieve self-realization——EEWORLD Q&A List (11th issue)
- ST Motor Review_bygyp1
- Problem of assigning initial value to timer of C51 MCU
- UPS-IPGuard Pro P1902 front panel device wiring diagram:
- 2012 Competition Award-winning Papers, Source Code, PCB (Part 5)
- DCDC Chip
- Seven tips for embedded development
- Design and implementation of 3D wireless RF mouse based on single chip microcomputer and touch control module
- Is anyone interested in this building automation sensor module?