Switching function is the most basic function of battery power supply system. If the mechanical switch (2-stage toggle switch, self-locking switch, etc.) is used to directly connect and cut off the power supply, regardless of the reliability of the mechanical parts, at least the automatic shutdown function (which needs to be automatically shut down when there is no operation for a long time and the battery power is low) is relatively difficult to realize. Therefore, the battery power supply system generally uses button switch, with a button to cooperate with software to realize the function of turning on once, pressing again and shutting down automatically.

This part of the content is about how to realize one key switch on and off function. The switch function uses a pin (PD4) of MCU as the input, connects to the button, receives the electrical signal generated by pressing the button, and then uses another pin (PD3) as the output to drive an electronic switch composed of transistor 8550 to realize the control of system power supply. The stm8s microcontroller itself is directly connected to the battery, using the shutdown mode of the MCU, the current of the MCU itself is reduced to μ a, and the battery consumption can be ignored.

1. Circuit design

The circuit diagram is as follows: C1 is a Vcap capacitor of stm8s single chip microcomputer, which keeps the internal voltage of 1.8V stable. It is the only external component that the stm8s single chip microcomputer must connect to. The ceramic chip capacitor with a capacity of 1 μ f is selected. In terms of circuit reliability, the closer this capacitor is to the IC of single chip microcomputer, the better.

K1 is a button. One end of the button is connected to the PD4 pin of the MCU, and the other end is grounded. If the button is pressed, a low-level pulse will be generated.

Q1 is a triode, which is connected to the PD3 of MCU through LED D1 and current limiting resistor R1. In this way, when PD3 outputs low level, Q1 is on, and there is voltage on VCC, which can supply power to the system. Otherwise, when PD3 outputs high level, Q1 stops VCC and has no voltage.

Led D1 also acts as a power indicator. When Q1 is on, it will emit light, otherwise it will not.

The remaining C2 ~ C5 are power decoupling capacitors, which are used to eliminate the interference signal on the power supply. P1 and P2 are connected to the positive and negative electrodes of 3.6V lithium battery respectively.

How to use stm8s single chip microcomputer to realize one key switch on / off function

2. Software design

The function of the software is to monitor the low-voltage pulse signal generated by pressing the button on the PD4 pin, and set the output level of PD3 pin according to the signal to control the conduction and cut-off of Q1, and set the working state of MCU itself, so as to realize the control of power supply.

1) Pin working mode setting

The first thing to do is to set the working mode of MCU pin, set PD4 as input mode and D3 as output mode.

In stm8s_ StdPeriph_ In lib library, you can use this function to set:

void GPIO_ Init(GPIO_ TypeDef* GPIOx, GPIO_ Pin_ TypeDef GPIO_ Pin, GPIO_ Mode_ TypeDef GPIO_ Mode)

Gpiox is the port number, with the values of gpioa, gpiob, gpioc and gpiod, respectively corresponding to the Pax, PBX, PCX, PDX ports, GPIO_ Pin is the pin number, and the value is GPIO_ PIN_ 1~GPIO_ PIN_ 7. The function passes through gpiox and GPIO_ Pin two parameters specify a unique pin.

GPIO_ Mode is working mode. There are four types of input mode values, namely, whether to pull up or not and whether to interrupt

GPIO_ MODE_ IN_ FL_ NO_ IT Input floating, no external interrupt

GPIO_ MODE_ IN_ PU_ NO_ IT Input pull-up, no external interrupt

GPIO_ MODE_ IN_ FL_ IT Input floaTIng, external interrupt

GPIO_ MODE_ IN_ PU_ IT Input pull-up, external interrupt

To pull up, the MCU will automatically provide a pull-up current for the pin, in the case of no external input, the pin is high level.

There are 8 kinds of output mode values, which are the combination of open drain output, high and low output level and high speed output

GPIO_ MODE_ OUT_ OD_ LOW_ FAST Output open-drain, low level, 10MHz

