Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me with coding of PWM for atmega8 in C

Status
Not open for further replies.

asin209

Newbie level 4
Joined
Aug 14, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Phillipines
Activity points
1,325
Hello.

Im trying to create PWM to control the speed of a brushless DC motor on
atmega8.

The pwm will be connected to the gates of the 3-half bridges.

So i will need to use 2 channels from timer1 and 1 channel from timer2.

Could anyone help me with the coding? because im a beginner in programming.

The coding must be in C language not assembly.

Thanks!!! in advance.
 

Re: PWM for atmega8

Why don't you try it yourself instead of asking someone to code?

Start doing some coding, ..... try it and come here to get help

Nandhu
 

Re: PWM for atmega8

hey yeah...i have done some coding. But I want a varying pwm. How do i implement that?
This is what ive got so far...

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

int main(void)
{
uint8_t delay;
uint16_t pwm;
long count;

DDRB=0xFF;// PortB as output

TCCR1A |= (1<<WGM10)|(1<<COM1A1)|(1<<COM1B1); //PWM phase correct 8-bit
TCCR1B |= (1<<CS10)|(1<<CS11); // using prescaler of 64 with a clock frequency of 1Mhz

TCCR2 |= (1<<WGM20)|(1<<COM21)|(1<<CS22); // prescaler of 64

while(1)
{
for(pwm=1;pwm<=254;pwm++)
{
OCR1A=pwm;

for(count=0;count<=100;count++)
delay--;
}

for(pwm=254;pwm>=1;pwm--)
{
OCR1A=pwm;

for(count=0;count<=100;count++)
delay--;
}

for(pwm=1;pwm<=254;pwm++)
{
OCR1B=pwm;

for(count=0;count<=100;count++)
delay--;
}

for(pwm=254;pwm>=1;pwm--)
{
OCR1B=pwm;

for(count=0;count<=100;count++)
delay--;
}

for(pwm=1;pwm<=254;pwm++)
{
OCR2=pwm;

for(count=0;count<=100;count++)
delay--;
}

for(pwm=254;pwm>=1;pwm--)
{
OCR2=pwm;

for(count=0;count<=100;count++)
delay--;
}

}
}
 

Re: PWM for atmega8

hey can u tell me properly that what you want to do???

you want to vary the width with fixed frequency of?????

and what is you step change for this????

you want to change the pwm for a single count????
 

Re: PWM for atmega8

i want to vary the pwm to vary the speed of the motor
the frequency should be 8Khz
the voltage should change from 0V to 5V
Yeah...need to change the pwm for a single count.
 

Re: PWM for atmega8

OCRnx is a multi-byte register. so be sure that when you write / read from it, you are mindful of that.

you can google "atomic access" for more.
 

Re: PWM for atmega8

just log in **broken link removed**
you will find so many codes with video and explanation. in this you can find your requirement


Regards
Piyush Manavar
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top