PWM Help

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am having some problems with PWM and I am hoping someone can help me out...

I am using the STK500 board with a mega8515 running at ~1 Mhz. I just want to use all three of the mega8515's PWM channels for fading the LEDs on the STK500 board.

I can get Timer0 to work just fine. This code works perfectly (Phase correct 8 bit PWM):

#include 
#include 
#include 
#include 
#include 

int main(void)
{
PORTB=0x00;
DDRB |= 1<<0;
TCCR0 = 1<<WGM00 | 1<<COM01 | 1<<COM00 | 1 << CS00;
OCR0=0x10;

while (1)
      {
       // just looking at the leds
      };
}

I can modify OCR0 from 1 to 255 and it works great.

Unfortunately using Timer1 is where the problem comes in.

Modified: 

DDRB |= 1<<0 | 1<<1 | 1<<1; // Three LEDs should be controlled by PWM

TCCR1A= 1<< COM1A1 | 1<<COM1B1 | 1<<WGM10; // Phase correct PWM mode (mode 1 - 8 bit)
TCCR1B= 1<< CS10; // No prescaling
OCR1A = 0x01;
OCR1B = 0xF0;

Unfortunately, the PORTB LEDs 1 and 2 never change their intensity. I also calculated the frequency from the given equation in the datasheet it seems that the maximum freq is ~200 Hz so if I set OCR1A it should be 1 Hz, which I think should result in a visibly less intense LED emission.

Does anyone know what I am doing incorrectly? It seems like it should be straightforward but I have been unable to get Timer1 PWM channels to work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is jumpered to what? (How do you have the STK500 wired?)

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
DDRB |= 1<<0 | 1<<1 | 1<<1; // Three LEDs should becontrolled by PWM

First, this is only setting two bits to outputs (you have duplicated bit 1). Second, the OCR1A is on port D pin 5, and OCR1B is on port E pin 2.

Quote:
it seems that the maximum freq is ~200 Hz so if I set OCR1A it should be 1 Hz

No, in the mode that you are using OCR1A has nothing to do with the frequency, it controls the duty cycle.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Alright thanks I now got it to work. For some reason when I was reading through the examples and manual I was under the impression that I could choose what output I wanted for each PWM channel.