Single chip microcomputer detection AC power failure program (digital segment switch)

Publisher:文江桂青Latest update time:2015-05-12 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
There is a controller called digital segment switch in lighting accessories. The basic working principle is to use the wall switch to turn on and off the power to realize the turn-on and turn-off of multiple loads. This involves how the single-chip microcomputer detects the loss of AC power. First, the synchronization signal of the AC power must be extracted to form a low-voltage signal that can be recognized by the single-chip microcomputer. There are generally two methods. One is to directly use the resistor voltage divider method to extract the synchronization signal (suitable for non-isolated circuits). The other method is to use an optocoupler to extract the isolated AC signal. The two methods are as follows:


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.

Reference address:Single chip microcomputer detection AC power failure program (digital segment switch)

Previous article:STM8S input capture
Next article:BCD code adjustment skills in single chip microcomputer development

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号