Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Using PIC EEPROM for storing data in registers

Status
Not open for further replies.

SkeeterB08

Newbie level 6
Joined
Sep 1, 2008
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
NorthEast Mississippi, USA
Activity points
1,397
I got to thinking about how each of my Hydrogen on Demand cells will be different and I need a way to saving a value of how long a delay should be between the time the lower sensor goes high and how long after the upper sensor goes high to keep from overfilling the cell with electrolyte. I was thinking of using an EEPROM to store that data that would be read and moved to the delay counter that is used when the Refill section of my code is running. I would probably read a switch that tells on an input that would tell my pic to read the other input that would increment the W Register to the desired delay. When the switch to increment the W register goes low it would write the W register to the EEPROM. I'm already thinking of what code to write for the subroutine to set the value to the W Register, but what Registers would be used in the onboard EEPROM and what commands would be used to read and write to the registers??

Edit:

I thought about leaving putting the value in code, but I would like to be able to change that value without having to recompile and reprogram the PIC with a new HEX file if I ever change HOD cells and require a different refill time to sufficiently fill the cell to above the Upper Water Level Sensor.
 

pic eeprom code

EEPROM examp,e program can be found here
**broken link removed**
and see also
**broken link removed**
 

pic eeprom write

I'm having a problem Writing to the EEPROM using the below bit of code. It is supposed to write the value of the Count1v Register into the EEPROM memory of the PIC. Where am I going wrong??

Code:
EPWrite
            movf EEPROM_ADR,w ; load with value in w
            banksel EEADR
            movwf EEADR
            clrf EEADRH
            banksel 0
            movf EEprom_Data,w
            banksel EEDATA
            movwf EEDATA

            banksel EECON1
            bcf EECON1,EEPGD
            bsf EECON1, WREN
    
            movlw   H'55'           ; magic sequence
            movwf   EECON2             
            movlw   H'AA'                 
            movwf   EECON2   
    
            bsf     EECON1,WR 
            bsf     EECON1,WREN           
            bcf     EECON1,EEIF     ; clear the interrupt flag

_wloop
            btfsc EECON1,EEIF
            goto _wloop

            banksel EECON1
            bcf EECON1, WREN ; disable EEPROM write
            banksel 0
            return
 

pic24 eeprom

Writing to the EEPROM memory of the PIC, some informative literature may be found here
**broken link removed**
 

eeprom write on pic

Hi,

Your code looks nearly right but couple of things might need changing.

You have not used any of the Interrupt commands - perhaps because your main code is not using them - ? - but think they should be included.

Also the way in which you are clearing then testing the flag is different to my working code - my example was on a 16F876a.




eewrite bsf STATUS,RP0 ; select bank1
movfw CHOURS ; get Chours
bcf STATUS,RP0 ; reset to bank0
bsf STATUS,RP1 ; select bank2
movwf EEDATA ; load Chours to EEdata
movlw 0x00 ; load eeaddresss 00
movwf EEADR ;
bcf STATUS,RP1 ; reset to bank0

bsf STATUS,RP0 ; select bank1 EEWRITE
bsf STATUS,RP1 ; select bank3
bcf EECON1,EEPGD ; points to data memory
bsf EECON1,WREN ; enable write
bcf INTCON,GIE ; disable all ints
movlw 0x55 ;
movwf EECON2 ; init write sequence
movlw 0xAA ; using 2 instructions
movwf EECON2 ;
bsf EECON1,WR ; write data
bsf INTCON,GIE ; turn ints back on
bcf STATUS,RP0 ; select bank0
bcf STATUS,RP1 ;
wd01 btfss PIR2,EEIF ; is int bit on ie WRTDONE
goto wd01 ; if done continue
bcf PIR2,EEIF ; reset interupt
bsf STATUS,RP0 ; select bank3
bsf STATUS,RP1
bcf EECON1,WREN ; disables write
bcf STATUS,RP1 ; select bank1
bcf STATUS,RP0 ; select bank0


; *************************************************************
; DECLARE EEPROM DATA 16f876a chip*
;
; preload EEPROM with base values -at programming time only !!
;
; data sequence:- clk hrs, clk mins, temp off, temp on, chill1, chill2,
;
org 0X2100*
de 0X09,0X59,0X50,0X49,0X60,0X70,0X55,0X40,0X80
de 0X00,0X10,0X15,0X22,0X15,0X10,0X30,0X21,0X30
 

writing to pic eeprom

My program seems to be getting stuck when writing to the EEPROM. Where am I going wrong?

Code:
       list      p=16f628A           ; list directive to define processor
       #include <p16f628A.inc>       ; processor specific variable definitions
       errorlevel   -302             ;hide banking message
    ;*****   
     __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF
    ;*****
    ;internal osc settings
    ;*****
    ; '__CONFIG' directive is used to embed configuration data within .asm file.
    ; The lables following the directive are located in the respective .inc file.
    ; See respective data sheet for additional information on configuration word.

    ;***** VARIABLE DEFINITIONS
w_temp        EQU     0x70              ; variable used for context saving
status_temp   EQU     0x71              ; variable used for context saving
Count1        EQU     0X20              ; First Counter for Delay 
Count1v       EQU     0X21              ; Count Storage for Refill Delay
EEPROM_ADDR   Equ     0x00		; Address of EEPROM byte used
    ;**********************************************************************
             ORG      0x000             ; processor reset vector
             goto     Start             ; go to beginning of program
             ORG      0x004             ; interrupt vector location
             movwf    w_temp            ; save off current W register contents
             movf     STATUS,w          ; move status register into W register
             movwf    status_temp       ; save off contents of STATUS register
             movf     status_temp,w     ; retrieve copy of STATUS register
             movwf    STATUS            ; restore pre-isr STATUS register contents
             swapf    w_temp,f
             swapf    w_temp,w          ; restore pre-isr W register contents
             retfie                     ; return from interrupt         

