[其他ST产品] STM32 (基于HAL库) 硬件IIC读写任意AT24CXX芯片

[复制链接]
1761|7
 楼主| grfqq325 发表于 2023-11-20 23:02 | 显示全部楼层 |阅读模式
HAL任意AT24Cxx芯片读写:

原理我就不讲了,直接实操:
一、配置
1、使用STM32CUBEMX进行引脚配置,IIC配置如下,切记IIC频率不能大于该从机芯片支持最高通信频率:


46385655b74f1e2b3d.png


 楼主| grfqq325 发表于 2023-11-20 23:02 | 显示全部楼层
2、利用串口进行数据查看,串口配置如下:
85565655b75036eb5d.png
 楼主| grfqq325 发表于 2023-11-20 23:02 | 显示全部楼层
3、时钟我们选择最高72MHZ,这里没有硬性要求都可以。

1758655b7510ee175.png
 楼主| grfqq325 发表于 2023-11-20 23:03 | 显示全部楼层
4、配置完成,生成keil工程代码即可。
15258655b751f58359.png
49426655b75264385e.png
到此配置完成。
 楼主| grfqq325 发表于 2023-11-20 23:03 | 显示全部楼层
二、代码
编写驱动文件:
24CXX.c文件代码如下:
  1. #include "24cxx.h"
  2. #include "myiic.h"
  3. #include "i2c.h"
  4. //初始化IIC接口

  5. #define AT24CXX_HANDLE        (&hi2c1)        //IIC接口
  6. #define AT24C_DEV_ADDR  (0XA0) //设备地址

  7. void AT24CXX_Init(void)
  8. {
  9.         //IIC_Init();//IIC初始化
  10.         AT24CXX_Check();
  11. }

  12. /*****************************************
  13. 函数名:void AT24CXX_Write(uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite)
  14. 参数:WriteAddr :要写入数据的地址  pBuffer:要写入的数据的首地址 NumToWrite:要写入数据的长度
  15. 功能描述:从指定地址开始写入多个字节数据
  16. 返回值:无
  17. *****************************************/
  18. void AT24CXX_Write(uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite)
  19. {
  20.         if(EE_TYPE < AT24C16)
  21.                 HAL_I2C_Mem_Write(AT24CXX_HANDLE,AT24C_DEV_ADDR,WriteAddr,I2C_MEMADD_SIZE_8BIT,pBuffer,NumToWrite,HAL_MAX_DELAY);
  22.         else
  23.                 HAL_I2C_Mem_Write(AT24CXX_HANDLE,AT24C_DEV_ADDR,WriteAddr,I2C_MEMADD_SIZE_16BIT,pBuffer,NumToWrite,HAL_MAX_DELAY);
  24. }
  25. /*****************************************
  26. 函数名:AT24CXX_Read(uint16_t ReadAddr,uint8_t *pBuffer,uint16_t NumToRead)
  27. 参数: ReadAddr:要读取数据的地址 pBuffer:回填数据首地址 NumToRead:数据长度
  28. 功能描述:从指定地址开始读取多个个字节数据
  29. 返回值:无
  30. *****************************************/
  31. void AT24CXX_Read(uint16_t ReadAddr,uint8_t *pBuffer,uint16_t NumToRead)
  32. {
  33.         if(EE_TYPE < AT24C16)
  34.                 HAL_I2C_Mem_Read(AT24CXX_HANDLE,AT24C_DEV_ADDR,ReadAddr,I2C_MEMADD_SIZE_8BIT,pBuffer,NumToRead,HAL_MAX_DELAY);
  35.         else
  36.                 HAL_I2C_Mem_Read(AT24CXX_HANDLE,AT24C_DEV_ADDR,ReadAddr,I2C_MEMADD_SIZE_16BIT,pBuffer,NumToRead,HAL_MAX_DELAY);
  37. }
  38. /*****************************************
  39. 函数名:uint8_t AT24CXX_Check(void)
  40. 参数:无
  41. 功能描述:检查AT24CXX是否正常,这里用了24XX的最后一个地址(255)来存储标志字.如果用其他24C系列,这个地址要修改
  42. 返回值:检测成功返回0 失败返回1
  43. *****************************************/
  44. uint8_t AT24CXX_Check(void)
  45. {
  46.         uint8_t temp;
  47.         uint8_t data = 0XAB;
  48.         AT24CXX_Read(EE_TYPE,&temp,1);//避免每次开机都写AT24CXX                          
  49.         if(temp != 0XAB)
  50.                 return 1;                  
  51.         else//排除第一次初始化的情况
  52.         {
  53.                 AT24CXX_Write(EE_TYPE,&data,1);
  54.             AT24CXX_Read(EE_TYPE,&temp,1);;          
  55.                 if(temp != 0XAB)
  56.                         return 1;
  57.         }
  58.         return 0;                                                                                          
  59. }

 楼主| grfqq325 发表于 2023-11-20 23:03 | 显示全部楼层
