Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Counter program writes to internal eprom Needed

Status
Not open for further replies.

janosandi

Full Member level 4
Joined
Jan 23, 2010
Messages
210
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,788
Hello Guys
i need to make my program count each time a button clicked & save the result in eeprom & when i want to readthe eeprom

Thx
 

Hi,

Well the EEprom routine regularly trips up many folk, but if you follow the examples in the datasheets you should have no problems.

Test and prove the write and read by outputing the read back data to a row of leds or a display, if you have one, to prove it works.
Or read the data back and compare it to your control number, if its equal then just turn on a led.
Then you can add those eeprom read and writesub - routines to you main code.

If you read a switch, it is always good to 'debouce' it, this means reading the switch port, if its pressed do a little delay, then read the port again to ensure it was really on. Alternativley you can also test to ensure the switch has been released.
You then add that to your variable which you then copy to eeprom.

Easy !!:D
 

Hello
Thx for ur reply dear
would u plz send me sample code ?
How can i add numbers to variable ?

Thx
your help is very appriciated
 

Hi,

It would help if you post your current code so we can see how you are getting on.

Everytime your program senses the switch, then 'add' or 'increment' 1 to a variable called say Sw _count .
You will have to keep an eye on it check it does not go over 255 counts without you being aware of it.

The 16f84a datasheet shows the assembly code for writing and reading a byte to eeprom.
Create a subroutine for each and try writing and reading one byte.

Are you running your code in a simulator or actually programming it into a chip ?

To confirm it has read back the eeprom byte you have written, compare the two values, if equal turn on one led if not turn on another led instead.

Again post your code to let us see how you are progressing.
 

Good morning
here is my program

Code:
	PROCESSOR	16F84
	#include <p16f84a.inc>

	__CONFIG _CP_OFF &_WDT_ON &_PWRTE_ON &_XT_OSC
	;*****Set up the Constants**** 

COUNT1  equ     30h                 
COUNT2  equ     31h     
COUNT3	equ		32h
COUNT4	equ		33h
COUNT5	equ		34h
COUNT6	equ		35h
COUNT7	equ		36h
COUNT8	equ		37h
COUNT9	equ		38h
COUNT10	equ		39h
COUNT11	equ		40h
COUNT12	equ		41h
Extra	equ		42h

		org	0x00
		goto	Main
Main
		bsf     03h,5	;Go to Bank 1
		movlw   00h           	;Put 0h into W
		movwf   TRISB        	;Move W onto TRISB – all pins set to output
		movlw	03h	;Put 3h into W
		movwf	TRISA	;Move w into TrisA - RA0 , RA1 pins set to input
		bcf     03h,5         	;Come back to Bank 0	
		movlw	0x80	;Put 80h into W
		movwf	PORTB	;Move W into PortB
		movlw	0x00	;Put 0h into W
		movwf	PORTA	;Move W into PortA - All output pins set to 0
		movlw	0x00
		movwf	Extra
Call Settingcounters
		
Waiting							
		Btfsc	PORTA,1		;Check Port1 RA1 Status
		Call	Lightson		;If 1 Call Lightson
		clrwdt
goto	Waiting

Lightsoff	movlw	0x00		;Put 0h into W
			movwf	PORTA		;Move W into PortA - All output pins set to 0
Return							;Return to End of loop 2

Lightson	movlw	0x4			;Put 4h into W
			movwf	PORTA		;Move w into PortA - PortA RA2 Set 1
			Call Loop1			
Return



Loop1   	Btfsc	PORTA,1		;Check PortA RA1 Status
			Nop 			
			decfsz  COUNT1,1	;COUNT1 5 Cycles x 200 
			goto    Loop1 		
			movlw	0xC8		
			movwf	COUNT1      ;Set COUNT1 To C8h - 200 
			clrwdt
        	decfsz  COUNT2,1    ;COUNT2  
        	goto    Loop1
			movlw	0x64
			movwf	COUNT2               
			decfsz	COUNT3,1	;1S Counter         	
			goto	Loop1
			movlw	0x0A
			movwf	COUNT3		
			decfsz	COUNT4,1	;60S Counter
			goto	Loop1
			movlw	0x0A
			movwf	COUNT4	
			call	Warningon
Return	

Warningon	movlw	0Ch
			movwf	PORTA
			Call	Loop2
Return
			
Loop2		Btfsc	PORTA,1
			;Call	Addextra
			Call 	Lightson    
			decfsz  COUNT6,1
			goto    Loop2 
			movlw	0xC8		;
			movwf	COUNT6       
        	decfsz  COUNT7,1     
        	goto    Loop2
			movlw	0x64
			movwf	COUNT7               
			decfsz	COUNT8,1	         	
			goto	Loop2
			movlw	0x0A
			movwf	COUNT8
			decfsz	COUNT9,1
			goto	Loop2
			movlw	0x0A
			movwf	COUNT9
			Call 	Lightsoff
Return




Settingcounters		movlw	0xC8		
					movwf	COUNT1		;Set COUNT1 to C8 - 200
					movlw	0x64		
					movwf	COUNT2		;Set COUNT2 to 64 - 100
					movlw	0x0A
					movwf	COUNT3		;Set COUNT3 to 0A - 10
					movlw	0x0A
					movwf	COUNT4		;Set COUNT4 to 3C - 60
					movlw	0xFF
					movwf	COUNT5
					movlw	0xC8
					movwf	COUNT6
					movlw	0x64
					movwf	COUNT7
					movlw	0x0A
					movwf	COUNT8
					movlw	0x0A
					movwf	COUNT9
					movlw	0xC8
					movwf	COUNT10
					movlw	0x64
					movwf	COUNT11
					movlw	0x14
					movwf	COUNT12

