如何编写使用内联汇编到闪存? [英] How to write to flash memory using inline assembly?

查看:233
本文介绍了如何编写使用内联汇编到闪存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的MPLAB C18编译器与 PIC18F87J11 ,我试图保存一些值到闪存,采用内嵌汇编,这是C和汇编code的组合。

I am using MPLAB C18 compiler with PIC18F87J11 and I am trying to save some values to flash memory, using inline assembly, which is a combination of C and assembly code.

它看起来像我能够尽快编写和正确读取闪存,但我动力循环我的照片,然后试图读什么,我从一个特定的地址保存previous,我没有得到相同的值。我节省了×09 0xB22A 地址。就像我说的,如果我保存该值然后立即读取它,一切都正确,但在重新设置PIC我得到 0×00

It looks like I can write and read to flash memory correctly, but as soon as I power cycle my PIC and then attempt to read what I previous saved from a specific address, I don't get the same value. I am saving 0x09 to 0xB22A address. Like I said, if I save the value then read it immediately, everything comes up correctly, but upon resetting the PIC I get 0x00.

我不是永久保存到闪存或什么是真正发生在这里?

这是我的code:

擦除存储器行

_asm 
MOVLW 0x00
MOVWF TBLPTRU,BANKED
MOVLW 0xB2
MOVWF TBLPTRH,BANKED
MOVLW 0x2A
MOVWF TBLPTRL,BANKED  
_endasm
EECON1bits.FREE = 1; 
INTCONbits.GIE = 0;

_asm 
MOVLW 0x55
MOVWF EECON2,BANKED
MOVLW 0xAA
MOVWF EECON2,BANKED
_endasm
EECON1bits.WR = 1;
INTCONbits.GIE = 1;

写入到闪存

_asm 
MOVLW    0x00
MOVWF    TBLPTRU,BANKED
MOVLW    0xB2
MOVWF    TBLPTRH,BANKED
MOVLW    0x2A
MOVWF    TBLPTRL,BANKED
MOVLW    0x09
MOVWF    TABLAT,BANKED
TBLWTPOSTINC
MOVLW    0x09
MOVWF    TABLAT,BANKED
TBLWT
_endasm

EECON1bits.WPROG = 1;
EECON1bits.WREN = 1;
INTCONbits.GIE = 0; 
_asm 
MOVLW 0x55
MOVWF EECON2,BANKED  
MOVLW 0xAA
MOVWF EECON2,BANKED 
 _endasm
EECON1bits.WR = 1;
INTCONbits.GIE = 1;
EECON1bits.WPROG = 0; 
EECON1bits.WREN = 0;

从闪存读取memeory

_asm  
 MOVLW 0x00
 MOVWF TBLPTRU,BANKED  
 MOVLW 0xB2 
 MOVWF TBLPTRH,BANKED
 MOVLW 0x2A 
 MOVWF TBLPTRL,BANKED 
READ_WORD: 
 TBLRDPOSTINC  
 MOVF TABLAT, 0,BANKED 
 MOVWF WORD_EVEN,ACCESS
 TBLRDPOSTINC  
 MOVF TABLAT, 0,BANKED
 MOVWF WORD_ODD,ACCESS
 _endasm

printf("\r\n");
PrintChar(WORD_EVEN);
printf("\r\n");
PrintChar(WORD_ODD);

原来的code可以在数据表找到第6章,但是记住,我不得不修改它一点点,以便能够与C使用它,我不知道有什么区别之间的访问 BANKED 我怀疑他们可能会涉及到的问题。

The original code can be found in the datasheet on chapter 6, but remember I had to modify it a little bit to be able to use it with C. I am not sure what the difference is between ACCESS and BANKED as I suspect they might be related to the problem.

推荐答案

它看起来像你的解决方案到已经:问题是在使用 BANKED

It looks like you arrived at the solution already: the problem is in using BANKED.

EECON2 之类的都驻留在银行15.访问它们是使用 ACCESS <最快的方式特殊功能寄存器(SFR) / code>,而忽略了银行寄存器计算的地址。

EECON2 and the like are Special Function Registers (SFRs) that reside in bank 15. The fastest way to access them is to use ACCESS, which ignores the bank register in computing the address.

看起来好像是你正在使用汇编语言, ACCESS 0手段和 BANKED 1,意味着一个这些值要求在 MOVWF 指令。数据表使用其示例中的实际数字,而不是符号常量。

It looks like in the assembly language you are using, ACCESS means 0 and BANKED means 1. One of these values is required in the movwf instruction. The datasheet uses the actual numbers in its examples, instead of symbolic constants.

数据表也说明RAM是如何开户。

The datasheet also explains the how the RAM is banked.

基本上,有作为保持4位再presenting银行0到15这个数字得到prepended来给指令中的8位数字,让所有的12位寄存器。这允许更快的执行速度。要存储在正确的位置,你需要这个寄存器首先设置为正确的银行。

Basically, there is as register that holds 4 bits representing banks 0 to 15. This number gets prepended to the 8 bit number given to the instruction, to give all 12 bits. This allows faster execution. To store in the correct location, you need to set this register to the correct bank first.

在两家银行,0和15,被映射到通用寄存器和特殊功能寄存器为好。由于这些是最常用的,快速的方式来得到他们是使用一个标志指示忽略计算地址银行登记,并直接进入你想要的GPR或SFR。

Two of the banks, 0 and 15, are mapped to the General Purpose registers and Special Function Registers as well. Since these are used most often, the fast way to get to them is to use a flag in the instruction to ignore the bank register in computing the address, and go straight to the GPR or SFR you want.

您的问题是,你是设置标志来使用的内存不同的银行从寄存器居住的地方。

Your problem is that you were setting the flag to use a different bank of memory from where the register reside.

这篇关于如何编写使用内联汇编到闪存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