From aee082068e89851f341ed769085e4350579011ca Mon Sep 17 00:00:00 2001 From: yelvlab Date: Tue, 7 Jan 2025 15:42:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=A1=AC=E4=BB=B6IIC?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9B=9E=E5=8E=BB=E6=95=B0=E6=8D=AE=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E4=B8=94=EF=BC=8C=E9=97=B4=E9=9A=94=E7=BC=A9=E5=B0=8F?= =?UTF-8?q?=E5=88=B0300ms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 3 ++- inc/gd60914.h | 34 ++++++++++++++++++++++++++++++++++ inc/i2c.h | 2 +- inc/main.h | 1 + src/fwdgt.c | 2 +- src/gd60914.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 11 ++++++++--- src/soft_i2c.c | 2 +- 8 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 inc/gd60914.h create mode 100644 src/gd60914.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ee44a95..fcc2e3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ set(OPT1 "_[12V]") #set(OPT1 "_[SW_IIC]") # Options 2 -set(OPT2 "") +set(OPT2 "_[HW_IIC]") #set(OPT2 "_[NO_LED]") enable_language(C) @@ -40,6 +40,7 @@ set(TARGET_C_SRC ${CMAKE_SOURCE_DIR}/src/i2c.c ${CMAKE_SOURCE_DIR}/src/fwdgt.c ${CMAKE_SOURCE_DIR}/src/rs485_protocol.c + ${CMAKE_SOURCE_DIR}/src/gd60914.c ${CMAKE_SOURCE_DIR}/src/ultrasonic_analog.c ) diff --git a/inc/gd60914.h b/inc/gd60914.h new file mode 100644 index 0000000..16f0fcb --- /dev/null +++ b/inc/gd60914.h @@ -0,0 +1,34 @@ +// +// Created by dell on 25-1-7. +// + +#ifndef GD60914_H +#define GD60914_H + +#include "gd32e23x_it.h" +#include "gd32e23x.h" + +#include "board_config.h" + +#ifdef SOFTWARE_IIC +#include "soft_i2c.h" +#else +#include "i2c.h" +#endif + +/******************************************************************************/ + +#define GD60914_ADDR (0x18 << 1) + +/******************************************************************************/ + +#define GD60914_HUM_TEMP 0x1A +#define GD60914_OBJ_TEMP 0x1F +#define GD60914_AMB_TEMP 0x1E +#define GD60914_TEMP_REG 0x1C + +void gd60914_get_object_tempture(void); + +void gd60914_read_temp(void); + +#endif //GD60914_H diff --git a/inc/i2c.h b/inc/i2c.h index ea09aa7..6b379f4 100644 --- a/inc/i2c.h +++ b/inc/i2c.h @@ -19,7 +19,7 @@ /******************************************************************************/ -#define I2C_SPEED 20000 +#define I2C_SPEED 100000 #define I2C_TIME_OUT (uint16_t)(5000) #define I2C_OK 1 diff --git a/inc/main.h b/inc/main.h index ebfdc0e..5e00524 100644 --- a/inc/main.h +++ b/inc/main.h @@ -43,6 +43,7 @@ OF SUCH DAMAGE. #include "usart.h" #include "fwdgt.h" #include "board_config.h" +#include "gd60914.h" #include "ultrasonic_analog.h" #ifdef SOFTWARE_IIC diff --git a/src/fwdgt.c b/src/fwdgt.c index 4beebce..688572f 100644 --- a/src/fwdgt.c +++ b/src/fwdgt.c @@ -15,7 +15,7 @@ void watchdog_init(void) { rcu_osci_stab_wait(RCU_IRC40K); /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */ - fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz) + fwdgt_config(625, FWDGT_PSC_DIV256); // Set timeout to 1 seconds (625 / 0.625 KHz) /* Enable FWDGT */ fwdgt_enable(); diff --git a/src/gd60914.c b/src/gd60914.c new file mode 100644 index 0000000..ccdeb21 --- /dev/null +++ b/src/gd60914.c @@ -0,0 +1,43 @@ +// +// Created by dell on 25-1-7. +// + +#include "gd60914.h" + +void gd60914_get_object_tempture(void) { + #ifdef SOFTWARE_IIC + soft_i2c_config(); + #else + i2c_config(); + #endif + + uint8_t data[2] = {0}; + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, data); +#else + i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, data); +#endif + + delay_ms(300); + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, data); +#else + i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, data); +#endif + + printf("%d\r\n", data[1] << 8 | data[0]); +} + +void gd60914_read_temp(void) { + + uint8_t value[2] = {0}; + #ifdef SOFTWARE_IIC + soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, value); + #else + i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, value); + #endif + + printf("%x %x\r\n", value[1], value[0]); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 34b0207..eb6e21c 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,12 @@ int main(void) /* configure FWDGT */ watchdog_init(); +#ifdef SOFTWARE_IIC + soft_i2c_config(); +#else + i2c_config(); +#endif + printf("system start!\r\n"); ultrasonic_config(); @@ -34,14 +40,13 @@ int main(void) // gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, RESET); while(1){ // printf("hello world!\r\n"); + delay_ms(500); + gd60914_get_object_tempture(); delay_ms(50); ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES); // delay_ms(2); - - // g_distance_uint16 = ultrasonic_calc_distance(); - // printf("Distance: %d cm\r\n", g_distance_uint16); watchdog_reload(); } } diff --git a/src/soft_i2c.c b/src/soft_i2c.c index 843cfd0..79883ae 100644 --- a/src/soft_i2c.c +++ b/src/soft_i2c.c @@ -11,7 +11,7 @@ \retval none */ void soft_i2c_delay(void) { - delay_us(20); // Adjust delay as needed + delay_us(1); // Adjust delay as needed /* delay to freq * 15KHz: delay_us(20); * 65KHz: delay_us(1);