diff --git a/inc/ultrasonic_driver.h b/inc/ultrasonic_driver.h index 5f74bb5..b39d5be 100644 --- a/inc/ultrasonic_driver.h +++ b/inc/ultrasonic_driver.h @@ -10,8 +10,8 @@ #define LED_PORT GPIOA #define LED_PIN GPIO_PIN_9 #define LED_RCU RCU_GPIOA -#define LED_TIMER TIMER13 -#define LED_IRQ TIMER13_IRQn +#define LED_TIMER TIMER16 +#define LED_IRQ TIMER16_IRQn #define USART_RCU RCU_USART0 #define USART_GPIO_RCU RCU_GPIOA @@ -24,19 +24,21 @@ #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_TIMER2 -#define US_TRAN_TIMER TIMER2 -#define US_TRAN_CH TIMER_CH_3 +#define US_TRAN_RCU RCU_TIMER13 +#define US_TRAN_TIMER TIMER13 +#define US_TRAN_CH TIMER_CH_0 void led_config(void); void usart_config(void); void ultrasonic_config(void); -void ultrasonic_peripheral_config(void); +void ultrasonic_transmit_config(void); void ultrasonic_pwm_out_cycles(uint8_t cycles); -void timer15_config(void); +void ultrasonic_transmit_delay(void); void recevice_exti_config(void); +void ultrasonic_echo_timer_config(void); #endif //ULTRASONIC_DRIVER_H diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 9f23683..1925547 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -98,46 +98,67 @@ void SysTick_Handler(void) { } -void TIMER13_IRQHandler(void) { - if (timer_interrupt_flag_get(TIMER13, TIMER_INT_FLAG_UP) == SET) +void TIMER16_IRQHandler(void) { + if (timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_UP) == SET) { - timer_interrupt_flag_clear(TIMER13, TIMER_INT_FLAG_UP); + timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_UP); static uint8_t led_status = 0; if (led_status) { //! turn on led & reconfig timer13 period to 19000(1900ms) gpio_bit_write(GPIOA, GPIO_PIN_9, RESET); - timer_autoreload_value_config(TIMER13, 19200); + timer_autoreload_value_config(TIMER16, 19200); } else { //! turn off led & reconfig timer13 period to 1000(100ms) gpio_bit_write(GPIOA, GPIO_PIN_9, SET); - timer_autoreload_value_config(TIMER13, 800); + timer_autoreload_value_config(TIMER16, 800); } led_status = !led_status; } } +void TIMER14_IRQHandler(void) +{ + if (timer_flag_get(TIMER14, TIMER_FLAG_CH0)) + { + timer_flag_clear(TIMER14, TIMER_FLAG_CH0); + + uint32_t capture_value = timer_channel_capture_value_register_read(TIMER14, TIMER_CH_0); + + timer_disable(TIMER14); + } +} + 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); - // timer_enable(TIMER14); + timer_enable(TIMER14); + /* + * GPIO Toggle will be delete when the project is ready + */ gpio_bit_toggle(GPIOA, GPIO_PIN_7); + /*******************************************************/ timer_disable(TIMER15); } } void EXTI0_1_IRQHandler(void) { - if (exti_interrupt_flag_get(EXTI_0) == SET) { + if (exti_interrupt_flag_get(EXTI_0) == SET) + { exti_interrupt_flag_clear(EXTI_0); + /* + * GPIO Toggle will be delete when the project is ready + */ gpio_bit_toggle(GPIOA, GPIO_PIN_5); + /*******************************************************/ - timer_event_software_generate(TIMER14, TIMER_EVENT_SRC_CH0G); + // timer_event_software_generate(TIMER14, TIMER_EVENT_SRC_CH0G); exti_interrupt_disable(EXTI_0); } diff --git a/src/main.c b/src/main.c index 53ba5f7..77c8ca4 100644 --- a/src/main.c +++ b/src/main.c @@ -25,13 +25,14 @@ int main(void) /* configure systick */ systick_config(); /* configure ultrasonic board hardware */ - ultrasonic_peripheral_config(); - timer15_config(); + ultrasonic_transmit_config(); + /* ---------- debug start ---------- */ - + ultrasonic_transmit_delay(); recevice_exti_config(); + ultrasonic_echo_timer_config(); @@ -42,6 +43,8 @@ int main(void) printf("XLSW-3DP-UltraSonic Analog 300K!\r\n"); printf("\r\n"); + delay_ms(2000); + while(1) { delay_ms(50); diff --git a/src/ultrasonic_driver.c b/src/ultrasonic_driver.c index e9f5ed5..030f7e9 100644 --- a/src/ultrasonic_driver.c +++ b/src/ultrasonic_driver.c @@ -60,7 +60,7 @@ void ultrasonic_config(void) gpio_mode_set(US_TRAN_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, US_TRAN_PIN); gpio_output_options_set(US_TRAN_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, US_TRAN_PIN); - gpio_af_set(US_TRAN_GPIO_PORT, GPIO_AF_1, US_TRAN_PIN); + gpio_af_set(US_TRAN_GPIO_PORT, US_TRAN_AF, US_TRAN_PIN); timer_oc_parameter_struct timer_ocinitpara; timer_parameter_struct timer_initpara; @@ -90,11 +90,9 @@ void ultrasonic_config(void) timer_auto_reload_shadow_enable(US_TRAN_TIMER); timer_interrupt_enable(US_TRAN_TIMER, TIMER_INT_UP); - // nvic_irq_enable(TIMER2_IRQn, 1); - // timer_enable(TIMER2); } -void ultrasonic_peripheral_config(void) { +void ultrasonic_transmit_config(void) { led_config(); usart_config(); ultrasonic_config(); @@ -123,12 +121,16 @@ void ultrasonic_pwm_out_cycles(uint8_t cycles) { // } } -void timer15_config(void) { +void ultrasonic_transmit_delay(void) { rcu_periph_clock_enable(RCU_GPIOA); + /* + * GPIO Init will be delete when the project is ready + */ gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7); gpio_bit_write(GPIOA, GPIO_PIN_7, SET); + /******************************************/ rcu_periph_clock_enable(RCU_TIMER15); timer_deinit(TIMER15); @@ -167,3 +169,32 @@ void recevice_exti_config(void) { exti_interrupt_enable(EXTI_0); } + +void ultrasonic_echo_timer_config(void) +{ + rcu_periph_clock_enable(RCU_TIMER14); + timer_deinit(TIMER14); + + timer_parameter_struct timer_initpara; + timer_struct_para_init(&timer_initpara); + timer_initpara.prescaler =71; + timer_initpara.alignedmode =TIMER_COUNTER_EDGE; + timer_initpara.counterdirection =TIMER_COUNTER_UP; + timer_initpara.period =65535; + timer_initpara.clockdivision =TIMER_CKDIV_DIV1; + timer_initpara.repetitioncounter =0; + timer_init(TIMER14, &timer_initpara); + + timer_ic_parameter_struct timer_icinitpara; + timer_channel_input_struct_para_init(&timer_icinitpara); + timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING; + timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI; + timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1; + timer_icinitpara.icfilter = 0x03; // 设置滤波器 + timer_input_capture_config(TIMER14, TIMER_CH_0, &timer_icinitpara); + + timer_interrupt_enable(TIMER0, TIMER_INT_UP); + nvic_irq_enable(TIMER15_IRQn, 0U); + + // timer_enable(TIMER0); +}