当前位置:文档之家› PIC单片机应用专题二:内、外EEPROM读写

PIC单片机应用专题二:内、外EEPROM读写

dvbb s

收藏本页

联系我们

论坛帮助

dvbb s

zby2005188重登录

网站主页

论坛首页

下载区

论坛经典文章

上线

用户控制面板

搜索

风格

论坛状态

论坛展区

道具中心

退出

产品中心:

QL200开发板

MCD2仿真调试器

QL2006编程器

QL-PIC280编程器

更多产品

我的收件箱(0)

>> 大家上传的比较好的资料全集中在这儿.(本版块是只读区,发主题请到其版块,可回贴)

搜一搜相关精彩主题

PIC单片机论坛→论坛展示区(非讨论区)→『单片机类精华贴子展示区』→PIC单片机应用专题二:内、外EEPROM读写

高性价比PIC单片机学习开发工

具MCD2 PIC单片机开发实验箱QL200B

高性能低价位PIC烧写器/编程器

QL2006

PIC单片机仿真器+烧写器+实验

板专业开发/生产型PIC编程器

QL-PIC280

学习/开发PIC单片机推荐用

QL200

PIC单片机应用专题二:内、外EEPROM读写

以下内容只有回复后才可以浏览

EEPROM常用来保存应用系统的参数,有广泛的应用,下面给出内、外EEPROM读写例程:

1、16F877A内部EEPROM的读写:

LIST P=16f877A

#INCLUDE

;

;- RAM LOCATIONS------

;

; Reserve RAM location 0x70 for ICD

;

Data_EE_Addr equ 0X71

Data_EE_Data equ 0X72

;--------VECTORS------------

org 0x0

nop

goto START

;-------PROGRAM--------------

org 0X20

START

call INIT

;

;---- Test the WRITE EEPROM Routine ----

;

movlw 0x0

movwf Data_EE_Addr

movlw '0' ; Load a ASCII code "0" to W

movwf Data_EE_Data

EE_Write

call WRITE_EEPROM

incf Data_EE_Data,F ; 0x30+ N

incf Data_EE_Addr,F ; Point to next EEPROM Address

btfss Data_EE_Addr,5 ; Write 32 bytes ?

goto EE_Write

;

;---- Test the READ EEPROM Routine ----

;

movlw 0x20 ; Get back EEPROM data to RAM with start addr at 0x20 movwf FSR

;

movlw 0x0 ; Point to EEPROM start address at 0x00

movwf Data_EE_Addr

EE_Read

call READ_EEPROM ; Read EEPROM data with curent address to W registre movwf INDF ; move W --> RAM

;

incf FSR,F

incf Data_EE_Addr,F ; Point to next EEPROM Address

btfss Data_EE_Addr,6 ; Read 32 bytes ?

goto EE_Read

;

GOTO $

;

;------ INTERNAL EEPROM READ ------

;

READ_EEPROM

banksel EEADR

movf Data_EE_Addr,W ; Load EEPROM Address into Pointer

movwf EEADR

;

; bcf INTCON,GIE

banksel EECON1

bcf EECON1,EEPGD

bsf EECON1,RD

;

banksel EEDATA

movf EEDATA,W

; bsf INTCON,GIE

;

banksel PORTA

return

;

;----INTERNAL EEPROM WRITE-----

;

WRITE_EEPROM

banksel EEADR

movf Data_EE_Addr,W ; Load EEPROM Address & Data into Register movwf EEADR

movf Data_EE_Data,W

movwf EEDATA

;

banksel EECON1

bcf EECON1,EEPGD ; Selected Data EEPROM for Writing

bsf EECON1,WREN ; Enable write function

; bcf INTCON,GIE ; Disable Interrupt

movlw 0x55 ; Write Sequence control

movwf EECON2

movlw 0xAA

movwf EECON2

bsf EECON1,WR ; Start EEPROM write operation

;

; bsf INTCON,GIE ; Enable Interrupt (If used interrupt )

bcf EECON1,WREN ; Disable write EEPROM

;

btfsc EECON1,WR ; Waiting for writing completed (4mS)

goto $-1 ; During write period, must hold both EEADR & EEDATA until c omplete

;

banksel PORTA

return

;

;---------INITIAL----------------

INIT

BCF PIR2,EEIF

banksel PIE2

BCF PIE2,EEIE

RETURN

;--------------------------------

END

2、外部EEPROM的读写,所用EEPROM为AT93C46,MCU为16F877A,AT93C46的指令表为:

;*************************************************************

;; received bytes from EEPROM memory locations 10h to 13h

; will be stored in RAM registers 20h to 23h.

;eePROM 93C46读写演示程序。

;将0x5A写到93c46的10h~13h,再将其读到RAM的20h~23h

;硬件设置:SW1的1,2,3断开

;*************************************************************

;

;**********************************************************

LIST P=16F877A

INCLUDE "P16F877.INC"

; Ram Register Definitions

;**************************************************** ;

DISP1 EQU 20H

DISP2 EQU DISP1+1

DISP3 EQU DISP2+1

DISP4 EQU DISP3+1 ;以上为四位显示数据存放位

count EQU DISP4+1 ;动态显示位

T1 EQU count+1 ;延时数据存放位

ctrl EQU T1+1 ;动态显示控制位

