tica, sans-serif">以下是我用于测试的代码(表示新人比较穷,没分送):
- #include <xc.h>
- // CONFIG1
- #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
- #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
- #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
- #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
- #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
- #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
- #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
- #pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
- #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
- // CONFIG2
- #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
- #pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection Bit (NO CPU system divide)
- #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.)
- #pragma config PLLMULT = 3x // PLL Multipler Selection Bit (3x Output Frequency Selected)
- #pragma config PLLEN = ENABLED // PLL Enable Bit (3x or 4x PLL Enabled)
- #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
- #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
- #pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
- #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
- #define UINT8 unsigned char
- #define UINT16 unsigned int
- #define NV_MEM_SIZE 32
- extern const unsigned char NVMEM[NV_MEM_SIZE];
- #define NV_ADDRESS (0x2000U - NV_MEM_SIZE)
- const UINT8 NVMEM[NV_MEM_SIZE]@NV_ADDRESS = {
- 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
- 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
- 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
- 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
- };
- UINT8 GetData[NV_MEM_SIZE];
- void ReadFlash(UINT16 FlashAddr, UINT8 *Data, UINT8 DataLength) {
- PMADR = FlashAddr; //读取数据的首地址
- for (UINT8 i = 0; i < DataLength; i++) //循环读取连续数据
- {
- CFGS = 0;
- RD = 1;
- NOP();
- NOP();
- *Data = PMDATL; //取数据低8位存入数组
- PMADR++;
- Data++;
- }
- }
- void UnlockFlash(){
- PMCON2 = 0x55;
- PMCON2 = 0xAA;
- WR = 1;
- NOP();
- NOP();
- }
- void EraseFlash(UINT16 address){
- OSCCON = 0b11101100; //切换到1MHz x 3PLL;
- GIE = 0;
- PMCON1bits.CFGS = 0;
- PMADR = address;
- PMCON1bits.FREE = 1; //启用程序闪存擦除启用
- PMCON1bits.WREN = 1; //允许编程
- UnlockFlash(); //解锁序列
- PMCON1bits.WREN = 0; //禁止编程
- GIE = 1;
- }
- void main(void) {
- ReadFlash(NV_ADDRESS,GetData,NV_MEM_SIZE);
- EraseFlash(NV_ADDRESS);
- while (1);
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
那个CONFIG1和CONFIG2看自己需求选择是否启用,只要与flash读写相关的部分一定要一样的就行了。
给你附一个我写的
Flash读写优化总结版.X.rar 下载积分: 积分 -1 分
243.1 KB, 下载次数: 69, 下载积分: 积分 -1 分
PIC单片机Flash读写函数
老哥,我这个要求不能引用pic.h和xc.h的库,需要自己定义配置寄存器,能不能指导下需要怎么操作配置寄存器啊
pic.h 和 xc.h都不让引用,那你就引用对应型号的芯片头文件撒,比如PIC16F1455.h,如果这个都不让引用那就纯属脑残了。
一周热门 更多>