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
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
Rich (BB code):
;**********************************************************************
; *
; Filename: simpleEP4.asm
; Puts manually entered number in first EEprom location
; into PortB
;SITS THERE TILL BUTTON PRESSED
; STARTS FROM LAST COUNT WHEN POWER CYCLED js *
; *
; *
;**********************************************************************
list p=16F628A ; list directive to define processor
#include <p16F628A.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file
__CONFIG 2330 ; _CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT
;**********************************************************************
ORG 20h ; this does not seem to have anything to do with
; anything. js
d1 equ 20
d2 equ 21
d3 equ 22
last equ 23
CLRF PORTA ;Initialize PORTA by setting output data latches
MOVLW 0x07 ;Turn comparators off and
MOVWF CMCON ;enable pins for I/O functions
BSF STATUS, RP0;Select Bank1
MOVLW 0x1F ;Value used to initialize data direction
MOVWF TRISA ;Set RA<4:0> as inputs TRISA<5> always
;read as ‘1’.TRISA<7:6>depend on oscillator mode
bsf status, rp0 ; set = bank1
movlw B'00000001' ; makes ra0 input
movwf trisa ; contents of W copied to PORT A ...
movlw B'00000000'
movwf trisb ; all PORT B out
bcf status, rp0 ; clear = bank0
clrf portb
BsF STATUS, RP0 ; Bank 1
MOVLW 00 ;FIRST LOCATION IN EEPROM JS
MOVWF EEADR ; Address to read
BSF EECON1, RD ; EE Read
MOVF EEDATA, W ; W = EEDATA
BCF STATUS, RP0 ; Bank 0
MOVWF PORTb
loopb:
BCF STATUS, RP0; BANK 0
btfss porta,0
incf portb
CALL DELAY
MOVF PORTB, W
MOVWF LAST
MOVF LAST,W
BsF STATUS, RP0 ; Bank 1
MOVWF EEDATA
bcf stAtus,RP0 ;BANK 0
CALL EEWRITE ;Call EEPROM write routine
goto loopb
EEWRITE:
BSF STATUS, RP0 ; Bank 1
BSF EECON1, WREN ; Enable write
MOVLW 55h
MOVWF EECON2 ; Write 55h
MOVLW h'AA'
MOVWF EECON2 ; Write AAh
BSF EECON1,WR ; Set WR bit
BcF STATUS, RP0 ; Bank 0
EEWR_WAIT: ;Wait for write cycle completed
BTFsc PIR1,EEIF ;Write cycle completed?
GOTO EEWR_WAIT ;No, wait
BcF PIR1,EEIF ;Yes, we can clear write flag
RETURN
; Actual delay = 1 seconds = 1000000 cycles
Delay
;999990 cycles
movlw 0x07
movwf d1
movlw 0x2F
movwf d2
movlw 0x03
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0
;6 cycles
goto $+1
goto $+1
goto $+1
;4 cycles (including call)
return
END ; directive 'end of program'
Attachments
-
3.1 KB Views: 19
Last edited by a moderator: