HomeХобби и стильRelated VideosMore From: Pianoman0488

PIC uC Tutorial #10: "Hello, world!" Interrupt-on-change style

125 ratings | 21241 views
Location of code and other future goodies: http://paulfmcgowan.com/ In this video, we get our feet wet with the interrupt structure of the 12F683 and make an example with the interrupt-on-change (ioc) functionality of the chip.
Html code for embedding videos on your blog
Text Comments (58)
Elec3D (3 years ago)
Hello I'm µC PIC expert. In ISR: you needn't INTCON.GIE=0; (it's HW) ans NEVER do INTCON.GIE=1; If interrupt arrive just before RETFIE (return from interrupt)?
Elec3D (3 years ago)
+Paul McGowan, Jr. Datasheet: PIC12F683 => 12.4 Interrupts When an interrupt is serviced, the following actions occur automatically: • The GIE is cleared to disable any further interrupt. • The return address is pushed onto the stack. • The PC is loaded with 0004h. The Return from Interrupt instruction, RETFIE, exits the interrupt routine, as well as sets the GIE bit, which re-enables unmasked interrupts.
Elec3D (3 years ago)
+Paul McGowan, Jr. Hello, All I'll say is for ISR part: first part I said "you needn't" so no problem but it's redundant like you say. Second part: I mean INTCON.GIE=1 is automatic (HardWare like INTCON.GIE=0 in ISR) but if Interrupt arrive just between INTCON.GIE=1(your software) and the return of interrupt => It will be a big problem because a new interrupt arrive in the current ISR. I let you understand all the consequences
Pianoman0488 (3 years ago)
+thth1236 Hello, I am not sure what you are saying.  I understand the first part - perhaps it is hardware and my code is redundant - but I do not think it hurts anything.  As for the second part of the comment, I do not follow what you are saying.  In short, I tell the program exactly what I want done so that there is no ambiguity.  Maybe you could clarify the portion of the comment regarding INTCON.GIE=1.  If I do not do that, the bit that allows interrupts to be utilized is not set and the program wouldn't do what I want it to do....................
Ronald J Kirby (3 years ago)
i have totally enjoyed the series so far  and it is so easy to not see something when videoing on the fly  but i think you have done really well and i am going to keep on with  every video  thank you so much for making these vids
Zachary Griffee (4 years ago)
Great video series so far. There was an element of suspense in this video as l was actually yelling at my screen as you wrote that curly bracket wrong. I found it amusing and ironic because I have ran into this simple mistake myself plenty of times and been coding for at least 15 years.
pjohn355 (4 years ago)
hi excellent tutorials i specially liket inturrupts tutorial,  can uoy do a tutorial to distinguish which line caused the inturrupt and say if uou click on line 1 led blinks once if you inturrupt line 2 then led blinks 2 times or blinks another led on another line. i am starting to learn electronics thanks  abdul
technics6 (4 years ago)
Paul, your IF was closed with "{" instead of "}".
Pianoman0488 (4 years ago)
I think awhile back someone caught that.  Fortunately, this compiler is tolerant to those small errors :)  Thanks for watching
Milan Karakas (4 years ago)
Good example, thanks. What causes interrupt when you touches the probe is not mains, but ESD (Electro-Static Discharge)event. If you move before touching this pin, your body (or clothing) rubbing against something (your sleeve, hand, or something else), so that your whole body become slightly positive or negative charged.  Be careful touching unprotected inputs. PIC has protection against ESD to some degree, but you can't be sure when your body will be charged over this level of protection.
Mihai Nicolaescu (5 years ago)
Very useful. Thanks!
ROBO Sapien (5 years ago)
Hi, PM Your videos are simple to understand and awesome...!!! I'm trying to learn from your knowledge, but couldn't get through controlling multiple servo motor (8 servo motors using pot sending motion data through UART Bluetooth communication) using PIC16F877A. Could you please throw some light on this topic, so that your students can understand.
AliSoliman (5 years ago)
Great Video, Thank you!
Nishant Sood (5 years ago)
I was able to make it work long before sorry got off mind to my comment here
Pianoman0488 (5 years ago)
Thanks!!! You're welcome!
Pianoman0488 (5 years ago)
As long as the compiler understands which register you're talking to, and you know you're setting or clearing the bits you want to set or clear, it should work. Usually, if the compiler doesn't understand, it throws an error on compile - but not always.
Pianoman0488 (5 years ago)
Make sure your input is debounced. Other than that, make sure there are no other registers that need set to tell the microcontroller about the edge triggering desired. Also, are pull-ups enabled? Imagine if you have a falling edge from V+ to ground, and then the source of this signal goes into a high-impedance state. If you have a pull-up (or down, or whatever), it will cause the microcontroller input to flip back to V+ potentially, which is the rising edge you may have the device set to see.
Lana89 (5 years ago)
Your videos are amazing! I'm a beginner and your tutorials are EXACTLY what I was looking for! Thank you and well done. :)
Nishant Sood (5 years ago)
did this >> Did if(INTCON.INTEDG == 0) still getting interrupt on both edges
Pianoman0488 (5 years ago)
They're built-into MikroC. Click "view -> library manager". If your chip has a module for it, it will most likely have a corresponding library for it...
Nishant Sood (5 years ago)
Ya so by reading the Datasheet I got the Register INTEDG, so within the ISR im doing a condition like if(INTCON.INTEDG==0) then it means the Interrupt is on the FALLING edge and I need to take some action!
Nishant Sood (5 years ago)
Another thing I want to know is how to get libraries? from MikroC into the IDE ,I mean I dont know the place from where to download them, For Example I didn;t had the Hardware UART library in my IDE its 4.15v
Pianoman0488 (5 years ago)
Look into the INT pin. The configuration register for this pin (datasheet "special features of cpu -> interrupts -> INT) shows that this pin is an edge-triggered-type of interrupt pin and that it can be configured for those two cases.
Nishant Sood (5 years ago)
What about INterrupts on RISING or FALLING edge? , How to go about them?
Bessenyei Szilárd (5 years ago)
with INTCON.IOC2 = 1; also work? (in the video you used IOC.IOC2 = 1;)
Pianoman0488 (6 years ago)
True story! IOC pins are what I'd use for something like a button array or other simple task. That INT pin would be a pin I'd use to allow me to know when, for example, data is ready in a peripheral ADC or something along those lines. Schmitt triggered inputs are precisely what you say as well - logic gates with hysteresis to make them immune to noise. Example: external timer input clock. The '4550 has quite an array of interrupts though, that's for sure. Thanks for continued feedback.
stelenes (6 years ago)
A stupid typo to anyone just sitting here watching the picture show carefully is not the same as the guy coordinating his video, 'lesson', breadboard, keystrokes, exposition, and where he's going next for some reversed bracket typo. Me & maqsoodu volunteer to be the video crew next time ;-), someone else can coordinate the breadboard stunts, and then Piano can be in peace to focus. What was funnier was he then corrected it without realizing. Good point that MikroC was actually being helpful
stelenes (6 years ago)
Hi, got a little more on the difference between IOC&INT -clears me up now & I'll share. You're right/ahead of me mentioning the IOC doesn't keep track of the edge (&requires accounting&polling). But a very important distinction:the INT pin is more reliable than the IOC pins. INT by default has a schmidt trigger (sharp, reproducable voltage level trigger w/desireable hysteresis for an accurate 'snap') &IOC are generally discouraged for critical reliable input reading but INT is dependable there.
stelenes (6 years ago)
WOW Piano...Can I say *thanks*. We need an index page of all the PIC videos w/titles &links for easy reference. Looked at your next interrupt video &it was great. I'll take your word for the "other functions". So far it seems you can get rise &fall edge from various lower interrupts also in the trees... maybe they'll be a chance to tackle something w/something else to learn; your 4550 ref. says "INT" has unique waking out of sleep mode. Maybe that's the special feature meriting the 'INT' deal!?
Pianoman0488 (6 years ago)
I think the INT pin has a few other capabilities than just the IOC functionality. The IOC, or interrupt-on-change, simply throws a flag when ANYTHING changes. It doesn't care whether it was a rising or falling edge, etc etc. The INT pin, however, has a few more parameters than can be discerned from the interrupt event. [...] feelings affirmative; check out the '4550's datasheet section 9.7, first paragraph. It'll tell you whether the edge was rising or falling...good luck!
Pianoman0488 (6 years ago)
When you're in the middle of teaching something, it's interesting how easily you miss the most simple mistakes. Thanks!
maqsoodu23 (6 years ago)
the reason why it wasn't compiling was because of your { } brackets. Before you compared you code to your original code, you should have made sure about the the parenthesis in the interrupt routine, right after " blink (); ". No harm done.
stelenes (6 years ago)
Confusion mostly gone: 683 datasheet has the "IOC" group:877A corresponds w/various PORTB pins although they are not "IOC" as you used,similar function works in both chips. In 683, there is a specific "INT" designation on pin5(GP2), like the pin33(RB0) in the pinouts. Seeing 877A's sole INT, same as 683, but no explicit IOC's confused me.IOClike function is taken from PORTB.x's enabled; INT's are other unique option requiring less coded reg levels? Or how exactly is INT on pinout special?
Pianoman0488 (6 years ago)
Looking at the datasheet under the "Interrupts" bookmark, I see that PORTB has a few edge-triggered external interrupts PORTB 2:0 as well as PORTB interrupt-on-change capabilities on PORTB 7:4. This implies that, besides single pin interrupts, any change whatsoever on PORTB will cause AN interrupt in which you have to go look at what changed. Make sense?
stelenes (6 years ago)
I'm very new at this so I may not express myself correctly as I'd like. But in this example I look at the 877A datasheet and see the only "external" interrupt is on pin, "RBO" unless I'm wrong. It seems that 12f683 has multiple external input pins capable of detecting signals to trigger an interrupt ... this is where I'm confused. Since my first question I've learned of more types of interrupts but in this case I was focusing on the type you could put a probe on like the video. thanks!
Pianoman0488 (6 years ago)
...looking at the interrupts section. Not sure what you mean when you claim to see "only one interrupt"? Alternatively, you can setup a timer to "interrupt on overflow" which would allow you to periodically check if your voltage input is satisfying your condition(s)...it all depends upon how fast this decision needs made...
Pianoman0488 (6 years ago)
All the PICs I've worked with have many interrupts that usually trigger one global interrupt line. At this point, you'd go to the ISR(interrupt service routine) and figure out 1. which interrupt flag has been set from what phenomenon and 2. what to do about it. Then, you exit the routine and continue in the main program. The 12f683 only has 6-pins, so it makes sense that they tried to jam-pack all interrupt features they could into the entire "port". The 16f877a is quite capable as well...
Pianoman0488 (6 years ago)
1. Yes. If the voltage you're wanting to monitor is generally fixed, you can use an internal comparator to cause an interrupt to occur. PICs usually let you define the reference voltage in a number of ways: internal DAC, set to V, set to Vgnd, set to external pin, etc. - then, on rising or falling edge of comparator output, interrupt is triggered.
stelenes (6 years ago)
(cont'd) using an if(mV>X){expression}... is the difference that when using an interrupt it is necessary to stop all other processes to update variables and the like? Trying to think of a practical difference between the two branching options. 2) Looking at the 877A datasheet interrupt logic, I only see one interrupt. This doesn't make much sense to me since your 12f683 has the capability on all the GPs. Can you comment on this lesser ability of the 40 pin chip?
stelenes (6 years ago)
Thanks for putting the time into developing your PIC tutorial resource. I introduced myself to uC's a few days ago and picked your Video #1 for the job. Watched the first few, but there is much material and I need to actually do stuff with them instead of sit back and watch. I actually skipped a bit to this one, since I have two questions. 1) Can a similar function to interrupt be made by simply monitoring any input, in my project to be, an analog input to see if it exceeds a certain voltage?
Pianoman0488 (6 years ago)
Interrupts take some getting used to. Keep messing with it, they're good to know and very useful.
Marc Paradis (6 years ago)
very complicated... :(
Alan Alban (6 years ago)
I have a PIC18, would u happen to know the code to initialize the interrupt module on that chip? i don't see any IOC registers on mine. thank you!
Pianoman0488 (6 years ago)
B0 = bit zero of the GPIO port on the 12F683
Marc Paradis (6 years ago)
GPIO.B0 ? what is B0 please?
Marc Paradis (6 years ago)
The datasheet is amazing!!! :O ;s
Pianoman0488 (6 years ago)
I think that is correct. I wasn't ever trained professionally in C per say, I learned on the job. I have seen well-written code with what you describe. Thanks for watching.
leeYT321987 (6 years ago)
I'm pretty sure that you can call functions that are defined below your current function if, at the beginning of your script, you declare a "function prototype" for any functions that may be called "out of order".
leeYT321987 (6 years ago)
It wouldn't compile because the closing brace of the IF statement in the ISR was backwards.
deviantmindset (6 years ago)
If I remember correctly, MPLAB comes bundled with a C compiler. (MPLABX at least) Also, check out Gooligum Electronics (google it), they have some great tutorials. Both ASM and C.
Pianoman0488 (6 years ago)
I hope to get some MPASM videos up in the future. Thanks for watching!
IILaymanII (6 years ago)
thanks this helped alot but i found it difficult because i right in MPLAB and thats only asm lol
Pianoman0488 (6 years ago)
I've totally done that. hahaha. All is well now?
TheBionicbone (6 years ago)
OK, i was being gormless. System procedure is "interrupt" not "interupt" LOL. How annoying is that when you've spent hours trying to find out why its not working :)
TheBionicbone (6 years ago)
Well, I'm stuck..If I code this in C then nothing really happens. If I use BASIC (my preferred) I get the interupt to work once but then ignore further changes. In the interupt procedure I've stopped GIE, do the blink, clear GPIF and restarting GIE. The datasheet states on page 32 that I must read the GPIO to end the mismatch before I clear the GPIF. Is this just a case of i = GPIO? because this still does not work? Also is the crystal doing anything in your circuit?
Pianoman0488 (6 years ago)
Stay tuned. Either the 18F4550 or the 16F877 is coming up soon...
Mike Crumby (6 years ago)
PS.. As an example the PIC18F452 has a more complicated Interrupt logic chart and is a bit confusing. I know it is not great for beginners, but the semi-beginners probably need a little hand holding as well. I still have trouble with some of the larger chip Logic charts. :(
Mike Crumby (6 years ago)
I would love to see these examples using a fairly recent chip like the PIC16F887 or something in that range. A lot of demo boards use these and it would seem to give a lot more options as well. Great videos though.. I play with both the PIC and ATMEL chips, even though there are more tutorials for Atmel, I love seeing the PIC examples and you are helping that arena. I love both chips. Keep up the work.. I subscribed too so I will be keeping an eye on them.

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.