初中生
最后登录1970-1-1
在线时间 小时
注册时间2023-4-11
|
我写了一个IIC的字节读取写入,但是写入读取时没有变化,运行阶段都正常运行了,没有出现报错,请问这是什么问题? 这是主程序 - #include "hxy_config.h"
-
-
-
- int main(void)
- {
- BSP_USART_INIT();
- printf("你好");
- I2c_Config();
-
- uint8_t arr[] = {15}, b[10];
- printf("%d", b[0]);
- I2c_Bity_Write(30, &arr[0]);
- printf("你好");
- I2c_Bity_Read(30,b,1);
-
- printf("%d", b[0]);
-
- while(1)
- {
-
- }
-
-
-
- }
-
-
-
-
复制代码
这是i2c头文件 -
- #ifndef _BSP_I2C_H
- #define _BSP_I2C_H
-
-
- #include "hxy_config.h"
-
- #define I2C_TIME_OUT (0xA000)
- #define EEPROM_ADDRESS (0xA0)
- #define EEPROM_SZIE 256
-
-
- void I2c_Config(void);
- static uint32_t i2c_error_show(uint16_t i);
-
- uint32_t I2c_Bity_Write(uint8_t bityaddr , uint8_t * pbuffer);
-
- void I2c_Wait_EE_Ready(void);
-
- uint8_t I2c_Bity_Read(uint8_t bityaddr, uint8_t * pbuffer, uint16_t bity_num);
- static uint8_t i2c_read_error_show(uint16_t i);
-
- #endif
-
-
复制代码 这是i2c.c文件
- #include "bsp_i2c.h"
-
-
- void I2c_Config(void)
- {
- //初始化GPIO,PB6为SCL,PB7为SDA
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE ); //开启I2C时钟
- GPIO_InitTypeDef i2c_gpio_pb6;
- GPIO_InitTypeDef i2c_gpio_pb7;
-
- i2c_gpio_pb6.GPIO_Pin = GPIO_Pin_6;
- i2c_gpio_pb6.GPIO_Mode = GPIO_Mode_AF_OD;
- i2c_gpio_pb6.GPIO_Speed = GPIO_Speed_50MHz;
-
- i2c_gpio_pb7.GPIO_Pin = GPIO_Pin_7;
- i2c_gpio_pb7.GPIO_Mode = GPIO_Mode_AF_OD;
- i2c_gpio_pb7.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOB, &i2c_gpio_pb6);
- GPIO_Init(GPIOB, &i2c_gpio_pb7);
-
-
-
- I2C_InitTypeDef i2c_init_struct;
-
- i2c_init_struct.I2C_ClockSpeed = 400000;
- i2c_init_struct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
- i2c_init_struct.I2C_DutyCycle = I2C_DutyCycle_2 ;
- i2c_init_struct.I2C_Mode = I2C_Mode_I2C;
- i2c_init_struct.I2C_OwnAddress1 = 0x0A; //只需不同就可
- i2c_init_struct.I2C_Ack = I2C_Ack_Enable;
- I2C_Init(I2C1, &i2c_init_struct); //初始化i2c
-
- I2C_Cmd(I2C1,ENABLE);
-
-
-
-
-
- }
-
-
-
- static uint32_t i2c_error_show(uint16_t i)
- {
- printf("i2c传输出现错误,错误原因为 :%d" , i);
- return 0;
-
- }
-
- uint32_t I2c_Bity_Write(uint8_t bityaddr , uint8_t * pbuffer)
- {
- //while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY) != RESET);
- I2C_GenerateSTART(I2C1,ENABLE);
-
- uint32_t time_out = I2C_TIME_OUT;
-
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)) //开始位
- {
- if(time_out == 0)
- return i2c_error_show(0);
-
- time_out--;
- }
-
- time_out = I2C_TIME_OUT;
-
- I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS,I2C_Direction_Transmitter);
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) //输入EEROM地址位
- {
- if(time_out <= 0)
- return i2c_error_show(0);
-
- time_out--;
- }
-
- time_out = I2C_TIME_OUT;
- I2C_SendData(I2C1, bityaddr);
-
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED )) //输入字节存放在EEROM位置
- {
- if(time_out <= 0)
- return i2c_error_show(0);
-
- time_out--;
- }
-
- time_out = I2C_TIME_OUT;
- I2C_SendData(I2C1, *pbuffer);
-
-
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED ))
- {
- if(time_out <= 0)
- return i2c_error_show(0);
-
- time_out--;
- }
-
- I2C_GenerateSTOP(I2C1, ENABLE );
-
-
- return 0;
-
-
- }
-
-
-
-
-
- static uint8_t i2c_read_error_show(uint16_t i)
- {
- printf("i2c读取出现错误,错误原因为 :%d" , i);
- return 0;
-
- }
-
- uint8_t I2c_Bity_Read(uint8_t bityaddr, uint8_t * pbuffer, uint16_t bity_num)
- {
- I2c_Wait_EE_Ready();
- while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY) != RESET); //确认是否在占用数据传输
- I2C_GenerateSTART(I2C1,ENABLE);
-
- uint32_t time_out = I2C_TIME_OUT;
-
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)) //开始位
- {
- if(time_out <= 0)
- return i2c_read_error_show(0);
-
- time_out--;
- }
-
- time_out = I2C_TIME_OUT;
-
- I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS,I2C_Direction_Transmitter);
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) //输入EEROM地址位
- {
- if(time_out <= 0)
- return i2c_read_error_show(1);
-
- time_out--;
- }
-
- time_out = I2C_TIME_OUT;
- I2C_SendData(I2C1, bityaddr);
-
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)) //输入字节存放在EEROM位置
- {
- if(time_out <= 0)
- return i2c_read_error_show(2);
-
- time_out--;
- }
-
- I2C_GenerateSTART(I2C1,ENABLE);
-
-
- ;
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)) //开始位
- {
- if(time_out <= 0)
- return i2c_read_error_show(3);
-
- time_out--;
- }
-
- time_out = I2C_TIME_OUT;
-
- I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS,I2C_Direction_Receiver);
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED )) //输入EEROM地址位
- {
- if(time_out <= 0)
- return i2c_read_error_show(4);
-
- time_out--;
- }
- uint8_t i = 0;
- while(i != bity_num)
- {
- time_out = I2C_TIME_OUT;
- *(pbuffer + i) = I2C_ReceiveData(I2C1);
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))
- {
- if(time_out <= 0)
- return i2c_read_error_show(5);
-
- time_out--;
- }
- i++;
- if(i == bity_num) I2C_AcknowledgeConfig(I2C1,DISABLE);
- }
- I2C_GenerateSTOP(I2C1, ENABLE );
- I2C_AcknowledgeConfig(I2C1,ENABLE);
-
- return 0;
-
-
- }
-
-
-
-
-
复制代码
|
|