写入 PIC 上的 EEPROM

发布于 2024-08-29 15:31:51 字数 1231 浏览 11 评论 0原文

这里有PIC单片机编程器吗?

我正在学习一些使用 pickit2 和它附带的 16F690 芯片的 PIC 微控制器编程。我目前正在尝试各种设施。如果我在 MPLAB 中设置 EEPROM vaklue,我可以在代码中成功读取 EEPROM 中的一个字节,但我似乎无法使用 PIC 本身修改该值。根本没有发生任何事情,我也不会读回修改后的值,我总是得到原始值,这对我来说意味着写入不起作用?

这是我该部分的代码,我错过了什么吗?我知道我做了很多不必要的银行切换,我添加了其中的大部分,以确保在错误的银行上不会出现问题。

        ; ------------------------------------------------------
        ; Now SET the EEPROM location ZERO to 0x08
        ; ------------------------------------------------------

        BANKSEL EEADR
        CLRF    EEADR           ; Set EE Address to zero

        BANKSEL EEDAT
        MOVLW   0x08            ; Store the value 0x08 in the EEPROM
        MOVWF   EEDAT

        BANKSEL EECON1
        BSF     EECON1, WREN    ; Enable writes to the EEPROM

        BANKSEL EECON2
        MOVLW   0x55            ; Do the thing we have to do so
        MOVWF   EECON2          ; that writes can work
        MOVLW   0xAA
        MOVWF   EECON2

        BANKSEL EECON1      
        BSF     EECON1, WR      ; And finally perform the write

WAIT
        BTFSC   EECON1, WR      ; Wait for write to finish
        GOTO    WAIT

        BANKSEL PORTC           ; Just to make sure we are on the right bank

Are there any PIC microcontroller programmers here?

I'm learning some PIC microcontroller programming using a pickit2 and the 16F690 chip that came with it. I'm working through trying out the various facilities at the moment. I can sucessfully read a byte from the EEPROM in code if I set the EEPROM vaklue in MPLAB but I don't seem to be able to modify the value using the PIC itsself. Simply nothing happens and I don't read back the modified value, I always get the original which implies to me that the write isn't working?

This is my code for that section, am I missing something? I know I'm doing a lot of unnecessary bank switches, I added most of them to ensure that being on the wrong bank wasn't the issue.

        ; ------------------------------------------------------
        ; Now SET the EEPROM location ZERO to 0x08
        ; ------------------------------------------------------

        BANKSEL EEADR
        CLRF    EEADR           ; Set EE Address to zero

        BANKSEL EEDAT
        MOVLW   0x08            ; Store the value 0x08 in the EEPROM
        MOVWF   EEDAT

        BANKSEL EECON1
        BSF     EECON1, WREN    ; Enable writes to the EEPROM

        BANKSEL EECON2
        MOVLW   0x55            ; Do the thing we have to do so
        MOVWF   EECON2          ; that writes can work
        MOVLW   0xAA
        MOVWF   EECON2

        BANKSEL EECON1      
        BSF     EECON1, WR      ; And finally perform the write

WAIT
        BTFSC   EECON1, WR      ; Wait for write to finish
        GOTO    WAIT

        BANKSEL PORTC           ; Just to make sure we are on the right bank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2024-09-05 15:31:51

16F690 数据表的第 122 页上,详细介绍了写入EEPROM:

BANKSEL EEADR ;
MOVF DATA_EE_ADDR,W;
MOVWF EEADR ;要写入的数据存储器地址
MOVF DATA_EE_DATA,W;
MOVWF EEDAT ;要写入的数据存储器值
银行 EECON1 ;
BCF EECON1, EEPGD ;指向数据存储器
BSF EECON1, WREN ;启用写入
BCF INTCON、GIE ;禁用 INT。
BTFSC INTCON、GIE;参见 AN576
转到 $-2
;开始所需的序列
MOVLW 55 小时;       
MOVWF EECON2 ;写入 55h
MOVLW AAh;
MOVWF EECON2 ;写入 AAh
BSF EECON1, WR ;设置 WR 位开始写入
BSF INTCON、GIE ;启用 INT。
SLEEP ;等待中断信号写入完成
;结束所需的序列
BCF EECON1, WREN ;禁用写入
BANKSEL 0x00 ;组 0

我注意到您特别遗漏了这一行:

 BCF     EECON1, EEPGD ;Point to DATA memory

如果始终设置 EEPGD,那么您将尝试写入程序内存(也称为覆盖闪存程序内存),除非您已经离开,否则它应该总是失败不遗余力地专门启用它。

除此之外,据我从阅读您的代码来看,其他一切看起来都很好。您可以轮询EECON1.WR而不是设置中断。这比让设备进入睡眠状态会消耗更多的电量,但当然,您应该一次只担心一件事。

On page 122 of the 16F690 datasheet, it details the proper way to write to EEPROM:

BANKSEL EEADR                   ;
MOVF    DATA_EE_ADDR, W;
MOVWF   EEADR          ;Data Memory Address to write
MOVF    DATA_EE_DATA, W;
MOVWF   EEDAT                   ;Data Memory Value to write
BANKSEL EECON1                  ;
BCF     EECON1, EEPGD ;Point to DATA memory
BSF     EECON1, WREN   ;Enable writes
BCF     INTCON, GIE             ;Disable INTs.
BTFSC   INTCON, GIE             ;SEE AN576
GOTO    $-2
; BEGIN REQUIRED SEQUENCE
MOVLW   55h            ;       
MOVWF   EECON2         ;Write 55h
MOVLW   AAh                     ;
MOVWF   EECON2                  ;Write AAh
BSF     EECON1, WR              ;Set WR bit to begin write
BSF     INTCON, GIE             ;Enable INTs.
SLEEP                  ;Wait for interrupt to signal write complete
; END REQUIRED SEQUENCE
BCF     EECON1, WREN   ;Disable writes
BANKSEL 0x00           ;Bank 0

I noticed that you are specifically missing this line:

 BCF     EECON1, EEPGD ;Point to DATA memory

If EEPGD is always set, then you'll try to write to program memory (aka overwrite the flash program memory) which should always fail unless you've gone out of your way to specifically enable that.

Aside from that, as far as I can tell from reading your code, everything else looks fine. It's okay that you're polling EECON1.WR instead of setting an interrupt. It will cost you more power than putting the device to sleep, but of course you should just worry about one thing at a time.

梦纸 2024-09-05 15:31:51

如果您使用高科技编译器,写入 EEPROM 就很简单
#include
然后在 main 中,
EEPROM_WRITE(0, 0x00);
或者真的;

EEPROM_WRITE(location to write to, value to write to it);

我实际上还没有足够的勇气在汇编中编写我的PIC。

If you use the high tech compiler, writing to EEPROM is as simple as
#include <pic.h>
and then in main,
EEPROM_WRITE(0, 0x00);
or really;

EEPROM_WRITE(location to write to, value to write to it);

I actually haven't been brave enough to write my PIC in assembly.

止于盛夏 2024-09-05 15:31:51

xc8编译器的方式:

#include <xc.h>

.....
value = eeprom_read(address);
eeprom_write(address, data);

The way for the xc8 compiler:

#include <xc.h>

.....
value = eeprom_read(address);
eeprom_write(address, data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文