piclist 2002\10\16\233738a >
Thread: code that causes crash writing to EEPROM
techref.massmind.org/techref/mems.htm?key=eeprom
flavicon
face BY : myke predko email (remove spam text)



----- Original Message -----
From: "Christopher Gill" <STOPspamchristopher.m.gillspamBeGonespamspamBeGoneSTUD.MAN.AC.UK>
To: <TakeThisOuTPICLISTspamTakeThisOuTMITVMA.MIT.EDU>
Sent: Wednesday, October 16, 2002 6:01 AM
Subject: code that causes crash writing to EEPROM


This is the code that I'm using that causes the crashes. Any suggestions why
would be appreciated
Thanks
chris

:
Chris' code deleted
:


Hi Chris,

I've just played around with your code for about a half an hour and there
are a number of problems that you are going to have to work through to get a
working application.

I don't know what the symptoms are of your "crashes" but using MPLAB 5.70, I
don't get any problems.

The application seems to be a data logger that loads the Data EEPROM with an
analog I/O pin value once every 65 msecs.

1.  The big problem with the code is, you don't define the variables "ADDR"
and "VALUE".  Assembly fails because they are not defined.  In my "fixed"
code below, I have put them in the common register area because...

2.  You seem to reference "ADDR" and "VALUE" in two different register banks
(0x0100 and 0x0180).  As I said, I defined them in my "fixed" code in the
common register area, which can be accessed across all four banks.

3.  I think you've taken code from a number of sources because register bank
switching is done three different ways.  I think the bank switches are
correct, but you should just work on one way.

4.  Along with point 3., you haven't set the register addresses for the
appropriate banks which will give you informational messages.  In the
"fixed" code below, I use the XOR convention to change register addresses to
the current bank.

5.  Your Analog to Digital conversion is incorrect and I don't believe it
will work in the actual PICmicro MCU as written.  Instead of polling on
"ADIF", you should be polling on the "GO" bit of ADCON0.  To use the "ADIF"
bit, I believe that you first have to set the "ADIE" bit.  This is off the
top of my head, but if you poll on "GO" and ignore interrupts (as you have d
one), you will never have any problems.

6.  In your data EEPROM write code, you do not poll on the "WR" bit, waiting
for it to be reset.  I believe the code will still work because of your long
(65 msec) TMR0 based delay, but it is not good form to ignore polling the
"WR" bit during a data EEPROM write.

7.  In your code, you write to PORTC with the ADC Value and immediately
clear PORTC and then later write to PORTC again with the ADC value.
Something here seems to have gone awry.


I've attached the source that I created that does not have any assembly
errors or messages.  My changes are marked with comments that start with
";*".

myke





list p=16f877

; Include file, change directory if needed
include "p16f877.inc"


;* - Added "ADDR" and "VALUE" in Common Registers
CBLOCK 0x070
ADDR, VALUE
ENDC
;* - End Added "ADDR" and "VALUE"



; Start at the reset vector
org 0x000
nop

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

BSF STATUS,RP1; move to bank 2
BCF STATUS,RP0;
MOVLW 0x00; define address
MOVWF ADDR;
BCF STATUS,RP1; back to bank 0
BCF STATUS,RP0;
;--------------------------------------------

Start
banksel PORTC
clrf PORTC ;Clear PORTC
movlw B'01000001' ;Fosc/8, A/D enabled
movwf ADCON0

banksel OPTION_REG
movlw B'10000111' ;TMR0 prescaler, 1:256
;* movwf OPTION_REG
;* clrf TRISC ;PORTC all outputs
movwf OPTION_REG ^ 0x080
clrf TRISC ^ 0x080 ;PORTC all outputs
movlw B'00001110' ;Left justify,1 analog channel
;* movwf ADCON1 ;VDD and VSS references
movwf ADCON1 ^ 0x080 ;VDD and VSS references


Main
banksel PORTC

main
btfss INTCON,T0IF ;Wait for Timer0 to timeout
goto main
bcf INTCON,T0IF

bsf ADCON0,GO ;Start A/D conversion
Wait
btfss PIR1,ADIF ;Wait for conversion to complete
;* - Should be polling on "ADCON0,GO", not "PIR1,ADIF"
goto Wait

movf ADRESH,W ;Write A/D result to PORTC
movwf PORTC ;LEDs

;* - Is this code correct?  Write to PORTC and then clear it

clrf PORTC
WaitPush
btfss PORTB,0
; goto WaitPush
;----------------------------------------
 MOVF ADRESH, w
BSF STATUS,RP1
BCF STATUS,RP0; move a/d value to value (bank 2)
MOVWF VALUE


BSF STATUS,RP1
BCF STATUS,RP0
INCF ADDR,1; increment address



BCF STATUS,RP1; return to bank 0
BCF STATUS,RP0



BSF STATUS,RP1
BSF STATUS,RP0
;* BTFSC EECON1, WR
BTFSC EECON1 ^ 0x0180, WR
 GOTO $-1
BCF STATUS, RP0
MOVF ADDR, W
;* MOVWF EEADR; write sequence from data sheets
MOVWF EEADR ^ 0x0100 ; write sequence from data sheets
MOVF VALUE, W
;* MOVWF EEDATA
MOVWF EEDATA ^ 0x0100
BSF STATUS, RP0
;* BCF EECON1, EEPGD
BCF EECON1 ^ 0x0180, EEPGD
;* BSF EECON1, WREN
BSF EECON1 ^ 0x0180, WREN


BCF INTCON, GIE
MOVLW 0X55
;* MOVWF EECON2
MOVWF EECON2 ^ 0x0180
MOVLW 0XAA; write sequence continued
;* MOVWF EECON2
MOVWF EECON2 ^ 0x0180
;* BSF EECON1, WR
BSF EECON1 ^ 0x0180, WR
BSF INTCON, GIE

;*  - Problem, do not wait for "WR" to be cleared

;* BCF EECON1, WREN
BCF EECON1 ^ 0x0180, WREN


movlw 0x1c
movwf STATUS; restore status to initial value

;-----------------------------------------------------------------------
movfw ADRESH
movwf PORTC; move a/d value to leds
goto Main ;Do it again

end

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


<001301c2758e$16061b60$6801a8c0@ym.phub.net.cable.rogers.com> 7bit

See also: techref.massmind.org/techref/mems.htm?key=eeprom

month overview.

new search...