generated from hulk/gd32e23x_template
	在board_config.h文件中添加#define SOFTWARE_IIC选项用以选择使用软件IIC还是硬件IIC,如果注释掉该行则为硬件IIC方式。并完整实现软件IIC功能。
This commit is contained in:
		| @@ -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 | ||||||
|   | |||||||
| @@ -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*/ | ||||||
|   | |||||||
| @@ -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 | ||||||
|   | |||||||
| @@ -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 | ||||||
|  |  | ||||||
| /******************************************************************************/ | /******************************************************************************/ | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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 */ | ||||||
|   | |||||||
| @@ -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); | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										73
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										73
									
								
								src/main.c
									
									
									
									
									
								
							| @@ -12,12 +12,14 @@ | |||||||
| #include "rs485.h" | #include "rs485.h" | ||||||
| #include "led.h" | #include "led.h" | ||||||
| #include "i2c.h" | #include "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 */ | ||||||
| @@ -59,73 +61,16 @@ int main(void) { | |||||||
|  |  | ||||||
|     // 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"); |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     /* Initialize watchdog */ |     /* Initialize watchdog */ | ||||||
|     watchdog_init(); |     watchdog_init(); | ||||||
|  |  | ||||||
|     while (1) { |     while (1) { | ||||||
|         soft_i2c_config(); |  | ||||||
|         printf("111\n"); |  | ||||||
|  |  | ||||||
|         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"); |  | ||||||
|         delay_ms(99); |         delay_ms(99); | ||||||
|         fwdgt_counter_reload(); |         fwdgt_counter_reload(); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -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}; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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; | ||||||
| } | } | ||||||
| @@ -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); | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user