unsigned char ReadEEPROM(unsigned char address){
EECON1=0; //ensure CFGS=0 and EEPGD=0
EEADR = address;
EECON1bits.RD = 1;
return(EEDATA);
}
void WriteEEPROM(unsigned char address,unsigned char data){
char SaveInt;
SaveInt=INTCON; //save interrupt status
EECON1=0; //ensure CFGS=0 and EEPGD=0
EECON1bits.WREN = 1; //enable write to EEPROM
EEADR = address; //setup Address
EEDATA = data; //and data
INTCONbits.GIE=0; //No interrupts
EECON2 = 0x55; //required sequence #1
EECON2 = 0xaa; //#2
EECON1bits.WR = 1; //#3 = actual write
INTCON=SaveInt; //restore interrupts
while(!PIR2bits.EEIF); //wait until finished
EECON1bits.WREN = 0; //disable write to EEPROM
}
Check my tutorials, one of the IR ones saves and restores from data EEPROM.
The datasheet also explains how to do it.
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
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 Doesn't exist!!!
bsf EECON1, WREN
movlw H'55' ; magic sequence
movwf EECON2
movlw H'AA'
movwf EECON2
bsf EECON1,WR
; bsf EECON1,WREN ;not needed
; bcf EECON1,EEIF ; clear the interrupt flag
_wloop
btfss EECON1,EEIF ;changed
goto _wloop
bcf EECON1,EEIF ; clear the interrupt flag
banksel EECON1
bcf EECON1, WREN ; disable EEPROM write
banksel 0
return
Right now, I'm in the testing phase. I'm trying to get the code working before I build my circuit. For some reason my computer won't let me reinstall MPLab and I've been doing all my coding using Windows Notepad. I've been using the MPASMWIN app that was installed with Microsim. I had to copy the directory with the application in it because it was complaining about the length of the directory when it tries to compile. I've been coding/compiling using the Notepad/MPASMWIN combo for a few days now, and it seems to be working and the code works on Oshonsoft's Pic Simulator IDE.
Edit:
Pic Simulator has a EEPROM memory viewer that I can look at to see if my data has been written to the EEPROM.
hi,
MPLAB cant handle long file location addresses.
Move your working files to a folder on the main hard drive, name it something like 'MyAsm' that should keep MPLAB happy.
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
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
still locking up in the write portion of the program. I'm still using Oshonsoft PIC Simulator IDE to test my program, and it still gets stuck in the same area.
I didn't realize it because it was working slowly at normal. I watched it and upped to ultimate to really see the work being done.
Eric, and I think I figured out a method of advancing to the next bit and writing the next value into the EEPROM. Is there any advice you would like to add to help me write the timeout value to the EEPROM?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?