⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM8示例程序
💻 C
字号:
/**
  ******************************************************************************
  * @file FLASH_ByteReadWriteOperation\main.c
  * @brief This file contains the main function for FLASH byte read write operation 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>&copy; COPYRIGHT 2008 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8s_lib.h"

/**
  * @addtogroup FLASH_ByteReadWriteOperation
  * @{
  */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#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)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void Delay(u16 nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}

/* Public functions ----------------------------------------------------------*/

/**
  * @brief How to Read / Write / Erase one Byte on FLASH memory.
  * @par Examples description
  * - Read one byte at address 0x40A5
  * - Write its coplement value at adress + 1
  * - Check programed value
  * - Erase 2 byte (address 40A5 & 40A6)
  * - Check the 2 bytes value is 0x00.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * - FLASH_SetProgrammingTime()
  * - FLASH_ReadByte()
  * - FLASH_ProgramByte()
  * - FLASH_EraseByte()
  */
void main(void)
{

  u8 val, val_comp = 0;
  u32 add = 0;
  
  /* Initialize I/Os in Output Mode */
  GPIO_Init(LEDS_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN,GPIO_MODE_OUT_PP_LOW_FAST);
  /* Define FLASH programming time */
  FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
	
	    /* Unlock Program & Data memories */
  FLASH_Unlock(FLASH_MEMTYPE_DATA);
	FLASH_Unlock(FLASH_MEMTYPE_PROG);

  /* Read a byte at a specified address */
  add = 0x40A5;
  val = FLASH_ReadByte(add);

  /* Program complement value (of previous read byte) at previous address + 1 */
  val_comp = (u8)(~val);
  FLASH_ProgramByte((add + 1), val_comp);
	FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);

  /* Check program action */
  val = FLASH_ReadByte((add + 1));
  if (val != val_comp)
  {
    while (1) /* Error while programming complement value */
    {
      GPIO_WriteReverse(LEDS_PORT, LED1_PIN);
      Delay((u16)60000);
      Delay((u16)60000);
    }
  }

  /* Erase byte at a specified address & address + 1 */
  FLASH_EraseByte(add);
	FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);
  FLASH_EraseByte((add + 1));
	FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);
  /* Erase action */
  val = FLASH_ReadByte(add);
  val_comp = FLASH_ReadByte((add + 1));
  if ((val != 0x00) & (val_comp != 0x00))
  {
    while (1) /* Error while programming complement value */
    {
      GPIO_WriteReverse(LEDS_PORT, LED2_PIN);
      Delay((u16)60000);
      Delay((u16)60000);      
    }
  }

  /* Pass */
  while (1)
  {
    GPIO_WriteReverse(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN));
    Delay((u16)60000);
    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 + -