GPIO_ MODE_ OUT_ PP_ LOW_ FAST Output push-pull, low level, 10MHz

GPIO_ MODE_ OUT_ OD_ LOW_ SLOW Output open-drain, low level, 2MHz

GPIO_ MODE_ OUT_ PP_ LOW_ SLOW Output push-pull, low level, 2MHz

GPIO_ MODE_ OUT_ OD_ HIZ_ FAST Output open-drain, high-impedance level,10MHz

GPIO_ MODE_ OUT_ PP_ HIGH_ FAST Output push-pull, high level, 10MHz

GPIO_ MODE_ OUT_ OD_ HIZ_ SLOW Output open-drain, high-impedance level, 2MHz

GPIO_ MODE_ OUT_ PP_ HIGH_ SLOW Output push-pull, high level, 2MHz

In this application, PD4 needs to be set to the band up (because the button is not pressed when the high level), interrupt input mode. When starting up, PD3 should be set to low level output mode (low level can make Q1 turn on). When shutting down, PD3 should be set to input mode without pull-up, so this part of code should be written as follows:

GPIO_ Init(GPIOD, GPIO_ PIN_ 4, GPIO_ MODE_ IN_ PU_ It); / / PD4 is set to input mode with pull-up and interrupt

GPIO_ Init(GPIOD, GPIO_ PIN_ 3, GPIO_ MODE_ OUT_ PP_ LOW_ Fast); / / set PD3 to low level input mode when starting

GPIO_ Init(GPIOD, GPIO_ PIN_ 3, GPIO_ MODE_ IN_ FL_ NO_ It); / / set PD3 to low level input mode when shutting down

In addition to GPIO_ The init function can set whether the pin outputs high level or low level. The following four functions can be used to continue the high and low level of the pin:

void GPIO_ Write(GPIO_ TypeDef* GPIOx, uint8_ T portval) / / output the level specified by portval

void GPIO_ WriteHigh(GPIO_ TypeDef* GPIOx, GPIO_ Pin_ Typedef portpins) / / output high level

void GPIO_ WriteLow(GPIO_ TypeDef* GPIOx, GPIO_ Pin_ Typedef portpins) / / output low level

void GPIO_ WriteReverse(GPIO_ TypeDef* GPIOx, GPIO_ Pin_ Typedef portpins) / / Flip high and low level once

2) Stop mode setting

Stm8s single-chip microcomputer has shutdown mode. In the shutdown mode, the power consumption of the single-chip microcomputer is reduced to the minimum, and all clocks stop working, but the ram and register values can be reserved. The shutdown mode can only be exited through the following ways:

-External interrupt (GPIO)

-Can receive interrupt

-End of SPI transmission

-I2C interrupt (slave address matching)

-Reset

By executing the halt instruction, that is, executing the halt() function of the library function, you enter the shutdown mode.

3) Set external interrupt

The external interrupt is set through the setextintsensitivity function

void EXTI_ SetExtIntSensitivity(EXTI_ Port_ TypeDef Port, EXTI_ Sensitivity_ TypeDef SensitivityValue)

Among them, port is the port to set external interrupt, and the values are gpioa, gpiob, gpioc and gpiod, which correspond to Pax, PBX, PCX and PDX ports of MCU respectively;

Sensitivityvalue is the type of external interrupt. It has the following values: falling edge and low level triggering interrupt, rising edge triggering interrupt, falling edge triggering interrupt, rising edge and falling edge triggering interrupt

EXTI_ SENSITIVITY_ FALL_ LOW Interrupt on Falling edge and Low level

EXTI_ SENSITIVITY_ RISE_ ONLY Interrupt on Rising edge only

EXTI_ SENSITIVITY_ FALL_ ONLY Interrupt on Falling edge only

EXTI_ SENSITIVITY_ RISE_ FALL Interrupt on Rising and Falling edges

