Power Timing Socket Made with PIC Microcontroller

Publisher:创新思绪Latest update time:2012-09-19 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This power timing socket has three operation buttons S1, S2, S3, and uses two LED digital tubes to display the timing time. The two buttons S2 and S3 are used to preset and set the timing time values ​​of the ones and tens digits respectively. The setting time adjustment range is 0 ~ 99 minutes. The S1 button is used as the "start/stop" operation button of the timing socket.

A buzzer prompt sound part is added to the program. The function of this part of the program is: every time the S1, S2 or S3 button is pressed, the buzzer will emit a "beep" sound to confirm the validity of the button pressing.

The timing program works in a countdown manner. When the program is running, the unit digit display value of the digital tube decreases by "1" every minute, and the tens digit display value of the digital tube decreases by "1" every ten minutes.

When the preset timing time is reached, the socket automatically cuts off the AC220V power supply output and the buzzer alarms. The buzzer alarm can only be stopped by pressing the "Start/Stop" button S1 again. At the same time, the two-digit digital tube displays the last preset timing time again. The program also returns to the initial state and the timing time for the next timing socket to output AC220V voltage supply can be preset at any time.

The hardware part of the circuit is shown in Figure 1.

Figure 1 Circuit hardware part
Figure 1 Circuit hardware part

Power supply circuit: AC220V AC mains is input into this timing socket from the three-pin plug at the lower left end of the circuit diagram 1, where the "live wire" L is divided into two paths and added to the fixed contact terminal ④ of the control voltage output relay J1 and the fuse tube of the primary winding of the power transformer T1, the "neutral wire" N terminal is added to the N terminal of the output socket JP1 and the other end of the power transformer T1, the "ground wire" E terminal is connected to the ground terminal E of the output socket, and the "live wire terminal" L of the output socket JP1 is controlled by the moving contact terminal ① of the relay J1. The AC220V AC mains is converted into AC12V voltage by the T1 power transformer and then added to the two bridge arms of the bridge rectifier circuit composed of D2~D5, and then filtered by C1, stabilized by MC7805, and filtered by C3 and C2 with high and low frequencies to obtain a stable DC5V DC voltage as the working power supply of the whole machine.

The main control MCU chip of this power timing socket is PIC16F627 microcontroller. One of the advantages of PIC16F series microcontrollers is:

The port driving current is large, and the source current or sink current of each port is greater than 20mA. In this way, its port driving capability can directly drive the LED digital tube without adding driving transistors, making the hardware circuit design of the socket very simple.

PIC16F627 microcontroller port settings:

The RA of the microcontroller has 6 I/O ports, represented by RA<5:0>. In this application, RA0-RA2 are set as output ports; among them, RA0 and RA1 ports are used for the "bit code" driving of the "unit" and "tens" of the two digital tubes respectively, and RA2 is used to control the operation of the b pole of the transistor Q3, and also control the closing or not of the contact of the relay J1, and then control the existence of the AC220V output voltage of the timing socket JP1. The RA3 port is set to the input/output time-sharing dual-use working state: ① When used as an input port: used as the "unit timing time preset adjustment key" of the timing socket; ② When used as an output port: used to drive the LED1 light-emitting diode as the working indicator of this timing socket. When the timing socket does not enter the timing output power supply, RA3 is used as an input port to maintain the setting function of the unit digital preset number used to set the timer; when the socket is working and outputting voltage, RA3 is converted to an output port to drive LED1 as the working indicator of the socket.

RA4 port is set as input port, used for the preset adjustment key of the ten-digit value of the timing time. RA5 port is an input port, and the button switch S1 connected to it is the "start/alarm release button".

RB port setting: The RB port of PIC16F627 in the circuit is set as an output port. Among them, RB0-RB6 ports are used to drive the "ag" 7 "segment codes" of the LED digital tube. Therefore, the LG5621BH (red) common anode LED digital tube is selected here. Therefore, only when the RB0-RB6 port outputs "low level", the "ag" code segment corresponding to the LED digital tube can be lit. RB7 port is used to drive the active buzzer. When the RB7 port outputs a high level, the high level voltage is added to the b pole of the transistor Q4 through the 1k resistor R9, Q4 is turned on, and the buzzer sounds an alarm.