Return

end

i want to save every time the timer start buton pushs
add it to internal eprom so i can know exactly how many times my program works every day

Thx for ur support
 

This code saves these 12 COUNTx values to the EEPROM. But as it uses indirect addressing (pointer) to work, COUNTx registers must be in continuous addresses. You have a problem with their addresses. After h'39', h'3A' follows, not h'40'. You need to change the addresses of COUNT11 and COUNT12. Also, an address for "loopCounter" must be defined.
Code:
SAVE_COUNTERS
bcf     STATUS,RP0      ;Switching to Bank 0
bcf     INTCON,GIE    ;Disabling interrupts
movlw     d'12'
movwf     loopCounter     ;Setting the loop counter
movlw     COUNT1     ;Address of COUNT1
movwf     FSR    ;Setting pointer to the start address
clrf     EEADR     ;Resetting the EEPROM data start address 

SC_LOOP
movf     INDF,W     ;Reading the COUNTx
movwf     EEDATA     ;Setting the data register
bsf     STATUS,RP0     ;Swithcing to Bank 1
bsf     EECON1,WREN     ;EEPROM write is enabled

movlw     h'55'
movwf     EECON2
movlw     h'AA'
movwf     EECON2
bsf     EECON1,WR     ;Starting the write operation.
btfsc     EECON1,WR    ;Checking if the write is complete
goto     $-1     ;Wait until it finishes

bcf     EECON1,WREN     ;EEPROM write is disabled
bcf     STATUS,RP0      ;Switching back to Bank 0
incf      FSR,F     ;Addressing the next COUNTx
incf      EEADR,F    ;Addressing the next EEPROM location
decfsz     loopCounter,F    ;Checking if there are more bytes to write
goto      SC_LOOP      ;Continue operation until all bytes are written

bsf     INTCON,GIE      ;Enabling interrupts
return

And this one restores the COUNTx values from EEPROM
Code:
RESTORE_COUNTERS
bcf     STATUS,RP0      ;Switching to Bank 0
movlw     d'12'
movwf     loopCounter     ;Setting the loop counter
movlw     COUNT1     ;Address of COUNT1
movwf     FSR    ;Setting pointer to the start address
clrf     EEADR     ;Resetting the EEPROM data start address 

RC_LOOP
bsf     STATUS,RP0     ;Switching to Bank 1
bsf     EECON1,RD     ;Starting the read operation.
bcf     STATUS,RP0      ;Switching back to Bank 0

movf     EEDATA,W     ;Reading the data
movwf     INDF     ;And placing it to COUNTx register

incf      FSR,F     ;Addressing the next COUNTx
incf      EEADR,F    ;Addressing the next EEPROM location
decfsz     loopCounter,F    ;Checking if there are more bytes to read
goto      RC_LOOP      ;Continue operation until all bytes are read
return

I haven't tested the code, I wrote it on the fly. Let me know if it doesn't work.
 

thx for ur reply

i dont want to save count to eprom i want to count the number of times the timer starts & save it to eprom then read it later
 

So you need something like this?:
Code:
Waiting                     
      Btfsc   PORTA,1      ;Check Port1 RA1 Status
      Call   Lightson      ;If 1 Call Lightson
      Call IncEepromCounter    ;<<<<<<<<<<<<<<<<<<<<<
      clrwdt
goto   Waiting
I just want to be sure that I understand it correctly before I start coding.
 

yes its right
i want to count & save the results on eprom to read it later

Code:
Waiting                      
      Btfsc   PORTA,1      ;Check Port1 RA1 Status 
      Call   Lightson      ;If 1 Call Lightson 
      Call IncEepromCounter    ;<<<<<<<<<<<<<<<<<<<<< 
      clrwdt 
goto   Waiting

ithink it must be here


Code:
LightsOn
Call IncEepromCounter

thx again
 

I assume that EEADR and EEDATA registers are initialized at the beginning of your program. This code works for 1 byte data only, thus allowing you to count up to 255. An 2 byte counter allows you count up to 65535, but requires a more compicated code. Also, when using buttons, you need some delay after you read the button to avoid bouncing problems which are caused by the imperfection of the physical button.
Code:
IncEepromCounter
bsf     STATUS,RP0     ;Switching to Bank 1
bsf     EECON1,RD     ;Starting the read operation.
bcf     STATUS,RP0      ;Switching back to Bank 0 

incf     EEDATA,F      ;Incrementing the fresh-read data

bcf     INTCON,GIE    ;Disabling interrupts 
bsf     STATUS,RP0     ;Swithcing to Bank 1
bsf     EECON1,WREN     ;EEPROM write is enabled
movlw     h'55'
movwf     EECON2
movlw     h'AA'
movwf     EECON2
bsf     EECON1,WR     ;Starting the write operation.
btfsc     EECON1,WR    ;Checking if the write is complete
goto     $-1     ;Wait until it finishes 

bcf     STATUS,RP0      ;Switching back to Bank 0 
bsf     INTCON,GIE      ;Enabling interrupts
return
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top