STM32-HAL库06-IIC驱动FM24CL16B非易失存储器
一、所用材料:
STM32VGT6自制控制板
STM32CUBEMX(HAL库软件)
MDK5
二、所学内容:
通过HAL库的硬件IIC对FM24CL16B存储器进行写与读取操作。
三、CUBEMX配置:
第一步:选取IIC1进行配置
第二步:配置SYS
第三步:配置RCC
第四步:配置时钟
第五步:文件配置
四、MDK5程序编写:
1.配置FM24CL16B.h文件
#ifndef __FM24CL16_H
#define __FM24CL16_H //========移植的时候,根据所选芯片引入相应的头文件==========在main.h里面查看
#include "stm32f4xx_hal.h"
#include "i2c.h"#define FM_24LC16_Write 0xA0
#define FM_24LC16_Read 0xA1
#define BufferSize 2048//电机参数内存分配
#define Rs_add 0
#define Rr_add 4void FM24CL16B_Write_data(uint16_t add , uint8_t data);
uint8_t FM24CL16B_Read_data(uint16_t add);
void Parameter_Write (uint16_t para_add , float para_data);
float Parameter_Read (uint16_t para_add);#endif
2.配置FM24CL16B.c文件
#include "FM24CL16B.h"
#include "i2c.h"
#include "stdio.h"/********************************************
函数名称:FM24CL16写数据
函数参数:add—写入数据的地址(0-2048)data—写入的数据(0-256)
函数解析:向指定地址写入特定数据,并且在写入后读取一次打印至串口
********************************************/
void FM24CL16B_Write_data(uint16_t add , uint8_t data)
{HAL_I2C_Mem_Write(&hi2c1, FM_24LC16_Write, add, I2C_MEMADD_SIZE_8BIT,&data,1,0xff);HAL_Delay(5);//此处延时必加printf("\r\n EEPROM 24C02 Write Test OK \r\n");FM24CL16B_Read_data(add);
}
/********************************************
函数名称:FM24CL16读数据
函数参数:add—读数据的地址(0-2048)
函数解析:读取指定地址的数据并返回该数据
********************************************/
uint8_t FM24CL16B_Read_data(uint16_t add)
{uint8_t Read_data;HAL_I2C_Mem_Read(&hi2c1, FM_24LC16_Read, add, I2C_MEMADD_SIZE_8BIT,&Read_data,1, 0xff);printf("地址:%d 16进制:0x%02X 10进制:%d \r\n",add,Read_data,Read_data);return Read_data;
}
/********************************************
函数名称:浮点数参数写入
函数参数:para_add—写入参数的头地址para_data-写入的参数浮点数值
函数解析:将浮点数参数值写入到指定的参数地址
********************************************/
void Parameter_Write (uint16_t para_add , float para_data)
{uint16_t zhengshu ;uint8_t zhengshu1 ;uint8_t zhengshu2 ;uint16_t xiaoshu ;uint8_t xiaoshu1 ;uint8_t xiaoshu2 ;zhengshu = (uint16_t)para_data;zhengshu1 = zhengshu/256;zhengshu2 = zhengshu%256;xiaoshu = (para_data-zhengshu)*1000.0;xiaoshu1 = xiaoshu/256;xiaoshu2 = xiaoshu%256;HAL_I2C_Mem_Write(&hi2c1, FM_24LC16_Write,para_add, I2C_MEMADD_SIZE_8BIT,&zhengshu1,1,0xff);HAL_Delay(5);//此处延时必加HAL_I2C_Mem_Write(&hi2c1, FM_24LC16_Write,para_add+1, I2C_MEMADD_SIZE_8BIT,&zhengshu2,1,0xff);HAL_Delay(5);//此处延时必加HAL_I2C_Mem_Write(&hi2c1, FM_24LC16_Write,para_add+2, I2C_MEMADD_SIZE_8BIT,&xiaoshu1,1,0xff);HAL_Delay(5);//此处延时必加HAL_I2C_Mem_Write(&hi2c1, FM_24LC16_Write,para_add+3, I2C_MEMADD_SIZE_8BIT,&xiaoshu2,1,0xff);HAL_Delay(5);//此处延时必加FM24CL16B_Read_data(para_add);FM24CL16B_Read_data(para_add+1);FM24CL16B_Read_data(para_add+2);FM24CL16B_Read_data(para_add+3);
}
/********************************************
函数名称:浮点数参数读取
函数参数:para_add—读取参数的头地址
函数解析:将浮点数参数值从内存中读出并返回
********************************************/
float Parameter_Read (uint16_t para_add)
{float para_data;uint8_t zhengshu1 ;uint8_t zhengshu2 ;uint8_t xiaoshu1 ;uint8_t xiaoshu2 ;zhengshu1 =FM24CL16B_Read_data(para_add);zhengshu2 =FM24CL16B_Read_data(para_add+1);xiaoshu1 = FM24CL16B_Read_data(para_add+2);xiaoshu2 = FM24CL16B_Read_data(para_add+3);para_data = zhengshu1*256+zhengshu2+(xiaoshu1*256+xiaoshu2)*0.001;printf("参数值为:%f",para_data);return para_data;
}
3.配置main.c文件
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_TIM2_Init();MX_TIM5_Init();MX_I2C1_Init();MX_USART2_UART_Init();/* USER CODE BEGIN 2 */HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);__HAL_TIM_CLEAR_IT(&htim5, TIM_IT_UPDATE);HAL_TIM_Base_Start_IT(&htim5); /* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE BEGIN 2 */Parameter_Write (Rr_add , 1458.0597);//写入Rr_add的参数值,并且会读取一次Parameter_Read (Rs_add); //读取Rs_add的参数值,此值之前已经写进去过了HAL_Delay(10000);/* USER CODE END 2 *//* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}