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.

Interups - Had a lecture on it and I dont understand in the slightest

Status
Not open for further replies.

Spadez

New Member
Hi,

I had a lecture on interups for the PIC16 series and I really didnt understand a thing, from start to finish, in an hour lecture. Its all going to be in my exam so I need to know it inside out.

Can anyone recommend a site where I can try to learn about interups in the easiest to understand way possible?

Also I have attached my sheet of paper from the class. If I could go through the lines trying to understand what is going on it would really help me out.

One of the first things it says is this:

movlw b'00000111' // Set timer to max (1:256 = TMR0 value)
movwf OPTION

How is moving the binary of "00000111" setting the timer to max?

movlw b'10100000' // Enable interrupt (bit7) and timer
movwf INTCON // For use with TMR0 (bit 5)


How does the binary of "10100000" enable the interupt bit 7?

Thank you for any help you can give.

James
 

Attachments

  • pichelp.jpg
    pichelp.jpg
    182.5 KB · Views: 280
Do you understand interrupts in general ?

Have you learned enough PIC16 ASM to understand the ASM here ?

If so you can find the answers to your questions in the processor data sheet. You MUST learn to understand these.

movlw b'10100000' // Enable interrupt (bit7) and timer
movwf INTCON // For use with TMR0 (bit 5)


How does the binary of "10100000" enable the interupt bit 7?
Look up the register INTCON in the processor data sheet and read about timer0.
 
On the 16 series there is only one interrupt vector 0x0004
There are enable bits (IE) and flag bits (IF)
Flag bits are set automatically when an interrupt occurs, eg: TMR0 overflows will set the T0IF bit
If the T0IE bit is set (enabled) the PC (program counter) will goto 0x0004, if T0IE is not set the T0IF is ignored.
On the 16F and lower series you need to save W & STATUS as your interrupt routine might modify them.
At the end of your interrupt routine you restore W & STATUS plus clear the T0IF bit
INTCON,2 is often written INTCON,T0IF which is easier to understand, (these names are assigned with the include file at the beginning of your program)
 
Hi,


movlw b'10100000' // Enable interrupt (bit7) and timer
movwf INTCON // For use with TMR0 (bit 5)


How does the binary of "10100000" enable the interupt bit 7?

Binary bit in the PICs are numbered from the least significant, bit 0 to the most significant bit, bit 7.

So b'10100000' has bits 5 and bits 7 set, and bits 0, 1, 2, 3, 4 and 6 are clear.

In the INTCON register, set bits turn on the different interrupts. Bit 5 turns on TIMER0 interrupt, so that when TMR0IF is set, an interrupt will occur.

Bit 7 also needs to be on because it is the Global Interrupt Enable, so without that none of the interrupts will work.

When TIMER0 overflows (goes from 0xff to 0x00) that sets TMR0IF (Timer 0 Interrupt Flag), so the setting of that causes an interrupt, as long as bits 5 and 7 of INTCON are set.
 
Ive been doing some reading today and I think I understand this:

The 3 LSB's of the OPTION register contain the bits needed to set the prescale on the PIC. By moving 0000 0111 to the OPTION register the prescaler is set to 1:256.

Using the internal clock of 4MHZ and a prescaler of 1:4, then the TMR register runs on a frequency of 1MHZ.

The global interupt bit is bit7 which has to be set to 1 for any of the other interrupts to take effect. By setting bit 5 to 1 then the TMR0 is also set.

INTCON register has both the enable bits to set the relevant interrups, but also the flags for the TOIE, INTE and RBIE, which states if the interrupt has been triggered.

Im going to keep reading into this, but I very much appreciate your help. If I have got any of this wrong up to this point please tell me.
 
Last edited:
Ive been looking at this today. I get that you want to backup the working. What I really dont understand is why you would want to swap the status nibbles. It just makes no sense to me. Would someone please explain this because I cant find any information on it online.
 
Swap does not affect the STATUS bits when saving W to a register. It's an old trick needed in 16F PICs, the 18F can save & restore these automatically.
 
I dont understand, im a bit of a simpleton.

Lets say STATUS is this:

11110000

The command swapf STATUS,W will put 00001111 in W. Then its swapped back to get the original copy of STATUS from W again later.

What I dont understand is, why is the swapf needed instead of movfw. How is STATUS changed when movf is used?
 
Last edited:
These are the STATUS bits:

bit 7:
IRP: Register Bank Select bit (used for indirect addressing)
0 = Bank 0, 1 (00h - FFh)
1 = Bank 2, 3 (100h - 1FFh)
The IRP bit is not used by the PIC16F8X. IRP should be maintained clear.

bit 6-5:
RP1:RP0: Register Bank Select bits (used for direct addressing)
00 = Bank 0 (00h - 7Fh)
01 = Bank 1 (80h - FFh)
10 = Bank 2 (100h - 17Fh)
11 = Bank 3 (180h - 1FFh)
Each bank is 128 bytes. Only bit RP0 is used by the PIC16F8X. RP1 should be maintained clear.

bit 4:
TO: Time-out bit
1 = After power-up, CLRWDT instruction, or SLEEP instruction
0 = A WDT time-out occurred

bit 3:
PD: Power-down bit
1 = After power-up or by the CLRWDT instruction
0 = By execution of the SLEEP instruction

bit 2:
Z: Zero bit
1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not zero
bit (for ADDWF and ADDLW instructions) (For borrow the polarity is reversed)

bit 1:
DC: Digit carry/borrow
1 = A carry-out from the 4th low order bit of the result occurred
0 = No carry-out from the 4th low order bit of the result
bit (for ADDWF and ADDLW instructions)

bit 0:
C: Carry/borrow
1 = A carry-out from the most significant bit of the result occurred
0 = No carry-out from the most significant bit of the result occurred
Note: For borrow the second operand the polarity is reversed. A subtraction is executed by adding the two’s complement of. For rotate (RRF, RLF) instructions, this bit is loaded with either the high or low order bit of the source register.

I think its fair to rule out that bit 0-4 arnt affected by a movwf command. That leaves 5,6,7. Ive got to admit my knowledge on the basics is probably hindering me with this later stuff, but is it something to do with a movwf command would affect the bank, which would affect one of the 3 MSB's of the STATUS register.

I really am trying to get this on my own but im probably overlooking something simple.
 
Thank you for the link. So, it is the Z flag.

So, from what I understand, movwf can be used to save the STATUS register into W because it saves the STATUS register before it affects the Z bit, however when trying to restore the STATUS register from the saved copy, the movwf would be incorrect because the Z flag had since been changed.

As a result its much better to use swapf for the copy and restore, since the Z flag is unaffected and by swapping the nibbles twice, you get the same result back.

However, what I dont understand, and am probably not required to understand (but I think it would help for a better understanding) is why the zero flag is affected by the movwf command.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top