# Design and Implementation of a Polar Wind and Solar Hybrid Power Supply Controller
## 4 Controller Software Design  
In this section, based on the Keil programming environment, the software part of the power supply system for aerosol stereo cultivation is compiled in C language. The system software is mainly divided into three parts: The system initialization program, main program and interruption service program. The overall structure of the software is shown in Figure 14. The acquisition control of the ADC, wind turbine, photovoltaic control, battery charge and discharge control and unloading control are sequentially executed every 5 ms in the main timer interrupt service routine. In the serial interrupt service program, it mainly completes the communication between the serial port and PC, and sends the related data and status information written in the main program to the serial assistant. Figure 12: Battery charge and discharge circuit. Figure 13: Load control circuit. When the system is powered on and reset, a series of initialization work must be completed first. First, all variables defined in the system should be initialized, and then the input and output mode of the GPIO port should be configured. After the port configuration is completed, the LED should be started to indicate the current working state of the system immediately. Output I/O initialization refers to an initial value for the switches and PWM switches that control whether the main circuit is connected or not. Generally, the output is disconnected at 0. The initialization work of the serial port is to define the working mode, haud rate, sending permission, interrupt permission, interrupt priority, and so on. The initialization of the ADC is to set the number of ADC channels, sampling speed, result storage form and ADC power. The main task of timer initialization is to set the mode of the timer, initial value, interrupt enable, and so on. PWM initialization is mainly to set the period of the PWM counter and the initial value of the primary and secondary flips of each PWM channel. Interrupt initialization is mainly to open the CPU total interrupt, timer interrupt and serial interrupt and to set high priority. After these initializations are completed, the program begins to run in the main program loop. After the initialization, the system starts to enter the infinite loop of the main loop to perform input and output control, protection control and state switching control. The flow chart is shown in Figure 15.  
The running of the main program mainly depends on the status flag bit. The acquisition of voltage and current information is completed in the interruption of the timer. After the relevant data acquisition is completed, there will be a set flag set to 1 (Finishflag = 1). After the detection of the mark bit in the main program, the voltage, current and system working state will be written into the serial transmission buffer in the form of a string, and sent to the serial port assistant of the PC. After the transmission is completed, the set flag is set to zero (Finishflag = 0). If the bit is not set to 1, the system's working status is judged directly. The judging link of the system working state is mainly to judge whether there is over-voltage, over-current, short circuit, no-load, and so on. If it does not happen, the current indicator state and the corresponding pin control state will be maintained, and the next step will be taken. If one of them occurs, the protection mechanism is activated, the fault is judged and the relevant flag bit is set to 1 (Error = 1), thus turning on the status indicator. If the fault flag of the corresponding circuit is detected as Error = 1 in the main timer, the output and load of the circuit are directly disconnected. At the same time, the fault information is sent to the serial port to start the slave timer. The slave timer closes after 5 ms, reopens the output and judges the working state of the system. If normal, set Error = 0, return to the normal indicator state, maintain the output state of the circuit, and close the load. Figure 14: Overall Software Architecture.