在board_config.h文件中添加#define SOFTWARE_IIC选项用以选择使用软件IIC还是硬件IIC,如果注释掉该行则为硬件IIC方式。并完整实现软件IIC功能。

This commit is contained in:
yelvlab 2024-12-29 22:07:49 +08:00
parent 1a6219f2b5
commit d01f7c52e6
13 changed files with 212 additions and 122 deletions

View File

@ -5,6 +5,10 @@
#ifndef BOARD_CONFIG_H #ifndef BOARD_CONFIG_H
#define BOARD_CONFIG_H #define BOARD_CONFIG_H
#define SOFTWARE_IIC
// #define DEBUG_VERBOES
/******************************************************************************/ /******************************************************************************/
#define I2C_SPEED 20000 #define I2C_SPEED 20000
@ -18,4 +22,15 @@
/******************************************************************************/ /******************************************************************************/
#define RS485_RCU RCU_USART0
#define RS485_GPIO_RCU RCU_GPIOA
#define RS485_GPIO_PORT GPIOA
#define RS485_TX_PIN GPIO_PIN_2
#define RS485_RX_PIN GPIO_PIN_3
#define RS485_PHY USART0
#define RS485_BAUDRATE 115200U
#define RS485_EN_PIN GPIO_PIN_1
/******************************************************************************/
#endif //BOARD_CONFIG_H #endif //BOARD_CONFIG_H

View File

@ -19,14 +19,6 @@
/******************************************************************************/ /******************************************************************************/
#define I2C_SCL_HIGH() gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN)
#define I2C_SCL_LOW() gpio_bit_reset(I2C_SCL_PORT, I2C_SCL_PIN)
#define I2C_SDA_HIGH() gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN)
#define I2C_SDA_LOW() gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN)
#define I2C_SDA_READ() gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN)
/******************************************************************************/
#define I2C_TIME_OUT (uint16_t)(10000) #define I2C_TIME_OUT (uint16_t)(10000)
#define I2C_OK 1 #define I2C_OK 1
#define I2C_FAIL 0 #define I2C_FAIL 0

View File

@ -13,8 +13,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include "board_config.h"
#include "soft_i2c.h"
#include "i2c.h" #include "i2c.h"
/***************************************************************************/
#define LDC1612_ADDR (0x2B << 1) #define LDC1612_ADDR (0x2B << 1)
/*Register Rddr*/ /*Register Rddr*/

View File

@ -35,6 +35,4 @@ OF SUCH DAMAGE.
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
// #define DEBUG_VERBOES
#endif /* MAIN_H */ #endif /* MAIN_H */

View File

@ -13,17 +13,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include "ldc1612.h"
/******************************************************************************/ #include "tmp112.h"
#include "board_config.h"
#define RS485_RCU RCU_USART0
#define RS485_GPIO_RCU RCU_GPIOA
#define RS485_GPIO_PORT GPIOA
#define RS485_TX_PIN GPIO_PIN_2
#define RS485_RX_PIN GPIO_PIN_3
#define RS485_PHY USART0
#define RS485_BAUDRATE 115200U
#define RS485_EN_PIN GPIO_PIN_1
/******************************************************************************/ /******************************************************************************/

View File

@ -22,6 +22,12 @@
/******************************************************************************/ /******************************************************************************/
#define SOFT_I2C_OK 1
#define SOFT_I2C_FAIL 0
#define SOFT_I2C_END 1
/******************************************************************************/
void soft_i2c_delay(void); void soft_i2c_delay(void);
void soft_i2c_config(void); void soft_i2c_config(void);
@ -40,4 +46,8 @@ void soft_i2c_send_byte(uint8_t data);
uint8_t soft_i2c_receive_byte(uint8_t ack); uint8_t soft_i2c_receive_byte(uint8_t ack);
uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]);
uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
#endif //SOFT_I2C_H #endif //SOFT_I2C_H

View File

@ -13,7 +13,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include "board_config.h"
#ifdef SOFTWARE_IIC
#include "soft_i2c.h"
#else
#include "i2c.h" #include "i2c.h"
#endif
/******************************************************************************/ /******************************************************************************/

View File

