PIC16F1455 Flash不能擦除和写入

2019-07-15 15:10发布

tica, sans-serif">以下是我用于测试的代码(表示新人比较穷,没分送):
  1. #include <xc.h>

  2. // CONFIG1
  3. #pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
  4. #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
  5. #pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
  6. #pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
  7. #pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
  8. #pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
  9. #pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
  10. #pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
  11. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

  12. // CONFIG2
  13. #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
  14. #pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection Bit (NO CPU system divide)
  15. #pragma config USBLSCLK = 48MHz // USB Low SPeed Clock Selection bit (System clock expects 48 MHz, FS/LS USB CLKENs divide-by is set to 8.)
  16. #pragma config PLLMULT = 3x     // PLL Multipler Selection Bit (3x Output Frequency Selected)
  17. #pragma config PLLEN = ENABLED  // PLL Enable Bit (3x or 4x PLL Enabled)
  18. #pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
  19. #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
  20. #pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
  21. #pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

  22. #define UINT8 unsigned char
  23. #define UINT16 unsigned int
  24. #define NV_MEM_SIZE 32
  25. extern const unsigned char NVMEM[NV_MEM_SIZE];
  26. #define NV_ADDRESS (0x2000U - NV_MEM_SIZE)
  27. const UINT8 NVMEM[NV_MEM_SIZE]@NV_ADDRESS = {
  28.     0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  29.     0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
  30.     0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  31.     0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  32. };
  33. UINT8 GetData[NV_MEM_SIZE];

  34. void ReadFlash(UINT16 FlashAddr, UINT8 *Data, UINT8 DataLength) {
  35.     PMADR = FlashAddr; //读取数据的首地址
  36.     for (UINT8 i = 0; i < DataLength; i++) //循环读取连续数据
  37.     {
  38.         CFGS = 0;
  39.         RD = 1;
  40.         NOP();
  41.         NOP();
  42.         *Data = PMDATL; //取数据低8位存入数组
  43.         PMADR++;
  44.         Data++;
  45.     }
  46. }
  47. void UnlockFlash(){
  48.     PMCON2 = 0x55;
  49.     PMCON2 = 0xAA;
  50.     WR = 1;
  51.     NOP();
  52.     NOP();
  53. }
  54. void EraseFlash(UINT16 address){
  55.     OSCCON = 0b11101100;        //切换到1MHz x 3PLL;
  56.     GIE = 0;
  57.     PMCON1bits.CFGS = 0;
  58.     PMADR = address;
  59.     PMCON1bits.FREE = 1;         //启用程序闪存擦除启用
  60.     PMCON1bits.WREN = 1;         //允许编程
  61.     UnlockFlash();              //解锁序列
  62.     PMCON1bits.WREN = 0;         //禁止编程
  63.     GIE = 1;
  64. }
  65. void main(void) {
  66.     ReadFlash(NV_ADDRESS,GetData,NV_MEM_SIZE);
  67.     EraseFlash(NV_ADDRESS);
  68.     while (1);
  69. }
复制代码
QQ截图20160110111812.jpg

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。