if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_SET)
{ HAL_Delay(500);
testarr[0]+=3;
if(HAL_I2C_Mem_Write(&hi2c1,0xa0,60,I2C_MEMADD_SIZE_8BIT,&testarr[0],1,3000)==HAL_OK)
{
printf (" testarr[0]=%drn",testarr[0]);
printf ("write OK !!!rn");
HAL_Delay(5);
}
else
printf ("write false !!!rn");
// HAL_I2C_Mem_Write(&hi2c1,EEPROM_I2C_ADDRESS,60,I2C_MEMADD_SIZE_8BIT,&testarr[1],1,3000);//&hi2c1³öÎÊÌ⣬ÁíÍâÒ²²»ÊÇ0xa1
if( HAL_I2C_Mem_Read(&hi2c1,0xa1,60,I2C_MEMADD_SIZE_8BIT,&testarr[1],1,3000)==HAL_OK)//Ìî0xa1,»òÕß0xa0¶¼¿ÉÒÔ
{
printf ("Read OK !!!rn");
printf (" testarr[1]=%drn",testarr[1]);
}
else
printf ("Read false !!!rn");
}
HAL_Delay(500);
testarr[0]+=3;
if(HAL_I2C_Mem_Write(&hi2c1,0xa0,60,I2C_MEMADD_SIZE_8BIT,&testarr[0],1,3000)==HAL_OK)
{
printf (" testarr[0]=%drn",testarr[0]);
printf ("write OK !!!rn");
HAL_Delay(5);
}
else
printf ("write false !!!rn");
// HAL_I2C_Mem_Write(&hi2c1,EEPROM_I2C_ADDRESS,60,I2C_MEMADD_SIZE_8BIT,&testarr[1],1,3000);//&hi2c1³öÎÊÌ⣬ÁíÍâÒ²²»ÊÇ0xa1
if( HAL_I2C_Mem_Read(&hi2c1,0xa1,60,I2C_MEMADD_SIZE_8BIT,&testarr[1],1,3000)==HAL_OK)//Ìî0xa1,»òÕß0xa0¶¼¿ÉÒÔ
{
printf ("Read OK !!!rn");
printf (" testarr[1]=%drn",testarr[1]);
}
else
printf ("Read false !!!rn");
}
1,这个问题搞了几天了,最后是借鉴硬石科技写的底层EEPROM,自己的一直通不了,很郁闷。
#ifndef __I2C_EEPROM_H__
#define __I2C_EEPROM_H__
/* °üº¬Í·Îļþ ----------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
/* ÀàÐͶ¨Òå ------------------------------------------------------------------*/
/* ºê¶¨Òå --------------------------------------------------------------------*/
#define I2C_OWN_ADDRESS 0x0A // stm32±¾»úI2CµØÖ·
#define I2C_SPEEDCLOCK 400000 // I2CͨÐÅËÙÂÊ(×î´óΪ400K)
#define I2C_DUTYCYCLE I2C_DUTYCYCLE_2 // I2CÕ¼¿Õ±Èģʽ£º1/2
#define EEPROM_I2Cx I2C1
#define EEPROM_I2C_RCC_CLK_ENABLE() __HAL_RCC_I2C1_CLK_ENABLE()
#define EEPROM_I2C_RCC_CLK_DISABLE() __HAL_RCC_I2C1_CLK_DISABLE()
#define EEPROM_I2C_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define EEPROM_I2C_GPIO_CLK_DISABLE() __HAL_RCC_GPIOB_CLK_DISABLE()
#define EEPROM_I2C_GPIO_PORT GPIOB
#define EEPROM_I2C_SCL_PIN GPIO_PIN_6
#define EEPROM_I2C_SDA_PIN GPIO_PIN_7
/*
* EEPROM 2kb = 2048bit = 2048/8 B = 256 B
* 32 pages of 8 bytes each
*
* Device Address
* 1 0 1 0 A2 A1 A0 R/W
* 1 0 1 0 0 0 0 0 = 0XA0
* 1 0 1 0 0 0 0 1 = 0XA1
*/
/* EEPROM Addresses defines */
#define EEPROM_I2C_ADDRESS 0xA0
/* À©Õ¹±äÁ¿ ------------------------------------------------------------------*/
extern I2C_HandleTypeDef hi2c_eeprom;
/* º¯ÊýÉùÃ÷ ------------------------------------------------------------------*/
void MX_I2C_EEPROM_Init(void);
void I2C_EEPROM_WriteData(uint16_t Addr, uint8_t Reg, uint8_t Value);
HAL_StatusTypeDef I2C_EEPROM_WriteBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
HAL_StatusTypeDef I2C_EEPROM_WriteAnyLength(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
uint8_t I2C_EEPROM_ReadData(uint16_t Addr, uint8_t Reg);
HAL_StatusTypeDef I2C_EEPROM_ReadBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
HAL_StatusTypeDef I2C_EEPROM_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
#endif /* __I2C_EEPROM_H__ */
/******************* (C) COPYRIGHT 2015-2020 ӲʯǶÈëʽ¿ª·¢ÍÅ¶Ó *****END OF FILE****/
/**
******************************************************************************
* ÎļþÃû³Ì: bsp_EEPROM.c
* ×÷ Õß: ӲʯǶÈëʽ¿ª·¢ÍŶÓ
* °æ ±¾: V1.0
* ±àдÈÕÆÚ: 2015-10-04
* ¹¦ ÄÜ: °åÔØEEPROM£¨AT24C02£©µ×²ãÇý¶¯³ÌÐò
******************************************************************************
* ˵Ã÷£º
* ±¾Àý³ÌÅäÌ×Ӳʯstm32¿ª·¢°åYS-F1ProʹÓá£
*
* ÌÔ±¦£º
* ÂÛ̳£ºhttp://www.ing10bbs.com
* °æȨ¹éӲʯǶÈëʽ¿ª·¢ÍŶÓËùÓУ¬ÇëÎðÉÌÓá£
******************************************************************************
*/
/* °üº¬Í·Îļþ ----------------------------------------------------------------*/
#include "bsp_EEPROM.h"
/* ˽ÓÐÀàÐͶ¨Òå --------------------------------------------------------------*/
/* ˽Óк궨Òå ----------------------------------------------------------------*/
#define EVAL_I2Cx_TIMEOUT_MAX 3000
/* ˽ÓбäÁ¿ ------------------------------------------------------------------*/
I2C_HandleTypeDef hi2c_eeprom;
uint32_t I2cxTimeout = EVAL_I2Cx_TIMEOUT_MAX;
/* À©Õ¹±äÁ¿ ------------------------------------------------------------------*/
/* ˽Óк¯ÊýÔÐÎ --------------------------------------------------------------*/
/* º¯ÊýÌå --------------------------------------------------------------------*/
/**
* º¯Êý¹¦ÄÜ: I2CÍâÉè³õʼ»¯
* ÊäÈë²ÎÊý: ÎÞ
* ·µ »Ø Öµ: ÎÞ
* ˵ Ã÷£ºÎÞ
*/
void MX_I2C_EEPROM_Init(void)
{
hi2c_eeprom.Instance = EEPROM_I2Cx;
hi2c_eeprom.Init.ClockSpeed = I2C_SPEEDCLOCK;
hi2c_eeprom.Init.DutyCycle = I2C_DUTYCYCLE;
hi2c_eeprom.Init.OwnAddress1 = 0;
hi2c_eeprom.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c_eeprom.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c_eeprom.Init.OwnAddress2 = 0;
hi2c_eeprom.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c_eeprom.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&hi2c_eeprom);
}
/**
* º¯Êý¹¦ÄÜ: I2CÍâÉèÓ²¼þ³õʼ»¯ÅäÖÃ
* ÊäÈë²ÎÊý: hi2c£ºI2C¾ä±úÀàÐÍÖ¸Õë
* ·µ »Ø Öµ: ÎÞ
* ˵ Ã÷: ¸Ãº¯Êý±»HAL¿âÄÚ²¿µ÷ÓÃ
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hi2c->Instance==EEPROM_I2Cx)
{
/* ʹÄÜÍâÉèʱÖÓ */
EEPROM_I2C_RCC_CLK_ENABLE();
EEPROM_I2C_GPIO_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = EEPROM_I2C_SCL_PIN|EEPROM_I2C_SDA_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(EEPROM_I2C_GPIO_PORT, &GPIO_InitStruct);
}
}
/**
* º¯Êý¹¦ÄÜ: I2CÍâÉèÓ²¼þ·´³õʼ»¯ÅäÖÃ
* ÊäÈë²ÎÊý: hi2c£ºI2C¾ä±úÀàÐÍÖ¸Õë
* ·µ »Ø Öµ: ÎÞ
* ˵ Ã÷: ¸Ãº¯Êý±»HAL¿âÄÚ²¿µ÷ÓÃ
*/
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
{
if(hi2c->Instance==EEPROM_I2Cx)
{
/* ½ûÓÃÍâÉèʱÖÓ */
EEPROM_I2C_GPIO_CLK_DISABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(EEPROM_I2C_GPIO_PORT, EEPROM_I2C_SCL_PIN|EEPROM_I2C_SDA_PIN);
}
}
/**
* º¯Êý¹¦ÄÜ: I2CͨÐÅ´íÎó´¦Àíº¯Êý
* ÊäÈë²ÎÊý: ÎÞ
* ·µ »Ø Öµ: ÎÞ
* ˵ Ã÷: Ò»°ãÔÚI2CͨÐų¬Ê±Ê±µ÷Óøú¯Êý
*/
static void I2C_EEPROM_Error (void)
{
/* ·´³õʼ»¯I2CͨÐÅ×ÜÏß */
HAL_I2C_DeInit(&hi2c_eeprom);
/* ÖØгõʼ»¯I2CͨÐÅ×ÜÏß*/
MX_I2C_EEPROM_Init();
printf("EEPROM I2CͨÐų¬Ê±£¡£¡£¡ ÖØÐÂÆô¶¯I2C...n");
}
/**
* º¯Êý¹¦ÄÜ: ͨ¹ýI2CдÈëÒ»¸öÖµµ½Ö¸¶¨¼Ä´æÆ÷ÄÚ
* ÊäÈë²ÎÊý: Addr£ºI2CÉ豸µØÖ·
* Reg£ºÄ¿±ê¼Ä´æÆ÷
* Value£ºÖµ
* ·µ »Ø Öµ: ÎÞ
* ˵ Ã÷: ÎÞ
*/
void I2C_EEPROM_WriteData(uint16_t Addr, uint8_t Reg, uint8_t Value)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(&hi2c_eeprom, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
/* ¼ì²âI2CͨÐÅ״̬ */
if(status != HAL_OK)
{
/* µ÷ÓÃI2CͨÐÅ´íÎó´¦Àíº¯Êý */
I2C_EEPROM_Error();
}
}
/**
* º¯Êý¹¦ÄÜ: ͨ¹ýI2CдÈëÒ»¶ÎÊý¾Ýµ½Ö¸¶¨¼Ä´æÆ÷ÄÚ
上一篇:STM32学习记录之cubemx配置IIC+逻辑分析仪的学习
下一篇: STM32CubeMX GPIO模拟I2C读写M24C64
推荐阅读最新更新时间:2024-11-08 21:35
设计资源 培训 开发板 精华推荐
- 基于Teseo-LIV3F的GNSS评估板,用于SPC5微控制器
- ADL5536-EVALZ,基于 ADL5536 20 MHz 至 1 GHz IF 增益模块的评估板
- 电子协会立创校园训练计划之USB-HUB
- AM2M-1205SH30-NZ 5 Vout、2W 单路输出 DC-DC 转换器的典型应用
- 具有 12V VDD 输入的 LTC2945HUD 3.3V 输入电源监视器的典型应用
- 降压-单转双电源板
- LTC3448、600mA 开关转换器通过在轻负载时自动切换到线性稳压器来降低噪声
- MIPI适配器
- 用于 Intel 486TM DX4TM Overdrive 微处理器的 LT1585CM-3.3 3.3V/4.6A 低压差稳压器的典型应用
- MIC29201-12WU 400mA低压降稳压器典型应用