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

📄 msp430f169flash.c

📁 msp430f169的flash初始化
💻 C
字号:
/**************************************************************
*  初始化 Flash
**************************************************************/
void init_flash(void)
{
    uchar temp,
          temp2;
    int flash_buf[40];
    _DINT();
    FCTL2 = FWKEY + FSSEL0 + FN0;               // 设置时钟频率为ACLK
    
    read_flash((uint *)FSEG_A,(int *)flash_buf,1);
    if(flash_buf[0] == 1)
    {
       read_flash((uint *)(FSEG_A + 1),(uint *)flash_buf,32);//0x1082开始
       
       for(temp = 0;temp < 4;temp++)
       {
         for(temp2 = 0;temp2 < 2;temp2++)
         {
           amp_bias_flash[temp][temp2] = flash_buf[temp * 2 + temp2 ];
         }
       }
       
       for(temp = 0;temp < 8;temp++)
       {
         pw_type_flash[temp] = flash_buf[24 + temp];
       }
    
     }
     else
     {
       write_flash_sys();
     }
   // _EINT();
}



/**************************************************************
*
*   Erase Flash
*   intput:
*          ptr: address that sepecify a Segment
*
**************************************************************/
void erase_flash(uint *address)
{
  _DINT();
  
  while ((FCTL3 & BUSY)!=0)
  {
    ;
  }
  FCTL3 = FWKEY;        		/* Lock = 0 */
  FCTL1 = FWKEY+ERASE;        		/* Erase = 1 */
  *((uint *)address) = 0;     		/* Erase flash segment */
  //_EINT();	//因为擦除时间较长,为不影响主程序时钟中断
  while ((FCTL3 & BUSY)!=0)
  {
    ;
  }
  
 // _EINT();
}

/**************************************************************
*   读Flash
*    输入参数说明:   
*           addrress:     读地址
*              data:      用以存储读取内容的缓存区地址
*              num:       要读取的字节数
***************************************************************/ 
void read_flash(uint *address,int *data,uchar num)
{
    uchar temp;
    _DINT();
    
    while((FCTL3 & 0x01) == 0x0001)
    {
      ;
    }// 等待flash空闲
    
    if(num < 127)
      for(temp = 0;temp < num;temp++)
      {
        *data = *address;//读数据,读数据时,flash地址自动加 1 
        address++;
        data++;//接收缓冲区地址加 1 
      }
    
    while((FCTL3 & 0x01) == 0x0001)
    {
      ;
    }// 等待flash空闲
    FCTL3  = FWKEY + LOCK;                      // Lock   
   //_EINT();
}
/**************************************************************
*
*  函数功能:写数据到Flash
*  输入参数:
*             address:    地址
*             data:     要写数据的首地址
*             num:     写入的字节数
***************************************************************/
void write_flash(uint *address,int *data,uchar num)
{
    uchar temp;
    _DINT();
    
    while((FCTL3 & 0x01) == 0x0001)
    {
      ;
    }// 等待Flash空闲
    
    FCTL3  = FWKEY;                             // 清除“LOCK”标识
    FCTL1  = FWKEY + WRT;                       // 准备写     
    if(num < 127)
      for(temp = 0;temp < num;temp++)
      {
        *address = *data;
        data++;   //发送缓冲地址+1
        address++;//flash地址+1
        while(!(FCTL3 & WAIT))
        {
          ;
        }//如果处于忙状态,则等待 ,若用软件仿真,去掉  
      }
    
    while((FCTL3 & 0x01) == 0x0001)
    {
      ;
    }// 等待Flash空闲
    FCTL3  = FWKEY + LOCK;                      // Lock   
    
   // _EINT();
}

void write_flash_sys(void)
{
  uchar temp,
        temp2;
  _DINT();
  int flash_buf[40];
  flash_buf[0] = 1; 
  for(temp = 0;temp < 4;temp++)
  {
     for(temp2 = 0;temp2 < 2;temp2++)
     {
       flash_buf[temp * 2 +temp2 + 1] = amp_bias_flash[temp][temp2];
     }
  } 
  
  for(temp = 0;temp < 8;temp++)
  {
    flash_buf[24 + temp] = pw_type_flash[temp];
  }
  
  erase_flash((uint *)FSEG_A);
  write_flash((uint *)FSEG_A,(int *)flash_buf,33);
  
  //_EINT();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -