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.

tm30 interrupt problem- i REALLY need help

Status
Not open for further replies.

tylerdurden

New Member
ok...
im going crazy with this issue.
i have a pic16f84 with a 4x3 keypad and an lcd conected to it. and i also made a kind of "alarm" program which stores 4 numbers in the memory. the program works perfect. it gets the key pressed and displays them on the lcd. but now i have to add a clock which is incremented by the tmr0 interrupt. by adding this to my program

Code:
				bsf		_rp0
				movlw	b'10000101'		; Prescaler e 1:64
				movwf	opcion
				bcf		_rp0
				movlw	b'10100000'		; Habilito int por TMR0
				movwf	intcon
				clrf	tmr0

				clrf	msec
				clrf	sec

Code:
int_reloj                      
			movwf	saveW	;save context
			swapf	saveW, f
			swapf	status, w
			movwf	saveS
			
			nop
			nop
			nop

;ISR_Code	bcf	_rp0
;			clrf 	tmr0
;			incf	msec,f		
;			bsf	timef,msf
;			movf	msec,w	
;			sublw	.100		; 100 msec?				
;			btfss	_z
;			goto	pop			
;			clrf	msec		
;			bsf	timef,sf
;			incf	sec,f


;			bsf	_rp0
;			bcf	porta,3
;			bcf	_rp0


;			btfss	porta,3
;			goto	prendo_led
;			goto	apago_led
;prendo_led		bsf	porta,3		; led on
;			goto	pop
;apago_led	bcf	porta,3		; led off
;			goto	pop

pop
			bcf		_t0if		;clear timer overflow
			swapf	saveS, w
			movwf	status
			swapf	saveW, w
			retfie

the program still works OK!!! but when i add more nops or simply I take out the coments to make the interrupt actually DO something the program behaves different. by different i mean, I get to see al the predefined messages on the lcd but where it is suposed to show the values stored in the eeprom or the values entered in the keypad I get strange (or just random) characters.
e.g. "password ((((" ---> intead of "password 9999"
i tried saving almost every register which might be changed during the interrupt but that doesnt seem to be the problem since using the interrrupt with more nops also produces the "funny" behaviour.
please I really need help, i searched all over the net and cant find any solution. I dont want to mess up all my code doing stupid things...
i would be very gratefull if someone could give me a clue.

Fernando Aramendi

Ps1: let me know if you need more data
Ps2:Happy new year!!!
 

motion

New Member
One problem I see with your context save is that you assume you are in bank0 upon entering the ISR. However, depending on your program this is not always the case. This means that you have to reserve two locations for saveW, one each for bank0 & bank1, having the same offset. This will prevent the ISR from overwriting another filereg. I would recommend locations 4Fh and 0CFh respectively for saveW.

After saving the W register, you should select a specific bank by setting or clearing the bank select bits rp0, prior to saving the status register or else it will suffer the same problem.

Code:
int_reloj                      
         movwf   saveW   ;save context 
         swapf   saveW, f 
         swapf   status, w 
         bcf      _rp0         ; select bank0
         movwf   saveS 
          
         nop 
         nop 
         nop 
 
         bcf      _t0if      ;clear timer overflow 

         swapf   saveS, w 
         movwf   status 
         swapf   saveW, w 
         retfie
 

Exo

Active Member
motion said:
you should select a specific bank by setting or clearing the bank select bits rp0, prior to saving the status register or else it will suffer the same problem.

You're a little wrong on that one.
you need to save status FIRST, and then select a specific bank.
The only way to do this is to use a shared memory space for status (one that is availabe in all banks)
The same applies to W-reg. Since you have no idea in what bank you are when you enter the interrupt code you should save W into a shared memory space

If you alter the bank before saving status, then status will be saved with the new bank already set. When the interrupt returns the main program will continue with possibly a wrong bank

this is, of course, only the case when using a pic with multiple memory banks.

TylerDurden, I suggest you take another good look at the pic's datasheet. There is a section somewhere that explains context saving.
 

motion

New Member
You're a little wrong on that one.
you need to save status FIRST, and then select a specific bank.

No, you are wrong on this one! If you checked the code I supplied, the bank is set after the status register image is moved to the W register but before it is subsequently saved to memory. This way the value of the status restored before retfie is the unaltered value. This is the same as the sample code supplied in the datasheets.
 

tylerdurden

New Member
OK, I tried motions code... but I still get the same thing. everything displayed on the lcd looks OK, except the numebers stored in the eeprom and the ones introduced by the keypad.
i tried this also

saveW equ 0x4f
saveS equ 0x4e

but i get the same thing. motion says i have to use for saveW 0x4Ff and 0xCF but i cant put:
saveW equ 4f
saveW equ cf
because i duplicate a label.
still i dont know how this may have something to do with my problem, as i said using the interrupt i posted (just push and pop) the programs works ok, when i add 3 nops in the middle, still it works ok, but when i add 9 nops or some usefull code i get this dummy characters on the display (only the numbers).
thanks a lot to motion and Exo, and to those who are willings to help me.
i look foward to hearing from you soon...
Fernando Aramendi
 

motion

New Member
Oops, I rechecked the datasheet and although the PIC16F84 has two banks, the general purpose registers are mapped on both banks. Therefore the problem I mentioned isn't one in the case of PIC16F84s. I must admit I don't remember having used interrupts on PIC16F84s in over 10 years in writing code for PIC microcontrollers.

I rechecked your code and I can't see anything wrong with the ISR. Maybe there is something wrong with the LCD writing routine. Maybe it is affected by being interrupted while writing to the LCD. You should consider using polling the t0if instead of interrupts, at least, just to test the code. Even with a prescale of 1:2, you can have up to 512 instruction cycles (half of F84's code space) to execute before needing to service t0if.
 

tylerdurden

New Member
please, someone has to know the answer, i tried almost everything... if you need more info about the project or more code to be posted please tell me.
thanks a lot
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Top