使用硬件IIC实现回去数据,并且,间隔缩小到300ms

This commit is contained in:
yelvlab 2025-01-07 15:42:56 +08:00
parent 93b24cc454
commit e86f8b0dd3
8 changed files with 91 additions and 6 deletions

View File

@ -12,7 +12,7 @@ set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d") string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
# Options 1 # Options 1
set(OPT1 "") set(OPT1 "_[HW_IIC]")
#set(OPT1 "_[SW_IIC]") #set(OPT1 "_[SW_IIC]")
# Options 2 # Options 2
@ -40,6 +40,7 @@ set(TARGET_C_SRC
${CMAKE_SOURCE_DIR}/src/i2c.c ${CMAKE_SOURCE_DIR}/src/i2c.c
${CMAKE_SOURCE_DIR}/src/fwdgt.c ${CMAKE_SOURCE_DIR}/src/fwdgt.c
${CMAKE_SOURCE_DIR}/src/rs485_protocol.c ${CMAKE_SOURCE_DIR}/src/rs485_protocol.c
${CMAKE_SOURCE_DIR}/src/gd60914.c
) )
add_executable(${PROJECT_NAME} ${TARGET_C_SRC}) add_executable(${PROJECT_NAME} ${TARGET_C_SRC})

34
inc/gd60914.h Normal file
View File

@ -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

View File

@ -19,7 +19,7 @@
/******************************************************************************/ /******************************************************************************/
#define I2C_SPEED 20000 #define I2C_SPEED 100000
#define I2C_TIME_OUT (uint16_t)(5000) #define I2C_TIME_OUT (uint16_t)(5000)
#define I2C_OK 1 #define I2C_OK 1

View File

@ -43,6 +43,7 @@ OF SUCH DAMAGE.
#include "usart.h" #include "usart.h"
#include "fwdgt.h" #include "fwdgt.h"
#include "board_config.h" #include "board_config.h"
#include "gd60914.h"
#ifdef SOFTWARE_IIC #ifdef SOFTWARE_IIC
#include "soft_i2c.h" #include "soft_i2c.h"

View File

@ -15,7 +15,7 @@ void watchdog_init(void) {
rcu_osci_stab_wait(RCU_IRC40K); rcu_osci_stab_wait(RCU_IRC40K);
/* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */ /* 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 */ /* Enable FWDGT */
fwdgt_enable(); fwdgt_enable();

43
src/gd60914.c Normal file
View File

@ -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]);
}

View File

@ -24,12 +24,18 @@ int main(void)
/* configure FWDGT */ /* configure FWDGT */
watchdog_init(); watchdog_init();
#ifdef SOFTWARE_IIC
soft_i2c_config();
#else
i2c_config();
#endif
printf("system start!\r\n"); printf("system start!\r\n");
// gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, RESET);
while(1){ while(1){
// printf("hello world!\r\n"); // printf("hello world!\r\n");
// delay_ms(500); delay_ms(500);
gd60914_get_object_tempture();
watchdog_reload(); watchdog_reload();
} }

View File

@ -11,7 +11,7 @@
\retval none \retval none
*/ */
void soft_i2c_delay(void) { void soft_i2c_delay(void) {
delay_us(20); // Adjust delay as needed delay_us(1); // Adjust delay as needed
/* delay to freq /* delay to freq
* 15KHz: delay_us(20); * 15KHz: delay_us(20);
* 65KHz: delay_us(1); * 65KHz: delay_us(1);