@ -32,6 +32,8 @@ void i2c_gpio_config(void) {
\retval none \retval none
*/ */
void i2c_config(void) { void i2c_config(void) {
/* configure I2C GPIO */
i2c_gpio_config();
/* enable I2C clock */ /* enable I2C clock */
rcu_periph_clock_enable(RCU_I2C); rcu_periph_clock_enable(RCU_I2C);
/* configure I2C clock */ /* configure I2C clock */

View File

@ -12,7 +12,12 @@ void ldc1612_set_conversion_time(uint8_t channel, uint16_t result) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
data[0] = (result >> 8) & 0xFF; data[0] = (result >> 8) & 0xFF;
data[1] = result & 0xFF; data[1] = result & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data); i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
#endif
} }
/** @brief set conversion offset. /** @brief set conversion offset.
@ -23,7 +28,12 @@ void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
data[0] = (result >> 8) & 0xFF; data[0] = (result >> 8) & 0xFF;
data[1] = result & 0xFF; data[1] = result & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data); i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
#endif
} }
/** @brief Before conversion,wait LC sensor stabilize for a short time. /** @brief Before conversion,wait LC sensor stabilize for a short time.
@ -34,7 +44,11 @@ void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
data[0] = (result >> 8) & 0xFF; data[0] = (result >> 8) & 0xFF;
data[1] = result & 0xFF; data[1] = result & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data); i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
#endif
} }
/** @brief set input frequency divide and fref divide. /** @brief set input frequency divide and fref divide.
@ -65,8 +79,11 @@ void ldc1612_set_freq_divide(uint8_t channel) {
data[0] = (value >> 8) & 0xFF; data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF; data[1] = value & 0xFF;
// printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div); // printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div);
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data); i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
#endif
} }
/** @brief Error output config. /** @brief Error output config.
@ -77,7 +94,11 @@ void ldc1612_set_error_config(uint16_t value) {
data[0] = (value >> 8) & 0xFF; data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF; data[1] = value & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
#else
i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data); i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
#endif
} }
/** @brief mux config. /** @brief mux config.
@ -88,7 +109,11 @@ void ldc1612_set_mux_config(uint16_t value) {
data[0] = (value >> 8) & 0xFF; data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF; data[1] = value & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
#else
i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data); i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
#endif
} }
/** @brief reset sensor. /** @brief reset sensor.
@ -98,7 +123,12 @@ void ldc1612_reset_sensor(void) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
data[0] = 0x80; data[0] = 0x80;
data[1] = 0x00; data[1] = 0x00;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
#else
i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data); i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
#endif
} }
/** @brief set drive current of sensor. /** @brief set drive current of sensor.
@ -109,7 +139,11 @@ void ldc1612_set_drive_current(uint8_t channel, uint16_t value) {
data[0] = (value >> 8) & 0xFF; data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF; data[1] = value & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data); i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
#endif
} }
/** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable .. /** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable ..
@ -120,7 +154,11 @@ void ldc1612_set_sensor_config(uint16_t value) {
data[0] = (value >> 8) & 0xFF; data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF; data[1] = value & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
#else
i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data); i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
#endif
} }
void ldc1612_single_ch0_config(void) { void ldc1612_single_ch0_config(void) {
@ -141,23 +179,41 @@ void ldc1612_single_ch0_config(void) {
void ldc1612_iic_get_sensor_infomation(void) { void ldc1612_iic_get_sensor_infomation(void) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
// ldc1612_iic_read_16bits(READ_MANUFACTURER_ID, data);
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data); i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#endif
printf("\tManufacturer: 0x%x", (data[0] << 8) | data[1]); printf("\tManufacturer: 0x%x", (data[0] << 8) | data[1]);
// ldc1612_iic_read_16bits(READ_DEVICE_ID, data);
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data); i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#endif
printf("\tDevice: 0x%x", (data[0] << 8) | data[1]); printf("\tDevice: 0x%x", (data[0] << 8) | data[1]);
} }
uint16_t ldc1612_get_manufacturer_id(void) { uint16_t ldc1612_get_manufacturer_id(void) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data); i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#endif
return (data[0] << 8) | data[1]; return (data[0] << 8) | data[1];
} }
uint16_t ldc1612_get_deveice_id(void) { uint16_t ldc1612_get_deveice_id(void) {
uint8_t data[2] = {0}; uint8_t data[2] = {0};
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data); i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#endif
return (data[0] << 8) | data[1]; return (data[0] << 8) | data[1];
} }
@ -169,11 +225,18 @@ uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
uint32_t raw_value = 0; uint32_t raw_value = 0;
uint8_t value[2] = {0}; uint8_t value[2] = {0};
// ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value); #ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value);
#else
i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value); i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value);
#endif
raw_value |= (uint32_t) ((value[0] << 8) | value[1]) << 16; raw_value |= (uint32_t) ((value[0] << 8) | value[1]) << 16;
// ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel + 1, value);
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value);
#else
i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value); i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value);
#endif
raw_value |= (uint32_t) ((value[0] << 8) | value[1]); raw_value |= (uint32_t) ((value[0] << 8) | value[1]);
return ldc1612_parse_raw_result(raw_value); return ldc1612_parse_raw_result(raw_value);
} }

View File

@ -11,13 +11,15 @@
#include "gd32e23x_libopt.h" #include "gd32e23x_libopt.h"
#include "rs485.h" #include "rs485.h"
#include "led.h" #include "led.h"
#include "i2c.h"
#include "soft_i2c.h" #include "soft_i2c.h"
#include "ldc1612.h" #include "ldc1612.h"
#include "tmp112.h" #include "board_config.h"
// uint32_t g_temperature_uint32; #ifdef SOFTWARE_IIC
// uint32_t g_eddy_current_value_uint32; #include "soft_i2c.h"
#else
#include "i2c.h"
#endif
void watchdog_init(void) { void watchdog_init(void) {
/* Enable the LSI clock */ /* Enable the LSI clock */
@ -25,7 +27,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_DIV256); // Set timeout to 1 seconds (625 / 0.625 KHz) fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
/* Enable FWDGT */ /* Enable FWDGT */
fwdgt_enable(); fwdgt_enable();
@ -46,72 +48,17 @@ int main(void) {
/* configure LED */ /* configure LED */
led_config(); led_config();
// watchdog_init(); watchdog_init();
/* configure I2C */ /* configure I2C */
// i2c_gpio_config(); soft_i2c_config();
// i2c_config();
// ldc1612_iic_get_sensor_infomation(); ldc1612_iic_get_sensor_infomation();
/* configure LDC1612 */ /* configure LDC1612 */
// ldc1612_single_ch0_config(); ldc1612_single_ch0_config();
uint8_t data[2] = {0};
printf("Start\n");
while (1) { while (1) {
soft_i2c_config(); delay_ms(99);
printf("111\n"); fwdgt_counter_reload();
soft_i2c_start();
soft_i2c_send_byte((0x2B << 1));
if (!soft_i2c_wait_ack())
{
printf("NACK\n");
}
soft_i2c_send_byte(0x7E);
if (!soft_i2c_wait_ack())
{
printf("NACK\n");
}
soft_i2c_delay();
soft_i2c_start();
soft_i2c_send_byte((0x2B << 1) + 1);
if (!soft_i2c_wait_ack())
{
printf("NACK\n");
}
soft_i2c_delay();
data[0] = soft_i2c_receive_byte(1);
data[1] = soft_i2c_receive_byte(0);
delay_us(5);
soft_i2c_stop();
printf("0x%x 0x%x\n", data[0], data[1]);
// i2c_start();
// delay_ms(1);
// i2c_stop();
// delay_ms(1);
delay_ms(1000);
// g_eddy_current_value_uint32 = 0;
// g_temperature_uint32 = 0;
// g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);
// g_temperature_uint32 = tmp112a_get_raw_channel_result();
printf("1\n");
} }
} }

