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.

Pic16F877A interrupt

Status
Not open for further replies.

williB

New Member
Do all interrupts goto PC 0x0004 and then i should check for which int occured?

what happens after an interrupt occurs?
where does program execution go?

i am using portB , interrupt on change feature.
although i could poll the int flag , i want to have the program execution goto an ISR after the interrupt.
 
williB said:
Do all interrupts goto PC 0x0004 and then i should check for which int occured?

Yes.

what happens after an interrupt occurs?
where does program execution go?

You've already answered those above.

i am using portB , interrupt on change feature.
although i could poll the int flag , i want to have the program execution goto an ISR after the interrupt.

If you use interrupts it jumps to 0x004, where you have to save registers, check what caused the interrupt, and respond accordingly - not forgetting to restore the registers afterwards!.

It's a question of enabling the interrupts or not, you can enable the 'feature' and NOT enable the interrupt, this requires polling the flag to see if it's been triggered.

You can check timer interrupts in my tutorials (which shows the stages required), and MicroChip have a number of application notes about interrupt on change on portb.
 
William At MyBlueRoom said:
On 16 series chips they sure do. You have to save the stack and usually turn off GIE while you mask off and process what called the INT.

The stack is automatically saved (or more precisely the Program Counter is placed on the stack) and the GIE bit is automatically cleared and both are restored by the retfie. You have to manually save the W, Status, PCLATH and any other registers which are used in the ISR such as FSR.

For detail of how to save the context in interrupts, refer to section 12.11 of the 16F877 datasheet.

HTH

Mike.
 
Last edited:
thanks all
i've got all applicable sheets in small piles on a large table , i'm not sure how anyone else does it (writes their programs),but it seems to work for me

i noticed this by looking at the register (memory) map, but they talk about in section 14.12

Since the upper 16 bytes of each bank are common in
the PIC16F876A/877A devices, temporary holding registers,
W_TEMP, STATUS_TEMP and PCLATH_TEMP,
should be placed in here. These 16 locations don’t
require banking and therefore, make it easier for context
save and restore.

a nice place to put global varibles to be sure
 
Yes, the upper 16 bytes are very useful and for this reason the ISR save variables are normally placed there. You can "save" some of this area by only placing W_TEMP (or int_work in my case) in this area, the rest being anywhere in bank 0 (or any other bank).

Here's how,

Code:
interupt	movwf	int_work;    has to be in common area
		swapf	STATUS,W;    move status to W
		bcf	STATUS,RP0;  switch
		bcf	STATUS,RP1;  to bank zero.
		movwf	int_status;  variables can be in non common area now.
		movfw	PCLATH
		movwf	int_pclath
		clrf	PCLATH
		movfw	FSR
		movwf	int_fsr
; ISR code
		movfw	int_pclath
		movwf	PCLATH
		movfw	int_fsr
		movwf	FSR
		swapf	int_status,W
		movwf	STATUS
		swapf	int_work,F
		swapf	int_work,W
		retfie

Mike.
 
hmm i did not know movfw = movf
i never tried using w in the movf instruction

say i wanted to service the PORTB int on change interrupt
after an interrupt occurs , say that the interrupt was not the one i was looking for or expecting.
what happens then?
INTCON <0> holds the flag bit RBIF when the interrupt that i am looking for occurs
mixed in with my seemingly fair grasp of what is going on are going to be some really dumb questions so bear with me

how do i test that the int i am looking for just happened?
 
williB said:
hmm i did not know movfw = movf
i never tried using w in the movf instruction

It's just a 'macro' (or assembler instruction), it actually produces a 'movf xxx, w' line - there are a number of such 'instructions' as part of MPASM.

how do i test that the int i am looking for just happened?

As I said above, check my LED Multiplexing tutorial (or the 7 segment one), it's clearly labelled in the interrupt routine.
 
Nigel i was wondering why they used this , now i know , thanks.
the 'swapf' command is used as it doesn't affect the STATUS register

so if i had three possible interrupting sources , i could just test for each one like you have here
Code:
btfss	PIR1,TMR2IF	; Flag set if TMR2 interrupt
	goto	INTX		; Jump if not timed out
then jump to the appropriate handler ?
that sounds doable
 
williB said:
Nigel i was wondering why they used this , now i know , thanks.

I simply took it off a MicroChip application note :D

so if i had three possible interrupting sources , i could just test for each one like you have here
Code:
btfss	PIR1,TMR2IF	; Flag set if TMR2 interrupt
	goto	INTX		; Jump if not timed out