Power timing socket function: When the timing socket is initially powered on, the two-digit digital tube display value is: "99", and it is in a standby state of stopping time, and its output jack JP1 has no voltage output. If the single-digit timing time value preset adjustment key S3 is pressed at this time, the single-digit digital tube will decrease by "1" from the current display value. For example, after pressing S3 for the first time and releasing the key, the single-digit digital tube display value changes from "9" to "8", and pressing S3 again changes the display value to "7", ... until the display value changes to "0", and then from "0" back to "9" in a reciprocating cycle. [page]

The function of the tens-digit timing preset adjustment button S2 is the same as S3, except that each time S2 is pressed and released, the tens-digit digital tube display value decreases by "1". Similarly, the display value is also displayed in a reciprocating cycle from 9 → 8 → 7 →…→ 0 → 9. S1 is the start/alarm release button.

When the S1 button is pressed for the first time, the timing socket enters the working state:

The RA2 port outputs a high level, which is added to the base b of the transistor Q3 through the 1K current-limiting resistor R8. Q3 is turned on, the relay J1 is energized, and the contacts ① and ④ of J1 are closed. The "live wire" L of the AC220V AC mains is added to the L port of the output JP1 of this socket through the fixed and moving contacts ④ and ① of the relay, so that the JP1 socket outputs AC220V AC voltage. At the same time, the AC voltage output indicator LED2 is lit, indicating that the socket has AC220V voltage output.

At the same time, the timing time starts counting down and decreases. The timing time is minutes, and the timing duration starts from the current display value of the digital tube and ends when it displays 00. During the countdown time, the LED1 working indicator starts to flash on and off at a frequency of 2S, and the number of "1" is reduced for every minute; the number of "1" is reduced for every 10 minutes. When the digital tube display value is "00", after another 60 seconds, the output jack JP1 is powered off, and the AC220V output voltage indicator LED2 is extinguished at the same time; the buzzer starts to alarm.

During the buzzer alarm, the digital tube always displays "00" and the LED1 working indicator stops flashing and remains on. The buzzer will not stop alarming until the S1 button is pressed again; the digital tube display value jumps back to the value of the last preset timing time, and LED1 turns off.

Within the countdown time range, the S1 ~ S3 button functions are shielded, invalid, and cannot be enabled.

Program design points: The program design includes PIC16F627 initialization program, main program, 1-minute timer interrupt subroutine, buzzer prompt sound subroutine, digital table query subroutine, etc.

In this program design, the flashing speed of LED1 should not be set too fast or too slow. If the flashing speed of LED1 is too fast, it will make people feel too "busy" and feel nervous. If it flashes too slowly, it will be filtered by C1, stabilized by MC7805, and filtered by high and low frequencies of C3 and C2 to obtain a stable DC5V voltage as the working power supply of the whole machine.

The main control MCU chip of this power timing socket is PIC16F627 microcontroller. One of the advantages of PIC16F series microcontrollers is:

The port driving current is large, and the source current or sink current of each port is greater than 20mA. In this way, its port driving capability can directly drive the LED digital tube without adding driving transistors, making the hardware circuit design of the socket very simple.

PIC16F627 microcontroller port settings:

The RA of the microcontroller has 6 I/O ports, using RA<5:0>

In this application, RA0-RA2 are set as output ports; RA0 and RA1 ports are used for the "bit code" driving of the "unit" and "tens" of the two digital tubes respectively, and RA2 is used to control the operation of the b pole of the transistor Q3, and the closing or not of the contact of the relay J1, and then, the existence of the AC220V output voltage of the timing socket JP1 is controlled. The RA3 port is set to the input/output time-sharing dual-use working state: ① When used as an input port: used as the "unit timing time preset adjustment key" of the timing socket; ② When used as an output port: used to drive the LED1 light-emitting diode as the working indicator of this timing socket. When the timing socket does not enter the timing output power supply, RA3 is used as an input port to maintain the setting function of the unit digital preset number used to set the timer; when the socket is working and outputting voltage, RA3 is converted to an output port to drive LED1 as the working indicator of the socket.

