翻译总成C [写入内存与PIC18] [英] Translate assembly to C [Writing to Memory with PIC18]

查看:201
本文介绍了翻译总成C [写入内存与PIC18]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写在我的PIC18F87J11闪存,但我理解装配一个问题。我的PIC数据表只有装配和我使用的C编译器。我想知道,如果有人可以帮我翻译code的C语言这一部分。这code可以发现这里,7.5节。

I am trying to write to flash memory on my PIC18F87J11 but I have a problem understanding assembly. The datasheet for my PIC has only assembly and I am using C compiler. I was wondering if someone can help me translate this portion of code to C language. This code can be found here, Section 7.5.

MOVLW CODE_ADDR_UPPER ; Load TBLPTR with the base address
MOVWF TBLPTRU
MOVLW CODE_ADDR_HIGH
MOVWF TBLPTRH
MOVLW CODE_ADDR_LOW
MOVWF TBLPTRL
MOVLW DATA0
MOVWF TABLAT
TBLWT*+
MOVLW DATA1
MOVWF TABLAT
TBLWT*
PROGRAM_MEMORY
BSF EECON1, WPROG ; enable single word write
BSF EECON1, WREN ; enable write to memory
BCF INTCON, GIE ; disable interrupts
MOVLW H'55'
MOVWF EECON2 ; write H'55'
MOVLW H'AA'
MOVWF EECON2 ; write H'AA'
BSF EECON1, WR ; start program (CPU stall)
BSF INTCON, GIE ; re-enable interrupts
BCF EECON1, WPROG ; disable single word write
BCF EECON1, WREN ; disable write to memory

我知道这是不是一个典型的问题要问,但如果我能得到一些帮助,这将是真棒。

I know this is not a typical question to ask, but if I can receive some help it would be awesome.

推荐答案

这可能是最容易打入几个步骤这一点。我会做的第一件事就是看看需要看装配前要完成什么一般概述。可以在参考 7.5.2找到。

It may be easiest to break this into a couple of steps. The first thing that I would do is look at the general overview of what needs to be accomplished prior to looking at the assembly. That can be found in 7.5.2 of your reference.

要注意的是,当WPROG位设置能够执行一个字写的能力和存储位置必须已经被删除这一点很重要。具体操作步骤如下:

It's important to note that the ability to perform a word write is enabled when the WPROG bit is set and that the memory location must already be erased. Here are the steps:


  1. 装入表指针数据的地址寄存器进行写操作。

  2. 写2个字节写入保持寄存器并执行表写。

  3. 将WREN位(EECON1 2>)以使能字节写入

  4. 禁止中断。

  5. 写H'55'即首先。

  6. 写H'AA'即首先。

  7. 将WR位。这将开始写周期。

  8. 的CPU将停止写的TIW(见参数D133A)的持续时间。

  9. 重新允许中断。

我们就可以打破组装成为清楚起见给出的步骤。我也评论code的每一行,这样就可以看到组件做什么(我绝不会做这种实际code,因为它只是分心)。

We can then break the assembly into the given steps for clarity. I have also commented every line of code so that you can see what the assembly is doing (I would never do this in actual code as it is just distracting).

1。装入表指针写入数据的地址。

MOVLW    CODE_ADDR_UPPER    ;Move the value CODE_ADDR_UPPER to the working register.
MOVWF    TBLPTRU            ;Move the working register to TBLPTRU.
MOVLW    CODE_ADDR_HIGH     ;Move the value CODE_ADDR_HIGH to the working register.
MOVWF    TBLPTRH            ;Move the working register to TBLPTRH
MOVLW    CODE_ADDR_LOW      ;Move the value CODE_ADDR_LOW to the working register.
MOVWF    TBLPTRL            ;Move the working register to TBLPTRL.

2。写2个字节写入保持寄存器并执行表写。

MOVLW    DATA0     ;Move DATA0 to the working register.
MOVWF    TABLAT    ;Move the working register to TABLAT.
TBLWT*+            ;Write the data stored in TABLAT to the position 
                   ;defined in TBLPTR and then, increment TBLPTR.
MOVLW    DATA1     ;Move DATA1 to the working register.
MOVWF    TABLAT    ;Move the working register to TABLAT.
TBLWT*             ;Write the data stored in TABLAT to the position defined in TBLPTR.

的步骤的其余部分都包含在PROGRAM_MEMORY部分。

PROGRAM_MEMORY    ; This is just a label.
    ;STEP 3
    BSF    EECON1, WPROG    ;Set the WPROG bit in the EECON1 register.
    BSF    EECON1, WREN     ;Set the WREN bit in the EECON1 register.

    ;STEP 4
    BCF    INTCON, GIE      ;Clear the GIE bit in the INTCON register.

    ;STEP 5
    MOVLW    H'55'          ;Move 0x55 to the working register
    MOVWF    EECON2         ;Write 0x55 to EECON2 register.

    ;STEP 6
    MOVLW    H'AA'          ;Move 0xAA to the working register.
    MOVWF    EECON2         ;Write 0xAA to the EECON2 register.

    ;STEP 7 AND 8
    BSF    EECON1, WR       ;Set the WR bit in the EECON1 register.

    ;STEP 9
    BSF    INTCON, GIE      ;Set the GIE bit in the INTCON register.
    BCF    EECON1, WPROG    ;Clear the WPROG bit in the EECON1 register.
    BCF    EECON1, WREN     ;Clear the WREN bit in the EECON1 register. 

由于我既没有PIC编译器或IDE我,我知道下面的C code不会是完美的(请随时编辑)。不过,这应该给你如何构建基于装配例如C code进行好主意。

Since I have neither the PIC compiler or IDE with me, I know the following C code will not be perfect (please feel free to edit). However, this should give you a good idea of how to construct the C code based on the assembly example.

static void FlashWriteWord(unsigned int store_addr, unsigned char * data)
{
    /* 1. Load Table Pointer with the address of the data to be written. */
    TBLPTR = store_addr;

    /* 2. Write the 2 bytes into the holding registers and perform a table write. */
    TABLAT = data;
    _asm
       TBLWTPOSTINC    /* Table writes are performed as inline assembly functions. */
    _endasm

    data++;
    _asm
        TBLWT          /* Table writes are performed as inline assembly functions. */
    _endasm

    /* 3. Set the write enable bit to enable byte writes. */
    EECON1bits.WPROG = 1;
    EECON1bits.WREN = 1;

    /* 4. Disable interrupts. */
    INTCONbits.GIE = 0;

    /* 5. Write H'55' to EECON2. */
    EECON2 = 0x55;

    /* 6. Write H'AA' to EECON2. */
    EECON2 = 0xAA;

    /* 7. Set the WR bit.  This will begin the write cycle. */
    /* 8. The CPU will stall for the duration of the write for Tiw (see Parameter D133A). */
    EECON1bits.WR = 1;

    /* 9. Re-enable interrupts. */
    INTCONbits.GIE = 1;
    EECON1bits.WPROG = 0;
    EECON1bits.WREN = 0;
}

希望帮助!

这篇关于翻译总成C [写入内存与PIC18]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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