Question on Controlling Timer/Counter's via Assembly vs. Arduino(C)

For the past week i’ve been verifing the differences between to programs (Assembly & Arduino) running on the same target(ATmega328P). The two programs perform the same operations which is to basically use the Timer/Counter_1 and Timer/Counter_2 to emit two PWM signals on separate pins continuously. Before someone says “there are better ways to examine the differences between the two languages” I just want to say “I agree there are” but i’m examining specifics in relation to my application and to just further my knowledge.

I have a question that I can’t seem to answer myself yet and thought I would get a better answer from you wonderful fellows that have had more experience in using the numerous libraries out there for PWM that use the Timer/Counters. There seems to be more than one library out there that do this.

In assembly, I can enable the timer/counters and have them running in the background before I enter my main loop. However, in my Arduino program I can’t seem to get my program to do this. I tried throwing the lines of code that does this in the void setup(){} but it doesn’t compile. For example the following just won’t compile.

#include <PWM.h>
int pwmpin1 = 2;
int pwmpin2 = 8;

void setup(){
pinMode(pwmpin1, OUTPUT);
pinMode(pwmpin2, OUTPUT);
SetPinFrequency(pwmpin1, 125);     //frequency
pwmWrite(pwmpin1, 64);     //duty cylce
SetPinFrequency(pwmpin2,250);     //frequency
pwmWrite(pwmpin2, 64);}     //duty cycle

void loop(){}

Therefore, i’m forced to do the following in order for it to compile

#include <PWM.h>
int pwmpin1 = 2;
int pwmpin2 = 8;

void setup(){
pinMode(pwmpin1, OUTPUT);
pinMode(pwmpin2, OUTPUT);}

void loop(){
SetPinFrequency(pwmpin1, 125);     //frequency
pwmWrite(pwmpin1, 64);     //duty cylce
SetPinFrequency(pwmpin2,250);     //frequency
pwmWrite(pwmpin2, 64);}     //duty cycle

Therefore, is there some way I can enable these counters in the background without doing this inside the main loop? I can’t seem to put down the words to explain what i’m wanting. I guess what i’m trying to do is enable the timers back to back in the most efficient way in mitigating the delay from time Timer1 is enabled to time Timer2 is enabled. Hope this explains what i’m asking.

I am by no means creating an argument on which language is better as this seems to be a highly sensitive subject as I’ve noticed in avrfreaks.net. Just asking for some friendly guidance from those of you who have messed around with different methodologies in enabling Timer/Counters.

You want the PWM output from the two timers to be well synchronized. Got it.

For those of us on this side of your monitor, neither sketch compiles. Probably the library you forgot to reference / include.

Like you said you might have to download and include the Library I’m using for it to compile. Here is the link to download if you wish

(https://code.google.com/p/arduino-pwm-frequency-library/downloads/list)

Here is the link that talks about the library for your convenience.

(http://forum.arduino.cc/index.php/topic,117425.0.html)

After adding the library as you said the second code snippet above should compile as mine did

Algol-60, Fortran, cobol (yuck) and there successors such as C/C++ where invented so that people would not was large parts of there lives with asm. Asm was invented to stop people wasting there lives on machine code.

Instead of doing things like this you would be better off fliping burgers and then spending the extra cash on extra bits for your Arduino.

Mark

Both of your examples compiled fine for me, using Arduino 1.0.6... What errors did you get?

shanecy: the most efficient way in mitigating the delay from time Timer1 is enabled to time Timer2 is enabled. Hope this explains what i'm asking.

When you have the various registers set up I thought it just took a single write to the appropriate "start" register to start a timer. I can't see how successive writes to a Timer1 register and a Timer2 register would take longer using C++ compared with assembler.

Have you studied the assembler code that the compiler produces? - it is pretty smart, after many years of development.

...R

holmes4 thanks for your wonderful advice. I hope one day i’m as smart as you.

Robin2 Thanks, I will check out what you said. With my .asm code I used FAST PWM Mode 7 and initialized the TCCR2B and TCCR2A registers which I believe starts the timer(“Don’t quote me on that though”). I embedded code before and after this execution to allow me to examine the number of clock cycles used under a scope. This is irrelevant to include here but just some background on what I was trying to do. However, I’m having trouble figuring out where to find this output .asm file for my arduino code? I know with Atmel Studio I can instruct the assembler to output various informative files such as the .lss file as well as others. Do I need to instruct the GCC in someway to view the .asm file for my arduino code?

westfw I’m running 1.0.5. I will update to a newer version and recompile. Thanks for your comment

shanecy: and initialized the TCCR2B and TCCR2A registers which I believe starts the timer("Don't quote me on that though").

You can write to the registers from Arduino C++ code. For example

TCCR2B = 0b00001111;

is a valid instruction (though I have not checked if it is sensible).

Have a look at the code in yet another software serial if you need convincing.

...R

On the ATmega328, timers 0 and 1 are designed so they can be perfectly synchronized (GTCCR register; TSM and PSRSYNC bits). Unfortunately, timer 0 is used for millis (and its ilk). However, it is fairly easy to move millis to timer 2 (requires modifying the core source code).