4 Commits

6 changed files with 44 additions and 40 deletions

View File

@@ -5,7 +5,7 @@
- [x] 超声驱动信号300KHz 50%duty 5cycles发送
- [x] PA2/PA3配置为USART0
- [x] LED配置存活状态闪烁
- [ ] 超声反射回波接受与精准计时
- [x] 超声反射回波接受与精准计时
- 超声波反射回来后到sensor成功接收GPIO上的反应主要分为两部分
1. 超声波在发送时产生的余震24V下大概为230us12V下大概为210us。前面所说的时间均为比较保守的时间
@@ -14,3 +14,5 @@
- [x] 产生一个210-230us可调节的准确延时。TIMER15配置为1us计一个数设置重载为需要的时间产生中断即可。
- [x] 在产生指定时间的中断服务函数中开启EXTI0PA0,sensor信号接收引脚开启TIMER14计时计数器1us计一个数计算接收到外部中断的时间
- [ ] 在外部中断服务函数中产生一个事件或中断进入到TIMER14的中断
- [x] TIMER14不存在F4x系列改用TIMER16.
- [x] 放弃上述流程多一层中断层直接在EXTI0的中断服务函数中直接读取TIMER16的CH_0计数值。

View File

@@ -49,6 +49,8 @@ void PendSV_Handler(void);
/* this function handles SysTick exception */
void SysTick_Handler(void);
void TIMER13_IRQHandler(void);
void TIMER5_IRQHandler(void);
void TIMER15_IRQHandler(void);
void EXTI0_1_IRQHandler(void);
#endif /* GD32E23X_IT_H */

View File

@@ -7,8 +7,21 @@
#include "gd32e23x.h"
#define POWER_SUPPLY_12V
// #define POWER_SUPPLY_24V
#ifdef POWER_SUPPLY_12V
#define TIME_CORRECTION_US 250
#define CAPTURE_VALUE_MAX 515
#elif defined(POWER_SUPPLY_24V)
#define TIME_CORRECTION_US 230
#define CAPTURE_VALUE_MAX 550
#else
#error "Please define either POWER_SUPPLY_12V or POWER_SUPPLY_24V"
#endif
#define ULTRASONIC_CYCLES 0x05U
#define ULTRASONIC_TRAN_US 100 // (ms)
#define ULTRASONIC_TRAN_US 500 // (ms)
#define LED_PORT GPIOA
#define LED_PIN GPIO_PIN_9
@@ -48,20 +61,15 @@
#define US_ECHO_TIMER TIMER16
#define US_ECHO_CH TIMER_CH_0
#define SOUND_SPEED 340 // m/s
#define TIME_CORRECTION_US 250
void led_config(void);
void usart_config(void);
void ultrasonic_config(void);
void ultrasonic_transmit_config(void);
void ultrasonic_pwm_out_cycles(const uint8_t cycles);
void ultrasonic_transmit_delay(const uint16_t micro_second);
void recevice_exti_config(void);
void ultrasonic_echo_timer_config(void);
void ultrasonic_recevice_config(void);
uint16_t calculate_distance(uint32_t us_value);
#endif //ULTRASONIC_DRIVER_H

View File

@@ -123,12 +123,9 @@ void TIMER15_IRQHandler(void) {
if (timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP) == SET)
{
timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
exti_interrupt_enable(EXTI_0); // turn on hardware external input interrupt
timer_counter_value_config(TIMER16, 0);
timer_enable(TIMER16); // turn on timer to calculate the first ultrasonic echo time
timer_disable(TIMER15);
}
}
@@ -137,13 +134,8 @@ void EXTI0_1_IRQHandler(void) {
if (exti_interrupt_flag_get(EXTI_0) == SET)
{
exti_interrupt_flag_clear(EXTI_0);
capture_value = timer_channel_capture_value_register_read(TIMER16, TIMER_CH_0);
// printf("aaa\n");
timer_disable(TIMER16);
exti_interrupt_disable(EXTI_0);
}
}

View File

