找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2984|回复: 2
打印 上一主题 下一主题
收起左侧

PIC16F1455 Flash不能擦除和写入

[复制链接]
跳转到指定楼层
楼主
本帖最后由 Antecer 于 2016-1-10 11:49 编辑

以下是我用于测试的代码:
  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好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:102111 发表于 2016-1-10 21:03 | 只看该作者
等了一天了,还是没得到解答 = =
回复

使用道具 举报

板凳
ID:78835 发表于 2023-11-28 08:12 | 只看该作者
解决了吗?我也遇到了类似的问题,我用PIC16F1825,可以写入数据,也可以读出数据,就是不能擦除数据。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表