微處理器 - Microchip PIC12F1822 - Assembly - Flash Memory



暫存器


注意事項:
1. 操作以row為單位,一個row代表16 bits
2. 寫入資料固定為14 bits,最高的兩個bits無法寫入
3. 最多可以連續寫入16 bytes

main.s

    list p=12f1822, r=hex
    #include <p12f1822.inc>
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF
    __config _CONFIG2, _LVP_OFF

    #define LED 0x00
 
    org 0x0000
    goto start

    org 0x0100
start:
    banksel TRISA
    bcf TRISA, LED
   
    banksel LATA
    movlw 0xff
    movwf LATA

write:
    banksel EEADRL
    movlw 0x00
    movwf EEADRL
    movlw 0x07
    movwf EEADRH
    call flash_erase_word
  
    banksel EEADRL
    movlw 0x00
    movwf EEADRL
    movlw 0x07
    movwf EEADRH
    movlw 0x34
    movwf EEDATL
    movlw 0x12
    movwf EEDATH
    call flash_write_word

verify:
    banksel EEADRL
    movlw 0x00
    movwf EEADRL
    movlw 0x07
    movwf EEADRH
    call flash_read_word

    banksel EEADRL
    movlw 0x34
    xorwf EEDATL, w
    btfss STATUS, Z
    goto led_off

    banksel EEADRH
    movlw 0x12
    xorwf EEDATH, w
    btfss STATUS, Z
    goto led_off

    banksel LATA
    bcf LATA, LED
    goto $

led_off:
    banksel LATA
    bsf LATA, LED
    goto $

flash_erase_word:
    banksel EECON1
    bcf INTCON, GIE
    bsf EECON1, EEPGD
    bcf EECON1, CFGS
    bsf EECON1, FREE
    bsf EECON1, WREN
    movlw 0x55
    movwf EECON2
    movlw 0xaa
    movwf EECON2
    bsf EECON1, WR
    nop
    nop
    bcf EECON1, WREN
    bsf INTCON, GIE
    return

flash_write_word:
    banksel EECON1
    bcf INTCON, GIE
    bsf EECON1, EEPGD
    bcf EECON1, CFGS
    bsf EECON1, WREN
    bcf EECON1, LWLO
    movlw 0x55
    movwf EECON2
    movlw 0xaa
    movwf EECON2
    bsf EECON1, WR
    nop
    nop
    bcf EECON1, WREN
    bsf INTCON, GIE
    return

flash_read_word:
    banksel EECON1
    bcf EECON1, CFGS
    bsf EECON1, EEPGD
    bcf INTCON, GIE
    bsf EECON1, RD
    nop
    nop
    bsf INTCON, GIE
    return
    end

編譯

$ gpasm main.s

完成