STM32F103C8T6 内部 FLASH读写


u16	people_id[50]={1,2,3} ;                               /* 假设公司最多50人 */
u16	people_clock_t[50] ;                         /* 每个人签到次数 */
u16	people_num = 2;                                         /* 人数 */



#define DEBUG
#define FLASH_SIZE    64          //所选MCU的FLASH容量大小(单位为K)
#if FLASH_SIZE<256
  #define SECTOR_SIZE           1024    //字节
#else
  #define SECTOR_SIZE           2048    //字节
#endif
FLASH_Status  Debug;
//Flash 写函数 以8 bit 方式写入
void FLASH_WriteData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite)
{
	uint16_t i;
	uint32_t  offsetAddress;  //偏移地址
	uint32_t  sectorPosition; //扇区位置
  uint32_t  sectorStartAddress;
 if(startAddress < FLASH_BASE||((startAddress+countToWrite)>=(FLASH_BASE+1024*FLASH_SIZE)))
  {
   return  ;//非法地址
  }
  //解锁写保护
   FLASH_Unlock();  
   
  //计算去掉0X08000000后的实际偏移地址
   offsetAddress = startAddress-FLASH_BASE;  
  //计算扇区地址
   sectorPosition = offsetAddress/SECTOR_SIZE;   
	//对应扇区的首地址
   sectorStartAddress=sectorPosition * SECTOR_SIZE + FLASH_BASE; 
	
	 FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

	//擦除这个扇区
   Debug=FLASH_ErasePage(sectorStartAddress);
#ifdef DEBUG	
	 if(Debug != FLASH_COMPLETE)
	  printf("扇区擦除失败\n\n");
#endif
   for(i=0;i

main

void main(void)
{
     write_to_flash();
	read_from_flash();
    while(1)
    {

    }
}

你可能感兴趣的:(单片机)