Do I need interrupts in this program?

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
Hi,

I'm programming an ATMEGA16 Microcontroller.

This what I am trying to do:
I need to output a 50Hz PWM Signal to control a servo. I want to vary the pulse width from 800uS to 2200 uS by using push buttons with deboucing. I also need to display the actual pulse width on 7 segments displays. Another feature is that if I keep the button pressed after 2 seconds (let's say) the pulse widh should start incrementing/decrementing by 1 until I unpress the button.

This is the code fragment I used to set the pwm signal:

TCCR1A |= 1<<WGM11 | 1<<COM1A1;
TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS11;
ICR1=19999; //set ICR1 to produce 50Hz frequency
//(8000000 / 8 / 20000 = 50hz)
OCR1A = 1000; //set a position for the servo

I know how to display a certain value on displays. As I have four 7-segments displays (with just one port for data) I need to set a delay for Multiplexing the displays. In general, I know how to control peripherals.
My question is: When it comes to combine all the features (displays, debouncing, pwm signal, etc) in one main code, what should I take into account? Do I need interrupts or something special? Should I use interrupt instead of _delay_ms(ms) for the delay?

I am starting to learn how to program AVRs, and I do not have much experience in C programming. So I am trying to learn basic things first.

Thank you.
 

shteii01

Joined Feb 19, 2010
4,644
Interrupt will interrupt any running instructions to execute a specific set of instructions.

delay function will stop execution of instructions for a defined period of time.

I think in your case either approach would work fine because the speed of what you are trying to accomplish is low.


I don't know the requirements of your project... If I were you, I would change the display from four 7 segment to one lcd and use i2c/spi to show stuff on the lcd. This way you use 2 to 4 pins to display stuff and the code is straight forward.
https://www.ebay.com/itm/0-91-128x3...802654?hash=item3d2d58d99e:g:bqwAAOSwo4pYSTj~
 

spinnaker

Joined Oct 29, 2009
7,830
Interrupt will interrupt any running instructions to execute a specific set of instructions.

delay function will stop execution of instructions for a defined period of time.

I think in your case either approach would work fine because the speed of what you are trying to accomplish is low.


I don't know the requirements of your project... If I were you, I would change the display from four 7 segment to one lcd and use i2c/spi to show stuff on the lcd. This way you use 2 to 4 pins to display stuff and the code is straight forward.
https://www.ebay.com/itm/0-91-128x3...802654?hash=item3d2d58d99e:g:bqwAAOSwo4pYSTj~
And good old fashioned polling is a lot easier to do for someone just starting out. Use polling where you can like with switches . While a timer interrupt can make live a little easier for someone used to working with interrupts, there is no reason polling can't be used there too. You just need to know that you could lose a few ticks while getting around to polling the timer.

And I have never had to use interrupts for delay. A delay function is so much easier to use. Just as long as you keep you delays short, say in the milliseconds, the user will never notice if everything else is written properly.
 
Top