赞
踩
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 < countToWrite; i++ ) { Debug = FLASH_ProgramHalfWord( startAddress, writeData[i] ); #ifdef DEBUG if ( Debug != FLASH_COMPLETE ) printf( "扇区写失败\n\n" ); #endif startAddress = startAddress + 2; } FLASH_Lock(); /*上锁写保护 */ } u16 STMFLASH_ReadHalfWord( u32 faddr ) { return(*(vu16 *) faddr); } /* 16位数据读取 */ void FLASH_ReadMoreData( u32 ReadAddr, u16 *pBuffer, u16 NumToRead ) { u16 i; for ( i = 0; i < NumToRead; i++ ) { pBuffer[i] = STMFLASH_ReadHalfWord( ReadAddr ); /* 读取2个字节. */ ReadAddr += 2; /* 偏移2个字节. */ } } void write_to_flash( void ) { u16 shuju[102]; u16 c = 0; memset( shuju, 0, sizeof(shuju) ); for ( c = 0; c < 50; c++ ) { shuju[c] = people_id[c]; /*复制 */ } for ( c = 0; c < 50; c++ ) { shuju[50 + c] = people_clock_t[c]; /*复制 */ } shuju[100] = people_num; FLASH_WriteData( 0x08004000, shuju, 101 ); /* 写入 */ } void read_from_flash( void ) { u16 shuju[102]; u16 c = 0; memset( shuju, 0, sizeof(shuju) ); FLASH_ReadMoreData( 0x08004000, shuju, 101 ); for ( c = 0; c < 50; c++ ) { people_id[c] = shuju[c]; /* 读取 */ } for ( c = 0; c < 50; c++ ) { people_clock_t[c] = shuju[50 + c]; /*复制 */ } people_num = shuju[100]; }
main
void main(void)
{
write_to_flash();
read_from_flash();
while(1)
{
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。