Add over-range judgment printing and compilation conditions under different voltages
This commit is contained in:
parent
9cdd0008a5
commit
2c96ee7848
@ -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,16 +61,11 @@
|
||||
#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);
|
||||
|
17
src/main.c
17
src/main.c
@ -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"
|
||||
@ -40,7 +40,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 +48,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]);
|
||||
|
@ -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);
|
||||
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user