RA4 port is set as input port, used for the preset adjustment key of the ten-digit value of the timing time. RA5 port is an input port, and the button switch S1 connected to it is the "start/alarm release button".

RB port setting: The RB port of PIC16F627 in the circuit is set as an output port. Among them, RB0-RB6 ports are used to drive the "ag" 7 "segment codes" of the LED digital tube. Therefore, the LG5621BH (red) common anode LED digital tube is selected here. Therefore, only when the RB0-RB6 port outputs "low level", the "ag" code segment corresponding to the LED digital tube can be lit. RB7 port is used to drive the active buzzer. When the RB7 port outputs a high level, the high level voltage is added to the b pole of the transistor Q4 through the 1k resistor R9, Q4 is turned on, and the buzzer sounds an alarm.

Power timing socket function: When the timing socket is initially powered on, the two-digit digital tube display value is: "99", and it is in a standby state of stopping time, and its output jack JP1 has no voltage output. If the single-digit timing time value preset adjustment key S3 is pressed at this time, the single-digit digital tube will decrease by "1" from the current display value. For example, after pressing S3 for the first time and releasing the key, the single-digit digital tube display value changes from "9" to "8", and pressing S3 again, the display value changes to "7", ... until the display value changes to "0", and then from "0" back to "9" in a reciprocating cycle.

The function of the tens-digit timing preset adjustment button S2 is the same as S3, except that each time S2 is pressed and released, the tens-digit digital tube display value decreases by "1". Similarly, the display value is also displayed in a reciprocating cycle from 9 → 8 → 7 →…→ 0 → 9. S1 is the start/alarm release button.

When the S1 button is pressed for the first time, the timing socket enters the working state:

The RA2 port outputs a high level, which is added to the base b of the transistor Q3 through the 1K current-limiting resistor R8. Q3 is turned on, the relay J1 is energized, and the contacts ① and ④ of J1 are closed. The "live wire" L of the AC220V AC mains is added to the L port of the output JP1 of this socket through the fixed and moving contacts ④ and ① of the relay, so that the JP1 socket outputs AC220V AC voltage. At the same time, the AC voltage output indicator LED2 is lit, indicating that the socket has AC220V voltage output.

At the same time, the timing time starts counting down and decreases. The timing time is minutes, and the timing duration starts from the current display value of the digital tube and ends when it displays 00. During the countdown time, the LED1 working indicator starts to flash on and off at a frequency of 2S, and the number of "1" is reduced for every minute; the number of "1" is reduced for every 10 minutes. When the digital tube display value is "00", after another 60 seconds, the output jack JP1 is powered off, and the AC220V output voltage indicator LED2 is extinguished at the same time; the buzzer starts to alarm.

During the buzzer alarm, the digital tube always displays "00" and the LED1 working indicator stops flashing and remains on. The buzzer will not stop alarming until the S1 button is pressed again; the digital tube display value jumps back to the value of the last preset timing time, and LED1 turns off.

Within the countdown time range, the S1 ~ S3 button functions are shielded, invalid, and cannot be enabled.

Program design points: The program design includes PIC16F627 initialization program, main program, 1-minute timer interrupt subroutine, buzzer prompt sound subroutine, digital table query subroutine, etc.

In this program design, the speed of LED1 flashing should not be set too fast or too slow. If the speed of LED1 flashing is too fast, people will feel too "busy" and feel flustered. If it flashes too slowly, people will wonder whether the timing socket is moving or working. It is appropriate to light up and turn off once every 2 seconds. In addition, LED2 is added to the circuit as an AC220V output power indicator. When the timing socket is working, LED2 lights up, indicating that the JP1 socket has AC220V voltage output; LED2 turns off, indicating that the power JP1 socket has no AC220V voltage output, to confirm whether the socket has AC voltage output.

