go away spammer

Author Topic: ATmega640 pwm problem  (Read 2714 times)

0 Members and 1 Guest are viewing this topic.

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
    • Tim's Workshop
ATmega640 pwm problem
« on: May 29, 2009, 05:33:32 PM »
I've been toying with my new board I built and I've come across a really weird problem.  I wrote some code to drive servos and I had success on on channel A and B of timer 1 but none with channel C.  I was able to pull the pin high by other means but not using PWM.  The weird part is that the code is identical.  I don't know what is going on.

Code: [Select]
#include "timer.h"
#include <math.h>
volatile unsigned long ms_count = 0;

void timer0_init()
{
  sei();
  TCCR0A = _BV(COM0A1) | _BV(COM0B1) |_BV(WGM01) | _BV(WGM00);
  TCCR0B = _BV(CS01) | _BV(CS00);
 
  TIMSK0 = _BV(TOIE0);

}

void timer1_init()
{
  // set ovf at 50 hz for servos
  ICR1H = 0x9C;
  ICR1L = 0x3F;
 
  //enable pwm and prescaler of 8
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1C1) | _BV(WGM11);
  TCCR1B = _BV(WGM13)  | _BV(WGM12) | _BV(CS11);
 
  // set data direction registers
  DDRB = _BV(DDB5) | _BV(DDB6) | _BV(DDB7);

}

void servo1A(int angle)
{
  int position = round(angle * 23.3 + 550);

  OCR1AH = position >>8;
  OCR1AL = position & 0x00FF;
}

void servo1B(int angle)
{
  int position = round(angle * 23.3 + 550);

  OCR1BH = position >>8;
  OCR1BL = position & 0x00FF;
}

void servo1C(int angle)
{
  int position = round(angle * 23.3 + 550);

  OCR1CH = position >>8;
  OCR1CL = position & 0x00FF;
}


void delay(unsigned long time)
{

  ms_count = 0;
  while (ms_count < time )
  {

  }

}

ISR(BADISR_vect)
{}

ISR(TIMER0_OVF_vect)
{
  ms_count++;
}

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
    • Tim's Workshop
Re: ATmega640 pwm problem
« Reply #1 on: May 29, 2009, 09:44:35 PM »
I SOLVED THE PROBLEM!!!  OC1C and OC0A share the same pin so when I enabled pwm on that pin with both timer 1 and timer 0 neither worked.

this was the problem line
Code: [Select]
TCCR0A = _BV(COM0A1) | _BV(COM0B1) |_BV(WGM01) | _BV(WGM00);

 


Get Your Ad Here

data_list