From 1cf43e0371a25ef44a4cc34e4def62debd8d9a1f Mon Sep 17 00:00:00 2001 From: yelvlab Date: Fri, 27 Sep 2024 18:22:57 +0800 Subject: [PATCH] shit --- inc/mlx90614.h | 2 ++ inc/ultrasonic_driver.h | 9 -------- src/mlx90614.c | 47 ++++++++++++++++++----------------------- src/systick.c | 33 ++++++++++++++--------------- 4 files changed, 38 insertions(+), 53 deletions(-) diff --git a/inc/mlx90614.h b/inc/mlx90614.h index 313dc72..cd02d45 100644 --- a/inc/mlx90614.h +++ b/inc/mlx90614.h @@ -5,6 +5,8 @@ #ifndef MLX90614_H #define MLX90614_H +#define I2C_EVENT_MASTER_MODE_SELECT ((uint32_t)(I2C_FLAG_I2CBSY | I2C_FLAG_MASTER | I2C_FLAG_RBNE)) + void i2c_config(void); int read_ir_mlx90614(void); diff --git a/inc/ultrasonic_driver.h b/inc/ultrasonic_driver.h index d2f1e65..b7aaffb 100644 --- a/inc/ultrasonic_driver.h +++ b/inc/ultrasonic_driver.h @@ -62,23 +62,14 @@ #define US_ECHO_CH TIMER_CH_0 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 receive_exti_config(void); - void ultrasonic_echo_timer_config(void); - void ultrasonic_receive_config(void); - uint16_t calculate_distance(uint32_t us_value); #endif //ULTRASONIC_DRIVER_H diff --git a/src/mlx90614.c b/src/mlx90614.c index d542785..96ae00b 100644 --- a/src/mlx90614.c +++ b/src/mlx90614.c @@ -5,9 +5,9 @@ #include "mlx90614.h" #include "gd32e23x.h" #include "systick.h" +#include -void i2c_config(void) -{ +void i2c_config(void) { rcu_periph_clock_enable(RCU_I2C0); rcu_periph_clock_enable(GPIOF); @@ -23,35 +23,34 @@ void i2c_config(void) nvic_irq_enable(I2C0_ER_IRQn, 2); } -int read_ir_mlx90614(void) -{ +int read_ir_mlx90614(void) { uint8_t Data[5]; int inttemp_ir = 0; uint32_t TIMEOUT = 0; while (TIMEOUT < 10000 && i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR0\r\n"); return -410; } TIMEOUT = 0; // I2C_GenerateSTART(I2C0, ENABLE); - i2c_ack_config(I2C0, I2C_ACK_ENABLE); - while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT)) + i2c_start_on_bus(I2C0); + // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT)) + while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_EVENT_MASTER_MODE_SELECT)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR1\r\n"); return -410; } // I2C_AcknowledgeConfig(I2C0,DISABLE); TIMEOUT = 0; - I2C_Send7bitAddress(I2C0, 0XB4, I2C_Direction_Transmitter); - while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + // I2C_Send7bitAddress(I2C0, 0XB4, I2C_Direction_Transmitter); + i2c_master_addressing(I2C0, 0XB4, I2C_TRANSMITTER); + // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_FLAG_TR)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { printf("ERROR2\r\n"); return -410; } @@ -61,8 +60,7 @@ int read_ir_mlx90614(void) I2C_GenerateSTART(I2C0, ENABLE); while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR3\r\n"); return -410; } @@ -70,8 +68,7 @@ int read_ir_mlx90614(void) I2C_Send7bitAddress(I2C0, 0XB4, I2C_Direction_Receiver); while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR4\r\n"); return -410; } @@ -80,8 +77,7 @@ int read_ir_mlx90614(void) Data[0] = I2C_ReceiveData(I2C0); while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR5\r\n"); return -410; } @@ -90,8 +86,7 @@ int read_ir_mlx90614(void) Data[1] = I2C_ReceiveData(I2C0); while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR6\r\n"); return -410; } @@ -100,8 +95,7 @@ int read_ir_mlx90614(void) Data[2] = I2C_ReceiveData(I2C0); while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR7\r\n"); return -410; } @@ -111,8 +105,7 @@ int read_ir_mlx90614(void) Data[3] = I2C_ReceiveData(I2C0); while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) TIMEOUT++; - if (TIMEOUT >= 10000) - { + if (TIMEOUT >= 10000) { //printf("ERROR11\r\n"); return -410; } @@ -121,7 +114,7 @@ int read_ir_mlx90614(void) I2C_GenerateSTOP(I2C0, ENABLE); I2C_AcknowledgeConfig(I2C0, ENABLE); //printf("data:%x,%x,%x\r\n",Data[0],Data[1],Data[2]); - inttemp_ir = (int)((Data[0] + Data[1] * 255) * 0.2 - 2731.5); + inttemp_ir = (int) ((Data[0] + Data[1] * 255) * 0.2 - 2731.5); // printf("temp:%d\r\n",inttemp_ir); diff --git a/src/systick.c b/src/systick.c index 2f49754..a7dea8e 100644 --- a/src/systick.c +++ b/src/systick.c @@ -22,13 +22,14 @@ volatile static float count_1ms = 0; * * ************************************************************************ */ -void systick_config(void) { +void systick_config(void) +{ //设置了 SysTick 定时器的时钟源为 HCLK/8 systick_clksource_set(SYSTICK_CLKSOURCE_HCLK_DIV8); //计算了每微秒所需的 SysTick 计数值 - count_1us = (float) SystemCoreClock / 8000000; + count_1us = (float)SystemCoreClock/8000000; //计算了每毫秒所需的 SysTick 计数值 - count_1ms = (float) count_1us * 1000; + count_1ms = (float)count_1us * 1000; } /** @@ -43,15 +44,16 @@ void delay_us(uint32_t count) { uint32_t ctl; //设置 SysTick 计数器的装载值 - SysTick->LOAD = (uint32_t) (count * count_1us); + SysTick->LOAD = (uint32_t)(count * count_1us); //清零 SysTick 计数器,以确保计数器从零开始计数 SysTick->VAL = 0x0000U; //使能 SysTick 定时器,开始进行计数 SysTick->CTRL = SysTick_CTRL_ENABLE_Msk; //等待 SysTick 计数器的计数值达到装载值时退出 - do { - ctl = SysTick->CTRL; //读取 CTRL 寄存器的值 - } while ((ctl & SysTick_CTRL_ENABLE_Msk) && !(ctl & SysTick_CTRL_COUNTFLAG_Msk)); + do + { + ctl = SysTick->CTRL; //读取 CTRL 寄存器的值 + }while((ctl & SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk)); //循环退出,禁用 SysTick 定时器 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; //将 SysTick 计数器的当前值清零,以便下次使用 @@ -70,15 +72,16 @@ void delay_ms(uint32_t count) { uint32_t ctl; //设置 SysTick 计数器的装载值 - SysTick->LOAD = (uint32_t) (count * count_1ms); + SysTick->LOAD = (uint32_t)(count * count_1ms); //清零 SysTick 计数器,以确保计数器从零开始计数 SysTick->VAL = 0x0000U; //使能 SysTick 定时器,开始进行计数 SysTick->CTRL = SysTick_CTRL_ENABLE_Msk; //等待 SysTick 计数器的计数值达到装载值时退出 - do { - ctl = SysTick->CTRL; //读取 CTRL 寄存器的值 - } while ((ctl & SysTick_CTRL_ENABLE_Msk) && !(ctl & SysTick_CTRL_COUNTFLAG_Msk)); + do + { + ctl = SysTick->CTRL; //读取 CTRL 寄存器的值 + }while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk)); //循环退出,禁用 SysTick 定时器 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; //将 SysTick 计数器的当前值清零,以便下次使用 @@ -94,9 +97,5 @@ void delay_ms(uint32_t count) { * ************************************************************************ */ void delay_5_nop(void) { - __NOP(); - __NOP(); - __NOP(); - __NOP(); - __NOP(); -} + __NOP();__NOP();__NOP();__NOP();__NOP(); +} \ No newline at end of file