Author Topic: Finding the right output pin  (Read 1348 times)

0 Members and 1 Guest are viewing this topic.

Offline LukeTopic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Finding the right output pin
« on: June 25, 2009, 12:48:31 PM »
Hi all. I'm trying to understand PWM and how to use it with the Axon. I've read through several tutorials and other information including forum posts as well as a bunch of the SoR Axon code.  If I understand correctly to use timer 4 for PWM I would need to set TCCR4A and TCCR4B with the PWM parameters I want to use (Fast PWM, Prescalars, compare output modes, etc).

The one issue I'm having trouble with is determining which output pin on the Axon to connect my servo to.  I know that all pins are set to output mode by default. If I wanted to explicitly set my pin to output, how would I find out which one to set?

Say I want to use Pin H3 on the Axon for PWM because I know H3 is on Timer 4. (I haven't been able to determine this by looking at datasheets, I only know this because of the "PWM_Init_timer4_H3" function names in timer640.c as provided by SoR).

My stab at some initial PWM code:
Code: [Select]
TCCR4A = 0;         

ICR4 = 19999 ;

//Set up 16 bit Fast PWM, PreScalar 8, and Non-Inverting on Channel A

TCCR4A = (1 << WGM41) | (1 << COM4A1);
TCCR4B = (1 << WGM43) | (1 << WGM42) | (1 << CS41);

OCR4A = ICR4 * (1.5 /20);  //center servo?

while (1)
{
    //just spin
}

How do I know which DDRx to set. Is pin H3:
Code: [Select]
DDRH = _BV(1);
I'm having trouble knowing if I have phrased my confusion coherently. If my question makes no sense, I'll try to clarify it. Also, if anyone sees something wrong in my code (aside from missing the statement to set the outputs), please feel free to let me know. You could save me alot of time troubleshooting later on.   :P

Thank you.

Offline sonictj

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
    • Tim's Workshop
Re: Finding the right output pin
« Reply #1 on: June 25, 2009, 05:04:43 PM »
PWM is specific to an output pin.  Got to the pin configurations portion of the ATmega640 datasheet to find the pins that have OCR4A, OCR4B, and  OCR4C. The pin will also be labeled Pxn where x is the port and n is the pin number.

Here is my servo initialization code for timer4

Code: [Select]
void timer4Servo_init()

{
  // set ovf at 50 hz for servos
  ICR4H = 0x9C;
  ICR4L = 0x3F;
 
  //enable pwm and prescaler of 8
  TCCR4A = _BV(COM4A1) | _BV(COM4B1) | _BV(COM4C1) | _BV(WGM41);
  TCCR4B = _BV(WGM43)  | _BV(WGM42) | _BV(CS41);
 
  // set data direction registers
  DDRH = _BV(DDH3) | _BV(DDH4) | _BV(DDH5);

}




Offline LukeTopic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: Finding the right output pin
« Reply #2 on: June 26, 2009, 08:52:16 AM »
Thank you. That was exactly what I was missing.

 


Get Your Ad Here