EEprom help

Discussion in 'Embedded Systems and Microcontrollers' started by k9act, Jan 25, 2014.

  1. k9act

    Thread Starter New Member

    Jan 25, 2014
    3
    0
    I need a counter that will save the last count in event of power outage.

    I have succeeded in getting the last count into EEprom and reading it back at power up cycle. It only took about two weeks, (ha ha) to figure out how to do this but now I realize that there was no way to clear it back to zero if I want it to start over.

    The attached is a simple binary counter program that I am using to learn this and will use the code to update a decimal 7 segment version when I get the EEprom stuff to work.


    Can someone tell me how to clear it to zero?

    Thanks,

    Jack

    Code ( (Unknown Language)):
    1.  
    2.                                                                      
    3. ;**********************************************************************
    4. ;                                                                     *
    5. ;    Filename:        simpleEP4.asm      
    6.  
    7. ;   Puts manually entered number in first EEprom location
    8. ;   into PortB  
    9.     ;SITS THERE TILL BUTTON PRESSED
    10. ;   STARTS FROM LAST COUNT WHEN POWER CYCLED  js                                 *
    11. ;                                                                     *
    12. ;                                                               *
    13. ;**********************************************************************
    14.  
    15.     list      p=16F628A           ; list directive to define processor
    16.     #include <p16F628A.inc>       ; processor specific variable definitions
    17.  
    18.     errorlevel  -302              ; suppress message 302 from list file
    19.  
    20.     __CONFIG  2330 ; _CP_OFF  & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT
    21.  
    22.  
    23. ;**********************************************************************
    24.     ORG 20h ;  this does not seem to have anything to do with
    25. ;              anything.  js   
    26.  
    27.    
    28. d1  equ 20
    29. d2  equ 21
    30. d3  equ 22
    31. last    equ 23
    32.  
    33.  
    34.     CLRF PORTA ;Initialize PORTA by setting output data latches
    35.     MOVLW 0x07 ;Turn comparators off and
    36.     MOVWF CMCON ;enable pins for I/O functions
    37.     BSF STATUS, RP0;Select Bank1
    38.     MOVLW 0x1F ;Value used to initialize data direction
    39.     MOVWF TRISA ;Set RA<4:0> as inputs TRISA<5> always
    40.                     ;read as ‘1’.TRISA<7:6>depend on oscillator mode
    41.    
    42.  
    43.     bsf status, rp0     ;   set = bank1
    44.  
    45.          movlw B'00000001'    ;  makes ra0 input
    46.          movwf trisa           ; contents of W copied to PORT A ...
    47.  
    48.          movlw B'00000000'
    49.          movwf trisb           ; all PORT B out
    50.      
    51.          bcf status, rp0    ;   clear = bank0
    52.         clrf    portb
    53.  
    54.    
    55.        
    56.     BsF STATUS, RP0 ; Bank 1
    57.  
    58.     MOVLW   00   ;FIRST LOCATION IN EEPROM JS
    59.     MOVWF EEADR ; Address to read
    60.     BSF EECON1, RD ; EE Read
    61.     MOVF EEDATA, W ; W = EEDATA
    62.  
    63.     BCF STATUS, RP0 ; Bank 0
    64.  
    65.     MOVWF   PORTb
    66.  
    67.  
    68.    
    69. loopb:
    70.     BCF STATUS, RP0;  BANK 0
    71.     btfss   porta,0
    72.     incf    portb
    73.  
    74.     CALL DELAY
    75.    
    76.     MOVF PORTB, W
    77.  
    78.     MOVWF LAST
    79.  
    80.  
    81.  
    82.  
    83.     MOVF    LAST,W
    84.     BsF STATUS, RP0 ; Bank 1
    85.     MOVWF   EEDATA
    86.    
    87.  
    88.     bcf     stAtus,RP0  ;BANK 0
    89.  
    90.     CALL EEWRITE ;Call EEPROM write routine
    91.  
    92.  
    93.  
    94.         goto loopb
    95.  
    96. EEWRITE:
    97.     BSF STATUS, RP0 ; Bank 1
    98.     BSF EECON1, WREN ; Enable write
    99.     MOVLW 55h
    100.     MOVWF EECON2 ; Write 55h
    101.     MOVLW h'AA'
    102.     MOVWF EECON2 ; Write AAh
    103.     BSF EECON1,WR ; Set WR bit
    104.  
    105.  
    106.  
    107.     BcF STATUS, RP0 ; Bank 0
    108.  
    109.  
    110. EEWR_WAIT: ;Wait for write cycle completed
    111.     BTFsc PIR1,EEIF ;Write cycle completed?
    112.     GOTO EEWR_WAIT ;No, wait
    113.     BcF PIR1,EEIF ;Yes, we can clear write flag
    114.     RETURN
    115.  
    116.  
    117. ; Actual delay = 1 seconds = 1000000 cycles
    118.  
    119. Delay
    120.             ;999990 cycles
    121.     movlw   0x07
    122.     movwf   d1
    123.     movlw   0x2F
    124.     movwf   d2
    125.     movlw   0x03
    126.     movwf   d3
    127. Delay_0
    128.     decfsz  d1, f
    129.     goto    $+2
    130.     decfsz  d2, f
    131.     goto    $+2
    132.     decfsz  d3, f
    133.     goto    Delay_0
    134.  
    135.             ;6 cycles
    136.     goto    $+1
    137.     goto    $+1
    138.     goto    $+1
    139.  
    140.             ;4 cycles (including call)
    141.     return
    142.  
    143.  
    144.  
    145.         END                       ; directive 'end of program'
    146.  
    147.  
     
    Last edited by a moderator: Jan 26, 2014
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    The device it the code was written for looks like a fairly big one, are you sure you need to do this in assembler? If you moved to C and used the libraries provided with your compiler things would get so much easier to look at and see what needs changing and where.
     
  3. k9act

    Thread Starter New Member

    Jan 25, 2014
    3
    0
    I wrote it for a 16f628a so I don't really understand what you mean.

    But finishing it seems pretty trivial compared to learning another new language.

    I just don't happen to know how to do it.

    js
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Umm, you mean how to simply write a zero to the EEPROM?

    Set counters to null: ENGAGE !!!
     
  5. k9act

    Thread Starter New Member

    Jan 25, 2014
    3
    0
    I tried to explain what I am doing but I will try again.

    The counter is counting turns on a machine. If the power goes off, the count is lost.

    My fix it to write the current count to eeprom so that in the event of a power failure, that count is loaded into the counter when the power comes back.

    It works perfectly except that when I finish the job, I want to reset and start counting from zero.

    Unfortunately, the normal MCLR reset obeys the same orders and resets to the last count instead of zero.

    I need a trick to outsmart my very clever code.

    js
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,027
    So.. if the problem is to know the difference between power fail and MCLR/ reset you can examine the POR/ BOD/ TO/ PD/ bits in the STATUS and PCON registers after any reset and know what generated it.

    That said, using MCLR/ as a 'reset the counter function' may prove problematic if your code grows. A reset button on the I/O that handled zeroing the counter and updating EEPROM would be my choice.

    Have fun!
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    A reset button would come in real handy for the unforeseen event of "NEXT !!!"
     
  8. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    Your code shows an eeprom write every cycle... The eeprom in the pic would last about a week before its destroyed.... You need to use "smart eeprom writing" so if the count hasn't changed... Then don't write..

    You only have only 100,000 or so write cycles... Also remember that the internal eeprom requires the same write delay to update its values... 5mS or so, or the write will fail...
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    You can use the same button or function that resets / restarts your run job on the machine.

    Assuming your MCU has a Broown Out interrupt AND the MCU is subject to the same power failure as the rest of the machine, you could use brown out to save the value of the counter.

    So much fir that "clever" code I guess. :eek:
     
  10. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    I don't believe the mid range pic16 can do this... Anyway! I don't think there wouldn't be enough time to store an eeprom value...
     
  11. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Maybe with external undervoltage detector and a diode and a big bank of caps on the PICs supply, you could get fast enough early warning and enough energy left to complete the write.
     
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    I guess the OP could ad a supercap, add code to monitor the voltage from an ADC. When the voltage starts to drop store the value. Complicated but it should work.


    Not to steal the OPs thread but how many times can an EEPROM be written?
     
  13. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    Just checked... 1,000,000...
    I use a ramtron device (FRAM)... It's external but the endurance is 1 trillion writes and there is no write delay... I use it in winch payout systems....
     
  14. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    What's the problem? This has been done since the dawn of eeproms.

    All you need is a "job start" button that resets the count to zero and memorises it.

    Any other situation (when that button is NOT pressed) the count always goes up and is always memorised.
     
  15. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    762
    Not to derail the thread, Ian. What is it actually?

    Could it be I know it by another name?
     
Loading...