In this application, when the button is pressed, the falling edge is generated. To select exi_ SENSITIVITY_ FALL_ Only mode.

After setting the interrupt mode, you also need to enable interrupts by using the enableinterrupts() function.

4) External interrupt handler

In stm8s officially provided by St_ StdPeriph_ In the template of Lib library, the interrupt handler is in stm8s_ It. C. stm8s_ It. C has set the entry function of each interrupt handler according to the interrupt vector. Just fill in the content in the corresponding function.

In this application, the external interrupt handling function of gpiod port is to be written, so it is necessary to write the external interrupt handling function of gpiod port in this application

In this application, the external interrupt handling function of gpiod port is to be written, so the content of switching on and off is added to the interrupt handling function:

INTERRUPT_ HANDLER(EXTI_ PORTD_ IRQHandler, 6)

{

}

5) Overall programming and source code

The flow chart of MCU program is as follows, including the flow of main program and stm8s_ It. C external interrupt handler flow:

How to use stm8s single chip microcomputer to realize one key switch on / off function

The specific source code is as follows. The key points have been introduced before, and will not be described in detail

A) Main. C source code:

void Delay(uint16_ T ncount) / / define the delay function, ffff is about 0.2S

{

while (nCount ! = 0)

{

nCount–;

}

}

bool fPowerOn_ Flag = false; / / defines the on / off flag

void main(void)

{

GPIO_ Init(GPIOD,GPIO_ PIN_ 4,GPIO_ MODE_ IN_ PU_ IT);

EXTI_ SetExtIntSensitivity(EXTI_ PORT_ POWER_ KEY,EXTI_ SENSITIVITY_ FALL_ ONLY);

enableInterrupts();

while (1)

{

if(fPowerOn_ flag == FALSE)

{

GPIO_ Init(GPIOD, GPIO_ PIN_ 3, GPIO_ MODE_ IN_ FL_ NO_ It); / / PD3 pin is set as input mode, Q1 is cut off

Halt(); / / enter shutdown mode

}else

{

GPIO_ Init(GPIOD, GPIO_ PIN_ 3, GPIO_ MODE_ OUT_ PP_ LOW_ Fast); / / PD3 pin is set to output mode and low level, Q1 is on

///////

//Insert user defined code

//////

}

}

}

B)stm8s_ It. C source code:

void Delay(uint16_ T ncount); / / delay function is defined in main. C

extern bool fPowerOn_ flag; //fPowerOn_ Flag is defined in main. C, so it should be declared as an external variable

INTERRUPT_ HANDLER(EXTI_ PORTD_ IRQHandler, 6)

{

Delay (6000); / / delay dithering

if(! GPIO_ ReadInputPin(GPIOD, GPIO_ PIN_ 3) ) / / judge whether the button is pressed

{

if(fPowerOn_ flag)

{

fPowerOn_ flag=FALSE;

}else

{

fPowerOn_ flag=TRUE;

}

}

}

3. Measured results after shutdown

The power off current measured by stm8s development board is about 40 μ a

According to the typical capacity of 18650 lithium-ion battery of 2500mah, it can support 2500mah / 0.04ma/24h/365 days = 7.1 years after shutdown, which has exceeded the battery’s charging cycle service life and is basically acceptable, but it should not reach the minimum value. We can also study datasheet and continue to optimize the software to further reduce the shutdown current.

4. Summary

The part of switching on and off is finished. Finally, the knowledge points learned in this part include:

1. In the hardware design of single chip microcomputer, at least one Vcap capacitor should be connected between the Vcap pin and the ground, and the porcelain chip capacitor with the capacity of 1 μ F should be selected. The closer the capacitor is to the IC of the MCU, the better.

2. Set the input and output mode setting method of each pin

3. How to enter and exit stop mode

4. Setting external interrupts and interrupt handlers

Responsible editor

Leave a Reply

Your email address will not be published. Required fields are marked *