├── .vscode └── settings.json ├── Docs ├── E题_运动目标控制与自动追踪系统.pdf └── 电赛报告.doc ├── GreenServo ├── K210 │ ├── README.md │ └── green.py └── STM32 │ ├── .gitignore │ ├── .mxproject │ ├── Core │ ├── Inc │ │ ├── gpio.h │ │ ├── main.h │ │ ├── stm32f1xx_hal_conf.h │ │ ├── stm32f1xx_it.h │ │ ├── tim.h │ │ └── usart.h │ └── Src │ │ ├── gpio.c │ │ ├── main.c │ │ ├── stm32f1xx_hal_msp.c │ │ ├── stm32f1xx_it.c │ │ ├── system_stm32f1xx.c │ │ ├── tim.c │ │ └── usart.c │ ├── Drivers │ ├── CMSIS │ │ ├── Device │ │ │ └── ST │ │ │ │ └── STM32F1xx │ │ │ │ ├── Include │ │ │ │ ├── stm32f103xb.h │ │ │ │ ├── stm32f1xx.h │ │ │ │ └── system_stm32f1xx.h │ │ │ │ └── LICENSE.txt │ │ ├── Include │ │ │ ├── cmsis_armcc.h │ │ │ ├── cmsis_armclang.h │ │ │ ├── cmsis_compiler.h │ │ │ ├── cmsis_gcc.h │ │ │ ├── cmsis_iccarm.h │ │ │ ├── cmsis_version.h │ │ │ ├── core_armv8mbl.h │ │ │ ├── core_armv8mml.h │ │ │ ├── core_cm0.h │ │ │ ├── core_cm0plus.h │ │ │ ├── core_cm1.h │ │ │ ├── core_cm23.h │ │ │ ├── core_cm3.h │ │ │ ├── core_cm33.h │ │ │ ├── core_cm4.h │ │ │ ├── core_cm7.h │ │ │ ├── core_sc000.h │ │ │ ├── core_sc300.h │ │ │ ├── mpu_armv7.h │ │ │ ├── mpu_armv8.h │ │ │ └── tz_context.h │ │ └── LICENSE.txt │ └── STM32F1xx_HAL_Driver │ │ ├── Inc │ │ ├── Legacy │ │ │ └── stm32_hal_legacy.h │ │ ├── stm32f1xx_hal.h │ │ ├── stm32f1xx_hal_cortex.h │ │ ├── stm32f1xx_hal_def.h │ │ ├── stm32f1xx_hal_dma.h │ │ ├── stm32f1xx_hal_dma_ex.h │ │ ├── stm32f1xx_hal_exti.h │ │ ├── stm32f1xx_hal_flash.h │ │ ├── stm32f1xx_hal_flash_ex.h │ │ ├── stm32f1xx_hal_gpio.h │ │ ├── stm32f1xx_hal_gpio_ex.h │ │ ├── stm32f1xx_hal_pwr.h │ │ ├── stm32f1xx_hal_rcc.h │ │ ├── stm32f1xx_hal_rcc_ex.h │ │ ├── stm32f1xx_hal_tim.h │ │ ├── stm32f1xx_hal_tim_ex.h │ │ ├── stm32f1xx_hal_uart.h │ │ ├── stm32f1xx_ll_bus.h │ │ ├── stm32f1xx_ll_cortex.h │ │ ├── stm32f1xx_ll_dma.h │ │ ├── stm32f1xx_ll_exti.h │ │ ├── stm32f1xx_ll_gpio.h │ │ ├── stm32f1xx_ll_pwr.h │ │ ├── stm32f1xx_ll_rcc.h │ │ ├── stm32f1xx_ll_system.h │ │ ├── stm32f1xx_ll_tim.h │ │ ├── stm32f1xx_ll_usart.h │ │ └── stm32f1xx_ll_utils.h │ │ ├── LICENSE.txt │ │ └── Src │ │ ├── stm32f1xx_hal.c │ │ ├── stm32f1xx_hal_cortex.c │ │ ├── stm32f1xx_hal_dma.c │ │ ├── stm32f1xx_hal_exti.c │ │ ├── stm32f1xx_hal_flash.c │ │ ├── stm32f1xx_hal_flash_ex.c │ │ ├── stm32f1xx_hal_gpio.c │ │ ├── stm32f1xx_hal_gpio_ex.c │ │ ├── stm32f1xx_hal_pwr.c │ │ ├── stm32f1xx_hal_rcc.c │ │ ├── stm32f1xx_hal_rcc_ex.c │ │ ├── stm32f1xx_hal_tim.c │ │ ├── stm32f1xx_hal_tim_ex.c │ │ └── stm32f1xx_hal_uart.c │ ├── MDK-ARM │ ├── .gitignore │ ├── DebugConfig │ │ └── STM32code_STM32F103C8_1.0.0.dbgconf │ ├── EventRecorderStub.scvd │ ├── RTE │ │ └── _STM32code │ │ │ └── RTE_Components.h │ ├── STM32code.uvguix.duruofu │ ├── STM32code.uvoptx │ ├── STM32code.uvprojx │ ├── startup_stm32f103xb.lst │ └── startup_stm32f103xb.s │ ├── STM32code.ioc │ ├── User │ ├── APP │ │ ├── app.c │ │ └── app.h │ ├── AT24C02 │ │ ├── at24c02.c │ │ └── at24c02.h │ ├── Buzzer │ │ ├── Buzzer.c │ │ └── Buzzer.h │ ├── Control │ │ ├── control.c │ │ └── control.h │ ├── KEY │ │ ├── key.c │ │ └── key.h │ ├── LED │ │ ├── led.c │ │ └── led.h │ ├── OLED │ │ ├── OLED.c │ │ ├── OLED.h │ │ ├── OLEDFONT.h │ │ ├── OLEDGUI │ │ │ ├── gui.c │ │ │ └── gui.h │ │ └── OLED_bmp.h │ ├── PWM │ │ ├── pwm.c │ │ └── pwm.h │ ├── USART │ │ ├── debug.c │ │ ├── debug.h │ │ ├── serial_it_config.c │ │ ├── serial_it_config.h │ │ ├── usart_2.c │ │ └── usart_2.h │ └── YunTai │ │ ├── yuntai.c │ │ └── yuntai.h │ └── keilkill.bat ├── README.md ├── RedServo ├── K210 │ ├── README.md │ └── red.py └── STM32 │ ├── .gitignore │ ├── .mxproject │ ├── Core │ ├── Inc │ │ ├── gpio.h │ │ ├── main.h │ │ ├── stm32f1xx_hal_conf.h │ │ ├── stm32f1xx_it.h │ │ ├── tim.h │ │ └── usart.h │ └── Src │ │ ├── gpio.c │ │ ├── main.c │ │ ├── stm32f1xx_hal_msp.c │ │ ├── stm32f1xx_it.c │ │ ├── system_stm32f1xx.c │ │ ├── tim.c │ │ └── usart.c │ ├── Drivers │ ├── CMSIS │ │ ├── Device │ │ │ └── ST │ │ │ │ └── STM32F1xx │ │ │ │ ├── Include │ │ │ │ ├── stm32f103xb.h │ │ │ │ ├── stm32f1xx.h │ │ │ │ └── system_stm32f1xx.h │ │ │ │ └── LICENSE.txt │ │ ├── Include │ │ │ ├── cmsis_armcc.h │ │ │ ├── cmsis_armclang.h │ │ │ ├── cmsis_compiler.h │ │ │ ├── cmsis_gcc.h │ │ │ ├── cmsis_iccarm.h │ │ │ ├── cmsis_version.h │ │ │ ├── core_armv8mbl.h │ │ │ ├── core_armv8mml.h │ │ │ ├── core_cm0.h │ │ │ ├── core_cm0plus.h │ │ │ ├── core_cm1.h │ │ │ ├── core_cm23.h │ │ │ ├── core_cm3.h │ │ │ ├── core_cm33.h │ │ │ ├── core_cm4.h │ │ │ ├── core_cm7.h │ │ │ ├── core_sc000.h │ │ │ ├── core_sc300.h │ │ │ ├── mpu_armv7.h │ │ │ ├── mpu_armv8.h │ │ │ └── tz_context.h │ │ └── LICENSE.txt │ └── STM32F1xx_HAL_Driver │ │ ├── Inc │ │ ├── Legacy │ │ │ └── stm32_hal_legacy.h │ │ ├── stm32f1xx_hal.h │ │ ├── stm32f1xx_hal_cortex.h │ │ ├── stm32f1xx_hal_def.h │ │ ├── stm32f1xx_hal_dma.h │ │ ├── stm32f1xx_hal_dma_ex.h │ │ ├── stm32f1xx_hal_exti.h │ │ ├── stm32f1xx_hal_flash.h │ │ ├── stm32f1xx_hal_flash_ex.h │ │ ├── stm32f1xx_hal_gpio.h │ │ ├── stm32f1xx_hal_gpio_ex.h │ │ ├── stm32f1xx_hal_pwr.h │ │ ├── stm32f1xx_hal_rcc.h │ │ ├── stm32f1xx_hal_rcc_ex.h │ │ ├── stm32f1xx_hal_tim.h │ │ ├── stm32f1xx_hal_tim_ex.h │ │ ├── stm32f1xx_hal_uart.h │ │ ├── stm32f1xx_ll_bus.h │ │ ├── stm32f1xx_ll_cortex.h │ │ ├── stm32f1xx_ll_dma.h │ │ ├── stm32f1xx_ll_exti.h │ │ ├── stm32f1xx_ll_gpio.h │ │ ├── stm32f1xx_ll_pwr.h │ │ ├── stm32f1xx_ll_rcc.h │ │ ├── stm32f1xx_ll_system.h │ │ ├── stm32f1xx_ll_tim.h │ │ ├── stm32f1xx_ll_usart.h │ │ └── stm32f1xx_ll_utils.h │ │ ├── LICENSE.txt │ │ └── Src │ │ ├── stm32f1xx_hal.c │ │ ├── stm32f1xx_hal_cortex.c │ │ ├── stm32f1xx_hal_dma.c │ │ ├── stm32f1xx_hal_exti.c │ │ ├── stm32f1xx_hal_flash.c │ │ ├── stm32f1xx_hal_flash_ex.c │ │ ├── stm32f1xx_hal_gpio.c │ │ ├── stm32f1xx_hal_gpio_ex.c │ │ ├── stm32f1xx_hal_pwr.c │ │ ├── stm32f1xx_hal_rcc.c │ │ ├── stm32f1xx_hal_rcc_ex.c │ │ ├── stm32f1xx_hal_tim.c │ │ ├── stm32f1xx_hal_tim_ex.c │ │ └── stm32f1xx_hal_uart.c │ ├── MDK-ARM │ ├── .gitignore │ ├── DebugConfig │ │ └── STM32code_STM32F103C8_1.0.0.dbgconf │ ├── EventRecorderStub.scvd │ ├── RTE │ │ └── _STM32code │ │ │ └── RTE_Components.h │ ├── STM32code.uvguix.duruofu │ ├── STM32code.uvoptx │ ├── STM32code.uvprojx │ ├── startup_stm32f103xb.lst │ └── startup_stm32f103xb.s │ ├── STM32code.ioc │ ├── User │ ├── APP │ │ ├── app.c │ │ └── app.h │ ├── AT24C02 │ │ ├── at24c02.c │ │ └── at24c02.h │ ├── Buzzer │ │ ├── Buzzer.c │ │ └── Buzzer.h │ ├── Control │ │ ├── control.c │ │ └── control.h │ ├── KEY │ │ ├── key.c │ │ └── key.h │ ├── LED │ │ ├── led.c │ │ └── led.h │ ├── OLED │ │ ├── OLED.c │ │ ├── OLED.h │ │ ├── OLEDFONT.h │ │ ├── OLEDGUI │ │ │ ├── gui.c │ │ │ └── gui.h │ │ └── OLED_bmp.h │ ├── PWM │ │ ├── pwm.c │ │ └── pwm.h │ ├── USART │ │ ├── debug.c │ │ ├── debug.h │ │ ├── serial_it_config.c │ │ ├── serial_it_config.h │ │ ├── usart_2.c │ │ └── usart_2.h │ └── YunTai │ │ ├── yuntai.c │ │ └── yuntai.h │ └── keilkill.bat └── attachments ├── image-20230804061932311.png ├── image-20230805083401173.png ├── 硬件框图.drawio └── 程序流程图.drawio /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "buzzer.h": "c", 4 | "main.h": "c", 5 | "app.h": "c", 6 | "tim.h": "c" 7 | } 8 | } -------------------------------------------------------------------------------- /Docs/E题_运动目标控制与自动追踪系统.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/Docs/E题_运动目标控制与自动追踪系统.pdf -------------------------------------------------------------------------------- /Docs/电赛报告.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/Docs/电赛报告.doc -------------------------------------------------------------------------------- /GreenServo/K210/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/GreenServo/K210/README.md -------------------------------------------------------------------------------- /GreenServo/K210/green.py: -------------------------------------------------------------------------------- 1 | import sensor, time, image 2 | import utime 3 | import lcd 4 | from fpioa_manager import fm 5 | from machine import UART 6 | from Maix import GPIO # 导入Maix库中的GPIO模块,用于控制GPIO引脚 7 | import time 8 | import uos 9 | 10 | 11 | fm.register(27, fm.fpioa.GPIO0) # 将引脚13映射为GPIO0 12 | fm.register(28, fm.fpioa.GPIO1) # 将引脚12映射为GPIO1 13 | fm.register(29, fm.fpioa.GPIO2) # 将引脚14映射为GPIO2 14 | led_r = GPIO(GPIO.GPIO0, GPIO.OUT) # 初始化GPIO0为输出模式,控制红色LED灯 15 | led_g = GPIO(GPIO.GPIO1, GPIO.OUT) # 初始化GPIO1为输出模式,控制绿色LED灯 16 | led_b = GPIO(GPIO.GPIO2, GPIO.OUT) # 初始化GPIO2为输出模式,控制蓝色LED灯 17 | 18 | # 绑定UART2的引脚 19 | fm.register(6, fm.fpioa.UART2_RX) # 将引脚6绑定到UART2的接收引脚 20 | fm.register(8, fm.fpioa.UART2_TX) # 将引脚8绑定到UART2的发送引脚 21 | # 初始化UART2对象 22 | uart = UART(UART.UART2, 115200, 8, 0, 0, timeout=1000, read_buf_len=4096) 23 | 24 | sensor.reset() 25 | sensor.set_pixformat(sensor.RGB565) 26 | sensor.set_framesize(sensor.QVGA) 27 | #sensor.set_auto_exposure(1) 28 | #sensor.set_auto_exposure(0, exposure=120000) 29 | sensor.set_auto_gain(0, gain_db = 5) 30 | sensor.set_auto_whitebal(0, rgb_gain_db = (0,0,0)) 31 | #sensor.set_brightness(0) #设置亮度 32 | #sensor.set_contrast(3) #对比度 33 | sensor.set_vflip(True) 34 | sensor.set_hmirror(True) 35 | #sensor.set_windowing((224, 224)) 36 | sensor.skip_frames(time = 900) 37 | 38 | lcd.init() 39 | 40 | flag = 0 41 | 42 | # led状态切换 43 | def led(num): 44 | if num == 0: 45 | led_r.value(1) 46 | led_g.value(0) 47 | led_b.value(0) 48 | elif num == 1: 49 | led_r.value(0) 50 | led_g.value(1) 51 | led_b.value(0) 52 | elif num == 2: 53 | led_r.value(0) 54 | led_g.value(0) 55 | led_b.value(1) 56 | 57 | 58 | # 串口接收 59 | def accept_uart(): 60 | global flag, uart, lcd, sensor 61 | #while True: 62 | #img=sensor.snapshot() # 获取图片 63 | #lcd.display(img) 64 | if uart.any(): # 判断是否有数据可读 65 | led(1) 66 | read_data = uart.read() # 读取数据 67 | #print("read_data =", read_data) 68 | #print(type(read_data)) 69 | if read_data: 70 | try: 71 | data_str = read_data.decode("utf-8") # 将字节数据转换为字符串 72 | segments = data_str.split(",") # 按逗号分割字符串 73 | result = {} # 创建字典存储解析结果 74 | for segment in segments: # 遍历每个键值对并解析 75 | key, value = segment.split(":") # 按冒号分隔键值对 76 | result[key.strip()] = value.strip() # 去除空格并保存到字典中 77 | print(result) # 打印解析结果 78 | led(2) 79 | flag = int(result['id']) 80 | print('收到',flag) 81 | return 1 82 | #break 83 | except: 84 | pass 85 | return 0 86 | 87 | 88 | # 识别激光确定位置 89 | def pointPosition(): 90 | global sensor, uart, lcd 91 | flag0 = 0 92 | #sensor.set_brightness(-3) #设置亮度 93 | #sensor.set_contrast(3) #对比度 94 | #sensor.set_auto_exposure(False,500) #曝光速度 95 | while flag0 == 0: 96 | print('识别激光确定位置中') 97 | flag0 = accept_uart() 98 | img = sensor.snapshot() # 获取图片 99 | red_x = 0 100 | 101 | red_y = 0 102 | green_x = 0 103 | green_y = 0 104 | #,(80, 100, -11, 0, 0, 10) 105 | for blobs in img.find_blobs([(50, 100, 10, 127, -20, 127)], 106 | x_stride = 1,y_stride = 1,pixels_threshold = 10, 107 | area_threshold = 10, 108 | merge = True, margin = 1): 109 | #if 50 < blobs.pixels(): # 找到面积最大的色块 110 | #print(blobs) 111 | #if blobs.code() == 1: 112 | red_x = blobs.x() 113 | red_y = blobs.y() 114 | img.draw_cross(blobs.cx(),blobs.cy(), color=(0,0,255), size = 8) 115 | for blobs in img.find_blobs([(70, 100, -50, 5, -10, 20)], 116 | x_stride = 5,y_stride = 5,pixels_threshold = 10, 117 | area_threshold = 10, 118 | merge = True, margin = 1): 119 | #if 100 < blobs.pixels(): # 找到面积最大的色块 120 | #print(blobs) 121 | #if blobs.code() == 1: 122 | #red_x = blobs.x() 123 | #red_y = blobs.y() 124 | #img.draw_cross(blobs.cx(),blobs.cy(), color=(0,0,255), size = 8) 125 | #if blobs.code() == 2: 126 | green_x = blobs.x() 127 | green_y = blobs.y() 128 | img.draw_cross(blobs.cx(),blobs.cy(), color=(255,0,0), size = 8) 129 | utime.sleep_ms(20) 130 | if red_x*red_y*green_x*green_y != 0: 131 | str2 = str(red_x)+','+str(red_y)+','+str(green_x)+','+str(green_y)+',0,0,0,0\r\n' 132 | utime.sleep_ms(50) 133 | uart.write(str2) # 发送数据 134 | print(str2+'发送完毕') 135 | lcd.display(img) 136 | 137 | 138 | def Fing_Write(): 139 | global sensor 140 | flag0 = 0 141 | while flag0 == 0: 142 | img=sensor.snapshot() 143 | for blobs in img.find_blobs([(60, 100, -17, 127, -6, 127)], 144 | x_stride = 100,y_stride = 100,pixels_threshold = 15000, area_threshold = 15000, 145 | merge = True, margin = 200): 146 | if abs(blobs.w()-blobs.h()) > 50: 147 | continue 148 | img.draw_rectangle(blobs[0:4]) 149 | #print(blobs) 150 | str2 = str(blobs.cx()) + ',' + str(blobs.cy()) + ',0,0,0,0,0,0\r\n' 151 | uart.write(str2) # 发送数据 152 | print(str2+'发送完毕') 153 | img.draw_cross(blobs.cx(),blobs.cy(), color=255, size = 8) 154 | lcd.display(img) 155 | flag0 = 1 156 | lcd.display(img) 157 | 158 | 159 | 160 | 161 | while True: 162 | img=sensor.snapshot() # 获取图片 163 | lcd.display(img) 164 | flag = 2 165 | #accept_uart() 166 | 167 | if flag == 1: 168 | flag = 0 169 | Fing_Write() 170 | if flag == 2: 171 | flag = 0 172 | pointPosition() 173 | 174 | 175 | 176 | #led(0) 177 | #img=sensor.snapshot() # 获取图片 178 | #lcd.display(img) 179 | 180 | #flag = 4 181 | #accept_uart() # 串口接受 182 | 183 | #if flag == 2: 184 | #print('进入2') 185 | #flag = 0 186 | #pointPosition(2) 187 | #if flag == 3: 188 | #print('进入3') 189 | #flag = 0 190 | #pointPosition(3) 191 | #if flag == 4: 192 | #print('进入第三题') 193 | #flag = 0 194 | #A4Ti(3) 195 | #if flag == 6: 196 | #print('进入第四题') 197 | #flag = 0 198 | #A4Ti(4) 199 | #pointPosition() 200 | -------------------------------------------------------------------------------- /GreenServo/STM32/.gitignore: -------------------------------------------------------------------------------- 1 | Output/* 2 | Output/*.hex -------------------------------------------------------------------------------- /GreenServo/STM32/.mxproject: -------------------------------------------------------------------------------- 1 | [PreviousLibFiles] 2 | LibFiles=Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_def.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_bus.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_system.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_utils.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio_ex.h;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_uart.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_usart.h;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_def.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_bus.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_system.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_utils.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio_ex.h;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_uart.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_usart.h;Drivers\CMSIS\Device\ST\STM32F1xx\Include\stm32f103xb.h;Drivers\CMSIS\Device\ST\STM32F1xx\Include\stm32f1xx.h;Drivers\CMSIS\Device\ST\STM32F1xx\Include\system_stm32f1xx.h;Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;Drivers\CMSIS\Include\cmsis_armcc.h;Drivers\CMSIS\Include\cmsis_armclang.h;Drivers\CMSIS\Include\cmsis_compiler.h;Drivers\CMSIS\Include\cmsis_gcc.h;Drivers\CMSIS\Include\cmsis_iccarm.h;Drivers\CMSIS\Include\cmsis_version.h;Drivers\CMSIS\Include\core_armv8mbl.h;Drivers\CMSIS\Include\core_armv8mml.h;Drivers\CMSIS\Include\core_cm0.h;Drivers\CMSIS\Include\core_cm0plus.h;Drivers\CMSIS\Include\core_cm1.h;Drivers\CMSIS\Include\core_cm23.h;Drivers\CMSIS\Include\core_cm3.h;Drivers\CMSIS\Include\core_cm33.h;Drivers\CMSIS\Include\core_cm4.h;Drivers\CMSIS\Include\core_cm7.h;Drivers\CMSIS\Include\core_sc000.h;Drivers\CMSIS\Include\core_sc300.h;Drivers\CMSIS\Include\mpu_armv7.h;Drivers\CMSIS\Include\mpu_armv8.h;Drivers\CMSIS\Include\tz_context.h; 3 | 4 | [PreviousUsedKeilFiles] 5 | SourceFiles=..\Core\Src\main.c;..\Core\Src\gpio.c;..\Core\Src\tim.c;..\Core\Src\usart.c;..\Core\Src\stm32f1xx_it.c;..\Core\Src\stm32f1xx_hal_msp.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;..\Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;..\Core\Src\system_stm32f1xx.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;..\Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;..\Core\Src\system_stm32f1xx.c;;; 6 | HeaderPath=..\Drivers\STM32F1xx_HAL_Driver\Inc;..\Drivers\STM32F1xx_HAL_Driver\Inc\Legacy;..\Drivers\CMSIS\Device\ST\STM32F1xx\Include;..\Drivers\CMSIS\Include;..\Core\Inc; 7 | CDefines=USE_HAL_DRIVER;STM32F103xB;USE_HAL_DRIVER;USE_HAL_DRIVER; 8 | 9 | [PreviousGenFiles] 10 | AdvancedFolderStructure=true 11 | HeaderFileListSize=6 12 | HeaderFiles#0=..\Core\Inc\gpio.h 13 | HeaderFiles#1=..\Core\Inc\tim.h 14 | HeaderFiles#2=..\Core\Inc\usart.h 15 | HeaderFiles#3=..\Core\Inc\stm32f1xx_it.h 16 | HeaderFiles#4=..\Core\Inc\stm32f1xx_hal_conf.h 17 | HeaderFiles#5=..\Core\Inc\main.h 18 | HeaderFolderListSize=1 19 | HeaderPath#0=..\Core\Inc 20 | HeaderFiles=; 21 | SourceFileListSize=6 22 | SourceFiles#0=..\Core\Src\gpio.c 23 | SourceFiles#1=..\Core\Src\tim.c 24 | SourceFiles#2=..\Core\Src\usart.c 25 | SourceFiles#3=..\Core\Src\stm32f1xx_it.c 26 | SourceFiles#4=..\Core\Src\stm32f1xx_hal_msp.c 27 | SourceFiles#5=..\Core\Src\main.c 28 | SourceFolderListSize=1 29 | SourcePath#0=..\Core\Src 30 | SourceFiles=; 31 | 32 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Inc/gpio.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file gpio.h 5 | * @brief This file contains all the function prototypes for 6 | * the gpio.c file 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __GPIO_H__ 22 | #define __GPIO_H__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "main.h" 30 | 31 | /* USER CODE BEGIN Includes */ 32 | 33 | /* USER CODE END Includes */ 34 | 35 | /* USER CODE BEGIN Private defines */ 36 | 37 | /* USER CODE END Private defines */ 38 | 39 | void MX_GPIO_Init(void); 40 | 41 | /* USER CODE BEGIN Prototypes */ 42 | 43 | /* USER CODE END Prototypes */ 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | #endif /*__ GPIO_H__ */ 49 | 50 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Inc/main.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : main.h 5 | * @brief : Header for main.c file. 6 | * This file contains the common defines of the application. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __MAIN_H 23 | #define __MAIN_H 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "stm32f1xx_hal.h" 31 | 32 | /* Private includes ----------------------------------------------------------*/ 33 | /* USER CODE BEGIN Includes */ 34 | 35 | /* USER CODE END Includes */ 36 | 37 | /* Exported types ------------------------------------------------------------*/ 38 | /* USER CODE BEGIN ET */ 39 | 40 | /* USER CODE END ET */ 41 | 42 | /* Exported constants --------------------------------------------------------*/ 43 | /* USER CODE BEGIN EC */ 44 | 45 | /* USER CODE END EC */ 46 | 47 | /* Exported macro ------------------------------------------------------------*/ 48 | /* USER CODE BEGIN EM */ 49 | 50 | /* USER CODE END EM */ 51 | 52 | /* Exported functions prototypes ---------------------------------------------*/ 53 | void Error_Handler(void); 54 | 55 | /* USER CODE BEGIN EFP */ 56 | 57 | /* USER CODE END EFP */ 58 | 59 | /* Private defines -----------------------------------------------------------*/ 60 | #define LED_1_Pin GPIO_PIN_13 61 | #define LED_1_GPIO_Port GPIOC 62 | #define LED_2_Pin GPIO_PIN_14 63 | #define LED_2_GPIO_Port GPIOC 64 | #define KEY_1_Pin GPIO_PIN_0 65 | #define KEY_1_GPIO_Port GPIOA 66 | #define KEY_1_EXTI_IRQn EXTI0_IRQn 67 | #define KEY_2_Pin GPIO_PIN_1 68 | #define KEY_2_GPIO_Port GPIOA 69 | #define KEY_2_EXTI_IRQn EXTI1_IRQn 70 | #define Buzzer_Pin_Pin GPIO_PIN_12 71 | #define Buzzer_Pin_GPIO_Port GPIOB 72 | #define I2C_SCL_Pin GPIO_PIN_8 73 | #define I2C_SCL_GPIO_Port GPIOB 74 | #define I2C_SDA_Pin GPIO_PIN_9 75 | #define I2C_SDA_GPIO_Port GPIOB 76 | /* USER CODE BEGIN Private defines */ 77 | 78 | /* USER CODE END Private defines */ 79 | 80 | #ifdef __cplusplus 81 | } 82 | #endif 83 | 84 | #endif /* __MAIN_H */ 85 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Inc/stm32f1xx_it.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f1xx_it.h 5 | * @brief This file contains the headers of the interrupt handlers. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F1xx_IT_H 22 | #define __STM32F1xx_IT_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Private includes ----------------------------------------------------------*/ 29 | /* USER CODE BEGIN Includes */ 30 | 31 | /* USER CODE END Includes */ 32 | 33 | /* Exported types ------------------------------------------------------------*/ 34 | /* USER CODE BEGIN ET */ 35 | 36 | /* USER CODE END ET */ 37 | 38 | /* Exported constants --------------------------------------------------------*/ 39 | /* USER CODE BEGIN EC */ 40 | 41 | /* USER CODE END EC */ 42 | 43 | /* Exported macro ------------------------------------------------------------*/ 44 | /* USER CODE BEGIN EM */ 45 | 46 | /* USER CODE END EM */ 47 | 48 | /* Exported functions prototypes ---------------------------------------------*/ 49 | void NMI_Handler(void); 50 | void HardFault_Handler(void); 51 | void MemManage_Handler(void); 52 | void BusFault_Handler(void); 53 | void UsageFault_Handler(void); 54 | void SVC_Handler(void); 55 | void DebugMon_Handler(void); 56 | void PendSV_Handler(void); 57 | void SysTick_Handler(void); 58 | void EXTI0_IRQHandler(void); 59 | void EXTI1_IRQHandler(void); 60 | void TIM4_IRQHandler(void); 61 | void USART1_IRQHandler(void); 62 | void USART2_IRQHandler(void); 63 | /* USER CODE BEGIN EFP */ 64 | 65 | /* USER CODE END EFP */ 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | 71 | #endif /* __STM32F1xx_IT_H */ 72 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Inc/tim.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file tim.h 5 | * @brief This file contains all the function prototypes for 6 | * the tim.c file 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __TIM_H__ 22 | #define __TIM_H__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "main.h" 30 | 31 | /* USER CODE BEGIN Includes */ 32 | 33 | /* USER CODE END Includes */ 34 | 35 | extern TIM_HandleTypeDef htim1; 36 | 37 | extern TIM_HandleTypeDef htim4; 38 | 39 | /* USER CODE BEGIN Private defines */ 40 | 41 | /* USER CODE END Private defines */ 42 | 43 | void MX_TIM1_Init(void); 44 | void MX_TIM4_Init(void); 45 | 46 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); 47 | 48 | /* USER CODE BEGIN Prototypes */ 49 | 50 | /* USER CODE END Prototypes */ 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | #endif /* __TIM_H__ */ 57 | 58 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Inc/usart.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file usart.h 5 | * @brief This file contains all the function prototypes for 6 | * the usart.c file 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USART_H__ 22 | #define __USART_H__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "main.h" 30 | 31 | /* USER CODE BEGIN Includes */ 32 | 33 | /* USER CODE END Includes */ 34 | 35 | extern UART_HandleTypeDef huart1; 36 | 37 | extern UART_HandleTypeDef huart2; 38 | 39 | /* USER CODE BEGIN Private defines */ 40 | 41 | /* USER CODE END Private defines */ 42 | 43 | void MX_USART1_UART_Init(void); 44 | void MX_USART2_UART_Init(void); 45 | 46 | /* USER CODE BEGIN Prototypes */ 47 | 48 | /* USER CODE END Prototypes */ 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /* __USART_H__ */ 55 | 56 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Src/gpio.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file gpio.c 5 | * @brief This file provides code for the configuration 6 | * of all used GPIO pins. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | #include "gpio.h" 23 | 24 | /* USER CODE BEGIN 0 */ 25 | 26 | /* USER CODE END 0 */ 27 | 28 | /*----------------------------------------------------------------------------*/ 29 | /* Configure GPIO */ 30 | /*----------------------------------------------------------------------------*/ 31 | /* USER CODE BEGIN 1 */ 32 | 33 | /* USER CODE END 1 */ 34 | 35 | /** Configure pins as 36 | * Analog 37 | * Input 38 | * Output 39 | * EVENT_OUT 40 | * EXTI 41 | */ 42 | void MX_GPIO_Init(void) 43 | { 44 | 45 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 46 | 47 | /* GPIO Ports Clock Enable */ 48 | __HAL_RCC_GPIOC_CLK_ENABLE(); 49 | __HAL_RCC_GPIOD_CLK_ENABLE(); 50 | __HAL_RCC_GPIOA_CLK_ENABLE(); 51 | __HAL_RCC_GPIOB_CLK_ENABLE(); 52 | 53 | /*Configure GPIO pin Output Level */ 54 | HAL_GPIO_WritePin(GPIOC, LED_1_Pin|LED_2_Pin, GPIO_PIN_RESET); 55 | 56 | /*Configure GPIO pin Output Level */ 57 | HAL_GPIO_WritePin(Buzzer_Pin_GPIO_Port, Buzzer_Pin_Pin, GPIO_PIN_SET); 58 | 59 | /*Configure GPIO pin Output Level */ 60 | HAL_GPIO_WritePin(GPIOB, I2C_SCL_Pin|I2C_SDA_Pin, GPIO_PIN_RESET); 61 | 62 | /*Configure GPIO pins : PCPin PCPin */ 63 | GPIO_InitStruct.Pin = LED_1_Pin|LED_2_Pin; 64 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 65 | GPIO_InitStruct.Pull = GPIO_NOPULL; 66 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 67 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 68 | 69 | /*Configure GPIO pins : PAPin PAPin */ 70 | GPIO_InitStruct.Pin = KEY_1_Pin|KEY_2_Pin; 71 | GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 72 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; 73 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 74 | 75 | /*Configure GPIO pin : PtPin */ 76 | GPIO_InitStruct.Pin = Buzzer_Pin_Pin; 77 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 78 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; 79 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; 80 | HAL_GPIO_Init(Buzzer_Pin_GPIO_Port, &GPIO_InitStruct); 81 | 82 | /*Configure GPIO pins : PBPin PBPin */ 83 | GPIO_InitStruct.Pin = I2C_SCL_Pin|I2C_SDA_Pin; 84 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; 85 | GPIO_InitStruct.Pull = GPIO_PULLUP; 86 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 87 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 88 | 89 | /* EXTI interrupt init*/ 90 | HAL_NVIC_SetPriority(EXTI0_IRQn, 1, 0); 91 | HAL_NVIC_EnableIRQ(EXTI0_IRQn); 92 | 93 | HAL_NVIC_SetPriority(EXTI1_IRQn, 1, 0); 94 | HAL_NVIC_EnableIRQ(EXTI1_IRQn); 95 | 96 | } 97 | 98 | /* USER CODE BEGIN 2 */ 99 | 100 | /* USER CODE END 2 */ 101 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Src/main.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : main.c 5 | * @brief : Main program body 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | /* Includes ------------------------------------------------------------------*/ 20 | #include "main.h" 21 | #include "tim.h" 22 | #include "usart.h" 23 | #include "gpio.h" 24 | 25 | /* Private includes ----------------------------------------------------------*/ 26 | /* USER CODE BEGIN Includes */ 27 | #include "app.h" //应用层头文件 28 | /* USER CODE END Includes */ 29 | 30 | /* Private typedef -----------------------------------------------------------*/ 31 | /* USER CODE BEGIN PTD */ 32 | 33 | /* USER CODE END PTD */ 34 | 35 | /* Private define ------------------------------------------------------------*/ 36 | /* USER CODE BEGIN PD */ 37 | /* USER CODE END PD */ 38 | 39 | /* Private macro -------------------------------------------------------------*/ 40 | /* USER CODE BEGIN PM */ 41 | 42 | /* USER CODE END PM */ 43 | 44 | /* Private variables ---------------------------------------------------------*/ 45 | 46 | /* USER CODE BEGIN PV */ 47 | 48 | /* USER CODE END PV */ 49 | 50 | /* Private function prototypes -----------------------------------------------*/ 51 | void SystemClock_Config(void); 52 | /* USER CODE BEGIN PFP */ 53 | 54 | /* USER CODE END PFP */ 55 | 56 | /* Private user code ---------------------------------------------------------*/ 57 | /* USER CODE BEGIN 0 */ 58 | 59 | /* USER CODE END 0 */ 60 | 61 | /** 62 | * @brief The application entry point. 63 | * @retval int 64 | */ 65 | int main(void) 66 | { 67 | /* USER CODE BEGIN 1 */ 68 | 69 | /* USER CODE END 1 */ 70 | 71 | /* MCU Configuration--------------------------------------------------------*/ 72 | 73 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 74 | HAL_Init(); 75 | 76 | /* USER CODE BEGIN Init */ 77 | 78 | /* USER CODE END Init */ 79 | 80 | /* Configure the system clock */ 81 | SystemClock_Config(); 82 | 83 | /* USER CODE BEGIN SysInit */ 84 | 85 | /* USER CODE END SysInit */ 86 | 87 | /* Initialize all configured peripherals */ 88 | MX_GPIO_Init(); 89 | MX_TIM1_Init(); 90 | MX_USART1_UART_Init(); 91 | MX_TIM4_Init(); 92 | MX_USART2_UART_Init(); 93 | /* USER CODE BEGIN 2 */ 94 | App_Init(); //应用层初始化 95 | 96 | 97 | 98 | 99 | /* USER CODE END 2 */ 100 | 101 | /* Infinite loop */ 102 | /* USER CODE BEGIN WHILE */ 103 | while (1) 104 | { 105 | /* USER CODE END WHILE */ 106 | 107 | /* USER CODE BEGIN 3 */ 108 | App_Task(); //应用层任务 109 | 110 | } 111 | /* USER CODE END 3 */ 112 | } 113 | 114 | /** 115 | * @brief System Clock Configuration 116 | * @retval None 117 | */ 118 | void SystemClock_Config(void) 119 | { 120 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 121 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 122 | 123 | /** Initializes the RCC Oscillators according to the specified parameters 124 | * in the RCC_OscInitTypeDef structure. 125 | */ 126 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 127 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; 128 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; 129 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; 130 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 131 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 132 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; 133 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 134 | { 135 | Error_Handler(); 136 | } 137 | 138 | /** Initializes the CPU, AHB and APB buses clocks 139 | */ 140 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 141 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 142 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 143 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 144 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 145 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 146 | 147 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) 148 | { 149 | Error_Handler(); 150 | } 151 | } 152 | 153 | /* USER CODE BEGIN 4 */ 154 | 155 | /* USER CODE END 4 */ 156 | 157 | /** 158 | * @brief This function is executed in case of error occurrence. 159 | * @retval None 160 | */ 161 | void Error_Handler(void) 162 | { 163 | /* USER CODE BEGIN Error_Handler_Debug */ 164 | /* User can add his own implementation to report the HAL error return state */ 165 | __disable_irq(); 166 | while (1) 167 | { 168 | } 169 | /* USER CODE END Error_Handler_Debug */ 170 | } 171 | 172 | #ifdef USE_FULL_ASSERT 173 | /** 174 | * @brief Reports the name of the source file and the source line number 175 | * where the assert_param error has occurred. 176 | * @param file: pointer to the source file name 177 | * @param line: assert_param error line source number 178 | * @retval None 179 | */ 180 | void assert_failed(uint8_t *file, uint32_t line) 181 | { 182 | /* USER CODE BEGIN 6 */ 183 | /* User can add his own implementation to report the file name and line number, 184 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 185 | /* USER CODE END 6 */ 186 | } 187 | #endif /* USE_FULL_ASSERT */ 188 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Src/stm32f1xx_hal_msp.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f1xx_hal_msp.c 5 | * @brief This file provides code for the MSP Initialization 6 | * and de-Initialization codes. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | #include "main.h" 23 | /* USER CODE BEGIN Includes */ 24 | 25 | /* USER CODE END Includes */ 26 | 27 | /* Private typedef -----------------------------------------------------------*/ 28 | /* USER CODE BEGIN TD */ 29 | 30 | /* USER CODE END TD */ 31 | 32 | /* Private define ------------------------------------------------------------*/ 33 | /* USER CODE BEGIN Define */ 34 | 35 | /* USER CODE END Define */ 36 | 37 | /* Private macro -------------------------------------------------------------*/ 38 | /* USER CODE BEGIN Macro */ 39 | 40 | /* USER CODE END Macro */ 41 | 42 | /* Private variables ---------------------------------------------------------*/ 43 | /* USER CODE BEGIN PV */ 44 | 45 | /* USER CODE END PV */ 46 | 47 | /* Private function prototypes -----------------------------------------------*/ 48 | /* USER CODE BEGIN PFP */ 49 | 50 | /* USER CODE END PFP */ 51 | 52 | /* External functions --------------------------------------------------------*/ 53 | /* USER CODE BEGIN ExternalFunctions */ 54 | 55 | /* USER CODE END ExternalFunctions */ 56 | 57 | /* USER CODE BEGIN 0 */ 58 | 59 | /* USER CODE END 0 */ 60 | /** 61 | * Initializes the Global MSP. 62 | */ 63 | void HAL_MspInit(void) 64 | { 65 | /* USER CODE BEGIN MspInit 0 */ 66 | 67 | /* USER CODE END MspInit 0 */ 68 | 69 | __HAL_RCC_AFIO_CLK_ENABLE(); 70 | __HAL_RCC_PWR_CLK_ENABLE(); 71 | 72 | /* System interrupt init*/ 73 | 74 | /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled 75 | */ 76 | __HAL_AFIO_REMAP_SWJ_NOJTAG(); 77 | 78 | /* USER CODE BEGIN MspInit 1 */ 79 | 80 | /* USER CODE END MspInit 1 */ 81 | } 82 | 83 | /* USER CODE BEGIN 1 */ 84 | 85 | /* USER CODE END 1 */ 86 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Src/tim.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file tim.c 5 | * @brief This file provides code for the configuration 6 | * of the TIM instances. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "tim.h" 22 | 23 | /* USER CODE BEGIN 0 */ 24 | 25 | /* USER CODE END 0 */ 26 | 27 | TIM_HandleTypeDef htim1; 28 | TIM_HandleTypeDef htim4; 29 | 30 | /* TIM1 init function */ 31 | void MX_TIM1_Init(void) 32 | { 33 | 34 | /* USER CODE BEGIN TIM1_Init 0 */ 35 | 36 | /* USER CODE END TIM1_Init 0 */ 37 | 38 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; 39 | TIM_MasterConfigTypeDef sMasterConfig = {0}; 40 | TIM_OC_InitTypeDef sConfigOC = {0}; 41 | TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; 42 | 43 | /* USER CODE BEGIN TIM1_Init 1 */ 44 | 45 | /* USER CODE END TIM1_Init 1 */ 46 | htim1.Instance = TIM1; 47 | htim1.Init.Prescaler = 72-1; 48 | htim1.Init.CounterMode = TIM_COUNTERMODE_UP; 49 | htim1.Init.Period = 20000-1; 50 | htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 51 | htim1.Init.RepetitionCounter = 0; 52 | htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; 53 | if (HAL_TIM_Base_Init(&htim1) != HAL_OK) 54 | { 55 | Error_Handler(); 56 | } 57 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 58 | if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) 59 | { 60 | Error_Handler(); 61 | } 62 | if (HAL_TIM_PWM_Init(&htim1) != HAL_OK) 63 | { 64 | Error_Handler(); 65 | } 66 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; 67 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 68 | if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) 69 | { 70 | Error_Handler(); 71 | } 72 | sConfigOC.OCMode = TIM_OCMODE_PWM1; 73 | sConfigOC.Pulse = 0; 74 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; 75 | sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; 76 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; 77 | sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; 78 | sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; 79 | if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) 80 | { 81 | Error_Handler(); 82 | } 83 | if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) 84 | { 85 | Error_Handler(); 86 | } 87 | sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; 88 | sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; 89 | sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; 90 | sBreakDeadTimeConfig.DeadTime = 0; 91 | sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; 92 | sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; 93 | sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; 94 | if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK) 95 | { 96 | Error_Handler(); 97 | } 98 | /* USER CODE BEGIN TIM1_Init 2 */ 99 | 100 | /* USER CODE END TIM1_Init 2 */ 101 | HAL_TIM_MspPostInit(&htim1); 102 | 103 | } 104 | /* TIM4 init function */ 105 | void MX_TIM4_Init(void) 106 | { 107 | 108 | /* USER CODE BEGIN TIM4_Init 0 */ 109 | 110 | /* USER CODE END TIM4_Init 0 */ 111 | 112 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; 113 | TIM_MasterConfigTypeDef sMasterConfig = {0}; 114 | 115 | /* USER CODE BEGIN TIM4_Init 1 */ 116 | 117 | /* USER CODE END TIM4_Init 1 */ 118 | htim4.Instance = TIM4; 119 | htim4.Init.Prescaler = 72-1; 120 | htim4.Init.CounterMode = TIM_COUNTERMODE_UP; 121 | htim4.Init.Period = 1000-1; 122 | htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 123 | htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; 124 | if (HAL_TIM_Base_Init(&htim4) != HAL_OK) 125 | { 126 | Error_Handler(); 127 | } 128 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 129 | if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) 130 | { 131 | Error_Handler(); 132 | } 133 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; 134 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 135 | if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) 136 | { 137 | Error_Handler(); 138 | } 139 | /* USER CODE BEGIN TIM4_Init 2 */ 140 | 141 | /* USER CODE END TIM4_Init 2 */ 142 | 143 | } 144 | 145 | void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) 146 | { 147 | 148 | if(tim_baseHandle->Instance==TIM1) 149 | { 150 | /* USER CODE BEGIN TIM1_MspInit 0 */ 151 | 152 | /* USER CODE END TIM1_MspInit 0 */ 153 | /* TIM1 clock enable */ 154 | __HAL_RCC_TIM1_CLK_ENABLE(); 155 | /* USER CODE BEGIN TIM1_MspInit 1 */ 156 | 157 | /* USER CODE END TIM1_MspInit 1 */ 158 | } 159 | else if(tim_baseHandle->Instance==TIM4) 160 | { 161 | /* USER CODE BEGIN TIM4_MspInit 0 */ 162 | 163 | /* USER CODE END TIM4_MspInit 0 */ 164 | /* TIM4 clock enable */ 165 | __HAL_RCC_TIM4_CLK_ENABLE(); 166 | 167 | /* TIM4 interrupt Init */ 168 | HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0); 169 | HAL_NVIC_EnableIRQ(TIM4_IRQn); 170 | /* USER CODE BEGIN TIM4_MspInit 1 */ 171 | 172 | /* USER CODE END TIM4_MspInit 1 */ 173 | } 174 | } 175 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) 176 | { 177 | 178 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 179 | if(timHandle->Instance==TIM1) 180 | { 181 | /* USER CODE BEGIN TIM1_MspPostInit 0 */ 182 | 183 | /* USER CODE END TIM1_MspPostInit 0 */ 184 | 185 | __HAL_RCC_GPIOA_CLK_ENABLE(); 186 | /**TIM1 GPIO Configuration 187 | PA8 ------> TIM1_CH1 188 | PA11 ------> TIM1_CH4 189 | */ 190 | GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_11; 191 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 192 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 193 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 194 | 195 | /* USER CODE BEGIN TIM1_MspPostInit 1 */ 196 | 197 | /* USER CODE END TIM1_MspPostInit 1 */ 198 | } 199 | 200 | } 201 | 202 | void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) 203 | { 204 | 205 | if(tim_baseHandle->Instance==TIM1) 206 | { 207 | /* USER CODE BEGIN TIM1_MspDeInit 0 */ 208 | 209 | /* USER CODE END TIM1_MspDeInit 0 */ 210 | /* Peripheral clock disable */ 211 | __HAL_RCC_TIM1_CLK_DISABLE(); 212 | /* USER CODE BEGIN TIM1_MspDeInit 1 */ 213 | 214 | /* USER CODE END TIM1_MspDeInit 1 */ 215 | } 216 | else if(tim_baseHandle->Instance==TIM4) 217 | { 218 | /* USER CODE BEGIN TIM4_MspDeInit 0 */ 219 | 220 | /* USER CODE END TIM4_MspDeInit 0 */ 221 | /* Peripheral clock disable */ 222 | __HAL_RCC_TIM4_CLK_DISABLE(); 223 | 224 | /* TIM4 interrupt Deinit */ 225 | HAL_NVIC_DisableIRQ(TIM4_IRQn); 226 | /* USER CODE BEGIN TIM4_MspDeInit 1 */ 227 | 228 | /* USER CODE END TIM4_MspDeInit 1 */ 229 | } 230 | } 231 | 232 | /* USER CODE BEGIN 1 */ 233 | 234 | /* USER CODE END 1 */ 235 | -------------------------------------------------------------------------------- /GreenServo/STM32/Core/Src/usart.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file usart.c 5 | * @brief This file provides code for the configuration 6 | * of the USART instances. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "usart.h" 22 | 23 | /* USER CODE BEGIN 0 */ 24 | 25 | /* USER CODE END 0 */ 26 | 27 | UART_HandleTypeDef huart1; 28 | UART_HandleTypeDef huart2; 29 | 30 | /* USART1 init function */ 31 | 32 | void MX_USART1_UART_Init(void) 33 | { 34 | 35 | /* USER CODE BEGIN USART1_Init 0 */ 36 | 37 | /* USER CODE END USART1_Init 0 */ 38 | 39 | /* USER CODE BEGIN USART1_Init 1 */ 40 | 41 | /* USER CODE END USART1_Init 1 */ 42 | huart1.Instance = USART1; 43 | huart1.Init.BaudRate = 115200; 44 | huart1.Init.WordLength = UART_WORDLENGTH_8B; 45 | huart1.Init.StopBits = UART_STOPBITS_1; 46 | huart1.Init.Parity = UART_PARITY_NONE; 47 | huart1.Init.Mode = UART_MODE_TX_RX; 48 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; 49 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; 50 | if (HAL_UART_Init(&huart1) != HAL_OK) 51 | { 52 | Error_Handler(); 53 | } 54 | /* USER CODE BEGIN USART1_Init 2 */ 55 | 56 | /* USER CODE END USART1_Init 2 */ 57 | 58 | } 59 | /* USART2 init function */ 60 | 61 | void MX_USART2_UART_Init(void) 62 | { 63 | 64 | /* USER CODE BEGIN USART2_Init 0 */ 65 | 66 | /* USER CODE END USART2_Init 0 */ 67 | 68 | /* USER CODE BEGIN USART2_Init 1 */ 69 | 70 | /* USER CODE END USART2_Init 1 */ 71 | huart2.Instance = USART2; 72 | huart2.Init.BaudRate = 115200; 73 | huart2.Init.WordLength = UART_WORDLENGTH_8B; 74 | huart2.Init.StopBits = UART_STOPBITS_1; 75 | huart2.Init.Parity = UART_PARITY_NONE; 76 | huart2.Init.Mode = UART_MODE_TX_RX; 77 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 78 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; 79 | if (HAL_UART_Init(&huart2) != HAL_OK) 80 | { 81 | Error_Handler(); 82 | } 83 | /* USER CODE BEGIN USART2_Init 2 */ 84 | 85 | /* USER CODE END USART2_Init 2 */ 86 | 87 | } 88 | 89 | void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) 90 | { 91 | 92 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 93 | if(uartHandle->Instance==USART1) 94 | { 95 | /* USER CODE BEGIN USART1_MspInit 0 */ 96 | 97 | /* USER CODE END USART1_MspInit 0 */ 98 | /* USART1 clock enable */ 99 | __HAL_RCC_USART1_CLK_ENABLE(); 100 | 101 | __HAL_RCC_GPIOA_CLK_ENABLE(); 102 | /**USART1 GPIO Configuration 103 | PA9 ------> USART1_TX 104 | PA10 ------> USART1_RX 105 | */ 106 | GPIO_InitStruct.Pin = GPIO_PIN_9; 107 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 108 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 109 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 110 | 111 | GPIO_InitStruct.Pin = GPIO_PIN_10; 112 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 113 | GPIO_InitStruct.Pull = GPIO_NOPULL; 114 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 115 | 116 | /* USART1 interrupt Init */ 117 | HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); 118 | HAL_NVIC_EnableIRQ(USART1_IRQn); 119 | /* USER CODE BEGIN USART1_MspInit 1 */ 120 | 121 | /* USER CODE END USART1_MspInit 1 */ 122 | } 123 | else if(uartHandle->Instance==USART2) 124 | { 125 | /* USER CODE BEGIN USART2_MspInit 0 */ 126 | 127 | /* USER CODE END USART2_MspInit 0 */ 128 | /* USART2 clock enable */ 129 | __HAL_RCC_USART2_CLK_ENABLE(); 130 | 131 | __HAL_RCC_GPIOA_CLK_ENABLE(); 132 | /**USART2 GPIO Configuration 133 | PA2 ------> USART2_TX 134 | PA3 ------> USART2_RX 135 | */ 136 | GPIO_InitStruct.Pin = GPIO_PIN_2; 137 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 138 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 139 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 140 | 141 | GPIO_InitStruct.Pin = GPIO_PIN_3; 142 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 143 | GPIO_InitStruct.Pull = GPIO_NOPULL; 144 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 145 | 146 | /* USART2 interrupt Init */ 147 | HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); 148 | HAL_NVIC_EnableIRQ(USART2_IRQn); 149 | /* USER CODE BEGIN USART2_MspInit 1 */ 150 | 151 | /* USER CODE END USART2_MspInit 1 */ 152 | } 153 | } 154 | 155 | void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) 156 | { 157 | 158 | if(uartHandle->Instance==USART1) 159 | { 160 | /* USER CODE BEGIN USART1_MspDeInit 0 */ 161 | 162 | /* USER CODE END USART1_MspDeInit 0 */ 163 | /* Peripheral clock disable */ 164 | __HAL_RCC_USART1_CLK_DISABLE(); 165 | 166 | /**USART1 GPIO Configuration 167 | PA9 ------> USART1_TX 168 | PA10 ------> USART1_RX 169 | */ 170 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); 171 | 172 | /* USART1 interrupt Deinit */ 173 | HAL_NVIC_DisableIRQ(USART1_IRQn); 174 | /* USER CODE BEGIN USART1_MspDeInit 1 */ 175 | 176 | /* USER CODE END USART1_MspDeInit 1 */ 177 | } 178 | else if(uartHandle->Instance==USART2) 179 | { 180 | /* USER CODE BEGIN USART2_MspDeInit 0 */ 181 | 182 | /* USER CODE END USART2_MspDeInit 0 */ 183 | /* Peripheral clock disable */ 184 | __HAL_RCC_USART2_CLK_DISABLE(); 185 | 186 | /**USART2 GPIO Configuration 187 | PA2 ------> USART2_TX 188 | PA3 ------> USART2_RX 189 | */ 190 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); 191 | 192 | /* USART2 interrupt Deinit */ 193 | HAL_NVIC_DisableIRQ(USART2_IRQn); 194 | /* USER CODE BEGIN USART2_MspDeInit 1 */ 195 | 196 | /* USER CODE END USART2_MspDeInit 1 */ 197 | } 198 | } 199 | 200 | /* USER CODE BEGIN 1 */ 201 | 202 | /* USER CODE END 1 */ 203 | -------------------------------------------------------------------------------- /GreenServo/STM32/Drivers/CMSIS/Device/ST/STM32F1xx/Include/system_stm32f1xx.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f1xx.h 4 | * @author MCD Application Team 5 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2017-2021 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | 19 | /** @addtogroup CMSIS 20 | * @{ 21 | */ 22 | 23 | /** @addtogroup stm32f10x_system 24 | * @{ 25 | */ 26 | 27 | /** 28 | * @brief Define to prevent recursive inclusion 29 | */ 30 | #ifndef __SYSTEM_STM32F10X_H 31 | #define __SYSTEM_STM32F10X_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /** @addtogroup STM32F10x_System_Includes 38 | * @{ 39 | */ 40 | 41 | /** 42 | * @} 43 | */ 44 | 45 | 46 | /** @addtogroup STM32F10x_System_Exported_types 47 | * @{ 48 | */ 49 | 50 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 51 | extern const uint8_t AHBPrescTable[16U]; /*!< AHB prescalers table values */ 52 | extern const uint8_t APBPrescTable[8U]; /*!< APB prescalers table values */ 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | /** @addtogroup STM32F10x_System_Exported_Constants 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @addtogroup STM32F10x_System_Exported_Macros 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @addtogroup STM32F10x_System_Exported_Functions 75 | * @{ 76 | */ 77 | 78 | extern void SystemInit(void); 79 | extern void SystemCoreClockUpdate(void); 80 | /** 81 | * @} 82 | */ 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | #endif /*__SYSTEM_STM32F10X_H */ 89 | 90 | /** 91 | * @} 92 | */ 93 | 94 | /** 95 | * @} 96 | */ 97 | -------------------------------------------------------------------------------- /GreenServo/STM32/Drivers/CMSIS/Device/ST/STM32F1xx/LICENSE.txt: -------------------------------------------------------------------------------- 1 | This software component is provided to you as part of a software package and 2 | applicable license terms are in the Package_license file. If you received this 3 | software component outside of a package or without applicable license terms, 4 | the terms of the Apache-2.0 license shall apply. 5 | You may obtain a copy of the Apache-2.0 at: 6 | https://opensource.org/licenses/Apache-2.0 7 | -------------------------------------------------------------------------------- /GreenServo/STM32/Drivers/CMSIS/Include/cmsis_version.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_version.h 3 | * @brief CMSIS Core(M) Version definitions 4 | * @version V5.0.2 5 | * @date 19. April 2017 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2017 ARM Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef __CMSIS_VERSION_H 32 | #define __CMSIS_VERSION_H 33 | 34 | /* CMSIS Version definitions */ 35 | #define __CM_CMSIS_VERSION_MAIN ( 5U) /*!< [31:16] CMSIS Core(M) main version */ 36 | #define __CM_CMSIS_VERSION_SUB ( 1U) /*!< [15:0] CMSIS Core(M) sub version */ 37 | #define __CM_CMSIS_VERSION ((__CM_CMSIS_VERSION_MAIN << 16U) | \ 38 | __CM_CMSIS_VERSION_SUB ) /*!< CMSIS Core(M) version number */ 39 | #endif 40 | -------------------------------------------------------------------------------- /GreenServo/STM32/Drivers/CMSIS/Include/tz_context.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * @file tz_context.h 3 | * @brief Context Management for Armv8-M TrustZone 4 | * @version V1.0.1 5 | * @date 10. January 2018 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2017-2018 Arm Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef TZ_CONTEXT_H 32 | #define TZ_CONTEXT_H 33 | 34 | #include 35 | 36 | #ifndef TZ_MODULEID_T 37 | #define TZ_MODULEID_T 38 | /// \details Data type that identifies secure software modules called by a process. 39 | typedef uint32_t TZ_ModuleId_t; 40 | #endif 41 | 42 | /// \details TZ Memory ID identifies an allocated memory slot. 43 | typedef uint32_t TZ_MemoryId_t; 44 | 45 | /// Initialize secure context memory system 46 | /// \return execution status (1: success, 0: error) 47 | uint32_t TZ_InitContextSystem_S (void); 48 | 49 | /// Allocate context memory for calling secure software modules in TrustZone 50 | /// \param[in] module identifies software modules called from non-secure mode 51 | /// \return value != 0 id TrustZone memory slot identifier 52 | /// \return value 0 no memory available or internal error 53 | TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module); 54 | 55 | /// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S 56 | /// \param[in] id TrustZone memory slot identifier 57 | /// \return execution status (1: success, 0: error) 58 | uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id); 59 | 60 | /// Load secure context (called on RTOS thread context switch) 61 | /// \param[in] id TrustZone memory slot identifier 62 | /// \return execution status (1: success, 0: error) 63 | uint32_t TZ_LoadContext_S (TZ_MemoryId_t id); 64 | 65 | /// Store secure context (called on RTOS thread context switch) 66 | /// \param[in] id TrustZone memory slot identifier 67 | /// \return execution status (1: success, 0: error) 68 | uint32_t TZ_StoreContext_S (TZ_MemoryId_t id); 69 | 70 | #endif // TZ_CONTEXT_H 71 | -------------------------------------------------------------------------------- /GreenServo/STM32/Drivers/STM32F1xx_HAL_Driver/LICENSE.txt: -------------------------------------------------------------------------------- 1 | This software component is provided to you as part of a software package and 2 | applicable license terms are in the Package_license file. If you received this 3 | software component outside of a package or without applicable license terms, 4 | the terms of the BSD-3-Clause license shall apply. 5 | You may obtain a copy of the BSD-3-Clause at: 6 | https://opensource.org/licenses/BSD-3-Clause 7 | -------------------------------------------------------------------------------- /GreenServo/STM32/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_gpio_ex.c 4 | * @author MCD Application Team 5 | * @brief GPIO Extension HAL module driver. 6 | * This file provides firmware functions to manage the following 7 | * functionalities of the General Purpose Input/Output (GPIO) extension peripheral. 8 | * + Extended features functions 9 | * 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | * Copyright (c) 2016 STMicroelectronics. 14 | * All rights reserved. 15 | * 16 | * This software is licensed under terms that can be found in the LICENSE file 17 | * in the root directory of this software component. 18 | * If no LICENSE file comes with this software, it is provided AS-IS. 19 | * 20 | ****************************************************************************** 21 | @verbatim 22 | ============================================================================== 23 | ##### GPIO Peripheral extension features ##### 24 | ============================================================================== 25 | [..] GPIO module on STM32F1 family, manage also the AFIO register: 26 | (+) Possibility to use the EVENTOUT Cortex feature 27 | 28 | ##### How to use this driver ##### 29 | ============================================================================== 30 | [..] This driver provides functions to use EVENTOUT Cortex feature 31 | (#) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout() 32 | (#) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout() 33 | (#) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout() 34 | 35 | @endverbatim 36 | ****************************************************************************** 37 | */ 38 | 39 | /* Includes ------------------------------------------------------------------*/ 40 | #include "stm32f1xx_hal.h" 41 | 42 | /** @addtogroup STM32F1xx_HAL_Driver 43 | * @{ 44 | */ 45 | 46 | /** @defgroup GPIOEx GPIOEx 47 | * @brief GPIO HAL module driver 48 | * @{ 49 | */ 50 | 51 | #ifdef HAL_GPIO_MODULE_ENABLED 52 | 53 | /** @defgroup GPIOEx_Exported_Functions GPIOEx Exported Functions 54 | * @{ 55 | */ 56 | 57 | /** @defgroup GPIOEx_Exported_Functions_Group1 Extended features functions 58 | * @brief Extended features functions 59 | * 60 | @verbatim 61 | ============================================================================== 62 | ##### Extended features functions ##### 63 | ============================================================================== 64 | [..] This section provides functions allowing to: 65 | (+) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout() 66 | (+) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout() 67 | (+) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout() 68 | 69 | @endverbatim 70 | * @{ 71 | */ 72 | 73 | /** 74 | * @brief Configures the port and pin on which the EVENTOUT Cortex signal will be connected. 75 | * @param GPIO_PortSource Select the port used to output the Cortex EVENTOUT signal. 76 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PORT. 77 | * @param GPIO_PinSource Select the pin used to output the Cortex EVENTOUT signal. 78 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PIN. 79 | * @retval None 80 | */ 81 | void HAL_GPIOEx_ConfigEventout(uint32_t GPIO_PortSource, uint32_t GPIO_PinSource) 82 | { 83 | /* Verify the parameters */ 84 | assert_param(IS_AFIO_EVENTOUT_PORT(GPIO_PortSource)); 85 | assert_param(IS_AFIO_EVENTOUT_PIN(GPIO_PinSource)); 86 | 87 | /* Apply the new configuration */ 88 | MODIFY_REG(AFIO->EVCR, (AFIO_EVCR_PORT) | (AFIO_EVCR_PIN), (GPIO_PortSource) | (GPIO_PinSource)); 89 | } 90 | 91 | /** 92 | * @brief Enables the Event Output. 93 | * @retval None 94 | */ 95 | void HAL_GPIOEx_EnableEventout(void) 96 | { 97 | SET_BIT(AFIO->EVCR, AFIO_EVCR_EVOE); 98 | } 99 | 100 | /** 101 | * @brief Disables the Event Output. 102 | * @retval None 103 | */ 104 | void HAL_GPIOEx_DisableEventout(void) 105 | { 106 | CLEAR_BIT(AFIO->EVCR, AFIO_EVCR_EVOE); 107 | } 108 | 109 | /** 110 | * @} 111 | */ 112 | 113 | /** 114 | * @} 115 | */ 116 | 117 | #endif /* HAL_GPIO_MODULE_ENABLED */ 118 | 119 | /** 120 | * @} 121 | */ 122 | 123 | /** 124 | * @} 125 | */ 126 | 127 | -------------------------------------------------------------------------------- /GreenServo/STM32/MDK-ARM/.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | -------------------------------------------------------------------------------- /GreenServo/STM32/MDK-ARM/DebugConfig/STM32code_STM32F103C8_1.0.0.dbgconf: -------------------------------------------------------------------------------- 1 | // File: STM32F101_102_103_105_107.dbgconf 2 | // Version: 1.0.0 3 | // Note: refer to STM32F101xx STM32F102xx STM32F103xx STM32F105xx STM32F107xx Reference manual (RM0008) 4 | // STM32F101xx STM32F102xx STM32F103xx STM32F105xx STM32F107xx datasheets 5 | 6 | // <<< Use Configuration Wizard in Context Menu >>> 7 | 8 | // Debug MCU configuration register (DBGMCU_CR) 9 | // Reserved bits must be kept at reset value 10 | // DBG_TIM11_STOP TIM11 counter stopped when core is halted 11 | // DBG_TIM10_STOP TIM10 counter stopped when core is halted 12 | // DBG_TIM9_STOP TIM9 counter stopped when core is halted 13 | // DBG_TIM14_STOP TIM14 counter stopped when core is halted 14 | // DBG_TIM13_STOP TIM13 counter stopped when core is halted 15 | // DBG_TIM12_STOP TIM12 counter stopped when core is halted 16 | // DBG_CAN2_STOP Debug CAN2 stopped when core is halted 17 | // DBG_TIM7_STOP TIM7 counter stopped when core is halted 18 | // DBG_TIM6_STOP TIM6 counter stopped when core is halted 19 | // DBG_TIM5_STOP TIM5 counter stopped when core is halted 20 | // DBG_TIM8_STOP TIM8 counter stopped when core is halted 21 | // DBG_I2C2_SMBUS_TIMEOUT SMBUS timeout mode stopped when core is halted 22 | // DBG_I2C1_SMBUS_TIMEOUT SMBUS timeout mode stopped when core is halted 23 | // DBG_CAN1_STOP Debug CAN1 stopped when Core is halted 24 | // DBG_TIM4_STOP TIM4 counter stopped when core is halted 25 | // DBG_TIM3_STOP TIM3 counter stopped when core is halted 26 | // DBG_TIM2_STOP TIM2 counter stopped when core is halted 27 | // DBG_TIM1_STOP TIM1 counter stopped when core is halted 28 | // DBG_WWDG_STOP Debug window watchdog stopped when core is halted 29 | // DBG_IWDG_STOP Debug independent watchdog stopped when core is halted 30 | // DBG_STANDBY Debug standby mode 31 | // DBG_STOP Debug stop mode 32 | // DBG_SLEEP Debug sleep mode 33 | // 34 | DbgMCU_CR = 0x00000007; 35 | 36 | // <<< end of configuration section >>> 37 | -------------------------------------------------------------------------------- /GreenServo/STM32/MDK-ARM/EventRecorderStub.scvd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /GreenServo/STM32/MDK-ARM/RTE/_STM32code/RTE_Components.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Auto generated Run-Time-Environment Configuration File 4 | * *** Do not modify ! *** 5 | * 6 | * Project: 'STM32code' 7 | * Target: 'STM32code' 8 | */ 9 | 10 | #ifndef RTE_COMPONENTS_H 11 | #define RTE_COMPONENTS_H 12 | 13 | 14 | /* 15 | * Define the Device Header File: 16 | */ 17 | #define CMSIS_device_header "stm32f10x.h" 18 | 19 | 20 | 21 | #endif /* RTE_COMPONENTS_H */ 22 | -------------------------------------------------------------------------------- /GreenServo/STM32/STM32code.ioc: -------------------------------------------------------------------------------- 1 | #MicroXplorer Configuration settings - do not modify 2 | File.Version=6 3 | GPIO.groupedBy=Group By Peripherals 4 | KeepUserPlacement=false 5 | Mcu.CPN=STM32F103C8T6 6 | Mcu.Family=STM32F1 7 | Mcu.IP0=NVIC 8 | Mcu.IP1=RCC 9 | Mcu.IP2=SYS 10 | Mcu.IP3=TIM1 11 | Mcu.IP4=TIM4 12 | Mcu.IP5=USART1 13 | Mcu.IP6=USART2 14 | Mcu.IPNb=7 15 | Mcu.Name=STM32F103C(8-B)Tx 16 | Mcu.Package=LQFP48 17 | Mcu.Pin0=PC13-TAMPER-RTC 18 | Mcu.Pin1=PC14-OSC32_IN 19 | Mcu.Pin10=PA9 20 | Mcu.Pin11=PA10 21 | Mcu.Pin12=PA11 22 | Mcu.Pin13=PA13 23 | Mcu.Pin14=PA14 24 | Mcu.Pin15=PB8 25 | Mcu.Pin16=PB9 26 | Mcu.Pin17=VP_SYS_VS_Systick 27 | Mcu.Pin18=VP_TIM1_VS_ClockSourceINT 28 | Mcu.Pin19=VP_TIM4_VS_ClockSourceINT 29 | Mcu.Pin2=PD0-OSC_IN 30 | Mcu.Pin3=PD1-OSC_OUT 31 | Mcu.Pin4=PA0-WKUP 32 | Mcu.Pin5=PA1 33 | Mcu.Pin6=PA2 34 | Mcu.Pin7=PA3 35 | Mcu.Pin8=PB12 36 | Mcu.Pin9=PA8 37 | Mcu.PinsNb=20 38 | Mcu.ThirdPartyNb=0 39 | Mcu.UserConstants= 40 | Mcu.UserName=STM32F103C8Tx 41 | MxCube.Version=6.6.1 42 | MxDb.Version=DB.6.0.60 43 | NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 44 | NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 45 | NVIC.EXTI0_IRQn=true\:1\:0\:true\:false\:true\:true\:true\:true 46 | NVIC.EXTI1_IRQn=true\:1\:0\:true\:false\:true\:true\:true\:true 47 | NVIC.ForceEnableDMAVector=true 48 | NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 49 | NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 50 | NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 51 | NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 52 | NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 53 | NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 54 | NVIC.SysTick_IRQn=true\:0\:0\:true\:false\:true\:false\:true\:false 55 | NVIC.TIM4_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true 56 | NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true 57 | NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true 58 | NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 59 | PA0-WKUP.GPIOParameters=GPIO_PuPd,GPIO_Label 60 | PA0-WKUP.GPIO_Label=KEY_1 61 | PA0-WKUP.GPIO_PuPd=GPIO_PULLDOWN 62 | PA0-WKUP.Locked=true 63 | PA0-WKUP.Signal=GPXTI0 64 | PA1.GPIOParameters=GPIO_PuPd,GPIO_Label 65 | PA1.GPIO_Label=KEY_2 66 | PA1.GPIO_PuPd=GPIO_PULLDOWN 67 | PA1.Locked=true 68 | PA1.Signal=GPXTI1 69 | PA10.Mode=Asynchronous 70 | PA10.Signal=USART1_RX 71 | PA11.Signal=S_TIM1_CH4 72 | PA13.Mode=Serial_Wire 73 | PA13.Signal=SYS_JTMS-SWDIO 74 | PA14.Mode=Serial_Wire 75 | PA14.Signal=SYS_JTCK-SWCLK 76 | PA2.Mode=Asynchronous 77 | PA2.Signal=USART2_TX 78 | PA3.Mode=Asynchronous 79 | PA3.Signal=USART2_RX 80 | PA8.Signal=S_TIM1_CH1 81 | PA9.Mode=Asynchronous 82 | PA9.Signal=USART1_TX 83 | PB12.GPIOParameters=GPIO_Speed,PinState,GPIO_PuPd,GPIO_Label 84 | PB12.GPIO_Label=Buzzer_Pin 85 | PB12.GPIO_PuPd=GPIO_PULLDOWN 86 | PB12.GPIO_Speed=GPIO_SPEED_FREQ_MEDIUM 87 | PB12.Locked=true 88 | PB12.PinState=GPIO_PIN_SET 89 | PB12.Signal=GPIO_Output 90 | PB8.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultOutputPP 91 | PB8.GPIO_Label=I2C_SCL 92 | PB8.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_OD 93 | PB8.GPIO_PuPd=GPIO_PULLUP 94 | PB8.GPIO_Speed=GPIO_SPEED_FREQ_HIGH 95 | PB8.Locked=true 96 | PB8.Signal=GPIO_Output 97 | PB9.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultOutputPP 98 | PB9.GPIO_Label=I2C_SDA 99 | PB9.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_OD 100 | PB9.GPIO_PuPd=GPIO_PULLUP 101 | PB9.GPIO_Speed=GPIO_SPEED_FREQ_HIGH 102 | PB9.Locked=true 103 | PB9.Signal=GPIO_Output 104 | PC13-TAMPER-RTC.GPIOParameters=GPIO_Label 105 | PC13-TAMPER-RTC.GPIO_Label=LED_1 106 | PC13-TAMPER-RTC.Locked=true 107 | PC13-TAMPER-RTC.Signal=GPIO_Output 108 | PC14-OSC32_IN.GPIOParameters=GPIO_Label 109 | PC14-OSC32_IN.GPIO_Label=LED_2 110 | PC14-OSC32_IN.Locked=true 111 | PC14-OSC32_IN.Signal=GPIO_Output 112 | PD0-OSC_IN.Mode=HSE-External-Oscillator 113 | PD0-OSC_IN.Signal=RCC_OSC_IN 114 | PD1-OSC_OUT.Mode=HSE-External-Oscillator 115 | PD1-OSC_OUT.Signal=RCC_OSC_OUT 116 | PinOutPanel.RotationAngle=0 117 | ProjectManager.AskForMigrate=true 118 | ProjectManager.BackupPrevious=false 119 | ProjectManager.CompilerOptimize=6 120 | ProjectManager.ComputerToolchain=false 121 | ProjectManager.CoupleFile=true 122 | ProjectManager.CustomerFirmwarePackage= 123 | ProjectManager.DefaultFWLocation=true 124 | ProjectManager.DeletePrevious=true 125 | ProjectManager.DeviceId=STM32F103C8Tx 126 | ProjectManager.FirmwarePackage=STM32Cube FW_F1 V1.8.5 127 | ProjectManager.FreePins=false 128 | ProjectManager.HalAssertFull=false 129 | ProjectManager.HeapSize=0x200 130 | ProjectManager.KeepUserCode=true 131 | ProjectManager.LastFirmware=true 132 | ProjectManager.LibraryCopy=1 133 | ProjectManager.MainLocation=Core/Src 134 | ProjectManager.NoMain=false 135 | ProjectManager.PreviousToolchain= 136 | ProjectManager.ProjectBuild=false 137 | ProjectManager.ProjectFileName=STM32code.ioc 138 | ProjectManager.ProjectName=STM32code 139 | ProjectManager.RegisterCallBack= 140 | ProjectManager.StackSize=0x400 141 | ProjectManager.TargetToolchain=MDK-ARM V5.32 142 | ProjectManager.ToolChainLocation= 143 | ProjectManager.UnderRoot=false 144 | ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_TIM1_Init-TIM1-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_TIM4_Init-TIM4-false-HAL-true,6-MX_USART2_UART_Init-USART2-false-HAL-true 145 | RCC.ADCFreqValue=36000000 146 | RCC.AHBFreq_Value=72000000 147 | RCC.APB1CLKDivider=RCC_HCLK_DIV2 148 | RCC.APB1Freq_Value=36000000 149 | RCC.APB1TimFreq_Value=72000000 150 | RCC.APB2Freq_Value=72000000 151 | RCC.APB2TimFreq_Value=72000000 152 | RCC.FCLKCortexFreq_Value=72000000 153 | RCC.FamilyName=M 154 | RCC.HCLKFreq_Value=72000000 155 | RCC.IPParameters=ADCFreqValue,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,MCOFreq_Value,PLLCLKFreq_Value,PLLMCOFreq_Value,PLLMUL,PLLSourceVirtual,SYSCLKFreq_VALUE,SYSCLKSource,TimSysFreq_Value,USBFreq_Value,VCOOutput2Freq_Value 156 | RCC.MCOFreq_Value=72000000 157 | RCC.PLLCLKFreq_Value=72000000 158 | RCC.PLLMCOFreq_Value=36000000 159 | RCC.PLLMUL=RCC_PLL_MUL9 160 | RCC.PLLSourceVirtual=RCC_PLLSOURCE_HSE 161 | RCC.SYSCLKFreq_VALUE=72000000 162 | RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK 163 | RCC.TimSysFreq_Value=72000000 164 | RCC.USBFreq_Value=72000000 165 | RCC.VCOOutput2Freq_Value=8000000 166 | SH.GPXTI0.0=GPIO_EXTI0 167 | SH.GPXTI0.ConfNb=1 168 | SH.GPXTI1.0=GPIO_EXTI1 169 | SH.GPXTI1.ConfNb=1 170 | SH.S_TIM1_CH1.0=TIM1_CH1,PWM Generation1 CH1 171 | SH.S_TIM1_CH1.ConfNb=1 172 | SH.S_TIM1_CH4.0=TIM1_CH4,PWM Generation4 CH4 173 | SH.S_TIM1_CH4.ConfNb=1 174 | TIM1.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE 175 | TIM1.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 176 | TIM1.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 177 | TIM1.IPParameters=Channel-PWM Generation1 CH1,Prescaler,Period,AutoReloadPreload,Channel-PWM Generation4 CH4 178 | TIM1.Period=20000-1 179 | TIM1.Prescaler=72-1 180 | TIM4.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE 181 | TIM4.IPParameters=Prescaler,Period,AutoReloadPreload 182 | TIM4.Period=1000-1 183 | TIM4.Prescaler=72-1 184 | USART1.IPParameters=VirtualMode 185 | USART1.VirtualMode=VM_ASYNC 186 | USART2.IPParameters=VirtualMode 187 | USART2.VirtualMode=VM_ASYNC 188 | VP_SYS_VS_Systick.Mode=SysTick 189 | VP_SYS_VS_Systick.Signal=SYS_VS_Systick 190 | VP_TIM1_VS_ClockSourceINT.Mode=Internal 191 | VP_TIM1_VS_ClockSourceINT.Signal=TIM1_VS_ClockSourceINT 192 | VP_TIM4_VS_ClockSourceINT.Mode=Internal 193 | VP_TIM4_VS_ClockSourceINT.Signal=TIM4_VS_ClockSourceINT 194 | board=custom 195 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/APP/app.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-08-02 12-02-17 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-04 15-50-04 6 | * @FilePath: \Project\RedServo\STM32\User\APP\app.c 7 | * @Description: 主程序入口,包含主循环和中断回调函数,以及一些全局变量的定义 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "app.h" 12 | #include "Buzzer.h" 13 | #include "at24c02.h" 14 | #include "string.h" 15 | #include 16 | 17 | //题目选择标志位 18 | extern uint8_t Problem_Flag = 0; 19 | //消息接收标志位 20 | extern uint8_t K210_Flag =0; 21 | 22 | //--------------------------舵机参考系--------------------- 23 | //手动调整 24 | uint16_t Screen_Width = 310; 25 | uint16_t Screen_Height = 294; 26 | //中心位置处参数 27 | extern uint16_t Centre_A = 1460; 28 | extern uint16_t Centre_B = 1457; 29 | // 舵机(左上位置) 30 | extern uint16_t left_PWM = 1613; 31 | extern uint16_t up_PWM =1270; 32 | // 舵机B (右下位置) 33 | extern uint16_t right_PWM = 1307; 34 | extern uint16_t down_PWM = 1565; 35 | 36 | 37 | //--------------------------K210参考系--------------------- 38 | //左上角坐标 39 | extern uint16_t K210_left = 0; 40 | extern uint16_t K210_up= 0; 41 | //右下角坐标 42 | extern uint16_t K210_right = 0; 43 | extern uint16_t K210_down= 0; 44 | //宽度长度 45 | extern uint16_t K210_Width = 0; 46 | extern uint16_t K210_Height= 0; 47 | 48 | extern uint16_t K210_data[8]; 49 | 50 | //实时位置 51 | extern uint16_t pwm_A ; 52 | extern uint16_t pwm_B ; 53 | 54 | #define DEVICE_ADDRESS 0x50 55 | 56 | /** 57 | * @description: 系统应用初始化 58 | * @return {*} 59 | */ 60 | void App_Init(void) 61 | { 62 | OLED_Init(); // 0.96oled初始化 63 | OLED_Clear(); 64 | USART_IT_Config(); // 总串口接收初始化 65 | PWM_Init(); 66 | //Yuntai_Control(); // 云台初始化 67 | HAL_TIM_Base_Start_IT(&htim4); // 启动定时器4 开始数据采样,执行控制 68 | //Yuntaiz_B_Move(Centre_B,0); 69 | //SearchTheScreen(); 70 | pwm_A =1500; 71 | pwm_B =1628; 72 | Yuntaiz_A_Move(pwm_A,0); 73 | Yuntaiz_B_Move(pwm_B,0); 74 | } 75 | 76 | 77 | 78 | //上电搜索屏幕 79 | void SearchTheScreen(void) 80 | { 81 | Yuntaiz_A_Move(2000,0); 82 | pwm_B =1628; 83 | Yuntaiz_B_Move(pwm_B,0); 84 | DEBUG_info("搜索屏幕","开始搜索屏幕 \r\n"); 85 | uint8_t data_str[255]={0}; 86 | //通知K210传递参数 87 | sprintf((char*)data_str, "id:%d,x:%d,y:%d",1,0,0); 88 | Usart2_SendString(data_str); 89 | 90 | 91 | for(;;) 92 | { 93 | for(pwm_A = 2000;pwm_A>=1000;pwm_A=pwm_A-100) 94 | { 95 | Yuntaiz_A_Move(pwm_A,0); 96 | HAL_Delay(100); 97 | if(K210_Flag == 1) 98 | { 99 | DEBUG_info("搜索屏幕","搜索到屏幕!\r\n"); 100 | printf("pwm_A:%d,pwm_B:%d\r\n",pwm_A,pwm_B); 101 | Buzzer_LongBeep(); 102 | K210_Flag = 0; 103 | return; 104 | } 105 | } 106 | for(pwm_A = 1000;pwm_A<=2000;pwm_A=pwm_A+100) 107 | { 108 | Yuntaiz_A_Move(pwm_A,0); 109 | HAL_Delay(100); 110 | if(K210_Flag == 1) 111 | { 112 | DEBUG_info("搜索屏幕","搜索到屏幕!\r\n"); 113 | printf("pwm_A:%d,pwm_B:%d\r\n",pwm_A,pwm_B); 114 | Buzzer_LongBeep(); 115 | K210_Flag = 0; 116 | return; 117 | } 118 | } 119 | } 120 | 121 | } 122 | 123 | 124 | //题目1 125 | void Problem1(void){ 126 | 127 | //运动目标位置复位,一键启动自动追踪系统 128 | uint8_t data_str[255]={0}; 129 | //通知K210传递参数 130 | sprintf((char*)data_str, "id:%d,x:%d,y:%d",2,0,0); 131 | Usart2_SendString(data_str); 132 | while(1) 133 | { 134 | if (K210_Flag == 1) 135 | { 136 | printf("进行计算"); 137 | //进行pid控制 138 | uint16_t red_x=K210_data[0] ,red_y=K210_data[1],green_x=K210_data[2],green_y=K210_data[3]; 139 | if(red_x > 2000) 140 | { 141 | red_x -=2000; 142 | } 143 | int16_t x_err = red_x - green_x, y_err = red_y -green_y; 144 | if(x_err <=2 && y_err<=2) 145 | { 146 | //声光提示 147 | Buzzer_LongBeep(); 148 | LED_Toggle(2); 149 | HAL_Delay(500); 150 | LED_Toggle(2); 151 | HAL_Delay(500); 152 | LED_Toggle(2); 153 | } 154 | else{ 155 | Yuntai_PID(x_err,y_err); 156 | } 157 | 158 | K210_Flag = 0; 159 | } 160 | } 161 | 162 | } 163 | 164 | 165 | //题目2 166 | void Problem2(void){ 167 | 168 | 169 | } 170 | 171 | //题目3 172 | void Problem3(void){ 173 | 174 | 175 | } 176 | 177 | 178 | 179 | 180 | /** 181 | * @description: 系统应用循环任务 182 | * @return {*} 183 | */ 184 | void App_Task(void) 185 | { 186 | static uint8_t last_Flag =0; 187 | Menu_Refresh(); // 刷新菜单 188 | switch (Problem_Flag) 189 | { 190 | case 1: 191 | if(last_Flag != Problem_Flag) 192 | { 193 | Problem1(); 194 | last_Flag = Problem_Flag; 195 | } 196 | break; 197 | case 2: 198 | if(last_Flag != Problem_Flag) 199 | { 200 | Problem2(); 201 | last_Flag = Problem_Flag; 202 | } 203 | break; 204 | case 3: 205 | if(last_Flag != Problem_Flag) 206 | { 207 | Problem3(); 208 | last_Flag = Problem_Flag; 209 | } 210 | break; 211 | default: 212 | break; 213 | } 214 | } 215 | 216 | 217 | 218 | 219 | // 定时器中断回调函数(1ms一次) 220 | uint8_t LED_Heartbeat = 0; // LED心跳 221 | uint16_t encoder_count = 0; 222 | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 223 | { 224 | if (htim == &htim4) // 判断中断是否来自于定时器4 225 | { 226 | // 心跳(50ms一次) 227 | LED_Heartbeat++; 228 | if (LED_Heartbeat == 30) 229 | { 230 | LED_Toggle(1); 231 | } 232 | } 233 | } 234 | 235 | 236 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/APP/app.h: -------------------------------------------------------------------------------- 1 | #ifndef __APP_H 2 | #define __APP_H 3 | 4 | #include "main.h" 5 | #include "tim.h" 6 | #include "led.h" 7 | #include "oled.h" 8 | #include "gui.h" 9 | #include "control.h" 10 | #include "yuntai.h" 11 | #include "serial_it_config.h" 12 | 13 | 14 | 15 | void App_Init(void); 16 | void App_Task(void); 17 | void MPU6050_Data_Read(void); 18 | void Motor_Speed_Read(void); 19 | void Car_PID_Ctrl(void); 20 | void Update_Parameters(void); 21 | //У׼ģʽ 22 | void System_Calibration(void); 23 | void SearchTheScreen(void); 24 | #endif // !__APP_H 25 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/AT24C02/at24c02.c: -------------------------------------------------------------------------------- 1 | #include "at24c02.h" 2 | 3 | 4 | // #define AT24C02_SDA_Pin GPIO_PIN_10 5 | // #define AT24C02_SDA_GPIO_Port GPIOB 6 | // #define AT24C02_SCL_Pin GPIO_PIN_11 7 | // #define AT24C02_SCL_GPIO_Port GPIOB 8 | 9 | #define I2C_SCL_SET AT24C02_SCL_GPIO_Port->BSRR = AT24C02_SCL_Pin 10 | #define I2C_SCL_CLR AT24C02_SCL_GPIO_Port->BRR = AT24C02_SCL_Pin 11 | 12 | #define I2C_SDA_SET AT24C02_SDA_GPIO_Port->BSRR = AT24C02_SDA_Pin 13 | #define I2C_SDA_CLR AT24C02_SDA_GPIO_Port->BRR = AT24C02_SDA_Pin 14 | #define I2C_SDA_IN AT24C02_SDA_GPIO_Port->IDR &AT24C02_SDA_Pin 15 | 16 | #define pr_err(...) 17 | #define pr_dbg(...) 18 | 19 | #define I2C_TIME 6 20 | 21 | 22 | 23 | 24 | 25 | void delay_us(uint32_t nus) 26 | { 27 | uint32_t temp; 28 | SysTick->LOAD = 9*nus; 29 | SysTick->VAL=0X00;//清空计数器 30 | SysTick->CTRL=0X01;//使能,减到零是无动作,采用外部时钟源 31 | do 32 | { 33 | temp=SysTick->CTRL;//读取当前倒计数值 34 | }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 35 | SysTick->CTRL=0x00; //关闭计数器 36 | SysTick->VAL =0X00; //清空计数器 37 | } 38 | 39 | 40 | //产生IIC起始信号 41 | void IIC_Start_1(void) 42 | { 43 | if (!(I2C_SDA_IN)) //释放SDA 44 | { 45 | I2C_SCL_CLR; 46 | delay_us(I2C_TIME / 2); 47 | I2C_SDA_SET; 48 | delay_us(I2C_TIME / 2); //保证起始信号为高电平 49 | } 50 | 51 | I2C_SCL_SET; //保证为高电平 52 | delay_us(I2C_TIME); 53 | 54 | I2C_SDA_CLR; // START:when CLK is high,DATA change form high to low 55 | delay_us(I2C_TIME); 56 | I2C_SCL_CLR; 57 | delay_us(I2C_TIME / 2); 58 | } 59 | //产生IIC停止信号 60 | void IIC_Stop_1(void) 61 | { 62 | delay_us(I2C_TIME / 2); 63 | I2C_SCL_CLR; 64 | delay_us(I2C_TIME / 2); 65 | I2C_SDA_CLR; // STOP:when CLK is high DATA change form low to high 66 | delay_us(I2C_TIME); 67 | 68 | I2C_SCL_SET; 69 | delay_us(I2C_TIME / 2); 70 | I2C_SDA_SET; //发送I2C总线结束信号 71 | delay_us(I2C_TIME / 2); 72 | } 73 | 74 | //产生ACK应答 75 | void IIC_Ack(void) 76 | { 77 | I2C_SDA_CLR; 78 | delay_us(I2C_TIME / 2); 79 | I2C_SCL_SET; 80 | delay_us(I2C_TIME); 81 | I2C_SCL_CLR; 82 | delay_us(I2C_TIME / 2); 83 | } 84 | //不产生ACK应答 85 | void IIC_NAck(void) 86 | { 87 | I2C_SDA_SET; 88 | delay_us(I2C_TIME / 2); 89 | I2C_SCL_SET; 90 | delay_us(I2C_TIME); 91 | I2C_SCL_CLR; 92 | delay_us(I2C_TIME / 2); 93 | } 94 | // IIC发送一个字节 95 | //返回从机有无应答 96 | // 1,有应答 97 | // 0,无应答 CLK结束为低电平 98 | bool IIC_Send_Byte(uint8_t txd) 99 | { 100 | uint8_t t, ack; 101 | delay_us(I2C_TIME / 2); // 102 | I2C_SCL_CLR; //拉低时钟开始数据传输 103 | delay_us(I2C_TIME / 2); // 104 | for (t = 0; t < 8; t++) 105 | { 106 | delay_us(I2C_TIME / 2); 107 | if (txd & 0x80) 108 | { 109 | I2C_SDA_SET; 110 | } 111 | else 112 | { 113 | I2C_SDA_CLR; 114 | } 115 | txd <<= 1; 116 | delay_us(I2C_TIME / 2); //对TEA5767这三个延时都是必须的 117 | I2C_SCL_SET; 118 | delay_us(I2C_TIME); 119 | I2C_SCL_CLR; //发出数据 120 | } 121 | //释放SDA 122 | delay_us(I2C_TIME / 2); 123 | I2C_SDA_SET; 124 | delay_us(I2C_TIME / 2); 125 | I2C_SCL_SET; 126 | delay_us(I2C_TIME); //等待应答周期 127 | ack = I2C_SDA_IN; 128 | I2C_SCL_CLR; 129 | delay_us(I2C_TIME / 2); // 130 | if (!ack) 131 | { 132 | return true; 133 | } 134 | else 135 | { 136 | return false; //应答失败 137 | } 138 | } 139 | //读1个字节,ack=1时,发送ACK,ack=0,发送nACK 140 | uint8_t IIC_Read_Byte(unsigned char ack) 141 | { 142 | unsigned char i, receive = 0; 143 | delay_us(I2C_TIME / 2); 144 | I2C_SCL_CLR; 145 | 146 | for (i = 0; i < 8; i++) 147 | { 148 | delay_us(I2C_TIME / 2); 149 | I2C_SCL_SET; 150 | delay_us(I2C_TIME / 2); 151 | receive <<= 1; 152 | if (I2C_SDA_IN) 153 | receive++; 154 | delay_us(I2C_TIME / 2); 155 | I2C_SCL_CLR; 156 | delay_us(I2C_TIME / 2); 157 | } 158 | if (!ack) 159 | IIC_NAck(); //发送nACK 160 | else 161 | IIC_Ack(); //发送ACK 162 | return receive; 163 | } 164 | 165 | #define CHECK_ACK(X) \ 166 | ack = X; \ 167 | if (ack == false) \ 168 | { \ 169 | ; \ 170 | break; \ 171 | } 172 | 173 | // addr 地址8字节对齐 一次 一次写入不得超过8字节 写入间隔不得小于10ms 174 | bool I2C_Write(uint8_t devAdd, uint8_t addr, uint8_t data[], int len) 175 | { 176 | bool ack = false; 177 | IIC_Start_1(); 178 | do 179 | { 180 | CHECK_ACK(IIC_Send_Byte(devAdd)); 181 | 182 | CHECK_ACK(IIC_Send_Byte(addr)); //发送低地址 183 | 184 | for (int i = 0; i < len; i++) 185 | { 186 | CHECK_ACK(IIC_Send_Byte(data[i])); //发送字节 187 | } 188 | } while (0); 189 | IIC_Stop_1(); //产生一个停止条件 190 | return ack; 191 | } 192 | 193 | bool I2C_Read(uint8_t devAdd, uint8_t nAddr, uint8_t data[], uint8_t len) 194 | { 195 | bool ack = false; 196 | IIC_Start_1(); 197 | do 198 | { 199 | CHECK_ACK(IIC_Send_Byte(devAdd)); //发送器件地址0XA0,写数据 200 | 201 | CHECK_ACK(IIC_Send_Byte(nAddr)); 202 | 203 | IIC_Start_1(); 204 | CHECK_ACK(IIC_Send_Byte(devAdd + 1)); //进入接收模式 205 | 206 | for (int i = 0; i < len - 1; i++) 207 | { 208 | data[i] = IIC_Read_Byte(1); 209 | } 210 | data[len - 1] = IIC_Read_Byte(0); 211 | } while (0); 212 | IIC_Stop_1(); //产生一个停止条件 213 | return ack; 214 | } -------------------------------------------------------------------------------- /GreenServo/STM32/User/AT24C02/at24c02.h: -------------------------------------------------------------------------------- 1 | #ifndef I2C_LIBRARY_H 2 | #define I2C_LIBRARY_H 3 | 4 | #include 5 | #include 6 | #include "main.h" 7 | 8 | void delay_us(uint32_t nus); 9 | 10 | void IIC_Start_1(void); 11 | void IIC_Stop_1(void); 12 | void IIC_Ack(void); 13 | void IIC_NAck(void); 14 | bool IIC_Send_Byte(uint8_t txd); 15 | uint8_t IIC_Read_Byte(unsigned char ack); 16 | 17 | bool I2C_Write(uint8_t devAdd, uint8_t addr, uint8_t data[], int len); 18 | bool I2C_Read(uint8_t devAdd, uint8_t nAddr, uint8_t data[], uint8_t len); 19 | 20 | #endif /* I2C_LIBRARY_H */ 21 | 22 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/Buzzer/Buzzer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-08-02 12-02-17 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-03 15-30-02 6 | * @FilePath: \Project\RedServo\STM32\User\Buzzer\Buzzer.c 7 | * @Description: 蜂鸣器模块,包含蜂鸣器初始化,蜂鸣器控制,蜂鸣器短响,蜂鸣器长响 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "Buzzer.h" 12 | 13 | //在这里修改蜂鸣器引脚 ,或者使用cubemx生成的宏定义(引脚命名为Buzzer_Pin) 14 | // #define Buzzer_Pin_Pin GPIO_PIN_11 15 | // #define Buzzer_Pin_GPIO_Port GPIOB 16 | 17 | /** 18 | * @brief 蜂鸣器初始化 19 | * @param 无 20 | * @retval 无 21 | */ 22 | void Buzzer_Init(void) 23 | { 24 | //引脚初始化 25 | } 26 | 27 | 28 | /** 29 | * @brief 蜂鸣器控制 30 | * @param 无 31 | * @retval 无 32 | */ 33 | void Buzzer_Control(uint8_t status) 34 | { 35 | //蜂鸣器控制 36 | if (status == 0) 37 | { 38 | HAL_GPIO_WritePin(Buzzer_Pin_GPIO_Port, Buzzer_Pin_Pin, GPIO_PIN_SET); 39 | } 40 | else if (status == 1) 41 | { 42 | HAL_GPIO_WritePin(Buzzer_Pin_GPIO_Port, Buzzer_Pin_Pin, GPIO_PIN_RESET); 43 | } 44 | 45 | } 46 | 47 | 48 | //蜂鸣器短响 49 | void Buzzer_ShortBeep(void) 50 | { 51 | Buzzer_Control(1); 52 | HAL_Delay(30); 53 | Buzzer_Control(0); 54 | } 55 | 56 | 57 | //蜂鸣器长响 58 | void Buzzer_LongBeep(void) 59 | { 60 | Buzzer_Control(1); 61 | HAL_Delay(1000); 62 | Buzzer_Control(0); 63 | } -------------------------------------------------------------------------------- /GreenServo/STM32/User/Buzzer/Buzzer.h: -------------------------------------------------------------------------------- 1 | #ifndef __Buzzer_H 2 | #define __Buzzer_H 3 | 4 | #include "main.h" 5 | 6 | void Buzzer_Init(void); 7 | void Buzzer_ShortBeep(void); 8 | void Buzzer_LongBeep(void); 9 | #endif // !__Buzzer_H 10 | 11 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/Control/control.c: -------------------------------------------------------------------------------- 1 | #include "control.h" 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/Control/control.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONTROL_H 2 | #define __CONTROL_H 3 | #include "main.h" 4 | 5 | #include "debug.h" 6 | 7 | void Motor_Speed_Read(void); 8 | void Car_PID_Ctrl(void); 9 | void Set_PID_Target(float temp_val); 10 | void Set_PID(int32_t kp, int32_t ki, int32_t kd); 11 | void Find_CCD_Zhongzhi(uint16_t *ADV); 12 | 13 | 14 | #endif // !__CONTROL_H 15 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/KEY/key.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-08-02 12-02-17 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-03 15-28-12 6 | * @FilePath: \Project\RedServo\STM32\User\KEY\key.c 7 | * @Description: 键盘扫描 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "key.h" 12 | #include "led.h" 13 | #include "gui.h" 14 | #include "yuntai.h" 15 | #include "Buzzer.h" 16 | #include "app.h" 17 | 18 | 19 | #define KEY1_Pin KEY_1_Pin 20 | #define KEY2_Pin KEY_2_Pin 21 | #define KEY3_Pin KEY_3_Pin 22 | #define KEY4_Pin KEY_4_Pin 23 | #define KEY5_Pin KEY_5_Pin 24 | #define KEY6_Pin KEY_6_Pin 25 | #define KEY7_Pin KEY_7_Pin 26 | #define KEY8_Pin KEY_8_Pin 27 | #define KEY9_Pin KEY_9_Pin 28 | #define KEY10_Pin KEY_10_Pin 29 | #define KEY11_Pin KEY_11_Pin 30 | 31 | 32 | 33 | #define DEBOUNCE_DELAY 250 // 设置消抖延时为200毫秒 34 | 35 | 36 | 37 | //GUI菜单标志位 38 | extern uint8_t GUI_Menu; 39 | 40 | //题目标志位 41 | extern uint8_t Problem_Flag; 42 | 43 | 44 | //急停标志位 45 | extern uint8_t Stop_Flag; 46 | 47 | // 初始位置 //舵机中值 48 | extern uint16_t pwm_A ; 49 | extern uint16_t pwm_B ; 50 | 51 | 52 | /** 53 | * @description: 按键初始化 (使用CubeMX自动生成的宏定义,就不用写这个函数了) 54 | * @return {*} 55 | */ 56 | void Key_Init(void) 57 | { 58 | 59 | } 60 | 61 | 62 | 63 | //题目切换按钮 64 | void Key_1_Callback(void) 65 | { 66 | 67 | //更换题目 68 | if(Problem_Flag < 3) 69 | { 70 | Problem_Flag++; 71 | } 72 | else 73 | { 74 | Problem_Flag = 0; 75 | } 76 | } 77 | 78 | 79 | 80 | // 急停按键 81 | void Key_2_Callback(void) 82 | { 83 | if(Stop_Flag == 1) 84 | { 85 | Stop_Flag=0; 86 | } 87 | else{ 88 | Stop_Flag=1; 89 | } 90 | 91 | } 92 | 93 | 94 | //设置舵机调试模式的按钮 95 | void Key_3_Callback(void) 96 | { 97 | 98 | } 99 | 100 | 101 | //上 102 | void Key_4_Callback(void) 103 | { 104 | 105 | } 106 | 107 | //下 108 | void Key_5_Callback(void){ 109 | 110 | } 111 | 112 | //左 113 | void Key_6_Callback(void){ 114 | 115 | } 116 | 117 | //右 118 | void Key_7_Callback(void){ 119 | 120 | } 121 | 122 | //题目1 //复位按键 123 | void Key_8_Callback(void){ 124 | 125 | } 126 | 127 | 128 | void Key_9_Callback(void){ 129 | //更换题目 130 | if(Problem_Flag == 2) 131 | { 132 | Problem_Flag =0; 133 | } 134 | else 135 | { 136 | Problem_Flag = 2; 137 | } 138 | } 139 | void Key_10_Callback(void){ 140 | //更换题目 141 | if(Problem_Flag == 3) 142 | { 143 | Problem_Flag =0; 144 | } 145 | else 146 | { 147 | Problem_Flag = 3; 148 | } 149 | } 150 | void Key_11_Callback(void){ 151 | //更换题目 152 | if(Problem_Flag == 4) 153 | { 154 | Problem_Flag =0; 155 | } 156 | else 157 | { 158 | Problem_Flag = 4; 159 | } 160 | } 161 | 162 | // 163 | /** 164 | * @description: 按键检测,外部中断回调函数 165 | * @param {uint16_t} GPIO_Pin 166 | * @return {*} 167 | */ 168 | void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) 169 | { 170 | 171 | /* Prevent unused argument(s) compilation warning */ 172 | UNUSED(GPIO_Pin); 173 | /* NOTE: This function Should not be modified, when the callback is needed, 174 | the HAL_GPIO_EXTI_Callback could be implemented in the user file 175 | */ 176 | // 按键按下 177 | if(GPIO_Pin == KEY1_Pin) 178 | { 179 | //printf("按键1"); 180 | Debounce(GPIO_Pin, Key_1_Callback); 181 | 182 | } 183 | else if(GPIO_Pin == KEY2_Pin) 184 | { 185 | 186 | // 按键2按下的处理代码 187 | Debounce(GPIO_Pin, Key_2_Callback); 188 | } 189 | } 190 | 191 | 192 | 193 | 194 | // 通用的按键消抖函数 195 | void Debounce(uint16_t GPIO_Pin, void (*callback)(void)) 196 | { 197 | static uint32_t lastTriggerTime = 0; 198 | uint32_t currentTime = HAL_GetTick(); // 获取当前时间戳 199 | 200 | if (currentTime - lastTriggerTime >= DEBOUNCE_DELAY) 201 | { 202 | Buzzer_ShortBeep(); 203 | callback(); // 调用传入的回调函数 204 | lastTriggerTime = currentTime; // 更新上一次触发的时间戳 205 | } 206 | } 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/KEY/key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H 2 | #define __KEY_H 3 | 4 | #include "main.h" 5 | void Debounce(uint16_t GPIO_Pin, void (*callback)(void)); 6 | void Key_Init(void); 7 | 8 | #endif // !__LED_H 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/LED/led.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-07 09-36-03 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-07-26 20-30-03 6 | * @FilePath: \MDK-ARMd:\duruofu\Project\Avoidance_Car\project\STM32ZET6\Users\LED\led.c 7 | * @Description: 控制一个led灯 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "led.h" 12 | 13 | /** 14 | * @brief LED初始化 15 | * @param 无 16 | * @retval 使用cubeMX生成只需要将引脚用户标签命名为LED_1即可,无需再次调用此函数 17 | */ 18 | void LED_Init(void) 19 | { 20 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 21 | 22 | /* GPIO Ports Clock Enable */ 23 | __HAL_RCC_GPIOB_CLK_ENABLE(); 24 | 25 | /*Configure GPIO pin Output Level */ 26 | HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_RESET); 27 | 28 | /*Configure GPIO pin : LED_Pin */ 29 | GPIO_InitStruct.Pin = LED_1_Pin; 30 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 31 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 32 | HAL_GPIO_Init(LED_1_GPIO_Port, &GPIO_InitStruct); 33 | } 34 | 35 | 36 | /** 37 | * @brief LED亮 38 | * @param 无 39 | * @retval 无 40 | */ 41 | void LED_On(uint8_t led_num) 42 | { 43 | if (led_num == 1) 44 | { 45 | HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_SET); 46 | } 47 | else if(led_num == 2) 48 | { 49 | HAL_GPIO_WritePin(LED_2_GPIO_Port, LED_2_Pin, GPIO_PIN_SET); 50 | } 51 | } 52 | 53 | 54 | /** 55 | * @brief LED灭 56 | * @param 无 57 | * @retval 无 58 | */ 59 | void LED_Off(uint8_t led_num) 60 | { 61 | if (led_num == 1) 62 | { 63 | HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_RESET); 64 | } 65 | else if(led_num == 2) 66 | { 67 | HAL_GPIO_WritePin(LED_2_GPIO_Port, LED_2_Pin, GPIO_PIN_RESET); 68 | } 69 | } 70 | 71 | 72 | /** 73 | * @brief LED翻转 74 | * @param 无 75 | * @retval 无 76 | */ 77 | void LED_Toggle(uint8_t led_num) 78 | { 79 | 80 | if (led_num == 1) 81 | { 82 | HAL_GPIO_TogglePin(LED_1_GPIO_Port, LED_1_Pin); 83 | } 84 | else if(led_num == 2) 85 | { 86 | HAL_GPIO_TogglePin(LED_2_GPIO_Port, LED_2_Pin); 87 | } 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/LED/led.h: -------------------------------------------------------------------------------- 1 | #ifndef __LED_H 2 | #define __LED_H 3 | 4 | #include "gpio.h" 5 | 6 | void LED_Init(void); 7 | void LED_On(uint8_t led_num); 8 | void LED_Off(uint8_t led_num); 9 | void LED_Toggle(uint8_t led_num); 10 | 11 | #endif // !__LED_H 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/OLED/OLED.h: -------------------------------------------------------------------------------- 1 | #ifndef _OLED_H_ 2 | #define _OLED_H_ 3 | #include 4 | #include 5 | #include "main.h" 6 | 7 | 8 | #define u8 unsigned char 9 | #define u32 unsigned int 10 | #define OLED_CMD 0 //写命令 11 | #define OLED_DATA 1 //写数据 12 | #define OLED_MODE 0 13 | 14 | //下面这些是固件库四线IIC引脚的初始化 15 | 16 | #define OLED_CS_Clr() OLED_CS=0 17 | #define OLED_CS_Set() OLED_CS=1 18 | #define OLED_RST_Clr() OLED_RST=0 19 | #define OLED_RST_Set() OLED_RST=1 20 | #define OLED_DC_Clr() OLED_DC=0 21 | #define OLED_DC_Set() OLED_DC=1 / 22 | 23 | //引脚定义:PD3 :OLED_SCLK;PG13:OLED_SDIN 24 | 25 | #define OLED_SCLK_Clr() HAL_GPIO_WritePin(I2C_SCL_GPIO_Port,I2C_SCL_Pin,GPIO_PIN_RESET); 26 | #define OLED_SCLK_Set() HAL_GPIO_WritePin(I2C_SCL_GPIO_Port,I2C_SCL_Pin,GPIO_PIN_SET); 27 | #define OLED_SDIN_Clr() HAL_GPIO_WritePin(I2C_SDA_GPIO_Port,I2C_SDA_Pin,GPIO_PIN_RESET); 28 | #define OLED_SDIN_Set() HAL_GPIO_WritePin(I2C_SDA_GPIO_Port,I2C_SDA_Pin,GPIO_PIN_SET); 29 | 30 | 31 | //OLED模式设置 32 | //0:4线串行模式 33 | //1:并行8080模式 34 | 35 | #define SIZE 16 36 | #define XLevelL 0x02 37 | #define XLevelH 0x10 38 | #define Max_Column 128 39 | #define Max_Row 64 40 | #define Brightness 0xFF 41 | #define X_WIDTH 128 42 | #define Y_WIDTH 64 43 | //-----------------OLED端口定义---------------- 44 | 45 | 46 | //OLED控制用函数 47 | void OLED_GPIO_Init(void); 48 | void OLED_WR_Byte(unsigned dat,unsigned cmd); 49 | void OLED_Display_On(void); 50 | void OLED_Display_Off(void); 51 | void OLED_Init(void); 52 | void OLED_Clear(void); 53 | void OLED_DrawPoint(u8 x,u8 y,u8 t); 54 | void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot); 55 | void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size); 56 | void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size); 57 | void OLED_ShowString(u8 x,u8 y, u8 *p,u8 Char_Size); 58 | void OLED_Set_Pos(unsigned char x, unsigned char y); 59 | void OLED_ShowCHinese(u8 x,u8 y,u8 no); 60 | void OLED_ShowCHinese1(u8 x,u8 y,u8 no); 61 | 62 | void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]); 63 | void fill_picture(unsigned char fill_Data); 64 | void Picture(void); 65 | void IIC_Start(void); 66 | void IIC_Stop(void); 67 | void Write_IIC_Command(unsigned char IIC_Command); 68 | void Write_IIC_Data(unsigned char IIC_Data); 69 | void Write_IIC_Byte(unsigned char IIC_Byte); 70 | void IIC_Wait_Ack(void); 71 | 72 | 73 | 74 | #endif /* 9_6_OLED_OLED_H_ */ 75 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/OLED/OLEDGUI/gui.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-27 09-48-56 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-02 17-11-18 6 | * @FilePath: \Project\RedServo\STM32\User\OLED\OLEDGUI\gui.c 7 | * @Description: 0.96OLED配合的GUI简单GUI界面 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "gui.h" 12 | #include 13 | 14 | 15 | //菜单页 16 | extern uint8_t GUI_Menu = 0; 17 | extern int32_t Servo_Kp ; // 舵机比例系数 18 | extern int32_t Servo_Ki ; // 舵机积分系数 19 | extern int32_t Servo_Kd ; // 舵机微分系数 20 | 21 | extern uint16_t pwm_A ; 22 | extern uint16_t pwm_B ; 23 | 24 | //题目标志位 25 | extern uint8_t Problem_Flag; 26 | 27 | 28 | //急停标志位 29 | extern uint8_t Stop_Flag; 30 | 31 | 32 | //消息接收标志位 33 | extern uint8_t K210_Flag; 34 | 35 | void Menu_Refresh(void) 36 | { 37 | uint8_t str_buff1[64]= {0}; 38 | uint8_t str_buff2[64]= {0}; 39 | uint8_t str_buff3[64]= {0}; 40 | uint8_t str_buff4[64]= {0}; 41 | uint8_t str_buff5[64]= {0}; 42 | uint8_t str_buff6[64]= {0}; 43 | 44 | //OLED_Clear(); 45 | switch(GUI_Menu) 46 | { 47 | case 0: 48 | { 49 | OLED_ShowString(40,0,"Serv",16); 50 | sprintf((char *)str_buff1, "Problem_Flag:%1d",Problem_Flag); 51 | sprintf((char *)str_buff2, "Stop_Flag:%d",Stop_Flag); 52 | sprintf((char *)str_buff4, "K210_Flag:%d",K210_Flag); 53 | sprintf((char *)str_buff5, "pwm_A:%5d",pwm_A); 54 | sprintf((char *)str_buff6, "pwm_B:%5d",pwm_B); 55 | break; 56 | } 57 | case 1: 58 | { 59 | OLED_ShowString(40,0,"Motor",16); 60 | sprintf((char *)str_buff1, "Problem_Flag:%1d",Problem_Flag); 61 | sprintf((char *)str_buff2, "Servo_Kp:%3.2f",(float)Servo_Kp); 62 | sprintf((char *)str_buff3, "Servo_Kp:%3.2f",(float)Servo_Ki); 63 | sprintf((char *)str_buff4, "Servo_Kp:%3.2f",(float)Servo_Kd); 64 | sprintf((char *)str_buff5, "K210_Flag:%3.2f",(float)K210_Flag); 65 | sprintf((char *)str_buff5, "pwm_A:%5d",pwm_A); 66 | sprintf((char *)str_buff6, "pwm_B:%5d",pwm_B); 67 | break; 68 | } 69 | case 2: 70 | { 71 | OLED_ShowString(40,0,"PID ",16); 72 | sprintf((char *)str_buff1, "Problem_Flag:%1d",Problem_Flag); 73 | 74 | break; 75 | } 76 | case 3: 77 | { 78 | 79 | 80 | 81 | } 82 | } 83 | OLED_ShowString(0, 2,str_buff1, 8); 84 | OLED_ShowString(0, 3,str_buff2, 8); 85 | OLED_ShowString(0, 4,str_buff3, 8); 86 | OLED_ShowString(0, 5,str_buff4, 8); 87 | OLED_ShowString(0, 6,str_buff5, 8); 88 | OLED_ShowString(0, 7,str_buff6, 8); 89 | } 90 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/OLED/OLEDGUI/gui.h: -------------------------------------------------------------------------------- 1 | #ifndef __GUI_H 2 | #define __GUI_H 3 | 4 | #include "OLED.h" 5 | #include "app.h" 6 | void Menu_Refresh(void); 7 | 8 | #endif // !__GUI_H 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/PWM/pwm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-07 15-49-11 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-07-26 21-36-15 6 | * @FilePath: \MDK-ARMd:\duruofu\Project\Avoidance_Car\project\STM32ZET6\Users\PWM\pwm.c 7 | * @Description: PWM模块(驱动电机使用,双路同频) 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "pwm.h" 12 | 13 | //在此设置使用的定时器和对应A,B两路PWM的通道 14 | 15 | #define TIM_1 htim1 16 | 17 | 18 | #define PWM_SERVO_CHANNEL_A TIM_CHANNEL_1 19 | #define PWM_SERVO_CHANNEL_B TIM_CHANNEL_4 20 | 21 | 22 | //设定占空比最大值(Counter Period计数值) 23 | #define PWM2_D 20000 24 | 25 | 26 | //急停标志位 27 | extern uint8_t Stop_Flag=0; 28 | 29 | //PWM初始化 30 | void PWM_Init(void) 31 | { 32 | //-----------舵机的PWM初始化----------- 33 | // 使能定时器的通道 34 | HAL_TIM_PWM_Start(&TIM_1,PWM_SERVO_CHANNEL_A); 35 | HAL_TIM_PWM_Start(&TIM_1,PWM_SERVO_CHANNEL_B); 36 | // 设置初始占空比为0 37 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_A, 0); 38 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_B, 0); 39 | } 40 | 41 | 42 | //--------------------舵机PWM调节-------------------- 43 | 44 | void SERVO_PWMA_Set(uint16_t pwm_d) 45 | { 46 | if (Stop_Flag==1)return; 47 | if(pwm_d > PWM2_D) 48 | { 49 | pwm_d = PWM2_D; 50 | } 51 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_A, pwm_d); 52 | } 53 | 54 | 55 | void SERVO_PWMB_Set(uint16_t pwm_d) 56 | { 57 | if (Stop_Flag==1)return; 58 | if(pwm_d > PWM2_D) 59 | { 60 | pwm_d = PWM2_D; 61 | } 62 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_B, pwm_d); 63 | } 64 | 65 | 66 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/PWM/pwm.h: -------------------------------------------------------------------------------- 1 | #ifndef __PWM_H 2 | #define __PWM_H 3 | 4 | #include "main.h" 5 | #include "tim.h" 6 | 7 | void PWM_Init(void); 8 | void Motor_PWMA_Set(uint16_t pwm_d); 9 | void Motor_PWMB_Set(uint16_t pwm_d); 10 | void SERVO_PWMA_Set(uint16_t pwm_d); 11 | void SERVO_PWMB_Set(uint16_t pwm_d); 12 | 13 | #endif // !__PWM_H 14 | 15 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/USART/debug.c: -------------------------------------------------------------------------------- 1 | #include "debug.h" 2 | #include "string.h" 3 | #include "yuntai.h" 4 | #define RXBUFFERSIZE 256 //最大接收字节数 5 | 6 | //定义串口句柄,使用串口1 7 | #define UART_HANDLE huart1 8 | 9 | //定义数据缓冲区 10 | uint8_t RxBuffer[RXBUFFERSIZE]; 11 | uint8_t Uart_RxBuffer; //接收中断缓冲 12 | uint8_t Uart_Rx_Cnt = 0; //接收缓冲计数 13 | 14 | uint16_t data[4]={0}; 15 | 16 | 17 | // 初始实时位置 18 | extern uint16_t pwm_A ; 19 | extern uint16_t pwm_B ; 20 | 21 | //中心位置处参数 22 | extern uint16_t Centre_A ; 23 | extern uint16_t Centre_B ; 24 | 25 | 26 | void Debug_Init(void) 27 | { 28 | /*串口硬件配置代码(使用cudeMX则不需要此部分) 29 | Init the GPIO of USART1 30 | */ 31 | //使能 USART1 的接收中断 32 | __HAL_UART_ENABLE_IT(&UART_HANDLE,UART_IT_RXNE); 33 | //开启 USART1 的连续接收中断,并指定接收缓冲区的地址和长度 34 | HAL_UART_Receive_IT(&UART_HANDLE,&Uart_RxBuffer,1); 35 | } 36 | 37 | //串口1接收完成回调函数 38 | void UART1_RxCpltCallback(UART_HandleTypeDef *huart) 39 | { 40 | /* Prevent unused argument(s) compilation warning */ 41 | UNUSED(huart); 42 | /* NOTE: This function Should not be modified, when the callback is needed, 43 | the HAL_UART_TxCpltCallback could be implemented in the user file 44 | */ 45 | 46 | if(Uart_Rx_Cnt >= 255) //溢出判断 47 | { 48 | Uart_Rx_Cnt = 0; 49 | memset(RxBuffer,0x00,sizeof(RxBuffer)); 50 | HAL_UART_Transmit(&UART_HANDLE, (uint8_t *)"数据溢出", 10,0xFFFF); 51 | } 52 | else 53 | { 54 | RxBuffer[Uart_Rx_Cnt++] = Uart_RxBuffer; 55 | //单字符判断 56 | if(Uart_RxBuffer == '1')//当发送1时,翻转电平 57 | { 58 | printf("1"); 59 | Centre_A = pwm_A; 60 | Centre_B = pwm_B; 61 | } 62 | else if(Uart_RxBuffer == '2')//当发送2时,翻转电平 63 | { 64 | DEBUG_printf("发送2"); 65 | } 66 | else if(Uart_RxBuffer == '3')//当发送3时,翻转电平 67 | { 68 | DEBUG_printf("发送3"); 69 | } 70 | else if(Uart_RxBuffer == '4')//当发送4时,翻转电平 71 | { 72 | DEBUG_printf("发送4"); 73 | } 74 | if((RxBuffer[Uart_Rx_Cnt-1] == 0x0A)&&(RxBuffer[Uart_Rx_Cnt-2] == 0x0D)) //判断结束位 75 | { 76 | //这里可以写多字节消息的判断 77 | //单字节消息0 78 | 79 | // 解析k210数据 80 | //sscanf((const char *)RxBuffer, "%d,%d\r\n", &data[0], &data[1]); 81 | //pwm_A =data[0]; 82 | //pwm_B =data[1]; 83 | //printf("%d,%d",Position_error[0],Position_error[1]); 84 | 85 | 86 | //复位 87 | Uart_Rx_Cnt = 0; 88 | memset(RxBuffer,0x00,sizeof(RxBuffer)); //清空数组 89 | } 90 | } 91 | 92 | HAL_UART_Receive_IT(&UART_HANDLE, (uint8_t *)&Uart_RxBuffer, 1); //因为接收中断使用了一次即关闭,所以在最后加入这行代码即可实现无限使用 93 | } 94 | 95 | //串口1错误回调函数(主要用来清除溢出中断) 96 | void UART1_ErrorCallback(UART_HandleTypeDef *huart) 97 | { 98 | if(HAL_UART_ERROR_ORE) 99 | { 100 | uint32_t temp = huart->Instance->SR; 101 | temp = huart->Instance->DR; 102 | } 103 | } 104 | 105 | /***************** 发送字符串 **********************/ 106 | void Usart_SendString(uint8_t *str) 107 | { 108 | unsigned int k=0; 109 | do 110 | { 111 | HAL_UART_Transmit(&UART_HANDLE,(uint8_t *)(str + k) ,1,1000); 112 | k++; 113 | } while(*(str + k)!='\0'); 114 | 115 | } 116 | 117 | /** 118 | * 函数功能: 重定向c库函数printf到DEBUG_USARTx 119 | * 输入参数: 无 120 | * 返 回 值: 无 121 | * 说 明:无 122 | */ 123 | int fputc(int ch, FILE *f) 124 | { 125 | HAL_UART_Transmit(&UART_HANDLE, (uint8_t *)&ch, 1, 0xffff); 126 | return ch; 127 | } 128 | 129 | /** 130 | * 函数功能: 重定向c库函数getchar,scanf到DEBUG_USARTx 131 | * 输入参数: 无 132 | * 返 回 值: 无 133 | * 说 明:无 134 | */ 135 | int fgetc(FILE *f) 136 | { 137 | uint8_t ch = 0; 138 | HAL_UART_Receive(&UART_HANDLE, &ch, 1, 0xffff); 139 | return ch; 140 | } 141 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/USART/debug.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "usart.h" 4 | #ifndef __DEBUG_H 5 | #define __DEBUG_H 6 | 7 | 8 | #define DEBUG // 定义 DEBUG 宏,用于控制调试输出 9 | #ifdef DEBUG 10 | // 如果 DEBUG 宏已定义,则定义以下宏用于调试输出 11 | #define DEBUG_printf(format, ...) printf(format "\r\n", ##__VA_ARGS__) 12 | #define DEBUG_info(tag, format, ...) printf("DEBUG_info[" tag "]:" format "\r\n", ##__VA_ARGS__) 13 | #define DEBUG_warnig(tag, format, ...) printf("DEBUG_warnig[" tag "]:" format "\r\n", ##__VA_ARGS__) 14 | #define DEBUG__error(tag, format, ...) printf("DEBUG__error[" tag "]:" format "\r\n",##__VA_ARGS__) 15 | #else 16 | // 如果 DEBUG 宏未定义,则定义以下宏为空,以屏蔽调试输出 17 | #define DEBUG_printf(format, ...) printf(format "\r\n", ##__VA_ARGS__) 18 | #define DEBUG_info(tag, format, ...) 19 | #define DEBUG_warnig(tag, format, ...) 20 | #define DEBUG__error(tag, format, ...) 21 | #endif 22 | 23 | //串口1接收中断初始化 24 | void Debug_Init(void); 25 | 26 | //串口1接收完成回调函数 27 | void UART1_RxCpltCallback(UART_HandleTypeDef *huart); 28 | 29 | void UART1_ErrorCallback(UART_HandleTypeDef *huart); 30 | 31 | #endif // !__DEBUG_H 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/USART/serial_it_config.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-08-02 12-02-17 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-03 15-29-16 6 | * @FilePath: \Project\RedServo\STM32\User\USART\serial_it_config.c 7 | * @Description: 串口模块汇总,包含串口初始化,串口接收中断初始化,串口接收完成回调函数,串口错误回调函数 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "serial_it_config.h" 12 | 13 | /** 14 | * @description: 串口接收中断初始化(总) 15 | * @return {*}Debug_Init 16 | */ 17 | void USART_IT_Config(void) 18 | { 19 | //串口1接收中断初始化 20 | Debug_Init(); 21 | //串口2接收中断初始化 22 | USART2_Init(); 23 | //串口3接收中断初始化 24 | } 25 | 26 | //串口接收完成回调函数 27 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 28 | 29 | { 30 | if(huart->Instance == USART1) 31 | { 32 | UART1_RxCpltCallback(huart); 33 | } 34 | else if(huart->Instance == USART2) 35 | { 36 | UART2_RxCpltCallback(huart); 37 | } 38 | else if(huart->Instance == USART3) 39 | { 40 | //UART2_RxCpltCallback(); 41 | } 42 | } 43 | 44 | 45 | //错误回调 46 | void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 47 | { 48 | if(huart->Instance == USART1) 49 | { 50 | UART1_ErrorCallback(huart); 51 | } 52 | else if(huart->Instance == USART2) 53 | { 54 | UART2_ErrorCallback(huart); 55 | } 56 | else if(huart->Instance == USART3) 57 | { 58 | //UART3_ErrorCallback(); 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/USART/serial_it_config.h: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "debug.h" 3 | #include "usart_2.h" 4 | 5 | #ifndef __USART_CONFIG_H 6 | #define __USART_CONFIG_H 7 | 8 | void USART_IT_Config(void); 9 | 10 | #endif // !__USART_CONFIG_H 11 | 12 | 13 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/USART/usart_2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-13 17-52-31 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-04 06-57-42 6 | * @FilePath: \Project\RedServo\STM32\User\USART\usart_2.c 7 | * @Description: 串口2的驱动代码(用于调试PID) 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "usart_2.h" 12 | #include "string.h" 13 | #include "debug.h" 14 | #include 15 | #include 16 | 17 | #define RXBUFFERSIZE_2 256 //最大接收字节数 18 | 19 | //定义串口句柄,使用串口2 20 | #define UART_HANDLE huart2 21 | 22 | //定义数据缓冲区 23 | uint8_t RxBuffer_2[RXBUFFERSIZE_2]; 24 | uint8_t Uart_RxBuffer_2; //接收中断缓冲 25 | uint8_t Uart_Rx_Cnt_2 = 0; //接收缓冲计数 26 | 27 | extern int8_t Position_error[2]={0}; 28 | 29 | //--------------------------K210参考系--------------------- 30 | //消息接收标志位 31 | extern uint8_t K210_Flag; 32 | //接收缓冲区 33 | extern uint16_t K210_data[8]={0}; 34 | void USART2_Init(void) 35 | { 36 | /*串口硬件配置代码(使用cudeMX则不需要此部分) 37 | Init the GPIO of USART1 38 | */ 39 | //使能 USART1 的接收中断 40 | __HAL_UART_ENABLE_IT(&UART_HANDLE,UART_IT_RXNE); 41 | //开启 USART1 的连续接收中断,并指定接收缓冲区的地址和长度 42 | HAL_UART_Receive_IT(&UART_HANDLE,&Uart_RxBuffer_2,1); 43 | } 44 | 45 | //串口2接收完成回调函数 46 | void UART2_RxCpltCallback(UART_HandleTypeDef *huart) 47 | { 48 | 49 | /* Prevent unused argument(s) compilation warning */ 50 | UNUSED(huart); 51 | /* NOTE: This function Should not be modified, when the callback is needed, 52 | the HAL_UART_TxCpltCallback could be implemented in the user file 53 | */ 54 | 55 | if(Uart_Rx_Cnt_2 >= 255) //溢出判断 56 | { 57 | Uart_Rx_Cnt_2 = 0; 58 | memset(RxBuffer_2,0x00,sizeof(RxBuffer_2)); 59 | HAL_UART_Transmit(&UART_HANDLE, (uint8_t *)"数据溢出", 10,0xFFFF); 60 | } 61 | else 62 | { 63 | RxBuffer_2[Uart_Rx_Cnt_2++] = Uart_RxBuffer_2; 64 | 65 | 66 | if((RxBuffer_2[Uart_Rx_Cnt_2-1] == 0x0A)&&(RxBuffer_2[Uart_Rx_Cnt_2-2] == 0x0D)) //判断结束位 67 | { 68 | 69 | // 这里可以写多字节消息的判断 70 | 71 | //Usart2_SendString(RxBuffer_2); 72 | // 解析k210数据 73 | sscanf((const char *)RxBuffer_2, "%d,%d,%d,%d,%d,%d,%d,%d\r\n", &K210_data[0], &K210_data[1],&K210_data[2],&K210_data[3],&K210_data[4], &K210_data[5],&K210_data[6],&K210_data[7]); 74 | DEBUG_info("K210","收到信息:%d,%d,%d,%d,%d,%d,%d,%d\r\n",K210_data[0], K210_data[1],K210_data[2],K210_data[3],K210_data[4], K210_data[5],K210_data[6],K210_data[7]); 75 | K210_Flag = 1; 76 | //DEBUG_info("K210","K210_Flag:%d\r\n",K210_Flag); 77 | 78 | //printf("%d,%d",Position_error[0],Position_error[1]); 79 | 80 | //复位 81 | Uart_Rx_Cnt_2 = 0; 82 | memset(RxBuffer_2,0x00,sizeof(RxBuffer_2)); //清空数组 83 | } 84 | } 85 | 86 | HAL_UART_Receive_IT(&UART_HANDLE, (uint8_t *)&Uart_RxBuffer_2, 1); //因为接收中断使用了一次即关闭,所以在最后加入这行代码即可实现无限使用 87 | 88 | 89 | 90 | } 91 | 92 | //串口1错误回调函数(主要用来清除溢出中断) 93 | void UART2_ErrorCallback(UART_HandleTypeDef *huart) 94 | { 95 | if(HAL_UART_ERROR_ORE) 96 | { 97 | uint32_t temp = huart->Instance->SR; 98 | temp = huart->Instance->DR; 99 | } 100 | } 101 | 102 | /***************** 发送字符串 **********************/ 103 | void Usart2_SendString(uint8_t *str) 104 | { 105 | unsigned int k=0; 106 | do 107 | { 108 | HAL_UART_Transmit(&UART_HANDLE,(uint8_t *)(str + k) ,1,1000); 109 | k++; 110 | } while(*(str + k)!='\0'); 111 | 112 | } 113 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/USART/usart_2.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "usart.h" 4 | 5 | 6 | #ifndef __USART2_2_H 7 | #define __USART2_2_H 8 | 9 | 10 | //串口1接收中断初始化 11 | void USART2_Init(void); 12 | //串口1接收完成回调函数 13 | void UART2_RxCpltCallback(UART_HandleTypeDef *huart); 14 | void UART2_ErrorCallback(UART_HandleTypeDef *huart); 15 | void Usart2_SendString(uint8_t *str); 16 | 17 | #endif // !__USART2_2_H 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /GreenServo/STM32/User/YunTai/yuntai.h: -------------------------------------------------------------------------------- 1 | #ifndef __YUNTAI_H 2 | #define __YUNTAI_H 3 | 4 | #include "main.h" 5 | #include "pwm.h" 6 | void Yuntai_Init(void); 7 | void Yuntai_PID(int16_t x_err,int16_t y_err); 8 | void Yuntaiz_A_Move(uint16_t pwm_d,int16_t Flow_Coefficient); 9 | void Yuntaiz_B_Move(uint16_t pwm_d,int16_t Flow_Coefficient); 10 | void Yuntaiz_AB_Move(uint16_t pwm_a,uint16_t pwm_b, int16_t Flow_Coefficient); 11 | void Yuntai_A4_Track(uint16_t pwm_a,uint16_t pwm_b, int16_t Flow_Coefficient); 12 | void Yuntai_Control(void); 13 | void Yuntaiz_AB_Move_2(uint16_t pwm_a, uint16_t pwm_b, int16_t Flow_Coefficient); 14 | void Yuntaiz_AB_Move_3(uint16_t pwm_a, uint16_t pwm_b, int16_t Flow_Coefficient); 15 | #endif // !__YUNTAI_H 16 | -------------------------------------------------------------------------------- /GreenServo/STM32/keilkill.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/GreenServo/STM32/keilkill.bat -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 2023电赛运动目标控制与自动追踪系统(E 题)【本科组】 2 | 3 | **** 4 | 5 | ## 题目要求: 6 | 7 | **1、 任务** 8 | 9 | 设计制作一个运动目标控制与自动追踪系统。系统包括模拟目标运动的红色光斑位置控制系统和指示自动追踪的绿色光斑位置控制系统。系统结构示意及摆放位置见图 1(a)。图中两个激光笔固定在各自独立的二维电控云台上。 10 | 11 | ![image-20230804061932311](attachments/image-20230804061932311.png) 12 | 13 | 红色激光笔发射的光斑用来模拟运动目标,光斑落在正前方距离 1m 处的白色屏幕上,光斑直径≤1cm。红色光斑位置控制系统控制光斑能在屏幕范围内任意移动。 14 | 15 | 绿色激光笔发射的光斑由绿色光斑位置系统控制,用于自动追踪屏幕上的红色光斑,指示目标的自动追踪效果,光斑直径≤1cm。绿色激光笔放置线段如 图 1(b)所示,该线段与屏幕平行,位于红色激光笔两侧,距红色激光笔距离大于 0.4m、小于 1m。绿色激光笔在两个放置线段上任意放置。屏幕为白色,有效面积大于 0.6╳0.6m2。用铅笔在屏幕中心画出一个边长 16 | 17 | 0.5m 的正方形,标识屏幕的边线;所画的正方形的中心为原点,用铅笔画出原点位置,所用铅笔痕迹宽≤1mm。 18 | 19 | **2、 要求** 20 | 21 | **1).** **基本要求** 22 | 23 | (1)设置运动目标位置复位功能。执行此功能,红色光斑能从屏幕任意位置回到原点。光斑中心距原点误差≤2cm。 24 | (2)启动运动目标控制系统。红色光斑能在 30 秒内沿屏幕四周边线顺时针移动一周,移动时光斑中心距边线距离≤2cm。 25 | (3)用约 1.8cm 宽的黑色电工胶带沿 A4 纸四边贴一个长方形,构成 A4 靶纸。将此 A4 靶纸贴在屏幕自定的位置。启动运动目标控制系统,红色光斑能在30 秒内沿胶带顺时针移动一周。超时不得分,光斑完全脱离胶带一次扣 2 分,连续脱离胶带移动 5cm 以上记为 0 分。 26 | (4)将上述 A4 靶纸以任意旋转角度贴在屏幕任意位置。启动运动目标控制系统,要求同(3)。 27 | 28 | **2).** **发挥部分** 29 | 30 | (1)运动目标位置复位,一键启动自动追踪系统,控制绿色光斑能在 2 秒内追踪红色光斑,追踪成功发出连续声光提示。此时两个光斑中心距离应≤3cm。 31 | (2)运动目标重复基本要求(3)~(4)的动作。绿色激光笔发射端可以放置在其放置线段的任意位置,同时启动运动目标及自动追踪系统,绿色光斑能自动追踪红色光斑。启动系统 2 秒后,应追踪成功,发出连续声光提示。此后,追踪过程中两个光斑中心距离大于 3cm 时,定义为追踪失败,一次扣 2 分。连续追踪失败 3 秒以上记为 0 分。运动目标控制系统和自动追踪系统均需设置暂停键。同时按下暂停键,红色和绿色光斑应立即制动,以便测量两个光斑中心距离。 32 | (3)其他。 33 | 34 | **3、 说明** 35 | (1)红色、绿色光斑位置控制系统必须相互独立,之间不得有任何方式通信;光斑直径小于 1cm;屏幕上无任何电子元件;控制系统不能采用台式计算机或笔记本电脑。不符合要求不进行测试。 36 | (2)基本要求(3)、(4)未得分不进行发挥部分(2)的测试。 37 | 38 | 39 | 40 | --- 41 | 42 | 43 | 44 | ## 摘要 45 | 46 | 本系统以STM32F103C8T6单片机最小系统作为控制核心,由K210视觉模块,激光笔模块,舵机云台模块,降压模块等模块组成。红色激光云台通过建立云台坐标与实际视觉坐标的映射关系,可实现原点复位功能,绕屏幕边框运动,可以识别屏幕任意位置角度的A4靶纸,并做到控制红红色光斑沿靶纸黑色边框做顺时针移动。绿色激光云台可实时识别红色激光落点,控制绿色激光点完成对红色光斑自动追踪,追踪响应时间小于2 秒。系统功能通过按键独立控制,且均设有急停按钮,可随时切换,急停。系统具有半自动机械校准模式,以达到更精确的运动目标控制指标。系统结构简单,稳定性好,易于使用。 47 | 48 | **关键词**:K210视觉模块 运动目标控制 自动追踪 舵机云台 49 | 50 | --- 51 | 52 | 53 | 54 | ## 一、系统方案 55 | 56 | ### 1.1 处理器选择 57 | 58 | 方案一:msp430方案 59 | 60 | 61 | 62 | 方案二:STM32F103C8T6方案 63 | 64 | ### 1.2 视觉模块选择 65 | 66 | 方案一:OPENMV方案 67 | 68 | 69 | 70 | 方案二:K210方案 71 | 72 | ### 1.3 二维云台选择 73 | 74 | 75 | 76 | ### 1.4 总体方案描述 77 | 78 | > 79 | 80 | ## 二、理论分析与计算 81 | 82 | ### **2.1 K210视觉识别激光原理** 83 | 84 | 85 | 86 | ## 三、电路与程序设计 87 | 88 | ### 3.1 红绿激光云台电路设计 89 | 90 | #### 3.1.1 电路总体框图与实物图 91 | 92 | 93 | 94 | #### 3.1.2 电路原理图 95 | 96 | 97 | 98 | ### 3.2 程序的设计 99 | 100 | #### 3.2.1 红色激光系统程序 101 | 102 | 103 | 104 | #### 3.2.2 绿色激光系统程序 105 | 106 | 107 | 108 | ## 四、 测试方案与测试结果 109 | 110 | ### 4.1 测试方案 111 | 112 | 113 | 114 | ### 4.2 测试条件 115 | 116 | ### 4.2 测试结果与分析 117 | 118 | ## 五、总结 119 | 120 | 121 | 122 | ## 附录 123 | 124 | -------------------------------------------------------------------------------- /RedServo/K210/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/RedServo/K210/README.md -------------------------------------------------------------------------------- /RedServo/STM32/.gitignore: -------------------------------------------------------------------------------- 1 | Output/* 2 | Output/*.hex -------------------------------------------------------------------------------- /RedServo/STM32/.mxproject: -------------------------------------------------------------------------------- 1 | [PreviousLibFiles] 2 | LibFiles=Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_def.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_bus.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_system.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_utils.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio_ex.h;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_uart.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_usart.h;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_tim_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_tim.h;Drivers\STM32F1xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_def.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_rcc_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_bus.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_rcc.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_system.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_utils.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_gpio_ex.h;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_gpio.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_dma.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_cortex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_pwr.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash_ex.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_exti.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_uart.h;Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_ll_usart.h;Drivers\CMSIS\Device\ST\STM32F1xx\Include\stm32f103xb.h;Drivers\CMSIS\Device\ST\STM32F1xx\Include\stm32f1xx.h;Drivers\CMSIS\Device\ST\STM32F1xx\Include\system_stm32f1xx.h;Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;Drivers\CMSIS\Include\cmsis_armcc.h;Drivers\CMSIS\Include\cmsis_armclang.h;Drivers\CMSIS\Include\cmsis_compiler.h;Drivers\CMSIS\Include\cmsis_gcc.h;Drivers\CMSIS\Include\cmsis_iccarm.h;Drivers\CMSIS\Include\cmsis_version.h;Drivers\CMSIS\Include\core_armv8mbl.h;Drivers\CMSIS\Include\core_armv8mml.h;Drivers\CMSIS\Include\core_cm0.h;Drivers\CMSIS\Include\core_cm0plus.h;Drivers\CMSIS\Include\core_cm1.h;Drivers\CMSIS\Include\core_cm23.h;Drivers\CMSIS\Include\core_cm3.h;Drivers\CMSIS\Include\core_cm33.h;Drivers\CMSIS\Include\core_cm4.h;Drivers\CMSIS\Include\core_cm7.h;Drivers\CMSIS\Include\core_sc000.h;Drivers\CMSIS\Include\core_sc300.h;Drivers\CMSIS\Include\mpu_armv7.h;Drivers\CMSIS\Include\mpu_armv8.h;Drivers\CMSIS\Include\tz_context.h; 3 | 4 | [PreviousUsedKeilFiles] 5 | SourceFiles=..\Core\Src\main.c;..\Core\Src\gpio.c;..\Core\Src\tim.c;..\Core\Src\usart.c;..\Core\Src\stm32f1xx_it.c;..\Core\Src\stm32f1xx_hal_msp.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;..\Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;..\Core\Src\system_stm32f1xx.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;..\Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;..\Core\Src\system_stm32f1xx.c;;; 6 | HeaderPath=..\Drivers\STM32F1xx_HAL_Driver\Inc;..\Drivers\STM32F1xx_HAL_Driver\Inc\Legacy;..\Drivers\CMSIS\Device\ST\STM32F1xx\Include;..\Drivers\CMSIS\Include;..\Core\Inc; 7 | CDefines=USE_HAL_DRIVER;STM32F103xB;USE_HAL_DRIVER;USE_HAL_DRIVER; 8 | 9 | [PreviousGenFiles] 10 | AdvancedFolderStructure=true 11 | HeaderFileListSize=6 12 | HeaderFiles#0=..\Core\Inc\gpio.h 13 | HeaderFiles#1=..\Core\Inc\tim.h 14 | HeaderFiles#2=..\Core\Inc\usart.h 15 | HeaderFiles#3=..\Core\Inc\stm32f1xx_it.h 16 | HeaderFiles#4=..\Core\Inc\stm32f1xx_hal_conf.h 17 | HeaderFiles#5=..\Core\Inc\main.h 18 | HeaderFolderListSize=1 19 | HeaderPath#0=..\Core\Inc 20 | HeaderFiles=; 21 | SourceFileListSize=6 22 | SourceFiles#0=..\Core\Src\gpio.c 23 | SourceFiles#1=..\Core\Src\tim.c 24 | SourceFiles#2=..\Core\Src\usart.c 25 | SourceFiles#3=..\Core\Src\stm32f1xx_it.c 26 | SourceFiles#4=..\Core\Src\stm32f1xx_hal_msp.c 27 | SourceFiles#5=..\Core\Src\main.c 28 | SourceFolderListSize=1 29 | SourcePath#0=..\Core\Src 30 | SourceFiles=; 31 | 32 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Inc/gpio.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file gpio.h 5 | * @brief This file contains all the function prototypes for 6 | * the gpio.c file 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __GPIO_H__ 22 | #define __GPIO_H__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "main.h" 30 | 31 | /* USER CODE BEGIN Includes */ 32 | 33 | /* USER CODE END Includes */ 34 | 35 | /* USER CODE BEGIN Private defines */ 36 | 37 | /* USER CODE END Private defines */ 38 | 39 | void MX_GPIO_Init(void); 40 | 41 | /* USER CODE BEGIN Prototypes */ 42 | 43 | /* USER CODE END Prototypes */ 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | #endif /*__ GPIO_H__ */ 49 | 50 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Inc/main.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : main.h 5 | * @brief : Header for main.c file. 6 | * This file contains the common defines of the application. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __MAIN_H 23 | #define __MAIN_H 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "stm32f1xx_hal.h" 31 | 32 | /* Private includes ----------------------------------------------------------*/ 33 | /* USER CODE BEGIN Includes */ 34 | 35 | /* USER CODE END Includes */ 36 | 37 | /* Exported types ------------------------------------------------------------*/ 38 | /* USER CODE BEGIN ET */ 39 | 40 | /* USER CODE END ET */ 41 | 42 | /* Exported constants --------------------------------------------------------*/ 43 | /* USER CODE BEGIN EC */ 44 | 45 | /* USER CODE END EC */ 46 | 47 | /* Exported macro ------------------------------------------------------------*/ 48 | /* USER CODE BEGIN EM */ 49 | 50 | /* USER CODE END EM */ 51 | 52 | /* Exported functions prototypes ---------------------------------------------*/ 53 | void Error_Handler(void); 54 | 55 | /* USER CODE BEGIN EFP */ 56 | 57 | /* USER CODE END EFP */ 58 | 59 | /* Private defines -----------------------------------------------------------*/ 60 | #define LED_1_Pin GPIO_PIN_13 61 | #define LED_1_GPIO_Port GPIOC 62 | #define LED_2_Pin GPIO_PIN_14 63 | #define LED_2_GPIO_Port GPIOC 64 | #define KEY_1_Pin GPIO_PIN_0 65 | #define KEY_1_GPIO_Port GPIOA 66 | #define KEY_1_EXTI_IRQn EXTI0_IRQn 67 | #define KEY_2_Pin GPIO_PIN_1 68 | #define KEY_2_GPIO_Port GPIOA 69 | #define KEY_2_EXTI_IRQn EXTI1_IRQn 70 | #define KEY_3_Pin GPIO_PIN_4 71 | #define KEY_3_GPIO_Port GPIOA 72 | #define KEY_3_EXTI_IRQn EXTI4_IRQn 73 | #define KEY_4_Pin GPIO_PIN_5 74 | #define KEY_4_GPIO_Port GPIOA 75 | #define KEY_4_EXTI_IRQn EXTI9_5_IRQn 76 | #define KEY_5_Pin GPIO_PIN_6 77 | #define KEY_5_GPIO_Port GPIOA 78 | #define KEY_5_EXTI_IRQn EXTI9_5_IRQn 79 | #define KEY_6_Pin GPIO_PIN_7 80 | #define KEY_6_GPIO_Port GPIOA 81 | #define KEY_6_EXTI_IRQn EXTI9_5_IRQn 82 | #define KEY_7_Pin GPIO_PIN_10 83 | #define KEY_7_GPIO_Port GPIOB 84 | #define KEY_7_EXTI_IRQn EXTI15_10_IRQn 85 | #define KEY_8_Pin GPIO_PIN_11 86 | #define KEY_8_GPIO_Port GPIOB 87 | #define KEY_8_EXTI_IRQn EXTI15_10_IRQn 88 | #define Buzzer_Pin_Pin GPIO_PIN_12 89 | #define Buzzer_Pin_GPIO_Port GPIOB 90 | #define KEY_9_Pin GPIO_PIN_13 91 | #define KEY_9_GPIO_Port GPIOB 92 | #define KEY_9_EXTI_IRQn EXTI15_10_IRQn 93 | #define KEY_10_Pin GPIO_PIN_14 94 | #define KEY_10_GPIO_Port GPIOB 95 | #define KEY_10_EXTI_IRQn EXTI15_10_IRQn 96 | #define KEY_11_Pin GPIO_PIN_15 97 | #define KEY_11_GPIO_Port GPIOB 98 | #define KEY_11_EXTI_IRQn EXTI15_10_IRQn 99 | #define I2C_SCL_Pin GPIO_PIN_8 100 | #define I2C_SCL_GPIO_Port GPIOB 101 | #define I2C_SDA_Pin GPIO_PIN_9 102 | #define I2C_SDA_GPIO_Port GPIOB 103 | /* USER CODE BEGIN Private defines */ 104 | 105 | /* USER CODE END Private defines */ 106 | 107 | #ifdef __cplusplus 108 | } 109 | #endif 110 | 111 | #endif /* __MAIN_H */ 112 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Inc/stm32f1xx_it.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f1xx_it.h 5 | * @brief This file contains the headers of the interrupt handlers. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F1xx_IT_H 22 | #define __STM32F1xx_IT_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Private includes ----------------------------------------------------------*/ 29 | /* USER CODE BEGIN Includes */ 30 | 31 | /* USER CODE END Includes */ 32 | 33 | /* Exported types ------------------------------------------------------------*/ 34 | /* USER CODE BEGIN ET */ 35 | 36 | /* USER CODE END ET */ 37 | 38 | /* Exported constants --------------------------------------------------------*/ 39 | /* USER CODE BEGIN EC */ 40 | 41 | /* USER CODE END EC */ 42 | 43 | /* Exported macro ------------------------------------------------------------*/ 44 | /* USER CODE BEGIN EM */ 45 | 46 | /* USER CODE END EM */ 47 | 48 | /* Exported functions prototypes ---------------------------------------------*/ 49 | void NMI_Handler(void); 50 | void HardFault_Handler(void); 51 | void MemManage_Handler(void); 52 | void BusFault_Handler(void); 53 | void UsageFault_Handler(void); 54 | void SVC_Handler(void); 55 | void DebugMon_Handler(void); 56 | void PendSV_Handler(void); 57 | void SysTick_Handler(void); 58 | void EXTI0_IRQHandler(void); 59 | void EXTI1_IRQHandler(void); 60 | void EXTI4_IRQHandler(void); 61 | void EXTI9_5_IRQHandler(void); 62 | void TIM4_IRQHandler(void); 63 | void USART1_IRQHandler(void); 64 | void USART2_IRQHandler(void); 65 | void EXTI15_10_IRQHandler(void); 66 | /* USER CODE BEGIN EFP */ 67 | 68 | /* USER CODE END EFP */ 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #endif /* __STM32F1xx_IT_H */ 75 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Inc/tim.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file tim.h 5 | * @brief This file contains all the function prototypes for 6 | * the tim.c file 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __TIM_H__ 22 | #define __TIM_H__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "main.h" 30 | 31 | /* USER CODE BEGIN Includes */ 32 | 33 | /* USER CODE END Includes */ 34 | 35 | extern TIM_HandleTypeDef htim1; 36 | 37 | extern TIM_HandleTypeDef htim4; 38 | 39 | /* USER CODE BEGIN Private defines */ 40 | 41 | /* USER CODE END Private defines */ 42 | 43 | void MX_TIM1_Init(void); 44 | void MX_TIM4_Init(void); 45 | 46 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); 47 | 48 | /* USER CODE BEGIN Prototypes */ 49 | 50 | /* USER CODE END Prototypes */ 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | #endif /* __TIM_H__ */ 57 | 58 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Inc/usart.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file usart.h 5 | * @brief This file contains all the function prototypes for 6 | * the usart.c file 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USART_H__ 22 | #define __USART_H__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "main.h" 30 | 31 | /* USER CODE BEGIN Includes */ 32 | 33 | /* USER CODE END Includes */ 34 | 35 | extern UART_HandleTypeDef huart1; 36 | 37 | extern UART_HandleTypeDef huart2; 38 | 39 | /* USER CODE BEGIN Private defines */ 40 | 41 | /* USER CODE END Private defines */ 42 | 43 | void MX_USART1_UART_Init(void); 44 | void MX_USART2_UART_Init(void); 45 | 46 | /* USER CODE BEGIN Prototypes */ 47 | 48 | /* USER CODE END Prototypes */ 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /* __USART_H__ */ 55 | 56 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Src/gpio.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file gpio.c 5 | * @brief This file provides code for the configuration 6 | * of all used GPIO pins. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | #include "gpio.h" 23 | 24 | /* USER CODE BEGIN 0 */ 25 | 26 | /* USER CODE END 0 */ 27 | 28 | /*----------------------------------------------------------------------------*/ 29 | /* Configure GPIO */ 30 | /*----------------------------------------------------------------------------*/ 31 | /* USER CODE BEGIN 1 */ 32 | 33 | /* USER CODE END 1 */ 34 | 35 | /** Configure pins as 36 | * Analog 37 | * Input 38 | * Output 39 | * EVENT_OUT 40 | * EXTI 41 | */ 42 | void MX_GPIO_Init(void) 43 | { 44 | 45 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 46 | 47 | /* GPIO Ports Clock Enable */ 48 | __HAL_RCC_GPIOC_CLK_ENABLE(); 49 | __HAL_RCC_GPIOD_CLK_ENABLE(); 50 | __HAL_RCC_GPIOA_CLK_ENABLE(); 51 | __HAL_RCC_GPIOB_CLK_ENABLE(); 52 | 53 | /*Configure GPIO pin Output Level */ 54 | HAL_GPIO_WritePin(GPIOC, LED_1_Pin|LED_2_Pin, GPIO_PIN_RESET); 55 | 56 | /*Configure GPIO pin Output Level */ 57 | HAL_GPIO_WritePin(Buzzer_Pin_GPIO_Port, Buzzer_Pin_Pin, GPIO_PIN_SET); 58 | 59 | /*Configure GPIO pin Output Level */ 60 | HAL_GPIO_WritePin(GPIOB, I2C_SCL_Pin|I2C_SDA_Pin, GPIO_PIN_RESET); 61 | 62 | /*Configure GPIO pins : PCPin PCPin */ 63 | GPIO_InitStruct.Pin = LED_1_Pin|LED_2_Pin; 64 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 65 | GPIO_InitStruct.Pull = GPIO_NOPULL; 66 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 67 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 68 | 69 | /*Configure GPIO pins : PAPin PAPin PAPin PAPin 70 | PAPin PAPin */ 71 | GPIO_InitStruct.Pin = KEY_1_Pin|KEY_2_Pin|KEY_3_Pin|KEY_4_Pin 72 | |KEY_5_Pin|KEY_6_Pin; 73 | GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 74 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; 75 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 76 | 77 | /*Configure GPIO pins : PBPin PBPin PBPin PBPin 78 | PBPin */ 79 | GPIO_InitStruct.Pin = KEY_7_Pin|KEY_8_Pin|KEY_9_Pin|KEY_10_Pin 80 | |KEY_11_Pin; 81 | GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 82 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; 83 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 84 | 85 | /*Configure GPIO pin : PtPin */ 86 | GPIO_InitStruct.Pin = Buzzer_Pin_Pin; 87 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 88 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; 89 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; 90 | HAL_GPIO_Init(Buzzer_Pin_GPIO_Port, &GPIO_InitStruct); 91 | 92 | /*Configure GPIO pins : PBPin PBPin */ 93 | GPIO_InitStruct.Pin = I2C_SCL_Pin|I2C_SDA_Pin; 94 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; 95 | GPIO_InitStruct.Pull = GPIO_PULLUP; 96 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 97 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 98 | 99 | /* EXTI interrupt init*/ 100 | HAL_NVIC_SetPriority(EXTI0_IRQn, 1, 0); 101 | HAL_NVIC_EnableIRQ(EXTI0_IRQn); 102 | 103 | HAL_NVIC_SetPriority(EXTI1_IRQn, 1, 0); 104 | HAL_NVIC_EnableIRQ(EXTI1_IRQn); 105 | 106 | HAL_NVIC_SetPriority(EXTI4_IRQn, 1, 0); 107 | HAL_NVIC_EnableIRQ(EXTI4_IRQn); 108 | 109 | HAL_NVIC_SetPriority(EXTI9_5_IRQn, 1, 0); 110 | HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); 111 | 112 | HAL_NVIC_SetPriority(EXTI15_10_IRQn, 1, 0); 113 | HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); 114 | 115 | } 116 | 117 | /* USER CODE BEGIN 2 */ 118 | 119 | /* USER CODE END 2 */ 120 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Src/main.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : main.c 5 | * @brief : Main program body 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | /* Includes ------------------------------------------------------------------*/ 20 | #include "main.h" 21 | #include "tim.h" 22 | #include "usart.h" 23 | #include "gpio.h" 24 | 25 | /* Private includes ----------------------------------------------------------*/ 26 | /* USER CODE BEGIN Includes */ 27 | #include "app.h" //应用层头文件 28 | /* USER CODE END Includes */ 29 | 30 | /* Private typedef -----------------------------------------------------------*/ 31 | /* USER CODE BEGIN PTD */ 32 | 33 | /* USER CODE END PTD */ 34 | 35 | /* Private define ------------------------------------------------------------*/ 36 | /* USER CODE BEGIN PD */ 37 | /* USER CODE END PD */ 38 | 39 | /* Private macro -------------------------------------------------------------*/ 40 | /* USER CODE BEGIN PM */ 41 | 42 | /* USER CODE END PM */ 43 | 44 | /* Private variables ---------------------------------------------------------*/ 45 | 46 | /* USER CODE BEGIN PV */ 47 | 48 | /* USER CODE END PV */ 49 | 50 | /* Private function prototypes -----------------------------------------------*/ 51 | void SystemClock_Config(void); 52 | /* USER CODE BEGIN PFP */ 53 | 54 | /* USER CODE END PFP */ 55 | 56 | /* Private user code ---------------------------------------------------------*/ 57 | /* USER CODE BEGIN 0 */ 58 | 59 | /* USER CODE END 0 */ 60 | 61 | /** 62 | * @brief The application entry point. 63 | * @retval int 64 | */ 65 | int main(void) 66 | { 67 | /* USER CODE BEGIN 1 */ 68 | 69 | /* USER CODE END 1 */ 70 | 71 | /* MCU Configuration--------------------------------------------------------*/ 72 | 73 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 74 | HAL_Init(); 75 | 76 | /* USER CODE BEGIN Init */ 77 | 78 | /* USER CODE END Init */ 79 | 80 | /* Configure the system clock */ 81 | SystemClock_Config(); 82 | 83 | /* USER CODE BEGIN SysInit */ 84 | 85 | /* USER CODE END SysInit */ 86 | 87 | /* Initialize all configured peripherals */ 88 | MX_GPIO_Init(); 89 | MX_TIM1_Init(); 90 | MX_USART1_UART_Init(); 91 | MX_TIM4_Init(); 92 | MX_USART2_UART_Init(); 93 | /* USER CODE BEGIN 2 */ 94 | App_Init(); //应用层初始化 95 | 96 | 97 | 98 | 99 | /* USER CODE END 2 */ 100 | 101 | /* Infinite loop */ 102 | /* USER CODE BEGIN WHILE */ 103 | while (1) 104 | { 105 | /* USER CODE END WHILE */ 106 | 107 | /* USER CODE BEGIN 3 */ 108 | App_Task(); //应用层任务 109 | 110 | } 111 | /* USER CODE END 3 */ 112 | } 113 | 114 | /** 115 | * @brief System Clock Configuration 116 | * @retval None 117 | */ 118 | void SystemClock_Config(void) 119 | { 120 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 121 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 122 | 123 | /** Initializes the RCC Oscillators according to the specified parameters 124 | * in the RCC_OscInitTypeDef structure. 125 | */ 126 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 127 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; 128 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; 129 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; 130 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 131 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 132 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; 133 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 134 | { 135 | Error_Handler(); 136 | } 137 | 138 | /** Initializes the CPU, AHB and APB buses clocks 139 | */ 140 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 141 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 142 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 143 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 144 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 145 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 146 | 147 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) 148 | { 149 | Error_Handler(); 150 | } 151 | } 152 | 153 | /* USER CODE BEGIN 4 */ 154 | 155 | /* USER CODE END 4 */ 156 | 157 | /** 158 | * @brief This function is executed in case of error occurrence. 159 | * @retval None 160 | */ 161 | void Error_Handler(void) 162 | { 163 | /* USER CODE BEGIN Error_Handler_Debug */ 164 | /* User can add his own implementation to report the HAL error return state */ 165 | __disable_irq(); 166 | while (1) 167 | { 168 | } 169 | /* USER CODE END Error_Handler_Debug */ 170 | } 171 | 172 | #ifdef USE_FULL_ASSERT 173 | /** 174 | * @brief Reports the name of the source file and the source line number 175 | * where the assert_param error has occurred. 176 | * @param file: pointer to the source file name 177 | * @param line: assert_param error line source number 178 | * @retval None 179 | */ 180 | void assert_failed(uint8_t *file, uint32_t line) 181 | { 182 | /* USER CODE BEGIN 6 */ 183 | /* User can add his own implementation to report the file name and line number, 184 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 185 | /* USER CODE END 6 */ 186 | } 187 | #endif /* USE_FULL_ASSERT */ 188 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Src/stm32f1xx_hal_msp.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f1xx_hal_msp.c 5 | * @brief This file provides code for the MSP Initialization 6 | * and de-Initialization codes. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | #include "main.h" 23 | /* USER CODE BEGIN Includes */ 24 | 25 | /* USER CODE END Includes */ 26 | 27 | /* Private typedef -----------------------------------------------------------*/ 28 | /* USER CODE BEGIN TD */ 29 | 30 | /* USER CODE END TD */ 31 | 32 | /* Private define ------------------------------------------------------------*/ 33 | /* USER CODE BEGIN Define */ 34 | 35 | /* USER CODE END Define */ 36 | 37 | /* Private macro -------------------------------------------------------------*/ 38 | /* USER CODE BEGIN Macro */ 39 | 40 | /* USER CODE END Macro */ 41 | 42 | /* Private variables ---------------------------------------------------------*/ 43 | /* USER CODE BEGIN PV */ 44 | 45 | /* USER CODE END PV */ 46 | 47 | /* Private function prototypes -----------------------------------------------*/ 48 | /* USER CODE BEGIN PFP */ 49 | 50 | /* USER CODE END PFP */ 51 | 52 | /* External functions --------------------------------------------------------*/ 53 | /* USER CODE BEGIN ExternalFunctions */ 54 | 55 | /* USER CODE END ExternalFunctions */ 56 | 57 | /* USER CODE BEGIN 0 */ 58 | 59 | /* USER CODE END 0 */ 60 | /** 61 | * Initializes the Global MSP. 62 | */ 63 | void HAL_MspInit(void) 64 | { 65 | /* USER CODE BEGIN MspInit 0 */ 66 | 67 | /* USER CODE END MspInit 0 */ 68 | 69 | __HAL_RCC_AFIO_CLK_ENABLE(); 70 | __HAL_RCC_PWR_CLK_ENABLE(); 71 | 72 | /* System interrupt init*/ 73 | 74 | /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled 75 | */ 76 | __HAL_AFIO_REMAP_SWJ_NOJTAG(); 77 | 78 | /* USER CODE BEGIN MspInit 1 */ 79 | 80 | /* USER CODE END MspInit 1 */ 81 | } 82 | 83 | /* USER CODE BEGIN 1 */ 84 | 85 | /* USER CODE END 1 */ 86 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Src/tim.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file tim.c 5 | * @brief This file provides code for the configuration 6 | * of the TIM instances. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "tim.h" 22 | 23 | /* USER CODE BEGIN 0 */ 24 | 25 | /* USER CODE END 0 */ 26 | 27 | TIM_HandleTypeDef htim1; 28 | TIM_HandleTypeDef htim4; 29 | 30 | /* TIM1 init function */ 31 | void MX_TIM1_Init(void) 32 | { 33 | 34 | /* USER CODE BEGIN TIM1_Init 0 */ 35 | 36 | /* USER CODE END TIM1_Init 0 */ 37 | 38 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; 39 | TIM_MasterConfigTypeDef sMasterConfig = {0}; 40 | TIM_OC_InitTypeDef sConfigOC = {0}; 41 | TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; 42 | 43 | /* USER CODE BEGIN TIM1_Init 1 */ 44 | 45 | /* USER CODE END TIM1_Init 1 */ 46 | htim1.Instance = TIM1; 47 | htim1.Init.Prescaler = 72-1; 48 | htim1.Init.CounterMode = TIM_COUNTERMODE_UP; 49 | htim1.Init.Period = 20000-1; 50 | htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 51 | htim1.Init.RepetitionCounter = 0; 52 | htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; 53 | if (HAL_TIM_Base_Init(&htim1) != HAL_OK) 54 | { 55 | Error_Handler(); 56 | } 57 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 58 | if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) 59 | { 60 | Error_Handler(); 61 | } 62 | if (HAL_TIM_PWM_Init(&htim1) != HAL_OK) 63 | { 64 | Error_Handler(); 65 | } 66 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; 67 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 68 | if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) 69 | { 70 | Error_Handler(); 71 | } 72 | sConfigOC.OCMode = TIM_OCMODE_PWM1; 73 | sConfigOC.Pulse = 0; 74 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; 75 | sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; 76 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; 77 | sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; 78 | sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; 79 | if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) 80 | { 81 | Error_Handler(); 82 | } 83 | if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) 84 | { 85 | Error_Handler(); 86 | } 87 | sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; 88 | sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; 89 | sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; 90 | sBreakDeadTimeConfig.DeadTime = 0; 91 | sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; 92 | sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; 93 | sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; 94 | if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK) 95 | { 96 | Error_Handler(); 97 | } 98 | /* USER CODE BEGIN TIM1_Init 2 */ 99 | 100 | /* USER CODE END TIM1_Init 2 */ 101 | HAL_TIM_MspPostInit(&htim1); 102 | 103 | } 104 | /* TIM4 init function */ 105 | void MX_TIM4_Init(void) 106 | { 107 | 108 | /* USER CODE BEGIN TIM4_Init 0 */ 109 | 110 | /* USER CODE END TIM4_Init 0 */ 111 | 112 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; 113 | TIM_MasterConfigTypeDef sMasterConfig = {0}; 114 | 115 | /* USER CODE BEGIN TIM4_Init 1 */ 116 | 117 | /* USER CODE END TIM4_Init 1 */ 118 | htim4.Instance = TIM4; 119 | htim4.Init.Prescaler = 72-1; 120 | htim4.Init.CounterMode = TIM_COUNTERMODE_UP; 121 | htim4.Init.Period = 1000-1; 122 | htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 123 | htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; 124 | if (HAL_TIM_Base_Init(&htim4) != HAL_OK) 125 | { 126 | Error_Handler(); 127 | } 128 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 129 | if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) 130 | { 131 | Error_Handler(); 132 | } 133 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; 134 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 135 | if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) 136 | { 137 | Error_Handler(); 138 | } 139 | /* USER CODE BEGIN TIM4_Init 2 */ 140 | 141 | /* USER CODE END TIM4_Init 2 */ 142 | 143 | } 144 | 145 | void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) 146 | { 147 | 148 | if(tim_baseHandle->Instance==TIM1) 149 | { 150 | /* USER CODE BEGIN TIM1_MspInit 0 */ 151 | 152 | /* USER CODE END TIM1_MspInit 0 */ 153 | /* TIM1 clock enable */ 154 | __HAL_RCC_TIM1_CLK_ENABLE(); 155 | /* USER CODE BEGIN TIM1_MspInit 1 */ 156 | 157 | /* USER CODE END TIM1_MspInit 1 */ 158 | } 159 | else if(tim_baseHandle->Instance==TIM4) 160 | { 161 | /* USER CODE BEGIN TIM4_MspInit 0 */ 162 | 163 | /* USER CODE END TIM4_MspInit 0 */ 164 | /* TIM4 clock enable */ 165 | __HAL_RCC_TIM4_CLK_ENABLE(); 166 | 167 | /* TIM4 interrupt Init */ 168 | HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0); 169 | HAL_NVIC_EnableIRQ(TIM4_IRQn); 170 | /* USER CODE BEGIN TIM4_MspInit 1 */ 171 | 172 | /* USER CODE END TIM4_MspInit 1 */ 173 | } 174 | } 175 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) 176 | { 177 | 178 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 179 | if(timHandle->Instance==TIM1) 180 | { 181 | /* USER CODE BEGIN TIM1_MspPostInit 0 */ 182 | 183 | /* USER CODE END TIM1_MspPostInit 0 */ 184 | 185 | __HAL_RCC_GPIOA_CLK_ENABLE(); 186 | /**TIM1 GPIO Configuration 187 | PA8 ------> TIM1_CH1 188 | PA11 ------> TIM1_CH4 189 | */ 190 | GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_11; 191 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 192 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 193 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 194 | 195 | /* USER CODE BEGIN TIM1_MspPostInit 1 */ 196 | 197 | /* USER CODE END TIM1_MspPostInit 1 */ 198 | } 199 | 200 | } 201 | 202 | void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) 203 | { 204 | 205 | if(tim_baseHandle->Instance==TIM1) 206 | { 207 | /* USER CODE BEGIN TIM1_MspDeInit 0 */ 208 | 209 | /* USER CODE END TIM1_MspDeInit 0 */ 210 | /* Peripheral clock disable */ 211 | __HAL_RCC_TIM1_CLK_DISABLE(); 212 | /* USER CODE BEGIN TIM1_MspDeInit 1 */ 213 | 214 | /* USER CODE END TIM1_MspDeInit 1 */ 215 | } 216 | else if(tim_baseHandle->Instance==TIM4) 217 | { 218 | /* USER CODE BEGIN TIM4_MspDeInit 0 */ 219 | 220 | /* USER CODE END TIM4_MspDeInit 0 */ 221 | /* Peripheral clock disable */ 222 | __HAL_RCC_TIM4_CLK_DISABLE(); 223 | 224 | /* TIM4 interrupt Deinit */ 225 | HAL_NVIC_DisableIRQ(TIM4_IRQn); 226 | /* USER CODE BEGIN TIM4_MspDeInit 1 */ 227 | 228 | /* USER CODE END TIM4_MspDeInit 1 */ 229 | } 230 | } 231 | 232 | /* USER CODE BEGIN 1 */ 233 | 234 | /* USER CODE END 1 */ 235 | -------------------------------------------------------------------------------- /RedServo/STM32/Core/Src/usart.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file usart.c 5 | * @brief This file provides code for the configuration 6 | * of the USART instances. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "usart.h" 22 | 23 | /* USER CODE BEGIN 0 */ 24 | 25 | /* USER CODE END 0 */ 26 | 27 | UART_HandleTypeDef huart1; 28 | UART_HandleTypeDef huart2; 29 | 30 | /* USART1 init function */ 31 | 32 | void MX_USART1_UART_Init(void) 33 | { 34 | 35 | /* USER CODE BEGIN USART1_Init 0 */ 36 | 37 | /* USER CODE END USART1_Init 0 */ 38 | 39 | /* USER CODE BEGIN USART1_Init 1 */ 40 | 41 | /* USER CODE END USART1_Init 1 */ 42 | huart1.Instance = USART1; 43 | huart1.Init.BaudRate = 115200; 44 | huart1.Init.WordLength = UART_WORDLENGTH_8B; 45 | huart1.Init.StopBits = UART_STOPBITS_1; 46 | huart1.Init.Parity = UART_PARITY_NONE; 47 | huart1.Init.Mode = UART_MODE_TX_RX; 48 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; 49 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; 50 | if (HAL_UART_Init(&huart1) != HAL_OK) 51 | { 52 | Error_Handler(); 53 | } 54 | /* USER CODE BEGIN USART1_Init 2 */ 55 | 56 | /* USER CODE END USART1_Init 2 */ 57 | 58 | } 59 | /* USART2 init function */ 60 | 61 | void MX_USART2_UART_Init(void) 62 | { 63 | 64 | /* USER CODE BEGIN USART2_Init 0 */ 65 | 66 | /* USER CODE END USART2_Init 0 */ 67 | 68 | /* USER CODE BEGIN USART2_Init 1 */ 69 | 70 | /* USER CODE END USART2_Init 1 */ 71 | huart2.Instance = USART2; 72 | huart2.Init.BaudRate = 115200; 73 | huart2.Init.WordLength = UART_WORDLENGTH_8B; 74 | huart2.Init.StopBits = UART_STOPBITS_1; 75 | huart2.Init.Parity = UART_PARITY_NONE; 76 | huart2.Init.Mode = UART_MODE_TX_RX; 77 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 78 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; 79 | if (HAL_UART_Init(&huart2) != HAL_OK) 80 | { 81 | Error_Handler(); 82 | } 83 | /* USER CODE BEGIN USART2_Init 2 */ 84 | 85 | /* USER CODE END USART2_Init 2 */ 86 | 87 | } 88 | 89 | void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) 90 | { 91 | 92 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 93 | if(uartHandle->Instance==USART1) 94 | { 95 | /* USER CODE BEGIN USART1_MspInit 0 */ 96 | 97 | /* USER CODE END USART1_MspInit 0 */ 98 | /* USART1 clock enable */ 99 | __HAL_RCC_USART1_CLK_ENABLE(); 100 | 101 | __HAL_RCC_GPIOA_CLK_ENABLE(); 102 | /**USART1 GPIO Configuration 103 | PA9 ------> USART1_TX 104 | PA10 ------> USART1_RX 105 | */ 106 | GPIO_InitStruct.Pin = GPIO_PIN_9; 107 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 108 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 109 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 110 | 111 | GPIO_InitStruct.Pin = GPIO_PIN_10; 112 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 113 | GPIO_InitStruct.Pull = GPIO_NOPULL; 114 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 115 | 116 | /* USART1 interrupt Init */ 117 | HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); 118 | HAL_NVIC_EnableIRQ(USART1_IRQn); 119 | /* USER CODE BEGIN USART1_MspInit 1 */ 120 | 121 | /* USER CODE END USART1_MspInit 1 */ 122 | } 123 | else if(uartHandle->Instance==USART2) 124 | { 125 | /* USER CODE BEGIN USART2_MspInit 0 */ 126 | 127 | /* USER CODE END USART2_MspInit 0 */ 128 | /* USART2 clock enable */ 129 | __HAL_RCC_USART2_CLK_ENABLE(); 130 | 131 | __HAL_RCC_GPIOA_CLK_ENABLE(); 132 | /**USART2 GPIO Configuration 133 | PA2 ------> USART2_TX 134 | PA3 ------> USART2_RX 135 | */ 136 | GPIO_InitStruct.Pin = GPIO_PIN_2; 137 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 138 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 139 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 140 | 141 | GPIO_InitStruct.Pin = GPIO_PIN_3; 142 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 143 | GPIO_InitStruct.Pull = GPIO_NOPULL; 144 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 145 | 146 | /* USART2 interrupt Init */ 147 | HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); 148 | HAL_NVIC_EnableIRQ(USART2_IRQn); 149 | /* USER CODE BEGIN USART2_MspInit 1 */ 150 | 151 | /* USER CODE END USART2_MspInit 1 */ 152 | } 153 | } 154 | 155 | void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) 156 | { 157 | 158 | if(uartHandle->Instance==USART1) 159 | { 160 | /* USER CODE BEGIN USART1_MspDeInit 0 */ 161 | 162 | /* USER CODE END USART1_MspDeInit 0 */ 163 | /* Peripheral clock disable */ 164 | __HAL_RCC_USART1_CLK_DISABLE(); 165 | 166 | /**USART1 GPIO Configuration 167 | PA9 ------> USART1_TX 168 | PA10 ------> USART1_RX 169 | */ 170 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); 171 | 172 | /* USART1 interrupt Deinit */ 173 | HAL_NVIC_DisableIRQ(USART1_IRQn); 174 | /* USER CODE BEGIN USART1_MspDeInit 1 */ 175 | 176 | /* USER CODE END USART1_MspDeInit 1 */ 177 | } 178 | else if(uartHandle->Instance==USART2) 179 | { 180 | /* USER CODE BEGIN USART2_MspDeInit 0 */ 181 | 182 | /* USER CODE END USART2_MspDeInit 0 */ 183 | /* Peripheral clock disable */ 184 | __HAL_RCC_USART2_CLK_DISABLE(); 185 | 186 | /**USART2 GPIO Configuration 187 | PA2 ------> USART2_TX 188 | PA3 ------> USART2_RX 189 | */ 190 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); 191 | 192 | /* USART2 interrupt Deinit */ 193 | HAL_NVIC_DisableIRQ(USART2_IRQn); 194 | /* USER CODE BEGIN USART2_MspDeInit 1 */ 195 | 196 | /* USER CODE END USART2_MspDeInit 1 */ 197 | } 198 | } 199 | 200 | /* USER CODE BEGIN 1 */ 201 | 202 | /* USER CODE END 1 */ 203 | -------------------------------------------------------------------------------- /RedServo/STM32/Drivers/CMSIS/Device/ST/STM32F1xx/Include/system_stm32f1xx.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f1xx.h 4 | * @author MCD Application Team 5 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2017-2021 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | 19 | /** @addtogroup CMSIS 20 | * @{ 21 | */ 22 | 23 | /** @addtogroup stm32f10x_system 24 | * @{ 25 | */ 26 | 27 | /** 28 | * @brief Define to prevent recursive inclusion 29 | */ 30 | #ifndef __SYSTEM_STM32F10X_H 31 | #define __SYSTEM_STM32F10X_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /** @addtogroup STM32F10x_System_Includes 38 | * @{ 39 | */ 40 | 41 | /** 42 | * @} 43 | */ 44 | 45 | 46 | /** @addtogroup STM32F10x_System_Exported_types 47 | * @{ 48 | */ 49 | 50 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 51 | extern const uint8_t AHBPrescTable[16U]; /*!< AHB prescalers table values */ 52 | extern const uint8_t APBPrescTable[8U]; /*!< APB prescalers table values */ 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | /** @addtogroup STM32F10x_System_Exported_Constants 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @addtogroup STM32F10x_System_Exported_Macros 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @addtogroup STM32F10x_System_Exported_Functions 75 | * @{ 76 | */ 77 | 78 | extern void SystemInit(void); 79 | extern void SystemCoreClockUpdate(void); 80 | /** 81 | * @} 82 | */ 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | #endif /*__SYSTEM_STM32F10X_H */ 89 | 90 | /** 91 | * @} 92 | */ 93 | 94 | /** 95 | * @} 96 | */ 97 | -------------------------------------------------------------------------------- /RedServo/STM32/Drivers/CMSIS/Device/ST/STM32F1xx/LICENSE.txt: -------------------------------------------------------------------------------- 1 | This software component is provided to you as part of a software package and 2 | applicable license terms are in the Package_license file. If you received this 3 | software component outside of a package or without applicable license terms, 4 | the terms of the Apache-2.0 license shall apply. 5 | You may obtain a copy of the Apache-2.0 at: 6 | https://opensource.org/licenses/Apache-2.0 7 | -------------------------------------------------------------------------------- /RedServo/STM32/Drivers/CMSIS/Include/cmsis_version.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_version.h 3 | * @brief CMSIS Core(M) Version definitions 4 | * @version V5.0.2 5 | * @date 19. April 2017 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2017 ARM Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef __CMSIS_VERSION_H 32 | #define __CMSIS_VERSION_H 33 | 34 | /* CMSIS Version definitions */ 35 | #define __CM_CMSIS_VERSION_MAIN ( 5U) /*!< [31:16] CMSIS Core(M) main version */ 36 | #define __CM_CMSIS_VERSION_SUB ( 1U) /*!< [15:0] CMSIS Core(M) sub version */ 37 | #define __CM_CMSIS_VERSION ((__CM_CMSIS_VERSION_MAIN << 16U) | \ 38 | __CM_CMSIS_VERSION_SUB ) /*!< CMSIS Core(M) version number */ 39 | #endif 40 | -------------------------------------------------------------------------------- /RedServo/STM32/Drivers/CMSIS/Include/tz_context.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * @file tz_context.h 3 | * @brief Context Management for Armv8-M TrustZone 4 | * @version V1.0.1 5 | * @date 10. January 2018 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2017-2018 Arm Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef TZ_CONTEXT_H 32 | #define TZ_CONTEXT_H 33 | 34 | #include 35 | 36 | #ifndef TZ_MODULEID_T 37 | #define TZ_MODULEID_T 38 | /// \details Data type that identifies secure software modules called by a process. 39 | typedef uint32_t TZ_ModuleId_t; 40 | #endif 41 | 42 | /// \details TZ Memory ID identifies an allocated memory slot. 43 | typedef uint32_t TZ_MemoryId_t; 44 | 45 | /// Initialize secure context memory system 46 | /// \return execution status (1: success, 0: error) 47 | uint32_t TZ_InitContextSystem_S (void); 48 | 49 | /// Allocate context memory for calling secure software modules in TrustZone 50 | /// \param[in] module identifies software modules called from non-secure mode 51 | /// \return value != 0 id TrustZone memory slot identifier 52 | /// \return value 0 no memory available or internal error 53 | TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module); 54 | 55 | /// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S 56 | /// \param[in] id TrustZone memory slot identifier 57 | /// \return execution status (1: success, 0: error) 58 | uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id); 59 | 60 | /// Load secure context (called on RTOS thread context switch) 61 | /// \param[in] id TrustZone memory slot identifier 62 | /// \return execution status (1: success, 0: error) 63 | uint32_t TZ_LoadContext_S (TZ_MemoryId_t id); 64 | 65 | /// Store secure context (called on RTOS thread context switch) 66 | /// \param[in] id TrustZone memory slot identifier 67 | /// \return execution status (1: success, 0: error) 68 | uint32_t TZ_StoreContext_S (TZ_MemoryId_t id); 69 | 70 | #endif // TZ_CONTEXT_H 71 | -------------------------------------------------------------------------------- /RedServo/STM32/Drivers/STM32F1xx_HAL_Driver/LICENSE.txt: -------------------------------------------------------------------------------- 1 | This software component is provided to you as part of a software package and 2 | applicable license terms are in the Package_license file. If you received this 3 | software component outside of a package or without applicable license terms, 4 | the terms of the BSD-3-Clause license shall apply. 5 | You may obtain a copy of the BSD-3-Clause at: 6 | https://opensource.org/licenses/BSD-3-Clause 7 | -------------------------------------------------------------------------------- /RedServo/STM32/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_gpio_ex.c 4 | * @author MCD Application Team 5 | * @brief GPIO Extension HAL module driver. 6 | * This file provides firmware functions to manage the following 7 | * functionalities of the General Purpose Input/Output (GPIO) extension peripheral. 8 | * + Extended features functions 9 | * 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | * Copyright (c) 2016 STMicroelectronics. 14 | * All rights reserved. 15 | * 16 | * This software is licensed under terms that can be found in the LICENSE file 17 | * in the root directory of this software component. 18 | * If no LICENSE file comes with this software, it is provided AS-IS. 19 | * 20 | ****************************************************************************** 21 | @verbatim 22 | ============================================================================== 23 | ##### GPIO Peripheral extension features ##### 24 | ============================================================================== 25 | [..] GPIO module on STM32F1 family, manage also the AFIO register: 26 | (+) Possibility to use the EVENTOUT Cortex feature 27 | 28 | ##### How to use this driver ##### 29 | ============================================================================== 30 | [..] This driver provides functions to use EVENTOUT Cortex feature 31 | (#) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout() 32 | (#) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout() 33 | (#) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout() 34 | 35 | @endverbatim 36 | ****************************************************************************** 37 | */ 38 | 39 | /* Includes ------------------------------------------------------------------*/ 40 | #include "stm32f1xx_hal.h" 41 | 42 | /** @addtogroup STM32F1xx_HAL_Driver 43 | * @{ 44 | */ 45 | 46 | /** @defgroup GPIOEx GPIOEx 47 | * @brief GPIO HAL module driver 48 | * @{ 49 | */ 50 | 51 | #ifdef HAL_GPIO_MODULE_ENABLED 52 | 53 | /** @defgroup GPIOEx_Exported_Functions GPIOEx Exported Functions 54 | * @{ 55 | */ 56 | 57 | /** @defgroup GPIOEx_Exported_Functions_Group1 Extended features functions 58 | * @brief Extended features functions 59 | * 60 | @verbatim 61 | ============================================================================== 62 | ##### Extended features functions ##### 63 | ============================================================================== 64 | [..] This section provides functions allowing to: 65 | (+) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout() 66 | (+) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout() 67 | (+) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout() 68 | 69 | @endverbatim 70 | * @{ 71 | */ 72 | 73 | /** 74 | * @brief Configures the port and pin on which the EVENTOUT Cortex signal will be connected. 75 | * @param GPIO_PortSource Select the port used to output the Cortex EVENTOUT signal. 76 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PORT. 77 | * @param GPIO_PinSource Select the pin used to output the Cortex EVENTOUT signal. 78 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PIN. 79 | * @retval None 80 | */ 81 | void HAL_GPIOEx_ConfigEventout(uint32_t GPIO_PortSource, uint32_t GPIO_PinSource) 82 | { 83 | /* Verify the parameters */ 84 | assert_param(IS_AFIO_EVENTOUT_PORT(GPIO_PortSource)); 85 | assert_param(IS_AFIO_EVENTOUT_PIN(GPIO_PinSource)); 86 | 87 | /* Apply the new configuration */ 88 | MODIFY_REG(AFIO->EVCR, (AFIO_EVCR_PORT) | (AFIO_EVCR_PIN), (GPIO_PortSource) | (GPIO_PinSource)); 89 | } 90 | 91 | /** 92 | * @brief Enables the Event Output. 93 | * @retval None 94 | */ 95 | void HAL_GPIOEx_EnableEventout(void) 96 | { 97 | SET_BIT(AFIO->EVCR, AFIO_EVCR_EVOE); 98 | } 99 | 100 | /** 101 | * @brief Disables the Event Output. 102 | * @retval None 103 | */ 104 | void HAL_GPIOEx_DisableEventout(void) 105 | { 106 | CLEAR_BIT(AFIO->EVCR, AFIO_EVCR_EVOE); 107 | } 108 | 109 | /** 110 | * @} 111 | */ 112 | 113 | /** 114 | * @} 115 | */ 116 | 117 | #endif /* HAL_GPIO_MODULE_ENABLED */ 118 | 119 | /** 120 | * @} 121 | */ 122 | 123 | /** 124 | * @} 125 | */ 126 | 127 | -------------------------------------------------------------------------------- /RedServo/STM32/MDK-ARM/.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | -------------------------------------------------------------------------------- /RedServo/STM32/MDK-ARM/DebugConfig/STM32code_STM32F103C8_1.0.0.dbgconf: -------------------------------------------------------------------------------- 1 | // File: STM32F101_102_103_105_107.dbgconf 2 | // Version: 1.0.0 3 | // Note: refer to STM32F101xx STM32F102xx STM32F103xx STM32F105xx STM32F107xx Reference manual (RM0008) 4 | // STM32F101xx STM32F102xx STM32F103xx STM32F105xx STM32F107xx datasheets 5 | 6 | // <<< Use Configuration Wizard in Context Menu >>> 7 | 8 | // Debug MCU configuration register (DBGMCU_CR) 9 | // Reserved bits must be kept at reset value 10 | // DBG_TIM11_STOP TIM11 counter stopped when core is halted 11 | // DBG_TIM10_STOP TIM10 counter stopped when core is halted 12 | // DBG_TIM9_STOP TIM9 counter stopped when core is halted 13 | // DBG_TIM14_STOP TIM14 counter stopped when core is halted 14 | // DBG_TIM13_STOP TIM13 counter stopped when core is halted 15 | // DBG_TIM12_STOP TIM12 counter stopped when core is halted 16 | // DBG_CAN2_STOP Debug CAN2 stopped when core is halted 17 | // DBG_TIM7_STOP TIM7 counter stopped when core is halted 18 | // DBG_TIM6_STOP TIM6 counter stopped when core is halted 19 | // DBG_TIM5_STOP TIM5 counter stopped when core is halted 20 | // DBG_TIM8_STOP TIM8 counter stopped when core is halted 21 | // DBG_I2C2_SMBUS_TIMEOUT SMBUS timeout mode stopped when core is halted 22 | // DBG_I2C1_SMBUS_TIMEOUT SMBUS timeout mode stopped when core is halted 23 | // DBG_CAN1_STOP Debug CAN1 stopped when Core is halted 24 | // DBG_TIM4_STOP TIM4 counter stopped when core is halted 25 | // DBG_TIM3_STOP TIM3 counter stopped when core is halted 26 | // DBG_TIM2_STOP TIM2 counter stopped when core is halted 27 | // DBG_TIM1_STOP TIM1 counter stopped when core is halted 28 | // DBG_WWDG_STOP Debug window watchdog stopped when core is halted 29 | // DBG_IWDG_STOP Debug independent watchdog stopped when core is halted 30 | // DBG_STANDBY Debug standby mode 31 | // DBG_STOP Debug stop mode 32 | // DBG_SLEEP Debug sleep mode 33 | // 34 | DbgMCU_CR = 0x00000007; 35 | 36 | // <<< end of configuration section >>> 37 | -------------------------------------------------------------------------------- /RedServo/STM32/MDK-ARM/EventRecorderStub.scvd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /RedServo/STM32/MDK-ARM/RTE/_STM32code/RTE_Components.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Auto generated Run-Time-Environment Configuration File 4 | * *** Do not modify ! *** 5 | * 6 | * Project: 'STM32code' 7 | * Target: 'STM32code' 8 | */ 9 | 10 | #ifndef RTE_COMPONENTS_H 11 | #define RTE_COMPONENTS_H 12 | 13 | 14 | /* 15 | * Define the Device Header File: 16 | */ 17 | #define CMSIS_device_header "stm32f10x.h" 18 | 19 | 20 | 21 | #endif /* RTE_COMPONENTS_H */ 22 | -------------------------------------------------------------------------------- /RedServo/STM32/User/APP/app.h: -------------------------------------------------------------------------------- 1 | #ifndef __APP_H 2 | #define __APP_H 3 | 4 | #include "main.h" 5 | #include "tim.h" 6 | #include "led.h" 7 | #include "oled.h" 8 | #include "gui.h" 9 | #include "control.h" 10 | #include "yuntai.h" 11 | #include "serial_it_config.h" 12 | 13 | 14 | 15 | void App_Init(void); 16 | void App_Task(void); 17 | void MPU6050_Data_Read(void); 18 | void Motor_Speed_Read(void); 19 | void Car_PID_Ctrl(void); 20 | void Update_Parameters(void); 21 | //У׼ģʽ 22 | void System_Calibration(void); 23 | #endif // !__APP_H 24 | -------------------------------------------------------------------------------- /RedServo/STM32/User/AT24C02/at24c02.c: -------------------------------------------------------------------------------- 1 | #include "at24c02.h" 2 | 3 | 4 | // #define AT24C02_SDA_Pin GPIO_PIN_10 5 | // #define AT24C02_SDA_GPIO_Port GPIOB 6 | // #define AT24C02_SCL_Pin GPIO_PIN_11 7 | // #define AT24C02_SCL_GPIO_Port GPIOB 8 | 9 | #define I2C_SCL_SET AT24C02_SCL_GPIO_Port->BSRR = AT24C02_SCL_Pin 10 | #define I2C_SCL_CLR AT24C02_SCL_GPIO_Port->BRR = AT24C02_SCL_Pin 11 | 12 | #define I2C_SDA_SET AT24C02_SDA_GPIO_Port->BSRR = AT24C02_SDA_Pin 13 | #define I2C_SDA_CLR AT24C02_SDA_GPIO_Port->BRR = AT24C02_SDA_Pin 14 | #define I2C_SDA_IN AT24C02_SDA_GPIO_Port->IDR &AT24C02_SDA_Pin 15 | 16 | #define pr_err(...) 17 | #define pr_dbg(...) 18 | 19 | #define I2C_TIME 6 20 | 21 | 22 | 23 | 24 | 25 | void delay_us(uint32_t nus) 26 | { 27 | uint32_t temp; 28 | SysTick->LOAD = 9*nus; 29 | SysTick->VAL=0X00;//清空计数器 30 | SysTick->CTRL=0X01;//使能,减到零是无动作,采用外部时钟源 31 | do 32 | { 33 | temp=SysTick->CTRL;//读取当前倒计数值 34 | }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 35 | SysTick->CTRL=0x00; //关闭计数器 36 | SysTick->VAL =0X00; //清空计数器 37 | } 38 | 39 | 40 | //产生IIC起始信号 41 | void IIC_Start_1(void) 42 | { 43 | if (!(I2C_SDA_IN)) //释放SDA 44 | { 45 | I2C_SCL_CLR; 46 | delay_us(I2C_TIME / 2); 47 | I2C_SDA_SET; 48 | delay_us(I2C_TIME / 2); //保证起始信号为高电平 49 | } 50 | 51 | I2C_SCL_SET; //保证为高电平 52 | delay_us(I2C_TIME); 53 | 54 | I2C_SDA_CLR; // START:when CLK is high,DATA change form high to low 55 | delay_us(I2C_TIME); 56 | I2C_SCL_CLR; 57 | delay_us(I2C_TIME / 2); 58 | } 59 | //产生IIC停止信号 60 | void IIC_Stop_1(void) 61 | { 62 | delay_us(I2C_TIME / 2); 63 | I2C_SCL_CLR; 64 | delay_us(I2C_TIME / 2); 65 | I2C_SDA_CLR; // STOP:when CLK is high DATA change form low to high 66 | delay_us(I2C_TIME); 67 | 68 | I2C_SCL_SET; 69 | delay_us(I2C_TIME / 2); 70 | I2C_SDA_SET; //发送I2C总线结束信号 71 | delay_us(I2C_TIME / 2); 72 | } 73 | 74 | //产生ACK应答 75 | void IIC_Ack(void) 76 | { 77 | I2C_SDA_CLR; 78 | delay_us(I2C_TIME / 2); 79 | I2C_SCL_SET; 80 | delay_us(I2C_TIME); 81 | I2C_SCL_CLR; 82 | delay_us(I2C_TIME / 2); 83 | } 84 | //不产生ACK应答 85 | void IIC_NAck(void) 86 | { 87 | I2C_SDA_SET; 88 | delay_us(I2C_TIME / 2); 89 | I2C_SCL_SET; 90 | delay_us(I2C_TIME); 91 | I2C_SCL_CLR; 92 | delay_us(I2C_TIME / 2); 93 | } 94 | // IIC发送一个字节 95 | //返回从机有无应答 96 | // 1,有应答 97 | // 0,无应答 CLK结束为低电平 98 | bool IIC_Send_Byte(uint8_t txd) 99 | { 100 | uint8_t t, ack; 101 | delay_us(I2C_TIME / 2); // 102 | I2C_SCL_CLR; //拉低时钟开始数据传输 103 | delay_us(I2C_TIME / 2); // 104 | for (t = 0; t < 8; t++) 105 | { 106 | delay_us(I2C_TIME / 2); 107 | if (txd & 0x80) 108 | { 109 | I2C_SDA_SET; 110 | } 111 | else 112 | { 113 | I2C_SDA_CLR; 114 | } 115 | txd <<= 1; 116 | delay_us(I2C_TIME / 2); //对TEA5767这三个延时都是必须的 117 | I2C_SCL_SET; 118 | delay_us(I2C_TIME); 119 | I2C_SCL_CLR; //发出数据 120 | } 121 | //释放SDA 122 | delay_us(I2C_TIME / 2); 123 | I2C_SDA_SET; 124 | delay_us(I2C_TIME / 2); 125 | I2C_SCL_SET; 126 | delay_us(I2C_TIME); //等待应答周期 127 | ack = I2C_SDA_IN; 128 | I2C_SCL_CLR; 129 | delay_us(I2C_TIME / 2); // 130 | if (!ack) 131 | { 132 | return true; 133 | } 134 | else 135 | { 136 | return false; //应答失败 137 | } 138 | } 139 | //读1个字节,ack=1时,发送ACK,ack=0,发送nACK 140 | uint8_t IIC_Read_Byte(unsigned char ack) 141 | { 142 | unsigned char i, receive = 0; 143 | delay_us(I2C_TIME / 2); 144 | I2C_SCL_CLR; 145 | 146 | for (i = 0; i < 8; i++) 147 | { 148 | delay_us(I2C_TIME / 2); 149 | I2C_SCL_SET; 150 | delay_us(I2C_TIME / 2); 151 | receive <<= 1; 152 | if (I2C_SDA_IN) 153 | receive++; 154 | delay_us(I2C_TIME / 2); 155 | I2C_SCL_CLR; 156 | delay_us(I2C_TIME / 2); 157 | } 158 | if (!ack) 159 | IIC_NAck(); //发送nACK 160 | else 161 | IIC_Ack(); //发送ACK 162 | return receive; 163 | } 164 | 165 | #define CHECK_ACK(X) \ 166 | ack = X; \ 167 | if (ack == false) \ 168 | { \ 169 | ; \ 170 | break; \ 171 | } 172 | 173 | // addr 地址8字节对齐 一次 一次写入不得超过8字节 写入间隔不得小于10ms 174 | bool I2C_Write(uint8_t devAdd, uint8_t addr, uint8_t data[], int len) 175 | { 176 | bool ack = false; 177 | IIC_Start_1(); 178 | do 179 | { 180 | CHECK_ACK(IIC_Send_Byte(devAdd)); 181 | 182 | CHECK_ACK(IIC_Send_Byte(addr)); //发送低地址 183 | 184 | for (int i = 0; i < len; i++) 185 | { 186 | CHECK_ACK(IIC_Send_Byte(data[i])); //发送字节 187 | } 188 | } while (0); 189 | IIC_Stop_1(); //产生一个停止条件 190 | return ack; 191 | } 192 | 193 | bool I2C_Read(uint8_t devAdd, uint8_t nAddr, uint8_t data[], uint8_t len) 194 | { 195 | bool ack = false; 196 | IIC_Start_1(); 197 | do 198 | { 199 | CHECK_ACK(IIC_Send_Byte(devAdd)); //发送器件地址0XA0,写数据 200 | 201 | CHECK_ACK(IIC_Send_Byte(nAddr)); 202 | 203 | IIC_Start_1(); 204 | CHECK_ACK(IIC_Send_Byte(devAdd + 1)); //进入接收模式 205 | 206 | for (int i = 0; i < len - 1; i++) 207 | { 208 | data[i] = IIC_Read_Byte(1); 209 | } 210 | data[len - 1] = IIC_Read_Byte(0); 211 | } while (0); 212 | IIC_Stop_1(); //产生一个停止条件 213 | return ack; 214 | } -------------------------------------------------------------------------------- /RedServo/STM32/User/AT24C02/at24c02.h: -------------------------------------------------------------------------------- 1 | #ifndef I2C_LIBRARY_H 2 | #define I2C_LIBRARY_H 3 | 4 | #include 5 | #include 6 | #include "main.h" 7 | 8 | void delay_us(uint32_t nus); 9 | 10 | void IIC_Start_1(void); 11 | void IIC_Stop_1(void); 12 | void IIC_Ack(void); 13 | void IIC_NAck(void); 14 | bool IIC_Send_Byte(uint8_t txd); 15 | uint8_t IIC_Read_Byte(unsigned char ack); 16 | 17 | bool I2C_Write(uint8_t devAdd, uint8_t addr, uint8_t data[], int len); 18 | bool I2C_Read(uint8_t devAdd, uint8_t nAddr, uint8_t data[], uint8_t len); 19 | 20 | #endif /* I2C_LIBRARY_H */ 21 | 22 | -------------------------------------------------------------------------------- /RedServo/STM32/User/Buzzer/Buzzer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-08-02 12-02-17 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-03 15-30-02 6 | * @FilePath: \Project\RedServo\STM32\User\Buzzer\Buzzer.c 7 | * @Description: 蜂鸣器模块,包含蜂鸣器初始化,蜂鸣器控制,蜂鸣器短响,蜂鸣器长响 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "Buzzer.h" 12 | 13 | //在这里修改蜂鸣器引脚 ,或者使用cubemx生成的宏定义(引脚命名为Buzzer_Pin) 14 | // #define Buzzer_Pin_Pin GPIO_PIN_11 15 | // #define Buzzer_Pin_GPIO_Port GPIOB 16 | 17 | /** 18 | * @brief 蜂鸣器初始化 19 | * @param 无 20 | * @retval 无 21 | */ 22 | void Buzzer_Init(void) 23 | { 24 | //引脚初始化 25 | } 26 | 27 | 28 | /** 29 | * @brief 蜂鸣器控制 30 | * @param 无 31 | * @retval 无 32 | */ 33 | void Buzzer_Control(uint8_t status) 34 | { 35 | //蜂鸣器控制 36 | if (status == 0) 37 | { 38 | HAL_GPIO_WritePin(Buzzer_Pin_GPIO_Port, Buzzer_Pin_Pin, GPIO_PIN_SET); 39 | } 40 | else if (status == 1) 41 | { 42 | HAL_GPIO_WritePin(Buzzer_Pin_GPIO_Port, Buzzer_Pin_Pin, GPIO_PIN_RESET); 43 | } 44 | 45 | } 46 | 47 | 48 | //蜂鸣器短响 49 | void Buzzer_ShortBeep(void) 50 | { 51 | Buzzer_Control(1); 52 | HAL_Delay(30); 53 | Buzzer_Control(0); 54 | } 55 | 56 | 57 | //蜂鸣器长响 58 | void Buzzer_LongBeep(void) 59 | { 60 | Buzzer_Control(1); 61 | HAL_Delay(1000); 62 | Buzzer_Control(0); 63 | } -------------------------------------------------------------------------------- /RedServo/STM32/User/Buzzer/Buzzer.h: -------------------------------------------------------------------------------- 1 | #ifndef __Buzzer_H 2 | #define __Buzzer_H 3 | 4 | #include "main.h" 5 | 6 | void Buzzer_Init(void); 7 | void Buzzer_ShortBeep(void); 8 | void Buzzer_LongBeep(void); 9 | #endif // !__Buzzer_H 10 | 11 | -------------------------------------------------------------------------------- /RedServo/STM32/User/Control/control.c: -------------------------------------------------------------------------------- 1 | #include "control.h" 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /RedServo/STM32/User/Control/control.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONTROL_H 2 | #define __CONTROL_H 3 | #include "main.h" 4 | 5 | #include "debug.h" 6 | 7 | void Motor_Speed_Read(void); 8 | void Car_PID_Ctrl(void); 9 | void Set_PID_Target(float temp_val); 10 | void Set_PID(int32_t kp, int32_t ki, int32_t kd); 11 | void Find_CCD_Zhongzhi(uint16_t *ADV); 12 | 13 | 14 | #endif // !__CONTROL_H 15 | -------------------------------------------------------------------------------- /RedServo/STM32/User/KEY/key.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-08-02 12-02-17 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-03 15-28-12 6 | * @FilePath: \Project\RedServo\STM32\User\KEY\key.c 7 | * @Description: 键盘扫描 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "key.h" 12 | #include "led.h" 13 | #include "gui.h" 14 | #include "yuntai.h" 15 | #include "Buzzer.h" 16 | #include "app.h" 17 | 18 | 19 | #define KEY1_Pin KEY_1_Pin 20 | #define KEY2_Pin KEY_2_Pin 21 | #define KEY3_Pin KEY_3_Pin 22 | #define KEY4_Pin KEY_4_Pin 23 | #define KEY5_Pin KEY_5_Pin 24 | #define KEY6_Pin KEY_6_Pin 25 | #define KEY7_Pin KEY_7_Pin 26 | #define KEY8_Pin KEY_8_Pin 27 | #define KEY9_Pin KEY_9_Pin 28 | #define KEY10_Pin KEY_10_Pin 29 | #define KEY11_Pin KEY_11_Pin 30 | 31 | 32 | 33 | #define DEBOUNCE_DELAY 250 // 设置消抖延时为200毫秒 34 | 35 | 36 | 37 | //GUI菜单标志位 38 | extern uint8_t GUI_Menu; 39 | 40 | //题目标志位 41 | extern uint8_t Problem_Flag; 42 | 43 | //校准确认标志位 44 | extern uint8_t Calibration_Flag; 45 | 46 | //急停标志位 47 | extern uint8_t Stop_Flag; 48 | 49 | // 初始位置 //舵机中值 50 | extern uint16_t pwm_A ; 51 | extern uint16_t pwm_B ; 52 | 53 | 54 | /** 55 | * @description: 按键初始化 (使用CubeMX自动生成的宏定义,就不用写这个函数了) 56 | * @return {*} 57 | */ 58 | void Key_Init(void) 59 | { 60 | 61 | } 62 | 63 | 64 | 65 | //题目切换按钮 66 | void Key_1_Callback(void) 67 | { 68 | if(Problem_Flag == 9) 69 | { 70 | if(Calibration_Flag == 0) 71 | { 72 | Calibration_Flag=1; 73 | } 74 | } 75 | //更换题目 76 | else if(Problem_Flag < 4) 77 | { 78 | Problem_Flag++; 79 | } 80 | else 81 | { 82 | Problem_Flag = 0; 83 | } 84 | 85 | } 86 | 87 | 88 | 89 | // 急停按键 90 | void Key_2_Callback(void) 91 | { 92 | if(Stop_Flag == 1) 93 | { 94 | Stop_Flag=0; 95 | } 96 | else{ 97 | Stop_Flag=1; 98 | } 99 | 100 | } 101 | 102 | 103 | //设置舵机调试模式的按钮 104 | void Key_3_Callback(void) 105 | { 106 | if (Problem_Flag==9) 107 | { 108 | Problem_Flag=0; 109 | }else 110 | { 111 | Problem_Flag=9; 112 | } 113 | } 114 | 115 | 116 | //上 117 | void Key_4_Callback(void) 118 | { 119 | 120 | } 121 | 122 | //下 123 | void Key_5_Callback(void){ 124 | 125 | } 126 | 127 | //左 128 | void Key_6_Callback(void){ 129 | 130 | } 131 | 132 | //右 133 | void Key_7_Callback(void){ 134 | 135 | } 136 | 137 | //题目1 //复位按键 138 | void Key_8_Callback(void){ 139 | Problem1(); 140 | } 141 | 142 | 143 | void Key_9_Callback(void){ 144 | //更换题目 145 | if(Problem_Flag == 2) 146 | { 147 | Problem_Flag =0; 148 | } 149 | else 150 | { 151 | Problem_Flag = 2; 152 | } 153 | } 154 | void Key_10_Callback(void){ 155 | //更换题目 156 | if(Problem_Flag == 3) 157 | { 158 | Problem_Flag =0; 159 | } 160 | else 161 | { 162 | Problem_Flag = 3; 163 | } 164 | } 165 | void Key_11_Callback(void){ 166 | //更换题目 167 | if(Problem_Flag == 4) 168 | { 169 | Problem_Flag =0; 170 | } 171 | else 172 | { 173 | Problem_Flag = 4; 174 | } 175 | } 176 | 177 | // 178 | /** 179 | * @description: 按键检测,外部中断回调函数 180 | * @param {uint16_t} GPIO_Pin 181 | * @return {*} 182 | */ 183 | void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) 184 | { 185 | 186 | /* Prevent unused argument(s) compilation warning */ 187 | UNUSED(GPIO_Pin); 188 | /* NOTE: This function Should not be modified, when the callback is needed, 189 | the HAL_GPIO_EXTI_Callback could be implemented in the user file 190 | */ 191 | // 按键按下 192 | if(GPIO_Pin == KEY1_Pin) 193 | { 194 | //printf("按键1"); 195 | Debounce(GPIO_Pin, Key_1_Callback); 196 | 197 | } 198 | else if(GPIO_Pin == KEY2_Pin) 199 | { 200 | 201 | // 按键2按下的处理代码 202 | Debounce(GPIO_Pin, Key_2_Callback); 203 | } 204 | else if(GPIO_Pin == KEY3_Pin) 205 | { 206 | 207 | // 按键3按下的处理代码 208 | Debounce(GPIO_Pin, Key_3_Callback); 209 | } 210 | else if(GPIO_Pin == KEY4_Pin) 211 | { 212 | // 按键4按下的处理代码 213 | Debounce(GPIO_Pin, Key_4_Callback); 214 | } 215 | else if(GPIO_Pin == KEY5_Pin) 216 | { 217 | // 按键5按下的处理代码 218 | Debounce(GPIO_Pin, Key_5_Callback); 219 | } 220 | else if(GPIO_Pin == KEY6_Pin) 221 | { 222 | // 按键6按下的处理代码 223 | Debounce(GPIO_Pin, Key_6_Callback); 224 | } 225 | else if(GPIO_Pin == KEY7_Pin) 226 | { 227 | // 按键7按下的处理代码 228 | Debounce(GPIO_Pin, Key_7_Callback); 229 | } 230 | else if(GPIO_Pin == KEY8_Pin) 231 | { 232 | DEBUG_printf("按键8"); 233 | // 按键8按下的处理代码 234 | Debounce(GPIO_Pin, Key_8_Callback); 235 | 236 | } 237 | else if(GPIO_Pin == KEY9_Pin) 238 | { 239 | Debounce(GPIO_Pin, Key_9_Callback); 240 | 241 | } 242 | else if(GPIO_Pin == KEY10_Pin) 243 | { 244 | Debounce(GPIO_Pin, Key_10_Callback); 245 | 246 | } 247 | else if(GPIO_Pin == KEY11_Pin) 248 | { 249 | Debounce(GPIO_Pin, Key_11_Callback); 250 | 251 | } 252 | } 253 | 254 | 255 | 256 | 257 | // 通用的按键消抖函数 258 | void Debounce(uint16_t GPIO_Pin, void (*callback)(void)) 259 | { 260 | static uint32_t lastTriggerTime = 0; 261 | uint32_t currentTime = HAL_GetTick(); // 获取当前时间戳 262 | 263 | if (currentTime - lastTriggerTime >= DEBOUNCE_DELAY) 264 | { 265 | Buzzer_ShortBeep(); 266 | callback(); // 调用传入的回调函数 267 | lastTriggerTime = currentTime; // 更新上一次触发的时间戳 268 | } 269 | } 270 | 271 | 272 | 273 | -------------------------------------------------------------------------------- /RedServo/STM32/User/KEY/key.h: -------------------------------------------------------------------------------- 1 | #ifndef __KEY_H 2 | #define __KEY_H 3 | 4 | #include "main.h" 5 | void Debounce(uint16_t GPIO_Pin, void (*callback)(void)); 6 | void Key_Init(void); 7 | 8 | #endif // !__LED_H 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /RedServo/STM32/User/LED/led.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-07 09-36-03 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-07-26 20-30-03 6 | * @FilePath: \MDK-ARMd:\duruofu\Project\Avoidance_Car\project\STM32ZET6\Users\LED\led.c 7 | * @Description: 控制一个led灯 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "led.h" 12 | 13 | /** 14 | * @brief LED初始化 15 | * @param 无 16 | * @retval 使用cubeMX生成只需要将引脚用户标签命名为LED_1即可,无需再次调用此函数 17 | */ 18 | void LED_Init(void) 19 | { 20 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 21 | 22 | /* GPIO Ports Clock Enable */ 23 | __HAL_RCC_GPIOB_CLK_ENABLE(); 24 | 25 | /*Configure GPIO pin Output Level */ 26 | HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_RESET); 27 | 28 | /*Configure GPIO pin : LED_Pin */ 29 | GPIO_InitStruct.Pin = LED_1_Pin; 30 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 31 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 32 | HAL_GPIO_Init(LED_1_GPIO_Port, &GPIO_InitStruct); 33 | } 34 | 35 | 36 | /** 37 | * @brief LED亮 38 | * @param 无 39 | * @retval 无 40 | */ 41 | void LED_On(uint8_t led_num) 42 | { 43 | if (led_num == 1) 44 | { 45 | HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_SET); 46 | } 47 | else if(led_num == 2) 48 | { 49 | HAL_GPIO_WritePin(LED_2_GPIO_Port, LED_2_Pin, GPIO_PIN_SET); 50 | } 51 | } 52 | 53 | 54 | /** 55 | * @brief LED灭 56 | * @param 无 57 | * @retval 无 58 | */ 59 | void LED_Off(uint8_t led_num) 60 | { 61 | if (led_num == 1) 62 | { 63 | HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_RESET); 64 | } 65 | else if(led_num == 2) 66 | { 67 | HAL_GPIO_WritePin(LED_2_GPIO_Port, LED_2_Pin, GPIO_PIN_RESET); 68 | } 69 | } 70 | 71 | 72 | /** 73 | * @brief LED翻转 74 | * @param 无 75 | * @retval 无 76 | */ 77 | void LED_Toggle(uint8_t led_num) 78 | { 79 | 80 | if (led_num == 1) 81 | { 82 | HAL_GPIO_TogglePin(LED_1_GPIO_Port, LED_1_Pin); 83 | } 84 | else if(led_num == 2) 85 | { 86 | HAL_GPIO_TogglePin(LED_2_GPIO_Port, LED_2_Pin); 87 | } 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /RedServo/STM32/User/LED/led.h: -------------------------------------------------------------------------------- 1 | #ifndef __LED_H 2 | #define __LED_H 3 | 4 | #include "gpio.h" 5 | 6 | void LED_Init(void); 7 | void LED_On(uint8_t led_num); 8 | void LED_Off(uint8_t led_num); 9 | void LED_Toggle(uint8_t led_num); 10 | 11 | #endif // !__LED_H 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /RedServo/STM32/User/OLED/OLED.h: -------------------------------------------------------------------------------- 1 | #ifndef _OLED_H_ 2 | #define _OLED_H_ 3 | #include 4 | #include 5 | #include "main.h" 6 | 7 | 8 | #define u8 unsigned char 9 | #define u32 unsigned int 10 | #define OLED_CMD 0 //写命令 11 | #define OLED_DATA 1 //写数据 12 | #define OLED_MODE 0 13 | 14 | //下面这些是固件库四线IIC引脚的初始化 15 | 16 | #define OLED_CS_Clr() OLED_CS=0 17 | #define OLED_CS_Set() OLED_CS=1 18 | #define OLED_RST_Clr() OLED_RST=0 19 | #define OLED_RST_Set() OLED_RST=1 20 | #define OLED_DC_Clr() OLED_DC=0 21 | #define OLED_DC_Set() OLED_DC=1 / 22 | 23 | //引脚定义:PD3 :OLED_SCLK;PG13:OLED_SDIN 24 | 25 | #define OLED_SCLK_Clr() HAL_GPIO_WritePin(I2C_SCL_GPIO_Port,I2C_SCL_Pin,GPIO_PIN_RESET); 26 | #define OLED_SCLK_Set() HAL_GPIO_WritePin(I2C_SCL_GPIO_Port,I2C_SCL_Pin,GPIO_PIN_SET); 27 | #define OLED_SDIN_Clr() HAL_GPIO_WritePin(I2C_SDA_GPIO_Port,I2C_SDA_Pin,GPIO_PIN_RESET); 28 | #define OLED_SDIN_Set() HAL_GPIO_WritePin(I2C_SDA_GPIO_Port,I2C_SDA_Pin,GPIO_PIN_SET); 29 | 30 | 31 | //OLED模式设置 32 | //0:4线串行模式 33 | //1:并行8080模式 34 | 35 | #define SIZE 16 36 | #define XLevelL 0x02 37 | #define XLevelH 0x10 38 | #define Max_Column 128 39 | #define Max_Row 64 40 | #define Brightness 0xFF 41 | #define X_WIDTH 128 42 | #define Y_WIDTH 64 43 | //-----------------OLED端口定义---------------- 44 | 45 | 46 | //OLED控制用函数 47 | void OLED_GPIO_Init(void); 48 | void OLED_WR_Byte(unsigned dat,unsigned cmd); 49 | void OLED_Display_On(void); 50 | void OLED_Display_Off(void); 51 | void OLED_Init(void); 52 | void OLED_Clear(void); 53 | void OLED_DrawPoint(u8 x,u8 y,u8 t); 54 | void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot); 55 | void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size); 56 | void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size); 57 | void OLED_ShowString(u8 x,u8 y, u8 *p,u8 Char_Size); 58 | void OLED_Set_Pos(unsigned char x, unsigned char y); 59 | void OLED_ShowCHinese(u8 x,u8 y,u8 no); 60 | void OLED_ShowCHinese1(u8 x,u8 y,u8 no); 61 | 62 | void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]); 63 | void fill_picture(unsigned char fill_Data); 64 | void Picture(void); 65 | void IIC_Start(void); 66 | void IIC_Stop(void); 67 | void Write_IIC_Command(unsigned char IIC_Command); 68 | void Write_IIC_Data(unsigned char IIC_Data); 69 | void Write_IIC_Byte(unsigned char IIC_Byte); 70 | void IIC_Wait_Ack(void); 71 | 72 | 73 | 74 | #endif /* 9_6_OLED_OLED_H_ */ 75 | -------------------------------------------------------------------------------- /RedServo/STM32/User/OLED/OLEDGUI/gui.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-27 09-48-56 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-02 17-11-18 6 | * @FilePath: \Project\RedServo\STM32\User\OLED\OLEDGUI\gui.c 7 | * @Description: 0.96OLED配合的GUI简单GUI界面 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "gui.h" 12 | #include 13 | 14 | 15 | //菜单页 16 | extern uint8_t GUI_Menu = 0; 17 | extern int32_t Servo_Kp ; // 舵机比例系数 18 | extern int32_t Servo_Ki ; // 舵机积分系数 19 | extern int32_t Servo_Kd ; // 舵机微分系数 20 | 21 | extern uint16_t pwm_A ; 22 | extern uint16_t pwm_B ; 23 | 24 | //题目标志位 25 | extern uint8_t Problem_Flag; 26 | 27 | 28 | //急停标志位 29 | extern uint8_t Stop_Flag; 30 | 31 | //校准确认标志位 32 | extern uint8_t Calibration_Flag; 33 | 34 | //消息接收标志位 35 | extern uint8_t K210_Flag; 36 | 37 | void Menu_Refresh(void) 38 | { 39 | uint8_t str_buff1[64]= {0}; 40 | uint8_t str_buff2[64]= {0}; 41 | uint8_t str_buff3[64]= {0}; 42 | uint8_t str_buff4[64]= {0}; 43 | uint8_t str_buff5[64]= {0}; 44 | uint8_t str_buff6[64]= {0}; 45 | 46 | //OLED_Clear(); 47 | switch(GUI_Menu) 48 | { 49 | case 0: 50 | { 51 | OLED_ShowString(40,0,"Serv",16); 52 | sprintf((char *)str_buff1, "Problem_Flag:%1d",Problem_Flag); 53 | sprintf((char *)str_buff2, "Stop_Flag:%d",Stop_Flag); 54 | sprintf((char *)str_buff3, "Calibration:%d",Calibration_Flag); 55 | sprintf((char *)str_buff4, "K210_Flag:%d",K210_Flag); 56 | sprintf((char *)str_buff5, "pwm_A:%5d",pwm_A); 57 | sprintf((char *)str_buff6, "pwm_B:%5d",pwm_B); 58 | break; 59 | } 60 | case 1: 61 | { 62 | OLED_ShowString(40,0,"Motor",16); 63 | sprintf((char *)str_buff1, "Problem_Flag:%1d",Problem_Flag); 64 | sprintf((char *)str_buff2, "Servo_Kp:%3.2f",(float)Servo_Kp); 65 | sprintf((char *)str_buff3, "Servo_Kp:%3.2f",(float)Servo_Ki); 66 | sprintf((char *)str_buff4, "Servo_Kp:%3.2f",(float)Servo_Kd); 67 | sprintf((char *)str_buff5, "K210_Flag:%3.2f",(float)K210_Flag); 68 | sprintf((char *)str_buff5, "pwm_A:%5d",pwm_A); 69 | sprintf((char *)str_buff6, "pwm_B:%5d",pwm_B); 70 | break; 71 | } 72 | case 2: 73 | { 74 | OLED_ShowString(40,0,"PID ",16); 75 | sprintf((char *)str_buff1, "Problem_Flag:%1d",Problem_Flag); 76 | 77 | break; 78 | } 79 | case 3: 80 | { 81 | 82 | 83 | 84 | } 85 | } 86 | OLED_ShowString(0, 2,str_buff1, 8); 87 | OLED_ShowString(0, 3,str_buff2, 8); 88 | OLED_ShowString(0, 4,str_buff3, 8); 89 | OLED_ShowString(0, 5,str_buff4, 8); 90 | OLED_ShowString(0, 6,str_buff5, 8); 91 | OLED_ShowString(0, 7,str_buff6, 8); 92 | } 93 | -------------------------------------------------------------------------------- /RedServo/STM32/User/OLED/OLEDGUI/gui.h: -------------------------------------------------------------------------------- 1 | #ifndef __GUI_H 2 | #define __GUI_H 3 | 4 | #include "OLED.h" 5 | #include "app.h" 6 | void Menu_Refresh(void); 7 | 8 | #endif // !__GUI_H 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /RedServo/STM32/User/PWM/pwm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-07 15-49-11 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-07-26 21-36-15 6 | * @FilePath: \MDK-ARMd:\duruofu\Project\Avoidance_Car\project\STM32ZET6\Users\PWM\pwm.c 7 | * @Description: PWM模块(驱动电机使用,双路同频) 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "pwm.h" 12 | 13 | //在此设置使用的定时器和对应A,B两路PWM的通道 14 | 15 | #define TIM_1 htim1 16 | 17 | 18 | #define PWM_SERVO_CHANNEL_A TIM_CHANNEL_1 19 | #define PWM_SERVO_CHANNEL_B TIM_CHANNEL_4 20 | 21 | 22 | //设定占空比最大值(Counter Period计数值) 23 | #define PWM2_D 20000 24 | 25 | 26 | //急停标志位 27 | extern uint8_t Stop_Flag=0; 28 | 29 | //PWM初始化 30 | void PWM_Init(void) 31 | { 32 | //-----------舵机的PWM初始化----------- 33 | // 使能定时器的通道 34 | HAL_TIM_PWM_Start(&TIM_1,PWM_SERVO_CHANNEL_A); 35 | HAL_TIM_PWM_Start(&TIM_1,PWM_SERVO_CHANNEL_B); 36 | // 设置初始占空比为0 37 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_A, 0); 38 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_B, 0); 39 | } 40 | 41 | 42 | //--------------------舵机PWM调节-------------------- 43 | 44 | void SERVO_PWMA_Set(uint16_t pwm_d) 45 | { 46 | if (Stop_Flag==1)return; 47 | if(pwm_d > PWM2_D) 48 | { 49 | pwm_d = PWM2_D; 50 | } 51 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_A, pwm_d); 52 | } 53 | 54 | 55 | void SERVO_PWMB_Set(uint16_t pwm_d) 56 | { 57 | if (Stop_Flag==1)return; 58 | if(pwm_d > PWM2_D) 59 | { 60 | pwm_d = PWM2_D; 61 | } 62 | __HAL_TIM_SET_COMPARE(&TIM_1, PWM_SERVO_CHANNEL_B, pwm_d); 63 | } 64 | 65 | 66 | -------------------------------------------------------------------------------- /RedServo/STM32/User/PWM/pwm.h: -------------------------------------------------------------------------------- 1 | #ifndef __PWM_H 2 | #define __PWM_H 3 | 4 | #include "main.h" 5 | #include "tim.h" 6 | 7 | void PWM_Init(void); 8 | void Motor_PWMA_Set(uint16_t pwm_d); 9 | void Motor_PWMB_Set(uint16_t pwm_d); 10 | void SERVO_PWMA_Set(uint16_t pwm_d); 11 | void SERVO_PWMB_Set(uint16_t pwm_d); 12 | 13 | #endif // !__PWM_H 14 | 15 | -------------------------------------------------------------------------------- /RedServo/STM32/User/USART/debug.c: -------------------------------------------------------------------------------- 1 | #include "debug.h" 2 | #include "string.h" 3 | #include "yuntai.h" 4 | #define RXBUFFERSIZE 256 //最大接收字节数 5 | 6 | //定义串口句柄,使用串口1 7 | #define UART_HANDLE huart1 8 | 9 | //定义数据缓冲区 10 | uint8_t RxBuffer[RXBUFFERSIZE]; 11 | uint8_t Uart_RxBuffer; //接收中断缓冲 12 | uint8_t Uart_Rx_Cnt = 0; //接收缓冲计数 13 | 14 | uint16_t data[4]={0}; 15 | 16 | 17 | // 初始实时位置 18 | extern uint16_t pwm_A ; 19 | extern uint16_t pwm_B ; 20 | 21 | //中心位置处参数 22 | extern uint16_t Centre_A ; 23 | extern uint16_t Centre_B ; 24 | 25 | 26 | void Debug_Init(void) 27 | { 28 | /*串口硬件配置代码(使用cudeMX则不需要此部分) 29 | Init the GPIO of USART1 30 | */ 31 | //使能 USART1 的接收中断 32 | __HAL_UART_ENABLE_IT(&UART_HANDLE,UART_IT_RXNE); 33 | //开启 USART1 的连续接收中断,并指定接收缓冲区的地址和长度 34 | HAL_UART_Receive_IT(&UART_HANDLE,&Uart_RxBuffer,1); 35 | } 36 | 37 | //串口1接收完成回调函数 38 | void UART1_RxCpltCallback(UART_HandleTypeDef *huart) 39 | { 40 | /* Prevent unused argument(s) compilation warning */ 41 | UNUSED(huart); 42 | /* NOTE: This function Should not be modified, when the callback is needed, 43 | the HAL_UART_TxCpltCallback could be implemented in the user file 44 | */ 45 | 46 | if(Uart_Rx_Cnt >= 255) //溢出判断 47 | { 48 | Uart_Rx_Cnt = 0; 49 | memset(RxBuffer,0x00,sizeof(RxBuffer)); 50 | HAL_UART_Transmit(&UART_HANDLE, (uint8_t *)"数据溢出", 10,0xFFFF); 51 | } 52 | else 53 | { 54 | RxBuffer[Uart_Rx_Cnt++] = Uart_RxBuffer; 55 | //单字符判断 56 | if(Uart_RxBuffer == '1')//当发送1时,翻转电平 57 | { 58 | printf("1"); 59 | Centre_A = pwm_A; 60 | Centre_B = pwm_B; 61 | } 62 | else if(Uart_RxBuffer == '2')//当发送2时,翻转电平 63 | { 64 | DEBUG_printf("发送2"); 65 | } 66 | else if(Uart_RxBuffer == '3')//当发送3时,翻转电平 67 | { 68 | DEBUG_printf("发送3"); 69 | } 70 | else if(Uart_RxBuffer == '4')//当发送4时,翻转电平 71 | { 72 | DEBUG_printf("发送4"); 73 | } 74 | if((RxBuffer[Uart_Rx_Cnt-1] == 0x0A)&&(RxBuffer[Uart_Rx_Cnt-2] == 0x0D)) //判断结束位 75 | { 76 | //这里可以写多字节消息的判断 77 | //单字节消息0 78 | 79 | // 解析k210数据 80 | //sscanf((const char *)RxBuffer, "%d,%d\r\n", &data[0], &data[1]); 81 | //pwm_A =data[0]; 82 | //pwm_B =data[1]; 83 | //printf("%d,%d",Position_error[0],Position_error[1]); 84 | 85 | 86 | //复位 87 | Uart_Rx_Cnt = 0; 88 | memset(RxBuffer,0x00,sizeof(RxBuffer)); //清空数组 89 | } 90 | } 91 | 92 | HAL_UART_Receive_IT(&UART_HANDLE, (uint8_t *)&Uart_RxBuffer, 1); //因为接收中断使用了一次即关闭,所以在最后加入这行代码即可实现无限使用 93 | } 94 | 95 | //串口1错误回调函数(主要用来清除溢出中断) 96 | void UART1_ErrorCallback(UART_HandleTypeDef *huart) 97 | { 98 | if(HAL_UART_ERROR_ORE) 99 | { 100 | uint32_t temp = huart->Instance->SR; 101 | temp = huart->Instance->DR; 102 | } 103 | } 104 | 105 | /***************** 发送字符串 **********************/ 106 | void Usart_SendString(uint8_t *str) 107 | { 108 | unsigned int k=0; 109 | do 110 | { 111 | HAL_UART_Transmit(&UART_HANDLE,(uint8_t *)(str + k) ,1,1000); 112 | k++; 113 | } while(*(str + k)!='\0'); 114 | 115 | } 116 | 117 | /** 118 | * 函数功能: 重定向c库函数printf到DEBUG_USARTx 119 | * 输入参数: 无 120 | * 返 回 值: 无 121 | * 说 明:无 122 | */ 123 | int fputc(int ch, FILE *f) 124 | { 125 | HAL_UART_Transmit(&UART_HANDLE, (uint8_t *)&ch, 1, 0xffff); 126 | return ch; 127 | } 128 | 129 | /** 130 | * 函数功能: 重定向c库函数getchar,scanf到DEBUG_USARTx 131 | * 输入参数: 无 132 | * 返 回 值: 无 133 | * 说 明:无 134 | */ 135 | int fgetc(FILE *f) 136 | { 137 | uint8_t ch = 0; 138 | HAL_UART_Receive(&UART_HANDLE, &ch, 1, 0xffff); 139 | return ch; 140 | } 141 | -------------------------------------------------------------------------------- /RedServo/STM32/User/USART/debug.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "usart.h" 4 | #ifndef __DEBUG_H 5 | #define __DEBUG_H 6 | 7 | 8 | #define DEBUG // 定义 DEBUG 宏,用于控制调试输出 9 | #ifdef DEBUG 10 | // 如果 DEBUG 宏已定义,则定义以下宏用于调试输出 11 | #define DEBUG_printf(format, ...) printf(format "\r\n", ##__VA_ARGS__) 12 | #define DEBUG_info(tag, format, ...) printf("DEBUG_info[" tag "]:" format "\r\n", ##__VA_ARGS__) 13 | #define DEBUG_warnig(tag, format, ...) printf("DEBUG_warnig[" tag "]:" format "\r\n", ##__VA_ARGS__) 14 | #define DEBUG__error(tag, format, ...) printf("DEBUG__error[" tag "]:" format "\r\n",##__VA_ARGS__) 15 | #else 16 | // 如果 DEBUG 宏未定义,则定义以下宏为空,以屏蔽调试输出 17 | #define DEBUG_printf(format, ...) printf(format "\r\n", ##__VA_ARGS__) 18 | #define DEBUG_info(tag, format, ...) 19 | #define DEBUG_warnig(tag, format, ...) 20 | #define DEBUG__error(tag, format, ...) 21 | #endif 22 | 23 | //串口1接收中断初始化 24 | void Debug_Init(void); 25 | 26 | //串口1接收完成回调函数 27 | void UART1_RxCpltCallback(UART_HandleTypeDef *huart); 28 | 29 | void UART1_ErrorCallback(UART_HandleTypeDef *huart); 30 | 31 | #endif // !__DEBUG_H 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /RedServo/STM32/User/USART/serial_it_config.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-08-02 12-02-17 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-03 15-29-16 6 | * @FilePath: \Project\RedServo\STM32\User\USART\serial_it_config.c 7 | * @Description: 串口模块汇总,包含串口初始化,串口接收中断初始化,串口接收完成回调函数,串口错误回调函数 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "serial_it_config.h" 12 | 13 | /** 14 | * @description: 串口接收中断初始化(总) 15 | * @return {*}Debug_Init 16 | */ 17 | void USART_IT_Config(void) 18 | { 19 | //串口1接收中断初始化 20 | Debug_Init(); 21 | //串口2接收中断初始化 22 | USART2_Init(); 23 | //串口3接收中断初始化 24 | } 25 | 26 | //串口接收完成回调函数 27 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 28 | 29 | { 30 | if(huart->Instance == USART1) 31 | { 32 | UART1_RxCpltCallback(huart); 33 | } 34 | else if(huart->Instance == USART2) 35 | { 36 | UART2_RxCpltCallback(huart); 37 | } 38 | else if(huart->Instance == USART3) 39 | { 40 | //UART2_RxCpltCallback(); 41 | } 42 | } 43 | 44 | 45 | //错误回调 46 | void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 47 | { 48 | if(huart->Instance == USART1) 49 | { 50 | UART1_ErrorCallback(huart); 51 | } 52 | else if(huart->Instance == USART2) 53 | { 54 | UART2_ErrorCallback(huart); 55 | } 56 | else if(huart->Instance == USART3) 57 | { 58 | //UART3_ErrorCallback(); 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /RedServo/STM32/User/USART/serial_it_config.h: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "debug.h" 3 | #include "usart_2.h" 4 | 5 | #ifndef __USART_CONFIG_H 6 | #define __USART_CONFIG_H 7 | 8 | void USART_IT_Config(void); 9 | 10 | #endif // !__USART_CONFIG_H 11 | 12 | 13 | -------------------------------------------------------------------------------- /RedServo/STM32/User/USART/usart_2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: DuRuofu duruofu@qq.com 3 | * @Date: 2023-07-13 17-52-31 4 | * @LastEditors: DuRuofu 5 | * @LastEditTime: 2023-08-04 06-57-42 6 | * @FilePath: \Project\RedServo\STM32\User\USART\usart_2.c 7 | * @Description: 串口2的驱动代码(用于调试PID) 8 | * Copyright (c) 2023 by duruofu@foxmail.com All Rights Reserved. 9 | */ 10 | 11 | #include "usart_2.h" 12 | #include "string.h" 13 | #include "debug.h" 14 | #include 15 | #include 16 | 17 | #define RXBUFFERSIZE_2 256 //最大接收字节数 18 | 19 | //定义串口句柄,使用串口2 20 | #define UART_HANDLE huart2 21 | 22 | //定义数据缓冲区 23 | uint8_t RxBuffer_2[RXBUFFERSIZE_2]; 24 | uint8_t Uart_RxBuffer_2; //接收中断缓冲 25 | uint8_t Uart_Rx_Cnt_2 = 0; //接收缓冲计数 26 | 27 | extern int8_t Position_error[2]={0}; 28 | 29 | //--------------------------K210参考系--------------------- 30 | //消息接收标志位 31 | extern uint8_t K210_Flag; 32 | //接收缓冲区 33 | extern uint16_t K210_data[8]={0}; 34 | void USART2_Init(void) 35 | { 36 | /*串口硬件配置代码(使用cudeMX则不需要此部分) 37 | Init the GPIO of USART1 38 | */ 39 | //使能 USART1 的接收中断 40 | __HAL_UART_ENABLE_IT(&UART_HANDLE,UART_IT_RXNE); 41 | //开启 USART1 的连续接收中断,并指定接收缓冲区的地址和长度 42 | HAL_UART_Receive_IT(&UART_HANDLE,&Uart_RxBuffer_2,1); 43 | } 44 | 45 | //串口2接收完成回调函数 46 | void UART2_RxCpltCallback(UART_HandleTypeDef *huart) 47 | { 48 | 49 | /* Prevent unused argument(s) compilation warning */ 50 | UNUSED(huart); 51 | /* NOTE: This function Should not be modified, when the callback is needed, 52 | the HAL_UART_TxCpltCallback could be implemented in the user file 53 | */ 54 | 55 | if(Uart_Rx_Cnt_2 >= 255) //溢出判断 56 | { 57 | Uart_Rx_Cnt_2 = 0; 58 | memset(RxBuffer_2,0x00,sizeof(RxBuffer_2)); 59 | HAL_UART_Transmit(&UART_HANDLE, (uint8_t *)"数据溢出", 10,0xFFFF); 60 | } 61 | else 62 | { 63 | RxBuffer_2[Uart_Rx_Cnt_2++] = Uart_RxBuffer_2; 64 | 65 | 66 | if((RxBuffer_2[Uart_Rx_Cnt_2-1] == 0x0A)&&(RxBuffer_2[Uart_Rx_Cnt_2-2] == 0x0D)) //判断结束位 67 | { 68 | 69 | // 这里可以写多字节消息的判断 70 | 71 | //Usart2_SendString(RxBuffer_2); 72 | // 解析k210数据 73 | sscanf((const char *)RxBuffer_2, "%d,%d,%d,%d,%d,%d,%d,%d\r\n", &K210_data[0], &K210_data[1],&K210_data[2],&K210_data[3],&K210_data[4], &K210_data[5],&K210_data[6],&K210_data[7]); 74 | DEBUG_info("K210","收到信息:%d,%d,%d,%d,%d,%d,%d,%d\r\n",K210_data[0], K210_data[1],K210_data[2],K210_data[3],K210_data[4], K210_data[5],K210_data[6],K210_data[7]); 75 | K210_Flag = 1; 76 | //DEBUG_info("K210","K210_Flag:%d\r\n",K210_Flag); 77 | 78 | //printf("%d,%d",Position_error[0],Position_error[1]); 79 | 80 | //复位 81 | Uart_Rx_Cnt_2 = 0; 82 | memset(RxBuffer_2,0x00,sizeof(RxBuffer_2)); //清空数组 83 | } 84 | } 85 | 86 | HAL_UART_Receive_IT(&UART_HANDLE, (uint8_t *)&Uart_RxBuffer_2, 1); //因为接收中断使用了一次即关闭,所以在最后加入这行代码即可实现无限使用 87 | 88 | 89 | 90 | } 91 | 92 | //串口1错误回调函数(主要用来清除溢出中断) 93 | void UART2_ErrorCallback(UART_HandleTypeDef *huart) 94 | { 95 | if(HAL_UART_ERROR_ORE) 96 | { 97 | uint32_t temp = huart->Instance->SR; 98 | temp = huart->Instance->DR; 99 | } 100 | } 101 | 102 | /***************** 发送字符串 **********************/ 103 | void Usart2_SendString(uint8_t *str) 104 | { 105 | unsigned int k=0; 106 | do 107 | { 108 | HAL_UART_Transmit(&UART_HANDLE,(uint8_t *)(str + k) ,1,1000); 109 | k++; 110 | } while(*(str + k)!='\0'); 111 | 112 | } 113 | -------------------------------------------------------------------------------- /RedServo/STM32/User/USART/usart_2.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "usart.h" 4 | 5 | 6 | #ifndef __USART2_2_H 7 | #define __USART2_2_H 8 | 9 | 10 | //串口1接收中断初始化 11 | void USART2_Init(void); 12 | //串口1接收完成回调函数 13 | void UART2_RxCpltCallback(UART_HandleTypeDef *huart); 14 | void UART2_ErrorCallback(UART_HandleTypeDef *huart); 15 | void Usart2_SendString(uint8_t *str); 16 | 17 | #endif // !__USART2_2_H 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /RedServo/STM32/User/YunTai/yuntai.h: -------------------------------------------------------------------------------- 1 | #ifndef __YUNTAI_H 2 | #define __YUNTAI_H 3 | 4 | #include "main.h" 5 | #include "pwm.h" 6 | void Yuntai_Init(void); 7 | void Yuntaiz_A_Move(uint16_t pwm_d,int16_t Flow_Coefficient); 8 | void Yuntaiz_B_Move(uint16_t pwm_d,int16_t Flow_Coefficient); 9 | void Yuntaiz_AB_Move(uint16_t pwm_a,uint16_t pwm_b, int16_t Flow_Coefficient); 10 | void Yuntai_A4_Track(uint16_t pwm_a,uint16_t pwm_b, int16_t Flow_Coefficient); 11 | void Yuntai_Control(void); 12 | void Yuntaiz_AB_Move_2(uint16_t pwm_a, uint16_t pwm_b, int16_t Flow_Coefficient); 13 | void Yuntaiz_AB_Move_3(uint16_t pwm_a, uint16_t pwm_b, int16_t Flow_Coefficient); 14 | #endif // !__YUNTAI_H 15 | -------------------------------------------------------------------------------- /RedServo/STM32/keilkill.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/RedServo/STM32/keilkill.bat -------------------------------------------------------------------------------- /attachments/image-20230804061932311.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/attachments/image-20230804061932311.png -------------------------------------------------------------------------------- /attachments/image-20230805083401173.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DuRuofu/STM32_Target_Tracking_System/98985b4f460fb3d85cb1e632b071ff854f44c3cd/attachments/image-20230805083401173.png -------------------------------------------------------------------------------- /attachments/硬件框图.drawio: -------------------------------------------------------------------------------- 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 | --------------------------------------------------------------------------------