constants replaced with macro definition
This commit is contained in:
parent
7cb354967c
commit
b6b6932c08
@ -7,6 +7,9 @@
|
||||
|
||||
#include "gd32e23x.h"
|
||||
|
||||
#define ULTRASONIC_CYCLES 0x05U
|
||||
#define ULTRASONIC_TRAN_US 100 // (ms)
|
||||
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_9
|
||||
#define LED_RCU RCU_GPIOA
|
||||
@ -14,22 +17,39 @@
|
||||
#define LED_TIMER TIMER5
|
||||
#define LED_IRQ TIMER5_IRQn
|
||||
|
||||
#define USART_RCU RCU_USART0
|
||||
#define USART_GPIO_RCU RCU_GPIOA
|
||||
#define USARET_GPIO_PORT GPIOA
|
||||
#define USART_TX_PIN GPIO_PIN_2
|
||||
#define USART_RX_PIN GPIO_PIN_3
|
||||
#define USART0_PHY USART0
|
||||
#define USART_BAUDRATE 115200U
|
||||
#define USART_RCU RCU_USART0
|
||||
#define USART_GPIO_RCU RCU_GPIOA
|
||||
#define USARET_GPIO_PORT GPIOA
|
||||
#define USART_TX_PIN GPIO_PIN_2
|
||||
#define USART_RX_PIN GPIO_PIN_3
|
||||
#define USART0_PHY USART0
|
||||
#define USART_BAUDRATE 115200U
|
||||
|
||||
#define US_TRAN_GPIO_RCU RCU_GPIOB
|
||||
#define US_TRAN_GPIO_PORT GPIOB
|
||||
#define US_TRAN_PIN GPIO_PIN_1
|
||||
#define US_TRAN_AF GPIO_AF_0
|
||||
#define US_TRAN_GPIO_RCU RCU_GPIOB
|
||||
#define US_TRAN_GPIO_PORT GPIOB
|
||||
#define US_TRAN_PIN GPIO_PIN_1
|
||||
#define US_TRAN_AF GPIO_AF_0
|
||||
|
||||
#define US_TRAN_RCU RCU_TIMER13
|
||||
#define US_TRAN_TIMER TIMER13
|
||||
#define US_TRAN_CH TIMER_CH_0
|
||||
#define US_TRAN_RCU RCU_TIMER13
|
||||
#define US_TRAN_TIMER TIMER13
|
||||
#define US_TRAN_CH TIMER_CH_0
|
||||
|
||||
#define US_TRAN_DELAY_RCU RCU_TIMER15
|
||||
#define US_TRAN_DELAY_TIMER TIMER15
|
||||
|
||||
#define US_FB_GPIO_RCU RCU_GPIOA
|
||||
#define US_FB_EXTI_RCU RCU_CFGCMP
|
||||
#define US_FB_GPIO_PORT GPIOA
|
||||
#define US_FB_GPIO_PIN GPIO_PIN_0
|
||||
#define US_FB_EXTI_IRQ EXTI0_1_IRQn
|
||||
#define US_FB_GPIO_EXTI EXTI_0
|
||||
|
||||
#define US_ECHO_RCU RCU_TIMER16
|
||||
#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);
|
||||
@ -42,4 +62,6 @@ void ultrasonic_transmit_delay(const uint16_t micro_second);
|
||||
void recevice_exti_config(void);
|
||||
void ultrasonic_echo_timer_config(void);
|
||||
|
||||
uint16_t calculate_distance(uint32_t us_value);
|
||||
|
||||
#endif //ULTRASONIC_DRIVER_H
|
||||
|
11
src/main.c
11
src/main.c
@ -12,9 +12,6 @@
|
||||
|
||||
#include "ultrasonic_driver.h"
|
||||
|
||||
#define ULTRASONIC_CYCLES 0x05U
|
||||
#define ULTRASONIC_TRAN_US 50 // (ms)
|
||||
|
||||
extern uint32_t capture_value;
|
||||
|
||||
/*!
|
||||
@ -30,13 +27,13 @@ int main(void)
|
||||
/* configure ultrasonic board hardware */
|
||||
ultrasonic_transmit_config();
|
||||
|
||||
|
||||
|
||||
/* ---------- debug start ---------- */
|
||||
ultrasonic_transmit_delay(250);
|
||||
ultrasonic_transmit_delay(TIME_CORRECTION_US);
|
||||
recevice_exti_config();
|
||||
ultrasonic_echo_timer_config();
|
||||
|
||||
/* ---------- debug start ---------- */
|
||||
|
||||
|
||||
|
||||
|
||||
/* ---------- debug end ---------- */
|
||||
|
@ -122,10 +122,8 @@ void ultrasonic_pwm_out_cycles(const uint8_t cycles) {
|
||||
}
|
||||
|
||||
void ultrasonic_transmit_delay(const uint16_t micro_second) {
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
|
||||
rcu_periph_clock_enable(RCU_TIMER15);
|
||||
timer_deinit(TIMER15);
|
||||
rcu_periph_clock_enable(US_TRAN_DELAY_RCU);
|
||||
timer_deinit(US_TRAN_DELAY_TIMER);
|
||||
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
@ -135,31 +133,31 @@ void ultrasonic_transmit_delay(const uint16_t micro_second) {
|
||||
timer_initpara.period =micro_second - 1;
|
||||
timer_initpara.clockdivision =TIMER_CKDIV_DIV1;
|
||||
timer_initpara.repetitioncounter =0;
|
||||
timer_init(TIMER15, &timer_initpara);
|
||||
timer_init(US_TRAN_DELAY_TIMER, &timer_initpara);
|
||||
|
||||
timer_auto_reload_shadow_enable(TIMER15); //使能自动影子重载
|
||||
timer_interrupt_enable(TIMER15, 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);
|
||||
}
|
||||
|
||||
void recevice_exti_config(void) {
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_CFGCMP);
|
||||
rcu_periph_clock_enable(US_FB_GPIO_RCU);
|
||||
rcu_periph_clock_enable(US_FB_EXTI_RCU);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);
|
||||
nvic_irq_enable(EXTI0_1_IRQn, 0U);
|
||||
gpio_mode_set(US_FB_GPIO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, US_FB_GPIO_PIN);
|
||||
nvic_irq_enable(US_FB_EXTI_IRQ, 0U);
|
||||
syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN0);
|
||||
|
||||
exti_init(EXTI_0, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
|
||||
exti_flag_clear(EXTI_0);
|
||||
exti_init(US_FB_GPIO_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
|
||||
exti_flag_clear(US_FB_GPIO_EXTI);
|
||||
|
||||
// exti_interrupt_enable(EXTI_0);
|
||||
}
|
||||
|
||||
void ultrasonic_echo_timer_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_TIMER16);
|
||||
timer_deinit(TIMER16);
|
||||
rcu_periph_clock_enable(US_ECHO_RCU);
|
||||
timer_deinit(US_ECHO_TIMER);
|
||||
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
@ -169,7 +167,7 @@ void ultrasonic_echo_timer_config(void)
|
||||
timer_initpara.period =59999;
|
||||
timer_initpara.clockdivision =TIMER_CKDIV_DIV1;
|
||||
timer_initpara.repetitioncounter =0;
|
||||
timer_init(TIMER16, &timer_initpara);
|
||||
timer_init(US_ECHO_TIMER, &timer_initpara);
|
||||
|
||||
timer_ic_parameter_struct timer_icinitpara;
|
||||
timer_channel_input_struct_para_init(&timer_icinitpara);
|
||||
@ -177,7 +175,5 @@ void ultrasonic_echo_timer_config(void)
|
||||
timer_icinitpara.icselection = TIMER_IC_SELECTION_INDIRECTTI;
|
||||
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
|
||||
timer_icinitpara.icfilter = 0x03; // 设置滤波器
|
||||
timer_input_capture_config(TIMER16, TIMER_CH_0, &timer_icinitpara);
|
||||
|
||||
// timer_enable(TIMER16);
|
||||
timer_input_capture_config(US_ECHO_TIMER, US_ECHO_CH, &timer_icinitpara);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user