HAL_24CXX.c文件代码如下:
  1. #ifndef AT24CXX_H__
  2. #define AT24CXX_H__
  3. /*****************************************
  4.                                                 本驱动文件仅适配HAL库版本
  5. ******************************************/
  6. #include "stm32f1xx_hal.h"        //链接HAL库

  7. #define AT24C01                127
  8. #define AT24C02                255
  9. #define AT24C04                511
  10. #define AT24C08                1023
  11. #define AT24C16                2047
  12. #define AT24C32                4095
  13. #define AT24C64          8191
  14. #define AT24C128        16383
  15. #define AT24C256        32767  
  16. //我使用的是AT24C02
  17. #define EE_TYPE AT24C02

  18. void AT24CXX_Init(void);

  19. void AT24CXX_Write(uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite);

  20. void AT24CXX_Read(uint16_t ReadAddr,uint8_t *pBuffer,uint16_t NumToRead);

  21. uint8_t AT24CXX_Check(void);

  22. #endif

 楼主| grfqq325 发表于 2023-11-20 23:03 | 显示全部楼层
三、示例演示:
代码如下:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2022 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "i2c.h"
  22. #include "usart.h"
  23. #include "gpio.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "HAL_24cxx.h"
  27. #include <stdio.h>
  28. /* USER CODE END Includes */

  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */

  31. /* USER CODE END PTD */

  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */

  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */

  37. /* USER CODE END PM */

  38. /* Private variables ---------------------------------------------------------*/

  39. /* USER CODE BEGIN PV */

  40. /* USER CODE END PV */

  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. /* USER CODE BEGIN PFP */

  44. /* USER CODE END PFP */

  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */

  47. /*****************************************
  48. 函数名:
  49. 参数:无
  50. 功能描述:printf输出重定向到串口1
  51. 返回值:
  52. *****************************************/
  53. int fputc(int ch,FILE *f){
  54. uint8_t temp[1]={ch};
  55. HAL_UART_Transmit(&huart1,temp,1,2);
  56. return ch;
  57. }

  58. /* USER CODE END 0 */

  59. /**
  60.   * @brief  The application entry point.
  61.   * @retval int
  62.   */
  63. int main(void)
  64. {
  65.   /* USER CODE BEGIN 1 */
  66.         uint8_t buff[6];
  67.         bool res;
  68.   /* USER CODE END 1 */

  69.   /* MCU Configuration--------------------------------------------------------*/

  70.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  71.   HAL_Init();

  72.   /* USER CODE BEGIN Init */

  73.   /* USER CODE END Init */

  74.   /* Configure the system clock */
  75.   SystemClock_Config();

  76.   /* USER CODE BEGIN SysInit */

  77.   /* USER CODE END SysInit */

  78.   /* Initialize all configured peripherals */
  79.   MX_GPIO_Init();
  80.   MX_I2C1_Init();
  81.   MX_USART1_UART_Init();
  82.   /* USER CODE BEGIN 2 */
  83.         printf("测试程序开始\r\n");
  84.        
  85.         res = AT24CXX_Check();
  86.         if(!res)
  87.         {
  88.                 printf("AT24CXX OK!\r\n");
  89.         }
  90.         else
  91.         {
  92.                 printf("AT24CXX ERROR!\r\n");
  93.         }
  94.         HAL_Delay(1000);
  95.         AT24CXX_Write(0,(uint8_t *)"hello",5);

  96.         AT24CXX_Read(0,buff,5);
  97.        
  98.         printf("buff:%s\r\n",buff);
  99.        
  100.         printf("测试程序结束\r\n");
  101.   /* USER CODE END 2 */

  102.   /* Infinite loop */
  103.   /* USER CODE BEGIN WHILE */
  104.   while (1)
  105.   {
  106.     /* USER CODE END WHILE */

  107.     /* USER CODE BEGIN 3 */
  108.   }
  109.   /* USER CODE END 3 */
  110. }

  111. /**
  112.   * @brief System Clock Configuration
  113.   * @retval None
  114.   */
  115. void SystemClock_Config(void)
  116. {
  117.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  118.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  119.   /** Initializes the RCC Oscillators according to the specified parameters
  120.   * in the RCC_OscInitTypeDef structure.
  121.   */
  122.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  123.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  124.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  125.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  126.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  127.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  128.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  129.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  130.   {
  131.     Error_Handler();
  132.   }

  133.   /** Initializes the CPU, AHB and APB buses clocks
  134.   */
  135.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  136.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  137.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  138.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  139.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  140.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  141.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  142.   {
  143.     Error_Handler();
  144.   }
  145. }

  146. /* USER CODE BEGIN 4 */

  147. /* USER CODE END 4 */

  148. /**
  149.   * @brief  This function is executed in case of error occurrence.
  150.   * @retval None
  151.   */
  152. void Error_Handler(void)
  153. {
  154.   /* USER CODE BEGIN Error_Handler_Debug */
  155.   /* User can add his own implementation to report the HAL error return state */
  156.   __disable_irq();
  157.   while (1)
  158.   {
  159.   }
  160.   /* USER CODE END Error_Handler_Debug */
  161. }

  162. #ifdef  USE_FULL_ASSERT
  163. /**
  164.   * @brief  Reports the name of the source file and the source line number
  165.   *         where the assert_param error has occurred.
  166.   * @param  file: pointer to the source file name
  167.   * @param  line: assert_param error line source number
  168.   * @retval None
  169.   */
  170. void assert_failed(uint8_t *file, uint32_t line)
  171. {
  172.   /* USER CODE BEGIN 6 */
  173.   /* User can add his own implementation to report the file name and line number,
  174.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  175.   /* USER CODE END 6 */
  176. }
  177. #endif /* USE_FULL_ASSERT */

 楼主| grfqq325 发表于 2023-11-20 23:04 | 显示全部楼层
最终效果如下:
66066655b7571b4db1.png

测试成功,本次分享到此结束。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

65

主题

687

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部