generated from hulk/gd32e23x_template
测试软件IIC
This commit is contained in:
parent
ce6289a82a
commit
6792caa04f
@ -69,7 +69,9 @@ void i2c_send_nack(void);
|
|||||||
|
|
||||||
uint8_t i2c_wait_ack(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);
|
void i2c_config(void);
|
||||||
|
|
||||||
|
@ -45,9 +45,9 @@
|
|||||||
|
|
||||||
/* select a system clock by uncommenting the following line */
|
/* select a system clock by uncommenting the following line */
|
||||||
//#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
|
//#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_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{ \
|
#define RCU_MODIFY(__delay) do{ \
|
||||||
volatile uint32_t i; \
|
volatile uint32_t i; \
|
||||||
|
25
src/i2c.c
25
src/i2c.c
@ -26,7 +26,7 @@ void i2c_gpio_config(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void i2c_delay(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();
|
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) {
|
void i2c_start(void) {
|
||||||
|
// sda_out();
|
||||||
I2C_SDA_HIGH();
|
I2C_SDA_HIGH();
|
||||||
I2C_SCL_HIGH();
|
I2C_SCL_HIGH();
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SDA_LOW();
|
I2C_SDA_LOW();
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SCL_LOW();
|
I2C_SCL_LOW();
|
||||||
|
i2c_delay();
|
||||||
|
i2c_delay();
|
||||||
// 从全高开始,SCL为高期间,SDA下降沿表示start信号,再拉低SCL
|
// 从全高开始,SCL为高期间,SDA下降沿表示start信号,再拉低SCL
|
||||||
}
|
}
|
||||||
|
|
||||||
void i2c_stop(void) {
|
void i2c_stop(void) {
|
||||||
|
// sda_out();
|
||||||
I2C_SCL_LOW();
|
I2C_SCL_LOW();
|
||||||
I2C_SDA_LOW();
|
I2C_SDA_LOW();
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SCL_HIGH();
|
I2C_SCL_HIGH();
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SDA_HIGH();
|
I2C_SDA_HIGH();
|
||||||
// 从全低开始,SCL为高期间,SDA上升沿表示stop信号,再拉高SCL
|
// 从全低开始,SCL为高期间,SDA上升沿表示stop
|
||||||
}
|
}
|
||||||
|
|
||||||
void i2c_send_ack(void) {
|
void i2c_send_ack(void) {
|
||||||
|
// sda_out();
|
||||||
I2C_SDA_LOW();
|
I2C_SDA_LOW();
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SCL_HIGH();
|
I2C_SCL_HIGH();
|
||||||
@ -80,6 +94,7 @@ void i2c_send_ack(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void i2c_send_nack(void) {
|
void i2c_send_nack(void) {
|
||||||
|
// sda_out();
|
||||||
I2C_SDA_HIGH();
|
I2C_SDA_HIGH();
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SCL_HIGH();
|
I2C_SCL_HIGH();
|
||||||
@ -91,6 +106,7 @@ void i2c_send_nack(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t i2c_wait_ack(void) {
|
uint8_t i2c_wait_ack(void) {
|
||||||
|
// sda_in();
|
||||||
I2C_SDA_HIGH();
|
I2C_SDA_HIGH();
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SCL_HIGH();
|
I2C_SCL_HIGH();
|
||||||
@ -102,6 +118,7 @@ uint8_t i2c_wait_ack(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void i2c_send_byte(uint8_t byte) {
|
void i2c_send_byte(uint8_t byte) {
|
||||||
|
// sda_out();
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
if (byte & 0x80) { //通过&操作获取第一位是1还是0
|
if (byte & 0x80) { //通过&操作获取第一位是1还是0
|
||||||
I2C_SDA_HIGH(); //SCL低电平期间,SDA高电平表示1
|
I2C_SDA_HIGH(); //SCL低电平期间,SDA高电平表示1
|
||||||
@ -113,11 +130,13 @@ void i2c_send_byte(uint8_t byte) {
|
|||||||
I2C_SCL_HIGH(); //SCL拉高电平,SDA电平状态保持不变
|
I2C_SCL_HIGH(); //SCL拉高电平,SDA电平状态保持不变
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
I2C_SCL_LOW(); //SCL拉低电平
|
I2C_SCL_LOW(); //SCL拉低电平
|
||||||
|
delay_us(5);
|
||||||
}
|
}
|
||||||
i2c_wait_ack();
|
// i2c_wait_ack();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t i2c_receive_byte(uint8_t ack) {
|
uint8_t i2c_receive_byte(uint8_t ack) {
|
||||||
|
// sda_in();
|
||||||
uint8_t byte = 0;
|
uint8_t byte = 0;
|
||||||
I2C_SDA_HIGH(); //从高开始
|
I2C_SDA_HIGH(); //从高开始
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
|
@ -16,10 +16,10 @@ void led_config(void) {
|
|||||||
|
|
||||||
timer_parameter_struct timer_initpara;
|
timer_parameter_struct timer_initpara;
|
||||||
timer_struct_para_init(&timer_initpara);
|
timer_struct_para_init(&timer_initpara);
|
||||||
timer_initpara.prescaler = 799;
|
timer_initpara.prescaler = 7199;
|
||||||
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
|
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
|
||||||
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
||||||
timer_initpara.period = 999;
|
timer_initpara.period = 9999;
|
||||||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||||
timer_init(LED_TIMER, &timer_initpara);
|
timer_init(LED_TIMER, &timer_initpara);
|
||||||
|
|
||||||
|
63
src/main.c
63
src/main.c
@ -55,18 +55,79 @@ int main(void) {
|
|||||||
rs485_config();
|
rs485_config();
|
||||||
/* configure LED */
|
/* configure LED */
|
||||||
led_config();
|
led_config();
|
||||||
|
|
||||||
|
// watchdog_init();
|
||||||
/* configure I2C */
|
/* configure I2C */
|
||||||
// i2c_gpio_config();
|
// i2c_gpio_config();
|
||||||
// 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) {
|
||||||
|
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");
|
||||||
delay_ms(99);
|
delay_ms(99);
|
||||||
fwdgt_counter_reload();
|
fwdgt_counter_reload();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user