野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 476|回复: 1

STM32 IIC写入读取出现问题

[复制链接]
发表于 2023-8-28 17:12:51 | 显示全部楼层 |阅读模式

我写了一个IIC的字节读取写入,但是写入读取时没有变化,运行阶段都正常运行了,没有出现报错,请问这是什么问题?

这是主程序

  1. #include "hxy_config.h"



  2. int main(void)
  3. {
  4.     BSP_USART_INIT();
  5.     printf("你好");
  6.        I2c_Config();

  7.     uint8_t  arr[] = {15}, b[10];
  8.     printf("%d", b[0]);
  9.     I2c_Bity_Write(30, &arr[0]);
  10.     printf("你好");
  11.     I2c_Bity_Read(30,b,1);
  12.    
  13.     printf("%d", b[0]);
  14.   
  15.     while(1)
  16.     {
  17.    
  18.     }
  19.         
  20.    

  21. }



复制代码


这是i2c头文件


  1. #ifndef _BSP_I2C_H
  2. #define _BSP_I2C_H


  3. #include "hxy_config.h"

  4. #define I2C_TIME_OUT  (0xA000)
  5. #define EEPROM_ADDRESS (0xA0)
  6. #define EEPROM_SZIE 256


  7. void I2c_Config(void);
  8. static uint32_t  i2c_error_show(uint16_t i);

  9. uint32_t  I2c_Bity_Write(uint8_t   bityaddr , uint8_t  * pbuffer);

  10. void I2c_Wait_EE_Ready(void);

  11. uint8_t  I2c_Bity_Read(uint8_t bityaddr, uint8_t  * pbuffer, uint16_t bity_num);
  12. static uint8_t  i2c_read_error_show(uint16_t i);

  13. #endif

