first version OK!!!!!
This commit is contained in:
parent
392ccaba81
commit
6581d6ff88
@ -6,3 +6,11 @@
|
||||
- [x] PA2/PA3配置为USART0
|
||||
- [x] LED配置存活状态闪烁
|
||||
- [ ] 超声反射回波接受与精准计时
|
||||
|
||||
- 超声波反射回来后到sensor成功接收,GPIO上的反应主要分为两部分,
|
||||
1. 超声波在发送时产生的余震,24V下大概为230us,12V下大概为210us。(前面所说的时间均为比较保守的时间)
|
||||
2. 超声波会在接触到目标后反射回sensor上,并产生一个低电平,主要就是检测这部分。
|
||||
|
||||
- [x] 产生一个210-230us可调节的准确延时。(TIMER15配置为1us计一个数,设置重载为需要的时间,产生中断即可。)
|
||||
- [x] 在产生指定时间的中断服务函数中,开启EXTI0(PA0,sensor信号接收引脚),开启TIMER14(计时计数器,1us计一个数,计算接收到外部中断的时间)
|
||||
- [ ] 在外部中断服务函数中,产生一个事件或中断,进入到TIMER14的中断
|
||||
|
@ -33,6 +33,7 @@ OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include <stdio.h>
|
||||
#include "main.h"
|
||||
#include "systick.h"
|
||||
#include "ultrasonic_driver.h"
|
||||
@ -118,31 +119,15 @@ void TIMER5_IRQHandler(void) {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
exti_interrupt_enable(EXTI_0); // turn on hardware external input interrupt
|
||||
|
||||
/*
|
||||
* GPIO Toggle will be delete when the project is ready
|
||||
*/
|
||||
gpio_bit_toggle(GPIOA, GPIO_PIN_7);
|
||||
/*******************************************************/
|
||||
timer_counter_value_config(TIMER16, 0);
|
||||
timer_enable(TIMER16); // turn on timer to calculate the first ultrasonic echo time
|
||||
|
||||
timer_disable(TIMER15);
|
||||
}
|
||||
@ -153,13 +138,11 @@ void EXTI0_1_IRQHandler(void) {
|
||||
{
|
||||
exti_interrupt_flag_clear(EXTI_0);
|
||||
|
||||
/*
|
||||
* GPIO Toggle will be delete when the project is ready
|
||||
*/
|
||||
gpio_bit_toggle(GPIOA, GPIO_PIN_5);
|
||||
/*******************************************************/
|
||||
capture_value = timer_channel_capture_value_register_read(TIMER16, TIMER_CH_0);
|
||||
|
||||
// timer_event_software_generate(TIMER14, TIMER_EVENT_SRC_CH0G);
|
||||
// printf("aaa\n");
|
||||
|
||||
timer_disable(TIMER16);
|
||||
|
||||
exti_interrupt_disable(EXTI_0);
|
||||
}
|
||||
|
@ -13,7 +13,9 @@
|
||||
#include "ultrasonic_driver.h"
|
||||
|
||||
#define ULTRASONIC_CYCLES 0x05U
|
||||
#define ULTRASONIC_TRAN_US 50 // (ms)
|
||||
#define ULTRASONIC_TRAN_US 1000 // (ms)
|
||||
|
||||
extern uint32_t capture_value;
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
@ -31,7 +33,7 @@ int main(void)
|
||||
|
||||
|
||||
/* ---------- debug start ---------- */
|
||||
ultrasonic_transmit_delay();
|
||||
ultrasonic_transmit_delay(240);
|
||||
recevice_exti_config();
|
||||
ultrasonic_echo_timer_config();
|
||||
|
||||
@ -51,6 +53,8 @@ int main(void)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -124,14 +124,6 @@ void ultrasonic_pwm_out_cycles(const uint8_t cycles) {
|
||||
void ultrasonic_transmit_delay(const uint16_t micro_second) {
|
||||
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);
|
||||
|
||||
@ -147,58 +139,45 @@ void ultrasonic_transmit_delay(const uint16_t micro_second) {
|
||||
|
||||
timer_auto_reload_shadow_enable(TIMER15); //使能自动影子重载
|
||||
timer_interrupt_enable(TIMER15, TIMER_INT_UP); // 使能自动重载中断
|
||||
nvic_irq_enable(TIMER15_IRQn, 2U);
|
||||
nvic_irq_enable(TIMER15_IRQn, 1U);
|
||||
}
|
||||
|
||||
void recevice_exti_config(void) {
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_CFGCMP);
|
||||
|
||||
/*
|
||||
* GPIO Init will be delete when the project is ready
|
||||
*/
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_5);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
|
||||
gpio_bit_write(GPIOA, GPIO_PIN_5, SET);
|
||||
/******************************************/
|
||||
|
||||
// exti_deinit();
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);
|
||||
nvic_irq_enable(EXTI0_1_IRQn, 1U);
|
||||
nvic_irq_enable(EXTI0_1_IRQn, 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_interrupt_enable(EXTI_0);
|
||||
// exti_interrupt_enable(EXTI_0);
|
||||
}
|
||||
|
||||
void ultrasonic_echo_timer_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_TIMER14);
|
||||
timer_deinit(TIMER14);
|
||||
rcu_periph_clock_enable(RCU_TIMER16);
|
||||
timer_deinit(TIMER16);
|
||||
|
||||
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.period =59999;
|
||||
timer_initpara.clockdivision =TIMER_CKDIV_DIV1;
|
||||
timer_initpara.repetitioncounter =0;
|
||||
timer_init(TIMER14, &timer_initpara);
|
||||
timer_init(TIMER16, &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.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_input_capture_config(TIMER14, TIMER_CH_0, &timer_icinitpara);
|
||||
timer_icinitpara.icfilter = 0x0A; // 设置滤波器
|
||||
timer_input_capture_config(TIMER16, TIMER_CH_0, &timer_icinitpara);
|
||||
|
||||
timer_interrupt_enable(TIMER0, TIMER_INT_UP);
|
||||
nvic_irq_enable(TIMER15_IRQn, 0U);
|
||||
|
||||
// timer_enable(TIMER0);
|
||||
// timer_enable(TIMER16);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user