Re: PIC16F18345 User ID修改

作者 Ryang 於 2018年07月12日 13:24:40
PIC16F1xxxx 的加強型中階 MCU 有心的 Table Read Flash 的功能,可以使用 FSR0H + FSR0L 來做索引定址到 32KW 的 Flash Memory.

至於要寫入到 NVM (Flash) 裡就只能用傳統方式來寫 Flash。這方法就要參考 Data Sheet 11.4 的說明。NVM 架構是採用 block 的架構,需先將欲寫入的 Block 先做 Erase 的動作後方可以寫入,且一次必須寫一個區塊。

建議如果要將 ID 放在 NVM 裡,初始資料可以用 const 的定義擺在 Flash 陣列裡,使用索引方式讀取 NVM 的資料。但要更改 NVM 裡的 ID 就只能依據 Data Sheet 的方式修改。

有關索引定址的方式可以參考 XC8 教育訓練課程,底下為參考程式:

#include <xc.h>


// ************ 設定 PIC16F1937 Configuration Bits  ******

__CONFIG  ( FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & BOREN_ON & CLKOUTEN_ON
           
& IESO_OFF & FCMEN_OFF & FCMEN_OFF );
__CONFIG ( PLLEN_ON & STVREN_OFF & BORV_HI & LVP_OFF);


const
unsigned char Lookup_Table[ ]  =
    {
0xA0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
    
18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
    
34,35,36,37,38,39,40,41,42,43,44,45,56,57,48,49,50,
    
51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
    
68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
    
84,85,86,87,88,89,80,91,92,93,94,95,96,97,98,99,100,
    
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
    
18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
    
34,35,36,37,38,39,40,41,42,43,44,45,56,57,48,49,50,
    
51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
    
68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
    
84,85,86,87,88,89,80,91,92,93,94,95,96,97,98,99,100,
    
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
    
18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
    
34,35,36,37,38,39,40,41,42,43,44,45,56,57,48,49,50,
    
51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
    
68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
    
84,85,86,87,88,89,80,91,92,93,94,95,96,97,98,99,100,
    
0xAA,0xAB};

unsigned char Input_Buffer[320] @0x2100;
const
unsigned char *near ROMPTR;
unsigned char *near RAMPTR ;           // PTR 指標位址設在 Common Memory
near unsigned int j ;            // 變數 j 放在 Common Memory

void main(void)
{
    
ROMPTR = Lookup_Table ;
    
RAMPTR = Input_Buffer ;
    for (
j=0; j<=302; j++) *RAMPTR++ = *ROMPTR++;

    
NOP( );
    while(
1);
}

來自: http://www.microchip.com.tw/newbb/viewtopic.php?forum=1&topic_id=22345&post_id=78375