HAL任意AT24Cxx芯片读写:
原理我就不讲了,直接实操: 一、配置 1、使用STM32CUBEMX进行引脚配置,IIC配置如下: 2、利用串口进行数据查看,串口配置如下: 3、时钟我们选择最高72MHZ,这里没有硬性要求都可以。 4、配置完成,生成keil工程代码即可。 到此配置完成。
二、代码 编写驱动文件: HAL_24CXX.c文件代码如下:
#include "HAL_24Cxx.h"
#include "i2c.h"
#include <stdio.h>
#define IICx hi2c1
#define AT24C_DEV_WRITEADDR 0xA0
#define AT24C_DEV_READADDR 0xA1
uint8_t HAL_AT24CXX_WriteOneByte(uint16_t WriteAddr,uint8_t WriteData)
{
uint8_t res = 0xff;
if(EE_TYPE < AT24C16)
{
if(HAL_I2C_Mem_Write(&IICx,AT24C_DEV_WRITEADDR,WriteAddr,I2C_MEMADD_SIZE_8BIT,&WriteData,1,0xff) == HAL_OK)
res = 0;
}
else
if(HAL_I2C_Mem_Write(&IICx,AT24C_DEV_WRITEADDR,WriteAddr,I2C_MEMADD_SIZE_16BIT,&WriteData,1,0xff) == HAL_OK)
res = 0;
HAL_Delay(10);
return res;
}
uint8_t HAL_AT24CXX_ReadOneByte(uint16_t ReadAddr)
{
uint8_t rxData = 0;
if(EE_TYPE < AT24C16)
{
HAL_I2C_Mem_Read(&IICx,AT24C_DEV_READADDR,ReadAddr,I2C_MEMADD_SIZE_8BIT,&rxData,1,0xff);
}
else
HAL_I2C_Mem_Read(&IICx,AT24C_DEV_READADDR,ReadAddr,I2C_MEMADD_SIZE_16BIT,&rxData,1,0xff);
HAL_Delay(10);
return rxData;
}
void HAL_AT24CXX_WriteLenByte(uint16_t WriteAddr,uint8_t *pData,uint8_t dataLen)
{
while(dataLen--)
{
HAL_AT24CXX_WriteOneByte(WriteAddr,*pData);
WriteAddr++;
pData++;
}
}
void HAL_AT24CXX_ReadLenByte(uint16_t ReadAddr,uint8_t *pData,uint8_t dataLen)
{
while(dataLen--)
{
*pData++ = HAL_AT24CXX_ReadOneByte(ReadAddr++);
}
}
bool HAL_AT24CXX_Check(void)
{
uint8_t temp;
temp = HAL_AT24CXX_ReadOneByte(EE_TYPE);
if(temp == 0XAB)
return true;
else
{
HAL_AT24CXX_WriteOneByte(EE_TYPE,0XAB);
temp = HAL_AT24CXX_ReadOneByte(EE_TYPE);
if(temp == 0XAB)
return true;
}
return false;
}
HAL_24CXX.c文件代码如下:
#ifndef HAL_24CXX_H__
#define HAL_24CXX_H__
#include "stm32f1xx_hal.h"
typedef enum
{
false,
true,
}bool;
#define AT24C01 127
#define AT24C02 255
#define AT24C04 511
#define AT24C08 1023
#define AT24C16 2047
#define AT24C32 4095
#define AT24C64 8191
#define AT24C128 16383
#define AT24C256 32767
#define EE_TYPE AT24C128
uint8_t HAL_AT24CXX_ReadOneByte(uint16_t ReadAddr);
uint8_t HAL_AT24CXX_WriteOneByte(uint16_t WriteAddr,uint8_t WriteData);
void HAL_AT24CXX_WriteLenByte(uint16_t WriteAddr,uint8_t *pData,uint8_t dataLen);
void HAL_AT24CXX_ReadLenByte(uint16_t ReadAddr,uint8_t *pData,uint8_t dataLen);
bool HAL_AT24CXX_Check(void);
#endif
三、示例演示: 代码如下:
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "HAL_24cxx.h"
#include <stdio.h>
void SystemClock_Config(void);
int fputc(int ch,FILE *f){
uint8_t temp[1]={ch};
HAL_UART_Transmit(&huart1,temp,1,2);
return ch;
}
int main(void)
{
uint8_t buff[6];
bool res;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
printf("测试程序开始\r\n");
res = HAL_AT24CXX_Check();
if(res == true)
{
printf("AT24CXX OK!\r\n");
}
else
{
printf("AT24CXX ERROR!\r\n");
}
HAL_Delay(1000);
HAL_AT24CXX_WriteLenByte(0,(uint8_t *)"hello",5);
HAL_AT24CXX_ReadLenByte(0,buff,5);
printf("buff:%s\r\n",buff);
printf("测试程序结束\r\n");
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
最终效果如下: 从图可以看出 测试成功。 完整工程链接:https://pan.baidu.com/s/1Xpw1AlGn-GOLxzSUf4UegQ 提取码: xtms
|