then jump to the appropriate handler ?
that sounds doable

Yes, that's what it's intended for - in the case of the tutorial it isn't really needed, as there's only a single interrupt source - but it's a good idea to include it anyway, and it's a good habit to get in to!.
 
Nigel
when you put the config word in there on the F877A

I would use the Load Configuration command , then increment up to 0x2007
and load again??
i am not too sure on this point?
or would simply having the pc point to the config 'area' and loading the new config word do ?
2.4.2.1 Load Configuration
After receiving this command, the program counter
(PC) will be set to 2000h. By then applying 16 cycles to
the clock pin, the chip will load 14 bits in a “data word,”
as described above, to be programmed into the configuration
memory. A description of the memory mapping
schemes of the program memory for normal operation
and configuration mode operation is shown in
Figure 2-1. After the configuration memory is entered,
the only way to get back to the user program memory
is to exit the Program/Verify Test mode by taking MCLR
low (VIL).

DS39589B
and what is the test row used for?
 
question
in the segment of code below right after MOVWF CCPR1L , i put movwf PORTD , and blinked some leds, lots of fun :)
but when i used PORTB it didnt work , why??
not only did it not work , the pwm stopped
what am i missing

Code:
LOOP    CALL    ADC 
        MOVWF   CCPR1L 
        GOTO    LOOP 

ADC     BTFSS   PIR1,TMR2IF 
        GOTO    ADC 
        BCF     PIR1,TMR2IF 

        MOVF    ADRESH,W 
        BSF     ADCON0,GO_DONE 
        RETURN 

        END
 
Could it be that one of the port B pins is shorted somehow and therefore causing a brown out. Have you tried inserting a clrf PORTB (and movlw 0xff movwf PORTB) before the loop.

Mike.
 
Could it be that one of the port B pins is shorted somehow and therefore causing a brown out. Have you tried inserting a clrf PORTB (and movlw 0xff movwf PORTB) before the loop.
i hav'nt tried that yet

could the LVP on RB3 somehow be messing it up?
i'm about to try turning it off.
this is what i put in the config "3F3A"
LVP- off
BOR- off
PWRTEN-off
WDT-off
HS osc
 
the flowchart on page 12 of DS39589B tells me i didnt program the config word
but i think i can.
 
wow i believe i did it, i followed the flow chart
the WDT blips are gone from the pwm output
this brings up a question...
there must be a way to trap the WDT event from causing blips in the pwm output?
 
You can stop the WDT from reseting your code by inserting CLRWDT instructions every so often in your code.

In your code above, you could do,
Code:
LOOP    CALL    ADC 
        MOVWF   CCPR1L 
        CLRWDT;                 <--- this should solve the problem
        GOTO    LOOP 

ADC     BTFSS   PIR1,TMR2IF 
        GOTO    ADC 
        BCF     PIR1,TMR2IF 

        MOVF    ADRESH,W 
        BSF     ADCON0,GO_DONE 
        RETURN 

        END

Mike.
 
GIE cleared by hardware!

Regarding the first answers this thread got:

GIE is AUTOMATICALLY cleared by hafdware once the interrupt occurs.

The user must set it again before leaving the ISR code.
 
atferrari said:
Regarding the first answers this thread got:

GIE is AUTOMATICALLY cleared by hafdware once the interrupt occurs.

The user must set it again before leaving the ISR code.

No need. The retfie sets the GIE bit automatically.

Mike.
 
Pommie said:
You can stop the WDT from reseting your code by inserting CLRWDT instructions every so often in your code.

In your code above, you could do,
Code:
LOOP    CALL    ADC 
        MOVWF   CCPR1L 
        CLRWDT;                 <--- this should solve the problem
        GOTO    LOOP 

ADC     BTFSS   PIR1,TMR2IF 
        GOTO    ADC 
        BCF     PIR1,TMR2IF 

        MOVF    ADRESH,W 
        BSF     ADCON0,GO_DONE 
        RETURN 

        END

Mike.
Thanks !!
since you are interrested in Renewable Energy ie the F&P alternator/motor,
i'm making a windmill control , using the F877A
it will measure
current comming from the alt.
battery voltage 12-24 or 48 Volts
windmill RPM
wind speed , all using sensors available to anyone.
Log the data for later analysis(sp) using a laptop
and maybe control the alt output by using pwm in a boost converter setup , if winds are low.
if winds are average , the alt will charge the battery directly
if winds are strong switch to a step down PWM setup
if batteries are fully charged then divert power to another application.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top