outdb EQU ctrl+1

indb EQU outdb+1

i EQU indb+1

rxdata equ i+1

txdata equ rxdata+1

addr equ txdata+1

loops equ addr+1

loops2 equ loops+1

hibyte equ loops2+1

lobyte equ hibyte+1

datbyt equ lobyte+1

CKPMN equ datbyt+1

;**************************************************** ;

; Other Definitions

;

;**************************************************** ;

;

;**************************************************** ; Bit Definitions

;**************************************************** ;

cs equ 7

sdi equ 4

org 0x000 ; Reset Vector

NOP

goto Start

;

;*********************************************************** ;

; This is the transmit/receive routine. The received bytes ; are don't cares until reading back from the array.

;

;*********************************************************** output movwf outdb

clrf rxdata

movlw 0x09

movwf i

again decfsz i,1

goto dbtran

return

dbtran bcf PORTC,3

ckpn movlw 0x01

andwf outdb,0

btfss W,0

goto daout

bsf PORTC,5

datan bsf PORTC,3

movlw 0x02

movwf T1

call delay

ckpt rrf outdb,1

btfss PORTC,4

goto datas

bsf STATUS,0

datat rrf rxdata,1

goto again

daout bcf PORTC,5

goto datan

datas bcf STATUS,0

goto datat

;************************************************************ delay decfsz T1,1

goto delay

;************************************************************

;*********************************************************** ;

; EWEN routine

;

;***********************************************************

EWEN

bcf STATUS, RP0 ; need to set bank 0

bsf PORTC, cs ; set chip select line high

movlw b'00000010' ; start bit is bit 3, 00 is

; op code for EWEN

call output ;

movlw b'01100000' ; 1 req for EWEN, 0000000 are don't cares ; which is the 8 lsb address bits

call output ;

bcf PORTC, cs ; bring chip select low to begin

; EEPROMs internal write cycle

retlw 0 ; return from subroutine

; remember CS must be low for at least 250 nsec

;************************************************************

;************************************************************ ;

; This routine outputs the two bytes required to send

; the start bit, op code bits, and address bits

;

;************************************************************

bcf STATUS, RP0 ; need to set bank 0

bsf PORTC, cs ; set chip select line high

movf hibyte, 0 ; put hibyte in w reg

call output ;

movf FSR, 0 ; put addr pointed to by FSR into

; w reg

call output ;

movf datbyt, 0 ; get ready to output data in datbyt

call output ;

bcf PORTC, cs ; bring chip select low to begin

; EEPROMs internal write cycle

incf FSR, 1 ; point to next location to

; write to

retlw 0

;**************************************************************

;************************************************************** ;

; This is the module that reads one byte of data

;

;**************************************************************

READ

bcf STATUS, RP0 ; need to set bank 0

bsf PORTC, cs ; set chip select line high

bsf SSPCON, CKP ; make sure CKP is 1 to output

; next intstruction and addr

movf hibyte, 0 ; move data from hibyte to w

call output ;

movf lobyte, 0 ; get ready to send next byte

; which is the 8 lsb address bits

call output ;

;******************************************************** ; This is where CKP bit is reset for receiving data.

;******************************************************** bcf SSPCON, CKP ; change clock polarity to 0

movlw 0x00 ; The byte xmitted here is a

; don't care

call output ;

bcf PORTC, cs ; bring chip select low to

; terminate read command

clrf INDF ; clr location pointed to by FSR

movf rxdata, 0 ; move received data to w reg

movwf INDF ; put received data in location

; pointed to by FSR

incf FSR, 1 ; point to next location to

; write to

incf lobyte, 1 ; next addr to read from

retlw 0 ;

;************************************************************** ;**************************************************************

;************************************************************** ;************************************************************** Start

bcf STATUS, RP0 ; need to set bank 0

clrf PORTC ; initialize port c

bsf STATUS, RP0 ; need to set bank 1

movlw 0x10 ; all bits are outputs except SDI

movwf TRISC ; for SPI input

movlw 0x00

movwf SSPSTAT

clrf PIE1 ; disables all peripheral ints

clrf INTCON ; disables all interrupts

bcf STATUS, RP0 ; need to set bank 0

clrf SSPCON ; clear SSP control register

movlw 0x31 ; SPI master, clk/16, ckp=1

movwf SSPCON ; SSPEN enabled

;*******************************************************

; Now, lets read back 10h through 13h (non-sequentially) and ; store it in ram locations 20h through 23h in the 16C74.

; With the Picmaster, I can read those memory locations

; and see that it was read in properly. This is how

; I can quickly verify that the read function is working

; properly.

;*******************************************************

movlw 0x20 ; where in RAM to begin storing

movwf FSR ; data read back from EEPROM

movlw 0x10 ; addr of where to begin reading

movwf lobyte ; EEPROM from

movlw b'00000011' ; start bit is bit 3, 10 is

movwf hibyte ; op code for read

rdnext call READ ;

btfss FSR, 2 ; have we 20h thru 23?

goto rdnext ; no, then read next location

;******************************************************** ; While program is in limbo routine, it is possible

; to halt the processor with Picmaster and look at

; the data contained 16C74 RAM locations 20h through 23h. ;******************************************************** limbo nop

goto limbo

;

;

end

相关主题
文本预览
相关文档 最新文档