The PIC16F627 microcontroller initialization procedure is as follows.

;*****【PIC16F627 initialization program】****************

START

BCF STATUS,6

BSF STATUS,5

; Set RP1=0, RPO=1 of STAUUS register

MOVLW 0

MOVWF TRISB

; Set port B as output

MOVLW 0F8H

MOVWF TRISA

; Set R2-R0 of port A to output

; Set R5-R3 of port A as input

MOVLW 07H

MOVWF OPTION_REG

; Set option register divider: TMR0

; Set the frequency division ratio to: 1:256

BCF STATUS,5

MOVLW 07H

MOVWF CMCON

; Set the number 07H to CMCON

; Turn off the internal comparator of 16F627

[page]

MOVLW 0

MOVWF PORTB

; Clear B port

;---------- The following is the interrupt setting--------

MOVLW 0A0H

MOVWF INTCON

; Turn on TMR0 interrupt enable bit

; and global interrupt enable bit

BCF INTCON,T0IF

MOVLW TMR0_B

; Clear TMR0 interrupt flag

MOVWF TMR0

; Assign initial value to TMR0

; and start the TMR0 timer

;*******[End of initialization procedure]************************

Program design flow module, the main program flowchart is shown in Figure 2.

Figure 2 Main program flow chart
Figure 2 Main program flow chart

The 1-minute timer interrupt subroutine flow chart is shown in Figure 3.

Figure 2 1-minute timer interrupt subroutine flow chart
Figure 2 1-minute timer interrupt subroutine flow chart

[page]

The digital tube display subroutine flow chart is shown in Figure 4.

Figure 4 Digital tube display subroutine flow
Figure 4 Digital tube display subroutine flow

The key prompt sound subroutine segment is as follows for reference.

;------ Key prompt sound subroutine starts-----;

FMQ

; Buzzer prompt sound subroutine

BSF PORTB,7

;RB<7> port outputs "high level", making Q4 conductive and buzzer LS1 sound.

CALL DELAY_0.2S

; Call the 0.2S delay subroutine to make the buzzer sound for 0.2S.

BCF PORTB,7

;RB<7> outputs "low level", turns off Q4 and the buzzer

RETURN

; The buzzer subroutine returns.

;-----Key prompt sound subroutine ends------;

Programming points: 1) This timer uses a timer module TMI0 of PIC16F627, and uses TMI0 to make a 50mS timer interrupt, and then uses the register "decrement, zero judgment" statement:

"DECFSZ F" determines whether 20×60 interrupts have been reached to determine the limit of the 1-minute display time. If 1 minute has not been reached, the current digital value will continue to be displayed. If 1 minute has been reached, the current digital value will be subtracted by "1" and then displayed for 1 minute... In this way: every 1 minute, the value displayed by the ones digit of the digital tube will be reduced by "1", and every 10 minutes, the value displayed by the tens digit of the digital tube will be reduced by "1", until the value displayed by the two digits of the digital tube is reduced to "00". 2) How to distinguish between the timer: preset display program and timer working display program. In this program design, a register flag bit is used to distinguish. The flag bit is "0" to execute the preset display program; the flag bit is "1" to execute the timer working program.

3) The anti-shake problem of the key. Mechanical keys are connected and disconnected many times when they are pressed and lifted. In addition, the speed of executing the program by the single-chip microcomputer is very fast, that is, the closed and disconnected states of the switch can be detected in an instant (within 1μS). Therefore, the anti-shake problem of the S1-S3 key switches must be done well. The anti-shake is solved by software. The specific method is to add a delay program during the process of pressing and lifting the key. Generally, the delay time is about 10mS. This solves the anti-shake problem of the key without making people feel that the speed of the key response is too slow. 4) Calling the display lookup program. Using the lookup table method to display the digital value of the digital tube is one of the commonly used methods in program design. It can make the software and hardware design more concise and easy to understand. However, in this timer design, the decimal point of the digital tube is used as a "working indicator light". Since the digital tube uses the "dynamic scanning" method to light up the work, the lookup table code will change. The following is a list of the code table of the lookup table program used for this common anode digital in this project, see Table 1.

