Writing and reading a PORTC value to EEPROM? (PIC16F877)

Discussion in 'Embedded Systems and Microcontrollers' started by GuitarMan216, Apr 9, 2012.

  1. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    In Assembly.

    I'm working on a simple code that rotates through 8 LED's(PORTC) from with the rlf instruction. All I need to do is store the value into EEPROM so the same LED is lit up when the circuit is powered on. Say RC2 is high when you power off, when you power it back on, RC2 is still high...same with the other pins. If that makes any sense.

    I know I need the 5 lines:

    movlw 0x55
    movwf EECON2
    movlw 0xAA
    movwf EECON2
    bsf EECON1, WR

    but other than that, I'm new to EEPROM.

    Thanks guys, you're always helpful.
     
  2. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Here's what I have to write:

    Code ( (Unknown Language)):
    1.  
    2.     banksel EECON1           
    3.     btfsc EECON1, WR       
    4.     goto $-1               
    5.     movlw 0xC3         
    6.     banksel EEADR          
    7.     movwf EEADR            
    8.     movf  PORTC, W         
    9.     movwf EEDATA             
    10.     banksel EECON1           
    11.     bcf EECON1, EEPGD      
    12.     bsf EECON1, WREN       
    13.     bcf INTCON, GIE        
    14.     movlw 0x55             
    15.     movwf EECON2             
    16.     movlw 0xAA             
    17.     movwf EECON2           
    18.     bsf EECON1, WR         
    19.     bsf INTCON, GIE        
    20.     bcf EECON1, WREN       
    21.  
    And to read:

    Code ( (Unknown Language)):
    1.  
    2.     movlw 0xC3     
    3.     banksel EEADR          
    4.     movwf EEADR            
    5.     banksel EECON1         
    6.     bcf EECON1, EEPGD      
    7.     bsf EECON1, RD         
    8.     banksel EEDATA         
    9.     movf EEDATA, W         
    10.  
     
  3. Felo

    Member

    Feb 20, 2012
    91
    13
    Hi,

    Keep in mind that EEPROM writing cycles are limited, (most pic's have 1M cycles). For example, let's say that your aplication will be writting to the EEPROM 10 times per sec.

    10x60x60=36000 , within an hour it would have written 36k times the EEPROM

    If it is intended to be used for say 8 hours a day, within a day you will have 288k cycles out, in 3,47 days of 8 hours, your application will be out of spec.

    I remember to have read an AP note from MC wich dealt with this problem and suggested a form o "rolling position" of EEPROM accessing, quite interesting.

    Good Luck
     
    GuitarMan216 likes this.
  4. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0

    Thanks. I need it to work for a very limited time though. Maybe a minute at the longest.
     
  5. Felo

    Member

    Feb 20, 2012
    91
    13
    this is my code for dealing with EEPROM

    To write

    Code ( (Unknown Language)):
    1. WRITE_EEPROM    ; PUT VALUE IN EEDATA AND EEADR  BEFORE CALLING!!
    2.         BANCO1  ;EEPROM_ADDR MACRO
    3.         BSF EECON1, WREN
    4.         MOVLW   0X55    ;OBLIGATORIO
    5.         MOVWF   EECON2  ;
    6.         MOVLW   0XAA    ;
    7.         MOVWF   EECON2  ;
    8.         BSF EECON1, WR
    9.         BTFSC EECON1,WR
    10.         GOTO $-1
    11. VERIF   NOP
    12.         MOVFW   EEDATA  ; LAST WRITTEN VALUE IN 'W'
    13.         BSF     EECON1, RD  ; START READ AT CURRENT ADDRESS
    14.         SUBWF   EEDATA, W   ; SUBTRACT W FROM EEDATA
    15.         BTFSS   STATUS, Z   ; ZERO FLAG SET?
    16.         GOTO    WRITE_EEPROM    ; NO, THERE WAS A WRITE-ERROR
    17.         BANCO0
    18.         RETURN     
     
    GuitarMan216 likes this.
  6. Felo

    Member

    Feb 20, 2012
    91
    13
    Too read

    Code ( (Unknown Language)):
    1.     BANCO1
    2.         MOVLW   0X01 ;DIRECCION A LEER (TARGET ADDRESS TO READ)
    3.         MOVWF   EEADR
    4.         BSF     EECON1,RD
    5.         MOVFW   EEDATA
    6.         BANCO0
    7.         MOVWF   INDICE ; OR ANYTHING
     
    GuitarMan216 likes this.
  7. GuitarMan216

    Thread Starter New Member

    Oct 6, 2011
    18
    0
    Thanks for all the help man. Not gonna lie, I laughed at the Banco1. Two semesters of espanol, and it's one of the few words I still remember. :D
     
  8. Felo

    Member

    Feb 20, 2012
    91
    13
    HEHEH, Spanglish coder in tha house!!!

    two semesters of spanish should have left you way more than a few words my friend, do as I and write your macros in spanish...:D
     
Loading...