你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

stm32f107 使用内部FLASH

[复制链接]
wxSupremeZ 提问时间:2021-2-3 23:03 /
小弟从网上找的例程是F103的 我用的是F107VCT6,看了半天代码,自己又找了一个103做实验,103调通了,可是107失败了,程序没变,请问还有哪些地方需要更改一下,下面附上flash.c,二楼放MAIN.c


    //从指定地址开始读取多个数据
    void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead)
    {
      uint16_t dataIndex;
     for(dataIndex=0;dataIndex<countToRead;dataIndex++)
      {
       readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);
      }
    }
     
    //读取指定地址的半字(16位数据)
    uint16_t FLASH_ReadHalfWord(uint32_t address)
    {
      return*(__IO uint16_t*)address;
    }
     
    //读取指定地址的全字(32位数据)
    uint32_t FLASH_ReadWord(uint32_t address)
    {
      uint32_t temp1,temp2;
     temp1=*(__IO uint16_t*)address;
     temp2=*(__IO uint16_t*)(address+2);
      return(temp2<<16)+temp1;
    }
   
   
   
    //从指定地址开始写入多个数据
void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite)
{
if(startAddress<FLASH_BASE||((startAddress+countToWrite*2)>=(FLASH_BASE+1024*FLASH_SIZE)))
  {
   return;//非法地址
  }
FLASH_Unlock();         //解锁写保护
  uint32_t offsetAddress=startAddress-FLASH_BASE;               //计算去掉0X08000000后的实际偏移地址
  uint32_t sectorPosition=offsetAddress/SECTOR_SIZE;            //计算扇区地址,对于STM32F103VET6为0~255

  uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE;    //对应扇区的首地址

FLASH_ErasePage(sectorStartAddress);//擦除这个扇区

  uint16_t dataIndex;
for(dataIndex=0;dataIndex<countToWrite;dataIndex++)
  {
   FLASH_ProgramHalfWord(startAddress+dataIndex*2,writeData[dataIndex]);
  }

FLASH_Lock();//上锁写保护
}

收藏 评论2 发布时间:2021-2-3 23:03

举报

2个回答
wxSupremeZ 回答时间:2021-2-3 23:03:57
这是flash.c
    //从指定地址开始读取多个数据
    void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead)
    {
      uint16_t dataIndex;
     for(dataIndex=0;dataIndex<countToRead;dataIndex++)
      {
       readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);
      }
    }
     
    //读取指定地址的半字(16位数据)
    uint16_t FLASH_ReadHalfWord(uint32_t address)
    {
      return*(__IO uint16_t*)address;
    }
     
    //读取指定地址的全字(32位数据)
    uint32_t FLASH_ReadWord(uint32_t address)
    {
      uint32_t temp1,temp2;
     temp1=*(__IO uint16_t*)address;
     temp2=*(__IO uint16_t*)(address+2);
      return(temp2<<16)+temp1;
    }
   
   
   
    //从指定地址开始写入多个数据
void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite)
{
if(startAddress<FLASH_BASE||((startAddress+countToWrite*2)>=(FLASH_BASE+1024*FLASH_SIZE)))
  {
   return;//非法地址
  }
FLASH_Unlock();         //解锁写保护
  uint32_t offsetAddress=startAddress-FLASH_BASE;               //计算去掉0X08000000后的实际偏移地址
  uint32_t sectorPosition=offsetAddress/SECTOR_SIZE;            //计算扇区地址,对于STM32F103VET6为0~255

  uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE;    //对应扇区的首地址

FLASH_ErasePage(sectorStartAddress);//擦除这个扇区

  uint16_t dataIndex;
for(dataIndex=0;dataIndex<countToWrite;dataIndex++)
  {
   FLASH_ProgramHalfWord(startAddress+dataIndex*2,writeData[dataIndex]);
  }

FLASH_Lock();//上锁写保护
}
wxSupremeZ 回答时间:2021-2-3 23:05:42
int yeshu1=58;
int yeshu2=59;
uint16_t WriteToFlash1[]={1,2,3,4};
uint16_t WriteToFlash2[]={5,6,7,8};
uint16_t xianshi[5]={0,0,0,0,0};
uint16_t Flash1=0;
uint16_t Flash2=0;
uint16_t Flash3=0;
uint16_t Flash4=0;

int main(void)
{
   
   
    uart_init(115200);
    lcd12864_config();
    SPI_GPIO_2_Init();
    RS_485_TX_EN;
    Disp_logo();
    WriteIns(0x01);   
   
    FLASH_WriteMoreData(yeshu1*SECTOR_SIZE+USER_FLASH_START_ADDR,WriteToFlash1,4);
                     USART_SendData(UART5,'a');
                 while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束

    FLASH_WriteMoreData(yeshu2*SECTOR_SIZE+USER_FLASH_START_ADDR,WriteToFlash2,4);
                     USART_SendData(UART5,'b');
                 while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束

    FLASH_ReadMoreData(yeshu2*SECTOR_SIZE+USER_FLASH_START_ADDR,xianshi,4);
                     USART_SendData(UART5,'c');
                 while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束
   
   

   
    Flash1= FLASH_ReadHalfWord(yeshu1*SECTOR_SIZE+USER_FLASH_START_ADDR);
                     USART_SendData(UART5,Flash1);
                 while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束

    Flash2= FLASH_ReadHalfWord(yeshu1*SECTOR_SIZE+USER_FLASH_START_ADDR+2);
                     USART_SendData(UART5,'e');
                 while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束

    Flash3= FLASH_ReadHalfWord(yeshu1*SECTOR_SIZE+USER_FLASH_START_ADDR+4);
                     USART_SendData(UART5,'f');
                 while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束

    Flash4= FLASH_ReadHalfWord(yeshu1*SECTOR_SIZE+USER_FLASH_START_ADDR+6);
                     USART_SendData(UART5,'g');
                 while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束

    Disp_Digital(Flash1 ,0x80);
    Disp_Digital(Flash2 ,0x90);
    Disp_Digital(Flash3 ,0x88);
    Disp_Digital(Flash4 ,0x98);   

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版