Table 1

If the value of the code table is written "upside down": RETLW 0X40 is written at the front; RETLW 0X10 is written at the end, then this timing socket becomes a "positive" timing socket.

The overall design of the program is modular, which makes it easier to read and the process clearer.

Component selection: The power transformer T1 in the circuit should be a transformer with a power greater than 3W and a secondary output voltage of AC12V ~ AC15V, so as to ensure that the DC voltage of the secondary output of T1 will not drop too low when the relay is working, affecting the normal operation of the single-chip microcomputer. LS1 chooses a 5V active buzzer. J1 can also choose other models. The higher the working voltage of the J1 coil, the smaller its working current, and the smaller the impact on the circuit voltage reduction value. The J1 contact current should be greater than 10A to avoid the possibility of the contact being burned when the timing socket drives a large AC load. The digital tube uses a 2-in-1. Recently, the author found that some people loudly hawked "high-efficiency electronic energy savers" in the streets and alleys of rural areas and the market. According to the vendors, this high-tech product has obtained a national patent, and each can carry a load of 1500~3000W. The price is cheap, only ten yuan per piece, and an average family only needs one. If there are many electrical appliances at home, you can buy more. It is easy to use and practical. After you bring it home, you only need to plug the two-pin plug of the electronic energy saver into any two-hole socket in your home to save 30~50% of electricity, or even more.

In order to convince the onlookers of the power saving effect of the electronic energy saver, the vendor also demonstrated it on the spot. The demonstration method was: connect a 100W light bulb to an electric meter, and after the power was turned on, the electric meter rotated rapidly.

The vendor then plugged the "electronic energy saver" into the socket prepared in advance, and the speed of the aluminum disk of the electric meter immediately slowed down by more than half. After seeing this with their own eyes, some villagers were convinced of the effect of the electronic energy saver in saving electricity.

Many people asked for product introductions, user manuals, and how to use them, and many of them paid for them. Can this "electronic energy saver" really save electricity?

In order to find out the truth, the author bought an electronic energy saver and tested it. Can the "electronic energy saver" sold on the street by the author save electricity?

Ma Xiangqin's common anode digital tube. The crystal oscillator is 4MHz.

During installation, production and debugging, please note: Since there is AC220V voltage connected to the circuit, it is recommended that the timing socket temporarily does not connect to the AC220V voltage of socket JP1 during the debugging stage for personal safety. After the program is debugged, the AC220V voltage will be connected to ensure that the debugging stage of the whole machine is carried out under a safe voltage. There are no special requirements for the components in the circuit, and it is sufficient to ensure the correctness of the installation and welding. The PIC16F627 chip should be installed in the IC socket so that it can be removed at any time and the program can be re-burned.

Program burning: The (.hex) file attached to the article can be used directly by readers to burn it into the PIC16F627 chip. When burning the program, you must pay attention to the "configuration bit" setting of the programmer, which plays a vital role in the PIC microcontroller and must not be ignored.

Performance Analysis:

The time error produced by this timing socket in 1 hour is about 8S. The timing accuracy of this timing socket is not high because the "dynamic display" of the digital tube and the TMI0 interrupt affect each other. When TMI0 overflows, the interrupt will not occur until the program is completed, which will cause a delay error. For general use in our daily life, its timing accuracy is high enough.

Editor's note: The author raised the question of how to reduce the error of PIC timer, and hopes that enthusiasts can give some ideas. If the scanning timing and timing timing are combined, with an interruption every 5ms, and 200 interruptions per minute, can it be improved?

Reference address:Power Timing Socket Made with PIC Microcontroller

Previous article:PIC microcontroller matrix keyboard + line inversion method
Next article:Analysis on the selection of components for PIC microcontroller human-machine interface module

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号