1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
===================================================================================
#include "gd32f4xx.h" #include "gd32f4xx_gpio.h" #include "gd32f4xx_rcu.h" #include "systick.h" #include "GPIO_LED_Driver.h" #include "battery_led.h"
int8_t battery_state = 0;
void GPIO_KEY_Config(void) { rcu_periph_clock_enable(RCU_GPIOC);
gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); }
void update_key_state(int8_t *key_flag, FlagStatus key_input, int8_t *battery_state, int8_t state_value) { if (key_input == RESET && *key_flag) { *key_flag = 0; *battery_state = state_value; } else if (key_input == SET && *key_flag == 0) { *key_flag = 1; } }
int main(void) {
systick_config(); battery_led_Init(); GPIO_KEY_Config();
int8_t key1_flag = 1; int8_t key2_flag = 1; int8_t key3_flag = 1; int32_t time_Control = 0; while(1) {
FlagStatus Key1 = gpio_input_bit_get(GPIOC, GPIO_PIN_0); FlagStatus Key2 = gpio_input_bit_get(GPIOC, GPIO_PIN_1); FlagStatus Key3 = gpio_input_bit_get(GPIOC, GPIO_PIN_2);
update_key_state(&key1_flag, Key1, &battery_state, 0); update_key_state(&key2_flag, Key2, &battery_state, 1); update_key_state(&key3_flag, Key3, &battery_state, 2);
if(time_Control < 20) { delay_1ms(25); time_Control++; } else { time_Control = 0; battery_machine_handle(&battery_state); } } } ============================================================================
#include "battery_led.h"
int8_t battery = 26; int8_t batteryNum = 0; int8_t flowNum = 1;
void battery_led_Init(void){ leds_switch_Init(); leds_switch_open(); leds_driver_init(); }
void set_led_state(int flowNum) { bsp_led_write(GPIOD, GPIO_PIN_8, flowNum > 0 ? RESET : SET); bsp_led_write(GPIOD, GPIO_PIN_9, flowNum > 1 ? RESET : SET); bsp_led_write(GPIOD, GPIO_PIN_10, flowNum > 2 ? RESET : SET); bsp_led_write(GPIOD, GPIO_PIN_11, flowNum > 3 ? RESET : SET); }
void battery_machine_handle(int8_t* battery_state) {
batteryNum = battery / 25; switch (*battery_state) { case 1: battery += 10; if(battery >= 100) {battery = 100;} *battery_state = 0; case 0: if(flowNum > 4) {flowNum = batteryNum;} set_led_state(flowNum); flowNum++; break; case 2: bsp_leds_close_all(); break; default: break; } } ================================================================
#include "GPIO_LED_Driver.h"
LED_PARAM leds[] = { {RCU_GPIOD,GPIOD,GPIO_PIN_8}, {RCU_GPIOD,GPIOD,GPIO_PIN_9}, {RCU_GPIOD,GPIOD,GPIO_PIN_10}, {RCU_GPIOD,GPIOD,GPIO_PIN_11},
};
uint8_t len = sizeof(leds)/sizeof(leds[0]);
void leds_switch_Init(void) { rcu_periph_clock_enable(RCU_GPIOC); gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6); gpio_output_options_set(GPIOC, GPIO_OTYPE_OD, GPIO_OSPEED_MAX, GPIO_PIN_6); }
static void led_pin_config(rcu_periph_enum rcu,uint32_t port,uint32_t pin){ rcu_periph_clock_enable(rcu); gpio_mode_set(port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, pin); gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, pin); }
void leds_driver_init(void){ for(int i=0;i<len;i++){ LED_PARAM led = leds[i]; led_pin_config(led.rcu,led.port,led.pin); gpio_bit_set(led.port,led.pin);
} }
void leds_switch_open() {
gpio_bit_reset(GPIOC,GPIO_PIN_6); }
void leds_switch_close() {
gpio_bit_set(GPIOC,GPIO_PIN_6); }
void leds_open(LED led){ gpio_bit_reset(leds[led].port,leds[led].pin); }
void leds_close(LED led){ gpio_bit_set(leds[led].port,leds[led].pin); }
void leds_open_all(){ for(int i=1;i<len;i++){ LED_PARAM led = leds[i]; gpio_bit_reset(led.port,led.pin); } }
void bsp_leds_close_all(){ for(int i=0;i<len;i++){ LED_PARAM led = leds[i]; gpio_bit_set(led.port,led.pin); } }
void bsp_led_write(uint32_t gpio_periph, uint32_t pin, bit_status bit_value){ gpio_bit_write(gpio_periph, pin, bit_value); }
|