Start
             clrf     PORTA
             clrf     PORTB
             MOVLW    0x07
             MOVWF    CMCON             ; Turn off comparator
             bsf      STATUS,RP0        ; bank one
             movlw    0xFF
             movwf    TRISA             ; porta all Input
             movlw    0x00 
             movwf    TRISB             ; portb all Output
             movlw    0x00              ;See datasheet for prefered
             movwf    OPTION_REG        ;settings of OPTION_REG
             bcf      STATUS,RP0        ; return to bank 0
             call     Systest
             btfsc    PORTA, 2          ; Check if Setup Switch = 1
             call     Setup             ; Go to Setup Subroutine
             ;movf     Count1v,w 
             ;movwf    Count1
             btfss    PORTA,0           ; Check if Upper Sensor = 1
             Call     Refill            ; If not go to Refill Subroutine
             Goto     Run               ; Then go to Operation Mode

Setup        
             btfss    PORTA,2           ; If Setup Switch = 0
             call     EP_Write          ; Write Count1v Data to EEPROM
             btfsc    PORTA,3           ; If Count Switch = 1
             incfsz   Count1v,1         ; Increment Count1v by 1
             btfsc    PORTA,2           ; If Setup Switch = 1
             goto     Setup             ; Return to top of Subroutine
             btfss    PORTA,2           ; If Setup Switch = 0
             call     EP_Write
             return                     ; Return to main program                

Systest
             call     Delay             ; Call Delay Routine to set Delay time
             movlw    0x80              ; Set PortB,7 High the others Low
             movwf    PORTB               
             call     Delay1            ; Call Delay Subroutine
             call     Delay             ; Call Delay Routine to set Delay time
             movlw    0x40              ; Set PortB,6 High the others Low
             movwf    PORTB
             call     Delay1            ;Call Delay Subroutine
             call     Delay             ; Call Delay Routine to set Delay time
             movlw    0x20              ; Set PortB,5 High the others Low
             movwf    PORTB
             call     Delay             ; Call Delay Routine to set Delay time
             call     Delay1            ; Call Delay Subroutine
             movlw    0x10              ; Set PortB,4 High the others Low
             movwf    PORTB
             call     Delay             ; Call Delay Routine to set Delay time
             Call     Delay1            ; Call Delay Subroutine
             movlw    0xF0              ; Set All Indicators High
             movwf    PORTB
             call     Delay             ; Call Delay Routine to set Delay time
             call     Delay1            ; Call Delay Subroutine
             movlw    0x80              ; Turn on Power LED
             movwf    PORTB
             RETURN                     ;Return to main program

Run
             movlw    0xC0              ; Set Operation Mode
             btfss    PORTA,0           ; Check if Upper Sensor = 1
             movlw    0xE0              ; Set Warning Mode
             movwf    PORTB             ; Move to PortB
             btfss    PORTA,1           ; Check if Lower Sensor = 1
             call     Delay             ; Then Goto Delay1 Subroutine
             btfss    PORTA,1           ; Check If Lower Sensor = 1
             Call     Refill            ; Go to Refill Subroutine
             Goto     Run               ; Return to Top 

Refill
             movlw    0xB8              ; Set Refill Mode
             btfsc    PORTA,0           ; Check If Upper Sensor = 1
             movlw    0xC0              ; Then Set Operation Mode 
             movwf    PORTB             ; Move to PORTB
             btfsc    PORTA,1           ; Check If Lower Sensor = 1
             call     Delay1            ; Then Go To Delay Subroutine
             btfss    PORTA,0           ; Check If Upper Sensor = 1
             goto     Refill            ; If Not Return to Top of subroutine
             Return                     ; Return to previous point

Delay:
             movlw    0x05              ; Set Counter to 05H
             movwf    Count1
             Return                     ; Return to previous point

Delay1:		
	     decfsz   Count1,1          ;Decrement Count1 1
	     goto     Delay1            ;If Count1 > 0 Return to top
             Return                     ;Return to Previous point
EP_Write
             movfw    Count1v    	; read current value
 	     bsf      STATUS,RP0 	; Bank 1
	     bsf      EECON1,WREN 	; Enable write
	     movwf    EEDATA		; set EEPROM data
	     movlw    EEPROM_Addr
	     movwf    EEADR		; set EEPROM address
	     movlw    0x55
	     movwf    EECON2 		; Write 55h
	     movlw    0xAA
             movwf    EECON2 		; Write AAh
	     bsf      EECON1,WR 	; Set WR bit
					; begin write
	     bcf      STATUS,RP0 	; Bank 0
		
	     btfss    PIR1,EEIF	        ; wait for write to complete.
	     goto     $-1
	     bcf      PIR1,EEIF   	; and clear the 'write complete' flag
	     bsf      STATUS,RP0 	; Bank 1
	     bcf      EECON1,WREN	; Disable write
	     bcf      STATUS,RP0 	; Bank 0
	     retlw    0x00
             Return

EP_READ
             BSF      STATUS, RP0       ; Bank 1
             MOVLW    EEPROM_ADDR       ;
             MOVWF    EEADR             ; Address to read
             BSF      EECON1, RD        ; EE Read
             MOVF     EEDATA, W         ; W = EEDATA
             BCF      STATUS, RP0       ; Bank 0
             RETURN

            END
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top