2

Why such horrible Arduino's Mega PWM doesn't work??

Below is my code.

I try to set Fast PWM 8-bit to dim and light up a LED connected to OCR1A Pin. I try to do it in C language, completelly without Arduino's lbraries and it doesn't work at all.

I'm working with documentation and the code seems me to be correct. Almost the same code worked on ATmega8 and ATmega16 (obviously, with special setting for those special ATmegas) but this one doesn't.

The comments in this code point out, that I tried to use few other flags to check if it work with some of another settings.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
//Anode of LED connected to OCR1A (digital pin 11)
#define LED (1<<PB5)
int main()
{
 DDRB |= (1 << LED);
 //FAST PWM 8-bit, set OCR1A at the Bottom
 TCCR1A = (1 << COM1A1) | (1 << WGM10); // | (1<<WGM11) | (1<<COM1A0)
 //Prescaler :1024
 TCCR1B = (1 << CS12) | (1 << WGM12); //| (1<<WGM13); //Presc :1024 (1<<CS10) |
 //Enable to interrupt Compare Match A
 TIMSK1 = (1 << OCIE1A);
 uint8_t i;
 sei();
 while (1)
 {
 for (i = 0; i < 255; i++)
 {
 OCR1A = i;
 // _delay_ms(5);
 }
 for (i = 255; i > 0; i--)
 {
 OCR1A = i;
 //_delay_ms(5);
 } 
 }
}

}

per1234
4,2782 gold badges23 silver badges43 bronze badges
asked Aug 9, 2017 at 16:00
2
  • In what way does it "not work"? Commented Aug 9, 2017 at 16:02
  • The program has been compiled and send to Arduino board (via Arduino IDE) but the LED, which should be dimmed and lighted up doesn't work. Commented Aug 9, 2017 at 16:09

3 Answers 3

1

I'm not sure this is right:

#define LED (1<<PB5)

Since later on you use:

DDRB |= (1<<LED);

That would expand out to:

DDRB |= (1<<(1<<PB5));

That would equate to:

DDRB |= (1<<(1<<5));

Take it a step further:

DDRB |= (1 << 32)

Shifting 1 to the left 32 times on an 8 bit register results in:

DDRB |= 0;

Which of course does nothing.

I can't comment on the rest of the code, other than to say "without delays in your loops you won't get much sense out of it".

answered Aug 9, 2017 at 16:13
1
  • My oversight.... Obviously, You're right. Now the LED is active. It means that it lights. But lights constantly without any charges in brightness. So, I tried to add small delays (several ms) within "for" loops but it doesn't influence the LED brightness changes. Changing in delays just causes that the LED lights darkly or brightly, but still constantly. Commented Aug 9, 2017 at 16:29
1

Majenko showed you one of your mistakes.

Then you wrote:

//Enable to interrupt Compare Match A
TIMSK1 = (1<<OCIE1A);

And this is your other mistake. You should not enable an interrupt unless you have defined an ISR for it. Otherwise your program will restart every time the interrupt fires.

answered Aug 9, 2017 at 17:53
0

My advice. Stay simple. Try to get your code to output a duty cycle first. From there try to get your code to vary the duty cycle.

Once you do that the issues with your code will be clear.

answered Aug 9, 2017 at 17:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.