View File

@ -4,9 +4,6 @@
#include "rs485.h" #include "rs485.h"
extern uint32_t g_temperature_uint32;
extern uint32_t g_eddy_current_value_uint32;
uint8_t package_header[3] = {0xB5, 0xF0, 0x04}; uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
uint8_t package_data[4] = {0}; uint8_t package_data[4] = {0};
@ -52,9 +49,9 @@ void process_command(uint8_t *cmd, size_t length) {
// printf("%d", length); // printf("%d", length);
sprintf(combined_str, "%c%c", cmd[3], cmd[4]); sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
if (strcmp(combined_str, "M1") == 0) { if (strcmp(combined_str, "M1") == 0) {
// eddy_current_value_report(); eddy_current_value_report();
} else if (strcmp(combined_str, "M2") == 0) { } else if (strcmp(combined_str, "M2") == 0) {
// tempture_value_report(); tempture_value_report();
} else { } else {
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C); printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
return; return;
@ -120,10 +117,14 @@ validation_result_t validate_data_length(uint8_t *data) {
} }
void eddy_current_value_report(void) { void eddy_current_value_report(void) {
package_data[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF; static uint32_t eddy_current_value_uint32 = 0;
package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF;
package_data[2] = (g_eddy_current_value_uint32 >> 8) & 0xFF; eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);
package_data[3] = g_eddy_current_value_uint32 & 0xFF;
package_data[0] = (eddy_current_value_uint32 >> 24) & 0xFF;
package_data[1] = (eddy_current_value_uint32 >> 16) & 0xFF;
package_data[2] = (eddy_current_value_uint32 >> 8) & 0xFF;
package_data[3] = eddy_current_value_uint32 & 0xFF;
uint8_t combined_data[7]; uint8_t combined_data[7];
memcpy(combined_data, package_header, 3); memcpy(combined_data, package_header, 3);
@ -135,10 +136,14 @@ void eddy_current_value_report(void) {
} }
void tempture_value_report(void) { void tempture_value_report(void) {
package_data[0] = (g_temperature_uint32 >> 24) & 0xFF; static uint32_t temperature_uint32 = 0;
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;
package_data[2] = (g_temperature_uint32 >> 8) & 0xFF; temperature_uint32 = tmp112a_get_raw_channel_result();
package_data[3] = g_temperature_uint32 & 0xFF;
package_data[0] = (temperature_uint32 >> 24) & 0xFF;
package_data[1] = (temperature_uint32 >> 16) & 0xFF;
package_data[2] = (temperature_uint32 >> 8) & 0xFF;
package_data[3] = temperature_uint32 & 0xFF;
uint8_t combined_data[7]; uint8_t combined_data[7];
memcpy(combined_data, package_header, 3); memcpy(combined_data, package_header, 3);

View File

@ -47,11 +47,11 @@ void soft_i2c_start(void) {
// sda_out(); // sda_out();
I2C_SDA_HIGH(); I2C_SDA_HIGH();
I2C_SCL_HIGH(); I2C_SCL_HIGH();
i2c_delay(); soft_i2c_delay();
I2C_SDA_LOW(); I2C_SDA_LOW();
i2c_delay(); soft_i2c_delay();
I2C_SCL_LOW(); I2C_SCL_LOW();
// i2c_delay(); // soft_i2c_delay();
// 从全高开始SCL为高期间SDA下降沿表示start信号再拉低SCL // 从全高开始SCL为高期间SDA下降沿表示start信号再拉低SCL
} }
@ -65,9 +65,9 @@ void soft_i2c_stop(void) {
// sda_out(); // sda_out();
I2C_SCL_LOW(); I2C_SCL_LOW();
I2C_SDA_LOW(); I2C_SDA_LOW();
i2c_delay(); soft_i2c_delay();
I2C_SCL_HIGH(); I2C_SCL_HIGH();
i2c_delay(); soft_i2c_delay();
I2C_SDA_HIGH(); I2C_SDA_HIGH();
// 从全低开始SCL为高期间SDA上升沿表示stop // 从全低开始SCL为高期间SDA上升沿表示stop
} }
@ -81,11 +81,11 @@ void soft_i2c_stop(void) {
void soft_i2c_send_ack(void) { void soft_i2c_send_ack(void) {
// sda_out(); // sda_out();
I2C_SDA_LOW(); I2C_SDA_LOW();
i2c_delay(); soft_i2c_delay();
I2C_SCL_HIGH(); I2C_SCL_HIGH();
i2c_delay(); soft_i2c_delay();
I2C_SCL_LOW(); I2C_SCL_LOW();
i2c_delay(); soft_i2c_delay();
I2C_SDA_HIGH(); I2C_SDA_HIGH();
// SCL产生一个正常的时钟周期其间SDA始终为低电平表示ACK // SCL产生一个正常的时钟周期其间SDA始终为低电平表示ACK
} }
@ -99,11 +99,11 @@ void soft_i2c_send_ack(void) {
void soft_i2c_send_nack(void) { void soft_i2c_send_nack(void) {
// sda_out(); // sda_out();
I2C_SDA_HIGH(); I2C_SDA_HIGH();
i2c_delay(); soft_i2c_delay();
I2C_SCL_HIGH(); I2C_SCL_HIGH();
i2c_delay(); soft_i2c_delay();
I2C_SCL_LOW(); I2C_SCL_LOW();
i2c_delay(); soft_i2c_delay();
I2C_SDA_HIGH(); I2C_SDA_HIGH();
// SCL产生一个正常的时钟周期其间SDA始终为高电平表示NACK // SCL产生一个正常的时钟周期其间SDA始终为高电平表示NACK
} }
@ -117,9 +117,9 @@ void soft_i2c_send_nack(void) {
uint8_t soft_i2c_wait_ack(void) { uint8_t soft_i2c_wait_ack(void) {
// sda_in(); // sda_in();
I2C_SDA_HIGH(); I2C_SDA_HIGH();
i2c_delay(); soft_i2c_delay();
I2C_SCL_HIGH(); I2C_SCL_HIGH();
i2c_delay(); soft_i2c_delay();
uint8_t ack = !I2C_SDA_READ(); uint8_t ack = !I2C_SDA_READ();
//ACK信号是第九个时钟期间SDA被从机在SCL高期间拉低并保持低电平。此处判断SDA是否被拉低被拉低则返回0取反为1表示收到ACK //ACK信号是第九个时钟期间SDA被从机在SCL高期间拉低并保持低电平。此处判断SDA是否被拉低被拉低则返回0取反为1表示收到ACK
I2C_SCL_LOW(); I2C_SCL_LOW();
@ -141,9 +141,9 @@ void soft_i2c_send_byte(uint8_t byte) {
I2C_SDA_LOW(); //SCL低电平期间SDA低电平表示0 I2C_SDA_LOW(); //SCL低电平期间SDA低电平表示0
} }
byte <<= 1; //左移一位,把原本第二位的数据移到第一位,再判断高低电平 byte <<= 1; //左移一位,把原本第二位的数据移到第一位,再判断高低电平
i2c_delay(); soft_i2c_delay();
I2C_SCL_HIGH(); //SCL拉高电平SDA电平状态保持不变 I2C_SCL_HIGH(); //SCL拉高电平SDA电平状态保持不变
i2c_delay(); soft_i2c_delay();
I2C_SCL_LOW(); //SCL拉低电平 I2C_SCL_LOW(); //SCL拉低电平
delay_us(5); delay_us(5);
} }
@ -163,12 +163,12 @@ uint8_t soft_i2c_receive_byte(uint8_t ack) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
byte <<= 1; byte <<= 1;
I2C_SCL_HIGH(); I2C_SCL_HIGH();
i2c_delay(); soft_i2c_delay();
if (I2C_SDA_READ()) { if (I2C_SDA_READ()) {
byte |= 0x01; byte |= 0x01;
} }
I2C_SCL_LOW(); I2C_SCL_LOW();
i2c_delay(); soft_i2c_delay();
} }
if (ack) { if (ack) {
soft_i2c_send_ack(); soft_i2c_send_ack();
@ -176,4 +176,53 @@ uint8_t soft_i2c_receive_byte(uint8_t ack) {
soft_i2c_send_nack(); soft_i2c_send_nack();
} }
return byte; return byte;
}
uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
soft_i2c_start();
soft_i2c_send_byte(slave_addr);
if (!soft_i2c_wait_ack()) {
soft_i2c_stop();
return SOFT_I2C_FAIL;
}
soft_i2c_send_byte(reg_addr);
if (!soft_i2c_wait_ack()) {
soft_i2c_stop();
return SOFT_I2C_FAIL;
}
soft_i2c_send_byte(data[0]);
if (!soft_i2c_wait_ack()) {
soft_i2c_stop();
return SOFT_I2C_FAIL;
}
soft_i2c_send_byte(data[1]);
if (soft_i2c_wait_ack()){}
soft_i2c_stop();
return SOFT_I2C_OK;
}
uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data)
{
soft_i2c_start();
soft_i2c_send_byte(slave_addr);
if (!soft_i2c_wait_ack()) {
soft_i2c_stop();
return SOFT_I2C_FAIL;
}
soft_i2c_send_byte(reg_addr);
if (!soft_i2c_wait_ack()) {
soft_i2c_stop();
return SOFT_I2C_FAIL;
}
soft_i2c_start();
soft_i2c_send_byte(slave_addr | 0x01);
if (!soft_i2c_wait_ack()) {
soft_i2c_stop();
return SOFT_I2C_FAIL;
}
soft_i2c_delay();
data[0] = soft_i2c_receive_byte(1);
data[1] = soft_i2c_receive_byte(0);
soft_i2c_stop();
return SOFT_I2C_OK;
} }

View File

@ -9,7 +9,13 @@ uint32_t tmp112a_get_raw_channel_result(void) {
uint8_t value[2] = {0}; uint8_t value[2] = {0};
// ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value); // ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value);
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(TMP112A_ADDR, 0x00, value);
#else
i2c_read_16bits(TMP112A_ADDR, 0x00, value); i2c_read_16bits(TMP112A_ADDR, 0x00, value);
#endif
raw_value = ((uint16_t) (value[0] << 4) | (value[1]>>4)); raw_value = ((uint16_t) (value[0] << 4) | (value[1]>>4));
return (raw_value * 625); return (raw_value * 625);
} }