Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need Help Eeprom 16f628a

Status
Not open for further replies.

OldCoalMiner

New Member
Could someone write code sample to help me understand how to read/write eeprom so when I power off circuit.
Program will remember what speed routine was running and when I turn power back on it will return back to that speed routine.
 

Attachments

  • kNIGHT RIDER LIGHTS.ASM
    8.7 KB · Views: 165
Could someone write code sample to help me understand how to read/write eeprom so when I power off circuit.
Program will remember what speed routine was running and when I turn power back on it will return back to that speed routine.
Look at Section 13 in the datasheet, starting on Page 89. There's even sample assembly code there. Everything you need. That's the way I learned to do it.
 
You seem to have the speed selected by a switch anyhow.

If that is the case, you don't need an eeprom. If it is pushbuttons that select the speed, you can leave the microcontroller in sleep mode and it will take so little current that your car battery won't notice.

I don't think that your code will work as it is. After RESET, there is no way of getting to any part of the code except SPEED1 or E1.

I suggest:-

Code:
tempswitch  	EQU     H'22'       ; temporary switch setting
MENU
				BTFSC	PORTA,SW1
				GOTO	SPEED1
				BTFSC	PORTA,SW2
				GOTO	SPEED2
				BTFSC	PORTA,SW3
				GOTO	SPEED3	
				BTFSC	PORTA,SW4
				GOTO	SPEED4

                                BTFSC	tempswitch,SW1
				GOTO	SPEED1
				BTFSC	tempswitch,SW2
				GOTO	SPEED2
				BTFSC	tempswitch,SW3
				GOTO	SPEED3	
				BTFSC	tempswitch,SW4
				GOTO	SPEED4

				GOTO MENU

;===============RESET : MAIN BOOT ROUTINE=======================
	      

RESET
		MOVLW	B'00000111'	;Disable Comparator module's
		MOVWF	CMCON
		BSF	STATUS,RP0	;Switch to register bank 1
					;Disable pull-ups
					;INT on rising edge
					;TMR0 to CLKOUT
					;TMR0 Incr low2high trans.
					;Prescaler assign to Timer0
					;Prescaler rate is 1:256
		MOVLW	B'11010111'	;Set PIC options (See datasheet).
		MOVWF	OPTION_REG	;Write the OPTION register.
		CLRF	INTCON		;Disable interrupts
		MOVLW	B'00000000'	; ALL RB PORTS OUTPUTS
		MOVWF	0X86		
					
		MOVLW	B'00110011'	;RA 0,1,4,5 ports are inputs
		MOVWF	0X85		;RA 2,3,6,7 PORTS OUTPUTS
		
		BCF	STATUS,RP0	;Switch Back to reg. Bank 0
		CLRF	PORTB
		
		goto	MENU

;===============================================================

		
	

;========================= MAIN PROGRAM =========================
;=======================SPEED ROUTINES===========================

SPEED1		
                CLR           tempswitch
                BSF           tempswitch, SW1
E1	
		MOVLW	B'00000100'		;LED ON TO SHOW SPEED1 SELECTED
		MOVWF	PORTA
		MOVLW	B'00000001'		;LED1
		MOVWF	PORTB
		CALL	DELAY_ROUTINE		
		MOVLW	B'00000010'		;LED2
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00000100'		;LED3
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00001000'		;LED4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00010000'		;LED5
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00100000'		;LED6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'01000000'		;LED7
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'10000000'		;LED8
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'01000000'		;LED7
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00100000'		;LED6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00010000'		;LED5
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00001000'		;LED4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00000100'		;LED3
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
		MOVLW	B'00000010'		;LED2
		MOVWF	PORTB
		CALL	DELAY_ROUTINE
	
		goto	MENU

SPEED2	

                CLR           tempswitch
                BSF           tempswitch, SW2
	

		MOVLW	B'00001000'		;LED ON TO SHOW SPEED2 SELECTED
		MOVWF	PORTA
		MOVLW	B'00000001'		;LED1
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00000010'		;LED2
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00000100'		;LED3
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00001000'		;LED4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00010000'		;LED5
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00100000'		;LED6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'01000000'		;LED7
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'10000000'		;LED8
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'01000000'		;LED7
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00100000'		;LED6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00010000'		;LED5
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00001000'		;LED4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00000100'		;LED3
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
		MOVLW	B'00000010'		;LED2
		MOVWF	PORTB
		CALL	DELAY_ROUTINE1
	
		goto	MENu
		
SPEED3	
                CLR           tempswitch
                BSF           tempswitch, SW3

E3	
		MOVLW	B'01000000'		;LED ON TO SHOW SPEED3 SELECTED
		MOVWF	PORTA
		MOVLW	B'00000001'		;LED1
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00000010'		;LED2
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00000100'		;LED3
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00001000'		;LED4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00010000'		;LED5
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00100000'		;LED6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'01000000'		;LED7
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'10000000'		;LED8
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'01000000'		;LED7
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00100000'		;LED6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00010000'		;LED5
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00001000'		;LED4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00000100'		;LED3
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
		MOVLW	B'00000010'		;LED2
		MOVWF	PORTB
		CALL	DELAY_ROUTINE2
	
		goto	MENU
		
		
SPEED4	

                CLR           tempswitch
                BSF           tempswitch, SW4
		
E4	
		MOVLW	B'10000000'		;LED ON TO SHOW SPEED SELECTED
		MOVWF	PORTA
		MOVLW	B'00000011'		;LED1,2
		MOVWF	PORTB
		CALL	DELAY_ROUTINE3
		MOVLW	B'00001100'		;LED3,4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE3
		MOVLW	B'00110000'		;LED5,6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE3
		MOVLW	B'11000000'		;LED7,8
		MOVWF	PORTB
		CALL	DELAY_ROUTINE3
		MOVLW	B'00110000'		;LED5,6
		MOVWF	PORTB
		CALL	DELAY_ROUTINE3
		MOVLW	B'00001100'		;LED3,4
		MOVWF	PORTB
		CALL	DELAY_ROUTINE3
	
		GOTO 	MENU

I haven't tested that, but the idea is that after each cycle of the lights the switches are checked. If no switches are set, the last position of the switches, that are stored in tempswitch, are used instead.

I don't think that your code, or even the stuff that I have added, is very neat or nearly as compact as it could be. If you have space, it doesn't matter.
 
Some RLF and RRF instructions and loops would clean up the code up a lot. ;)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top