Question about using AVR8 timers.


Jack Gassett

Recommended Posts

This thread is forwarded from an email discussion:

Another question:  I want to make the "servo" lib run on Arduino IDE  on Papillio. So I started to have a look, but I have a question:  I want to run a timer (let's say timer 2) but it  seems not working or I did not program it correctly. I even do not know whether  the timer IP is correctly connected in the VHDL (because it seems not). I  noticed that you made an IR interface, and there is a timer implemented in  the source code, so I suppose something is working, but which mode ? Fast PWM ?  other ? Please help.  The code I tested below gives no output on pin 3,  why ?    void setup()

{

  TCCR2 = 0;

   

  TCCR2 |=_BV(PWM2);

  // Mode Toggle

  TCCR2 |=  _BV(COM21);

   

  // Prescaler /1024

  TCCR2 |=  _BV(CS22); 

  TCCR2 |=  _BV(CS20); 

  // Compare value

  OCR2 = 64;        //Timer 2 output -> Port A03 (= arduino  pin 3)

  pinMode(3, OUTPUT);    }

void loop()

{

   

}

  Best regards,  Laurent

Link to comment
Share on other sites

With the IR interface the code uses PWM2 and the Papilio needs the following code to define where the PWM2 output connects:   

  //Move the PWM2 output to the appropriate pin for the Papilio Platforms BPW5008 Infrared Wing  PWM2_LOC=AL7; //AL7  for Wing Slot AL, AH7 for Wing Slot AH, BL7 for Wing Slot BL, BH7 for  Wing Slot BH, CL7 for Wing Slot CL, CH7 for Wing Slot CH.   

I just started a new Papilio Introduction Series  where I am writing a weekly article about Papilio topics. This is a  timely question since the very next article I'm going to write about is  how to move peripherals to the pin that you need. In the article I'm  going to list all of the available peripherals that can be moved, I'm  pretty sure the timers can be moved but I need to consult the source  code again to make sure. I will try and speed up the timeline for  writing the article and work on it this weekend or Monday.   

So if you can control the servo lib with PWM output  then you should just be able to use the code above, if the timer output  is needed then I need a little time to dig further into that.

    Jack.

Link to comment
Share on other sites

Ok, I just looked at the source code and the atmega103 datasheet,  I had to refresh my memory on how timers and PWM worked. It looks like  the output of the timer is the PWM output so all you should have to do  to get your code to work is tell the timer peripheral to connect the  PWM2 output to pin 3. For more information about how to reference the  pins look at this page: http://papilio.cc/index.php?n=Papilio.Pinouts.

 

Finally, the AVR8 is patterned after the AtMega103 so there might be  slight differences in how the timer works then how the timer works for  the Arduino. From what I remember the timer is the same, but the UART is  different. The AVR8 uses a SPI master from an AtMega128 project so it  should match what is on the Arduino. So if things do not work as  expected compare the datasheets for differences between the AtMega103  and the AtMega8.

 

Hope this does it. :)

void setup()

{

  PWM2_LOC=3;cleardot.gif

  TCCR2 = 0;

   

  TCCR2 |=_BV(PWM2);

  // Mode Toggle

  TCCR2 |=  _BV(COM21);

   

  // Prescaler /1024

  TCCR2 |=  _BV(CS22); 

  TCCR2 |=  _BV(CS20); 

  // Compare value

  OCR2 = 64;        //Timer 2 output -> Port A03 (= arduino  pin 3)

  pinMode(3, OUTPUT);    }

void loop()

{

   

}

Jack.

Link to comment
Share on other sites

  • 3 weeks later...

Jack,

I need to modify the timer behavior for my purpose.

I have implemented the 16 bit-timer 1 in timer_counter.vhd but I also need to change things in Papilio_AVR8.vhd to connect new interrupt lines. In the way, I noticed that OCx_PWMx lines are connected to "open" !? And I can not find any mention of  PWMx_LOC somewhere in the source code. So where is it ?

I have downloaded the latest source code (soft core 1.6.0). Is there everything in there ?

Laurent

Link to comment
Share on other sites

Hello Laurent,

I'm thinking back and I think I simply overlooked the external interrupts... I don't remember connecting them and if they are "open" then that definitely means they are not connected to anything.

This is a pretty big hole in the available functionality that needs to be corrected. I will put it on my to-do list with a top priority.

In the meantime, there is a tutorial here about how you would connect that "open" interrupt to any pin you desire. This method doesn't set it up to be movable with code but it can keep you moving forward with what you are working on.

Sorry for the inconvenience,

Jack.

Link to comment
Share on other sites

Jack,

Interrupts lines are mapped well except timer1 lines which is "normal" because timer 1 is not implemented.

My question was about OCx_PWMx outputs. I followed the tutorial and it is pretty clear how to connect a timer output to an external pin BUT:

In the messages above, you told me how to redirect the PWM2 output to any pin with  PWM2_LOC. I tested it, and it is working well. The problem is that I can not see where it is implemented in the sources as all OCx_PWMx are "open" in the latest source code available.

Maybe you have a more advanced source code on this part ?

In a word: I do not see how to generate a bitfile for my 250K Papilio which has the output "relocate" function available as it seems I do not have the good source files.

Laurent

Link to comment
Share on other sites

Ohhhh, ok I see.

Yes, the source code that ships with the Arduino IDE is what I call the AVR8 "Shifty" branch. For more information check out the wiki page I wrote last week. It talks about the Vanilla branch, which is what you are looking at, and the "Shifty" branch at the bottom.

The "Shifty" code can be found at:

http://gadgetforge.gadgetfactory.net/gf/project/avr_core/scmsvn/?action=browse&path=%2Ftrunk%2F

I need to get it checked into Github as an actual branch...

Jack.

Link to comment
Share on other sites

OK I see.

That is not obvious to get all files that have been changed since I am not sure the version of file included in zip 1.6.0. I probably have better to move external pin by hand as described in the video tutorial.

But, if you can make a tarball, I would be interested for sure ;-)

I have simulated my implementation of the 16bit timer 1 intimer_counter.vhd (with code dedicated to servo specific timer). I will test it next thrusday and make it available if people are interested in.

Regards,

Laurent

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.