复制代码
这是i2c.c文件

  1. #include "bsp_i2c.h"


  2. void I2c_Config(void)
  3. {
  4.     //初始化GPIO,PB6为SCL,PB7为SDA
  5.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);  
  6.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE );  //开启I2C时钟
  7.     GPIO_InitTypeDef  i2c_gpio_pb6;
  8.     GPIO_InitTypeDef  i2c_gpio_pb7;
  9.    
  10.     i2c_gpio_pb6.GPIO_Pin = GPIO_Pin_6;
  11.     i2c_gpio_pb6.GPIO_Mode = GPIO_Mode_AF_OD;
  12.     i2c_gpio_pb6.GPIO_Speed = GPIO_Speed_50MHz;
  13.    
  14.     i2c_gpio_pb7.GPIO_Pin = GPIO_Pin_7;
  15.     i2c_gpio_pb7.GPIO_Mode = GPIO_Mode_AF_OD;
  16.     i2c_gpio_pb7.GPIO_Speed = GPIO_Speed_50MHz;
  17.    
  18.     GPIO_Init(GPIOB, &i2c_gpio_pb6);
  19.     GPIO_Init(GPIOB, &i2c_gpio_pb7);
  20.    
  21.    
  22.    
  23.     I2C_InitTypeDef  i2c_init_struct;
  24.    
  25.     i2c_init_struct.I2C_ClockSpeed = 400000;
  26.     i2c_init_struct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  27.     i2c_init_struct.I2C_DutyCycle = I2C_DutyCycle_2 ;
  28.     i2c_init_struct.I2C_Mode = I2C_Mode_I2C;
  29.     i2c_init_struct.I2C_OwnAddress1 = 0x0A;   //只需不同就可
  30.     i2c_init_struct.I2C_Ack = I2C_Ack_Enable;
  31.     I2C_Init(I2C1, &i2c_init_struct);      //初始化i2c
  32.    
  33.     I2C_Cmd(I2C1,ENABLE);
  34.    
  35.    
  36.    

  37.    
  38. }



  39. static uint32_t  i2c_error_show(uint16_t i)
  40. {
  41.     printf("i2c传输出现错误,错误原因为 :%d" , i);
  42.     return  0;
  43.    
  44. }

  45. uint32_t  I2c_Bity_Write(uint8_t   bityaddr , uint8_t * pbuffer)
  46. {
  47.     //while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY) != RESET);
  48.     I2C_GenerateSTART(I2C1,ENABLE);
  49.    
  50.     uint32_t time_out = I2C_TIME_OUT;
  51.    
  52.   while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT))        //开始位   
  53.     {
  54.         if(time_out == 0)
  55.             return i2c_error_show(0);
  56.         
  57.         time_out--;
  58.     }
  59.    
  60.     time_out = I2C_TIME_OUT;
  61.    
  62.     I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS,I2C_Direction_Transmitter);
  63.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))        //输入EEROM地址位
  64.     {
  65.         if(time_out <= 0)
  66.             return i2c_error_show(0);
  67.         
  68.         time_out--;
  69.     }
  70.    
  71.     time_out = I2C_TIME_OUT;
  72.     I2C_SendData(I2C1, bityaddr);
  73.    
  74.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED ))        //输入字节存放在EEROM位置
  75.     {
  76.         if(time_out <= 0)
  77.             return i2c_error_show(0);
  78.         
  79.         time_out--;
  80.     }
  81.    
  82.     time_out = I2C_TIME_OUT;
  83.     I2C_SendData(I2C1, *pbuffer);
  84.    
  85.    
  86.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED ))        
  87.     {
  88.         if(time_out <= 0)
  89.             return i2c_error_show(0);
  90.         
  91.         time_out--;
  92.     }
  93.    
  94.     I2C_GenerateSTOP(I2C1, ENABLE );
  95.    
  96.    
  97.     return 0;
  98.    
  99.    
  100. }  


  101.    


  102. static uint8_t  i2c_read_error_show(uint16_t i)
  103. {
  104.     printf("i2c读取出现错误,错误原因为 :%d" , i);
  105.     return  0;
  106.    
  107. }

  108. uint8_t  I2c_Bity_Read(uint8_t bityaddr, uint8_t  * pbuffer, uint16_t bity_num)
  109. {
  110.     I2c_Wait_EE_Ready();
  111.     while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY) != RESET);        //确认是否在占用数据传输
  112.     I2C_GenerateSTART(I2C1,ENABLE);
  113.    
  114.     uint32_t time_out = I2C_TIME_OUT;
  115.    
  116.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT))        //开始位
  117.     {
  118.         if(time_out <= 0)
  119.             return i2c_read_error_show(0);
  120.         
  121.         time_out--;
  122.     }
  123.    
  124.     time_out = I2C_TIME_OUT;
  125.    
  126.     I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS,I2C_Direction_Transmitter);
  127.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))        //输入EEROM地址位
  128.     {
  129.         if(time_out <= 0)
  130.             return i2c_read_error_show(1);
  131.         
  132.         time_out--;
  133.     }
  134.    
  135.     time_out = I2C_TIME_OUT;
  136.     I2C_SendData(I2C1, bityaddr);
  137.    
  138.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED))        //输入字节存放在EEROM位置
  139.     {
  140.         if(time_out <= 0)
  141.             return i2c_read_error_show(2);
  142.         
  143.         time_out--;
  144.     }
  145.    
  146.     I2C_GenerateSTART(I2C1,ENABLE);
  147.    
  148.    
  149.     ;
  150.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT))        //开始位
  151.     {
  152.         if(time_out <= 0)
  153.             return i2c_read_error_show(3);
  154.         
  155.         time_out--;
  156.     }
  157.    
  158.     time_out = I2C_TIME_OUT;
  159.    
  160.     I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS,I2C_Direction_Receiver);
  161.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ))        //输入EEROM地址位
  162.     {
  163.         if(time_out <= 0)
  164.             return i2c_read_error_show(4);
  165.         
  166.         time_out--;
  167.     }
  168.     uint8_t i = 0;
  169.     while(i != bity_num)
  170.     {
  171.       time_out = I2C_TIME_OUT;
  172.         *(pbuffer + i) = I2C_ReceiveData(I2C1);
  173.         while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))        
  174.       {
  175.             if(time_out <= 0)
  176.                 return i2c_read_error_show(5);
  177.             
  178.             time_out--;
  179.       }
  180.       i++;
  181.         if(i == bity_num)  I2C_AcknowledgeConfig(I2C1,DISABLE);
  182.   }
  183.     I2C_GenerateSTOP(I2C1, ENABLE );
  184.     I2C_AcknowledgeConfig(I2C1,ENABLE);
  185.    
  186.     return 0;
  187.    
  188.    
  189. }  
  190.    
  191.    
  192.    

复制代码


回复

使用道具 举报

 楼主| 发表于 2023-10-5 20:59:24 | 显示全部楼层
改成模拟输出问题就消失了,看样子103的I2C硬件真有有问题
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-30 03:26 , Processed in 0.033461 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表