📄 main.c
字号:
/**
******************************************************************************
* @file I2C_EEPROMSequentialRead\main.c
* @brief This file contains the main function for I2C EEPROM Sequential Read example.
* @author STMicroelectronics - MCD Application Team
* @version V1.0.1
* @date 09/22/2008
******************************************************************************
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2008 STMicroelectronics</center></h2>
* @image html logo.bmp
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s_lib.h"
/* Evalboard I/Os configuration */
#define LEDS_PORT (GPIOH)
#define LED1_PIN (GPIO_PIN_3)
#define LED2_PIN (GPIO_PIN_2)
#define LED3_PIN (GPIO_PIN_1)
#define LED4_PIN (GPIO_PIN_0)
/**
* @addtogroup I2C_EEPROMSequentialRead
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Delay (u16 nCount);
/* Private functions ---------------------------------------------------------*/
void Delay(u16 nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
/* Public functions ----------------------------------------------------------*/
/**
* @brief How to make a Sequential Current Read with a M24C64 I2C EEPROM.
* @par Examples description
* - Read 8 bytes from current EEPROM internal address.
* - Look in the Buffer variable using Watch window to see read data.
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* - GPIO_Init
* - GPIO_WriteHigh
* - GPIO_WriteReverse
* - I2C_GenerateSTART
* - I2C_GenerateSTOP
* - I2C_DeInit
* - I2C_Send7bitAddress
* - I2C_CheckEvent
* - I2C_ClearFlag
* - I2C_ReceiveData
* - I2C_AcknowledgeConfig
*/
void main(void)
{
u8 Buffer[8];
u8 i;
/* Initialize I/Os in Output Mode */
GPIO_Init(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN), GPIO_MODE_OUT_PP_LOW_FAST);
/* Optional: put here as example only */
I2C_DeInit();
/* Initialize the I2C */
I2C_Init(400000, 0xA0, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 50);
/* All LEDs are ON per default */
GPIO_WriteHigh(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN));
/* Generate start & wait event detection */
I2C_GenerateSTART(ENABLE);
while (!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT));
/* Send slave Address in read direction & wait event */
I2C_Send7bitAddress(0xA0, I2C_DIRECTION_RX);
while (!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));
I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
/* Read seven first data */
for (i = 0; i < 7; i++)
{
/* waiting for byte from slave */
while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));
Buffer[i] = (u8)(I2C_ReceiveData());
}
/* Need to stop ACK for the last byte */
I2C_AcknowledgeConfig(I2C_ACK_NONE);
/* Read last byte */
while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));
Buffer[i] = (u8)(I2C_ReceiveData());
/* Send STOP Condition to end transmition*/
I2C_GenerateSTOP(ENABLE);
/* Toggle all LEDs when I2C communication is terminated */
while (1)
{
GPIO_WriteReverse(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN));
Delay((u16)60000);
}
}
/**
* @brief Reports the name of the source file and the source line number where
* the assert error has occurred.
* User can add his own implementation to report the file name and line number.
* ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
*/
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
/* Add your own code to manage an assert error */
/* Infinite loop */
while (1)
{
}
}
/**
* @}
*/
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -