NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Interrupts
March 26, 2011 by hariharan |
Normally, hardware interrupts are programmed by using the isr() notation. In the crystal time project, signal() notation is used. What's the difference? And also, how do we program hardware interrupts to do something first time, second time, and third time? |
---|---|
March 26, 2011 by bretm |
In older versions of avr-gcc SIGNAL was the only way to do it, so you'll see this more often in older code. The difference is that interrupts are disabled inside an ISR but are enabled inside a SIGNAL. That means SIGNAL code has to support re-entrancy in many cases (because another interrupt can happen while you're processing the first one) which makes the code harder to write correctly, so ISR is recommended over SIGNAL for new code.
|
March 26, 2011 by hariharan |
Then, will this code work?
|
March 26, 2011 by Ralphxyz |
Short answer NO!! Now look up:
What interrupt vector do you want to use? Then you should limit what is going on while handling your interrupt, what would happen while your interrupt was attempting to do the printfs another interrupt was called? Things like doing prints should be done in your main() function probable in a while loop. Back to the books. Ralph |
March 26, 2011 by hariharan |
will this work?
|
March 26, 2011 by Ralphxyz |
Well did you try it? For one thing the ISR goes outside of your main() function like you had it originally. When I said to do your prints in your main() functions you would set a variable in the ISR and then print that variable in the main(). Does asking if a code set works really make much sense? What do expect us to do load it and try it out for you? You get much better answers if you were to ask "Why doesn't this code work". Ralph |
March 26, 2011 by hariharan |
My Bad! |
March 26, 2011 by hariharan |
I tried a simpler code for getting to know interrupts. I made a code that will make a led blink at a slower rate than before when there is a interrupt. this code has the isr outside the main(). But why is'nt it working?
|
March 26, 2011 by leedawg |
Your code does not work because you have not set up the interrupts yet. You need to define what type of interuptt is going to happen. As your code reads right now, the AVR has no way of knowing that when you push a button to fire the interrupt that you have defined. So it looks as though you have defined the interrupt fine but you need to tell the avr what is going to make the interrupt fire and you set that up in your int main routine, per the data sheet for the avr. Let me see if I can dig up some code to show you in a bit. Lee |
March 26, 2011 by leedawg |
Alrighty here it is you need to add something like this to your main routine to set up the interrupts for the avr
That ought to do it so your code should read something more like this..
Good luck it might not run because I may have made an error somewhere you compiler will tell you when you try to compile the code but the principle is what you need. Go read the data sheet on page 70 in the PDF file and it will tell you all about the pin change interrupt register and mask register that is where I got all the info on it. Let us know how it went. Lee |
March 26, 2011 by leedawg |
I made an error that I just saw in my code in the comments where I wrote PC4 I meant PC5 which is the pin you defined as your input pin. Good luck |
March 26, 2011 by hariharan |
Even though, it gives an error message. Like: in function _vector_4, expected '>' before '{'token, expected expression before '}' token |
March 26, 2011 by hariharan |
srry, Expected ')' before '{' token |
March 26, 2011 by hariharan |
I found the error! in line 47, there should be another ')' before '{' thanks every one for helping me! i have another question. how do you program a timer interrupt? in the crystal clock program, they use a signal() notation. can u do that with a ISR() notation? |
March 26, 2011 by leedawg |
Yes just look up the library its under your start menu and will tell you all the things you can use in the interrupt library. ISR will work fine and you can use another command for the timer interrupt. And im sorry I missed a brace like I said I was just typing it into the forum here and did not run it through my compiler which usually always tells me I have an error and have to track it down. Glad to hear it is working for you now though. Lee |
March 26, 2011 by Ralphxyz |
And once again go back to the specsheet and search the Nerdkits forum for Timer Interrupts, you will find lots of examples in the forum and the specifics in the specsheet. It don't come easy there is certain steps you just gotta do, like reading the specsheet and seeing what has been said before. You are not the first to ask these questions, you rarely will be. I am glad you have made good progress. Ralph |
March 27, 2011 by hariharan |
i wanted to modify the crystal time code to display hours and minutes. so, i looked at the libc document and modified the code. But it does not work. why?
|
March 28, 2011 by hariharan |
i figured out that i shouldn't have used #pragma vector=TIMER0_OVF_vect __interrupt void MotorPWMBottom(). but how do i program that m+=1 when the_time = 60 and h+=1 when m = 60? i want to fire a interrupt when the_time= 60, but can i do it without using any pin change interrupt or timer interrupt? |
Please log in to post a reply.
Did you know that NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...
|