PWM code Atmega32A Atmel 6.2

The topic on number crunching

PWM code Atmega32A Atmel 6.2

Postby Dorien » Wed May 25, 2016 7:21 pm

Hi all,

For a project I need to brighten up a LED on an STK500 board via PWM. I am working with an ATMEGA32A and write the code in C (AtmelStudio 6.2). I have been doing quite a bit of research (i.e. spending hours and hours on forums and in the datasheet).
This is what I found out/came up with so far:
- the LED that is an output for PWM is OC0=PB3
- I chose to put the register TCCR0 on fast PWM, non inverted, no prescaler
- the trigger to start the PWM cycle is pressing switch 0 (this is part of the project, I have to set a trigger (alarm) to start the cycle)
- when I set OCR0 = 0 that means PWM = 0. so when I set OC0 = 115 that would be a PWM of 45% (of 255), right? I need to add 'value' to OCR0 in order to make the LED shine brighter and brighter each clockcycle, I used a for-loop for that

My question:
When I build the code in Atmel Studio it does not give an error (hurray for me :-), but when I flash it to the STK500 nothing happens with the LED when I press switch 0

I'll try to attach my code. Can someone give me some advice on this?

Thanks in advance!
Attachments
PWM_extra_opdracht_cod_v0.2.c
My code so far...
(1.03 KiB) Downloaded 56 times
Dorien
 
Posts: 3
Joined: Wed May 25, 2016 3:47 pm

Re: PWM code Atmega32A Atmel 6.2

Postby ag » Fri May 27, 2016 3:33 pm

Hi Dorian,

I don't see any loop in your main.

Regards,
Ag
ag
 
Posts: 529
Joined: Thu Jan 02, 2014 10:37 am

Re: PWM code Atmega32A Atmel 6.2

Postby Dorien » Sat Jun 04, 2016 10:16 am

Hi Ag,

you are right, I had to include the init_pwm in the main.

I changed the code a bit, but now the result is that the LED on PINB3 turns on when I flash the code. It stays on all the time.
Is that due to the fact that the calculation in the ISR happens s fast that I can't see it? Of does the program not run the ISR at all?

I want the LED to start at 0 and then become brighter and brighter , like a wake up light.

What am I doing wrong?
Attachments
PWM_extra_opdracht_cod_v0.2.c
(947 Bytes) Downloaded 43 times
Dorien
 
Posts: 3
Joined: Wed May 25, 2016 3:47 pm

Re: PWM code Atmega32A Atmel 6.2

Postby ag » Sat Jun 04, 2016 12:39 pm

Hi Dorian,

Currently, your program still does not have an endless loop in the main function. Therefore, your code only runs straight through in one shot, lighting up the LED only once and then stopping. Within the main function, you need to add an endless loop ( e.g. WHILE(1) ...) which encloses you code, so that the code which brightens up you led via PWM keeps repeating (as long as the swith is activated) over and over again.

Try folowing the code step by step using the debugger to get a better idea of what is actually going on.

Regards,
Ag
ag
 
Posts: 529
Joined: Thu Jan 02, 2014 10:37 am

Re: PWM code Atmega32A Atmel 6.2

Postby Dorien » Thu Jun 09, 2016 7:24 pm

Hi Ag,
I started from scratch again and will try to build my code step by step. I encounter a problem with this small piece of code:

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRA &= ~(1<<PA0);//input
DDRB = (1<<PB3);//output

while(1)
{

if((PINA & (1<<PA0)) == 0)//press switch A0 starts toggling LED 3
{
PORTB ^= (1<<PB3); //toggle LED3
}
_delay_ms(500);
}
}

Can you explain why the LED3 burns constantly? I expected it to be off till switch A0 is activated and than toggle the LED (this works well).
Dorien
 
Posts: 3
Joined: Wed May 25, 2016 3:47 pm

Re: PWM code Atmega32A Atmel 6.2

Postby ag » Fri Jun 10, 2016 10:31 am

Hi Dorian,

At the start of your loop, PIN 0 of PORTA is in it´s initial state. Since you have not set the internal pull up resisiter for this pin, that state is logical 0. I assume you have not connected an external pull up resistor to that pin, setting it to logical 1, otherwise your LED3 would not be lighting until the switch is activated.

Try setting the internal pull up resistors for PORTA and see if that helps.

Regards,
Ag
ag
 
Posts: 529
Joined: Thu Jan 02, 2014 10:37 am

Re: PWM code Atmega32A Atmel 6.2

Postby 3dtech » Wed Dec 21, 2016 9:49 am

thanks for this valuable information.
3dtech
 
Posts: 1
Joined: Wed Dec 21, 2016 6:03 am
Location: MONA TILES COMPOUND, NR. CHHANI CIRCLE, CHHANI ROAD, VADODARA - 390002. GUJARAT, INDIA.


Return to Microcontrollers & Embedded

Who is online

Users browsing this forum: No registered users and 1 guest