测试软件IIC

This commit is contained in:
yelvlab 2024-12-28 14:20:30 +08:00
parent 8c1cf00d2f
commit d97d017f42
6 changed files with 106 additions and 18 deletions

View File

@ -69,7 +69,9 @@ void i2c_send_nack(void);
uint8_t i2c_wait_ack(void);
void i2c_send_byte(uint8_t byte);
uint8_t i2c_receive_byte(uint8_t ack);
void i2c_config(void);

View File

@ -45,9 +45,9 @@
/* select a system clock by uncommenting the following line */
//#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
#define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M)
// #define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M)
// #define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
// #define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000)
#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000)
#define RCU_MODIFY(__delay) do{ \
volatile uint32_t i; \

View File

@ -26,7 +26,7 @@ void i2c_gpio_config(void) {
}
void i2c_delay(void) {
delay_us(5); // Adjust delay as needed
delay_us(40); // Adjust delay as needed
}
/*!
@ -48,27 +48,41 @@ void si2c_config(void) {
I2C_SDA_HIGH();
}
// void sda_out(void) {
// gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, I2C_SDA_PIN);
// gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
// }
//
// void sda_in(void) {
// gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, I2C_SDA_PIN);
// }
void i2c_start(void) {
// sda_out();
I2C_SDA_HIGH();
I2C_SCL_HIGH();
i2c_delay();
I2C_SDA_LOW();
i2c_delay();
I2C_SCL_LOW();
i2c_delay();
i2c_delay();
// 从全高开始SCL为高期间SDA下降沿表示start信号再拉低SCL
}
void i2c_stop(void) {
// sda_out();
I2C_SCL_LOW();
I2C_SDA_LOW();
i2c_delay();
I2C_SCL_HIGH();
i2c_delay();
I2C_SDA_HIGH();
// 从全低开始SCL为高期间SDA上升沿表示stop信号再拉高SCL
// 从全低开始SCL为高期间SDA上升沿表示stop
}
void i2c_send_ack(void) {
// sda_out();
I2C_SDA_LOW();
i2c_delay();
I2C_SCL_HIGH();
@ -80,6 +94,7 @@ void i2c_send_ack(void) {
}
void i2c_send_nack(void) {
// sda_out();
I2C_SDA_HIGH();
i2c_delay();
I2C_SCL_HIGH();
@ -91,6 +106,7 @@ void i2c_send_nack(void) {
}
uint8_t i2c_wait_ack(void) {
// sda_in();
I2C_SDA_HIGH();
i2c_delay();
I2C_SCL_HIGH();
@ -102,6 +118,7 @@ uint8_t i2c_wait_ack(void) {
}
void i2c_send_byte(uint8_t byte) {
// sda_out();
for (int i = 0; i < 8; i++) {
if (byte & 0x80) { //通过&操作获取第一位是1还是0
I2C_SDA_HIGH(); //SCL低电平期间SDA高电平表示1
@ -113,11 +130,13 @@ void i2c_send_byte(uint8_t byte) {
I2C_SCL_HIGH(); //SCL拉高电平SDA电平状态保持不变
i2c_delay();
I2C_SCL_LOW(); //SCL拉低电平
delay_us(5);
}
i2c_wait_ack();
// i2c_wait_ack();
}
uint8_t i2c_receive_byte(uint8_t ack) {
// sda_in();
uint8_t byte = 0;
I2C_SDA_HIGH(); //从高开始
for (int i = 0; i < 8; i++) {

View File

@ -16,10 +16,10 @@ void led_config(void) {
timer_parameter_struct timer_initpara;
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 799;
timer_initpara.prescaler = 7199;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 999;
timer_initpara.period = 9999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(LED_TIMER, &timer_initpara);

View File

@ -15,8 +15,20 @@
#include "ldc1612.h"
#include "tmp112.h"
uint32_t g_temperature_uint32;
uint32_t g_eddy_current_value_uint32;
// uint32_t g_temperature_uint32;
// uint32_t g_eddy_current_value_uint32;
void watchdog_init(void) {
/* Enable the LSI clock */
rcu_osci_on(RCU_IRC40K);
rcu_osci_stab_wait(RCU_IRC40K);
/* 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)
/* Enable FWDGT */
fwdgt_enable();
}
/*!
\brief main function
@ -32,21 +44,76 @@ int main(void) {
rs485_config();
/* configure LED */
led_config();
// watchdog_init();
/* configure I2C */
// i2c_gpio_config();
// i2c_config();
// ldc1612_iic_get_sensor_infomation();
/* configure LDC1612 */
ldc1612_single_ch0_config();
// ldc1612_single_ch0_config();
uint8_t data[2] = {0};
printf("Start\n");
while (1) {
delay_ms(100);
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");
si2c_config();
printf("111\n");
i2c_start();
i2c_send_byte((0x2B << 1) + 1);
if (!i2c_wait_ack())
{
printf("NACK\n");
}
i2c_delay();
i2c_send_byte(0x7E);
// if (!i2c_wait_ack())
// {
// printf("NACK\n");
// }
i2c_delay();
i2c_start();
i2c_send_byte((0x2B << 1));
if (!i2c_wait_ack())
{
printf("NACK\n");
}
i2c_delay();
data[0] = i2c_receive_byte(1);
i2c_delay();
data[1] = i2c_receive_byte(0);
delay_us(5);
i2c_stop();
// printf("OK\n");
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

@ -52,9 +52,9 @@ void process_command(uint8_t *cmd, size_t length) {
// printf("%d", length);
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
if (strcmp(combined_str, "M1") == 0) {
eddy_current_value_report();
// eddy_current_value_report();
} else if (strcmp(combined_str, "M2") == 0) {
tempture_value_report();
// tempture_value_report();
} else {
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
return;