@@ -4,7 +4,7 @@
\version 2024-02-22, V2.1.0, firmware for GD32E23x
*/
#include "main.h"
// #include "main.h"
#include <stdio.h>
#include "gd32e23x.h"
#include "systick.h"
@@ -27,10 +27,7 @@ int main(void)
systick_config();
/* configure ultrasonic board hardware */
ultrasonic_transmit_config();
ultrasonic_transmit_delay(TIME_CORRECTION_US);
recevice_exti_config();
ultrasonic_echo_timer_config();
ultrasonic_recevice_config();
/* ---------- debug start ---------- */
@@ -40,7 +37,6 @@ int main(void)
/* ---------- debug end ---------- */
printf("\r\n");
printf("START!\r\n");
printf("XLSW-3DP-UltraSonic Analog 300K!\r\n");
printf("\r\n");
@@ -49,18 +45,20 @@ int main(void)
while(1)
{
delay_ms(ULTRASONIC_TRAN_US);
ultrasonic_pwm_out_cycles(ULTRASONIC_CYCLES);
// printf("Send ultra sonic driver signal!\r\n");
delay_ms(2);
printf("cap_val:%ld\n", capture_value);
delay_ms(2);
printf("cap_val:%ld\t", capture_value);
const char* result = (capture_value <= CAPTURE_VALUE_MAX) ? "Distance: %d\n" : "Over Range\n";
distance_uint16 = calculate_distance(capture_value);
printf("Distance:%d\n", distance_uint16);
printf(result, distance_uint16);
}
}
/* retarget the C library printf function to the USART */
int _write (int fd, char *pBuffer, int size)
{
int _write (int fd, char *pBuffer, int size) {
for (int i = 0; i < size; i++)
{
usart_data_transmit(USART0, (uint8_t)pBuffer[i]);

View File

@@ -6,8 +6,7 @@
#include "gd32e23x.h"
#include "systick.h"
void led_config(void)
{
void led_config(void) {
rcu_periph_clock_enable(LED_RCU);
gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN);
@@ -34,8 +33,7 @@ void led_config(void)
nvic_irq_enable(LED_IRQ, 0);
}
void usart_config(void)
{
void usart_config(void) {
rcu_periph_clock_enable(USART_GPIO_RCU);
rcu_periph_clock_enable(USART_RCU);
@@ -54,8 +52,7 @@ void usart_config(void)
usart_enable(USART0_PHY);
}
void ultrasonic_config(void)
{
void ultrasonic_config(void) {
rcu_periph_clock_enable(US_TRAN_GPIO_RCU);
gpio_mode_set(US_TRAN_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, US_TRAN_PIN);
@@ -135,8 +132,8 @@ void ultrasonic_transmit_delay(const uint16_t micro_second) {
timer_initpara.repetitioncounter =0;
timer_init(US_TRAN_DELAY_TIMER, &timer_initpara);
timer_auto_reload_shadow_enable(US_TRAN_DELAY_TIMER); //使能自动影子重载
timer_interrupt_enable(US_TRAN_DELAY_TIMER, TIMER_INT_UP); // 使能自动重载中断
timer_auto_reload_shadow_enable(US_TRAN_DELAY_TIMER);
timer_interrupt_enable(US_TRAN_DELAY_TIMER, TIMER_INT_UP);
nvic_irq_enable(TIMER15_IRQn, 1U);
}
@@ -154,8 +151,7 @@ void recevice_exti_config(void) {
// exti_interrupt_enable(EXTI_0);
}
void ultrasonic_echo_timer_config(void)
{
void ultrasonic_echo_timer_config(void) {
rcu_periph_clock_enable(US_ECHO_RCU);
timer_deinit(US_ECHO_TIMER);
@@ -174,10 +170,16 @@ void ultrasonic_echo_timer_config(void)
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_BOTH_EDGE;
timer_icinitpara.icselection = TIMER_IC_SELECTION_INDIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x03; // 设置滤波器
timer_icinitpara.icfilter = 0x03;
timer_input_capture_config(US_ECHO_TIMER, US_ECHO_CH, &timer_icinitpara);
}
void ultrasonic_recevice_config(void) {
ultrasonic_transmit_delay(TIME_CORRECTION_US);
recevice_exti_config();
ultrasonic_echo_timer_config();
}
uint16_t calculate_distance(uint32_t us_value) {
uint16_t distace = (TIME_CORRECTION_US + us_value) * 17;
/*