PLEASE EXPLAIN PWM CODE
Discussion in "Project Help" started by gaurav7577 Oct 25, 2013.
Fri Oct 25 2013, 06:44 pm
Hi ....I was working on tutorial section of this website i re write code present here ,is it right????? (pls look in to main routine)
i got PWM in proteus but i didn't understand how this code gets executed.
#include<reg52.h>
sbit a=P1^0;
unsigned char pwm_width;
bit pwm_flag = 0;
void pwm_setup(){
TMOD = 0;
pwm_width = 160;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void timer0() interrupt 1 {
if(!pwm_flag) { //Start of High level
pwm_flag = 1; //Set flag
a = 1; //Set PWM o/p pin
TH0 = pwm_width; //Load timer
TF0 = 0; //Clear interrupt flag
return; //Return
}
else { //Start of Low level
pwm_flag = 0; //Clear flag
a = 0; //Clear PWM o/p pin
TH0 = 255 - pwm_width; //Load timer
TF0 = 0; //Clear Interrupt flag
return; //return
}
}
void main()
{
pwm_setup();
while(1);
}
i got PWM in proteus but i didn't understand how this code gets executed.
#include<reg52.h>
sbit a=P1^0;
unsigned char pwm_width;
bit pwm_flag = 0;
void pwm_setup(){
TMOD = 0;
pwm_width = 160;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void timer0() interrupt 1 {
if(!pwm_flag) { //Start of High level
pwm_flag = 1; //Set flag
a = 1; //Set PWM o/p pin
TH0 = pwm_width; //Load timer
TF0 = 0; //Clear interrupt flag
return; //Return
}
else { //Start of Low level
pwm_flag = 0; //Clear flag
a = 0; //Clear PWM o/p pin
TH0 = 255 - pwm_width; //Load timer
TF0 = 0; //Clear Interrupt flag
return; //return
}
}
void main()
{
pwm_setup();
while(1);
}
[ Edited Fri Oct 25 2013, 06:45 pm ]
Sat Oct 26 2013, 09:18 pm
Use the 'Code' button to help keep the format of your code.
Which bits do you understand ? which don't you understand ?
How about some of you others out there helping gaurav while Ajay or I aren't around
void timer0() interrupt 1 { if(!pwm_flag) { //Start of High level pwm_flag = 1; //Set flag a = 1; //Set PWM o/p pin TH0 = pwm_width; //Load timer TF0 = 0; //Clear interrupt flag return; //Return } else { //Start of Low level pwm_flag = 0; //Clear flag a = 0; //Clear PWM o/p pin TH0 = 255 - pwm_width; //Load timer TF0 = 0; //Clear Interrupt flag return; //return }
Which bits do you understand ? which don't you understand ?
How about some of you others out there helping gaurav while Ajay or I aren't around
[ Edited Sat Oct 26 2013, 09:21 pm ]
Tue Oct 29 2013, 12:28 pm
PWM is similar to a square wave with different high and low width. In the code above timer is used to generate delay for high and low period of the wave. Pwm_flag used to identify which part of wave is completed. Accordingly loads timer registers. Of you are facing any specific issue in understanding the code please post that only.
Wed Oct 30 2013, 01:26 pm
Thanks Ajay for ur reply ,if i want continuous PWM o/p whether i have to write main() this way
void main()
{
pwm_setup();
while(1);
}
or this
void main()
{
while(1)
{
pwm_setup();
}
}
void main()
{
pwm_setup();
while(1);
}
or this
void main()
{
while(1)
{
pwm_setup();
}
}
Wed Oct 30 2013, 10:28 pm
The first way of doing it is perfect. Timer once started will keep generating pwm output. So setup function only needs to call once.
Fri Apr 18 2014, 11:36 am
I have a doubt regarding Timer setup for PWM in http://www.8051projects.net/pulse-width-modulation/8051-pwm-example.php
line 4 reads
Why is it char shouldn't it be int ?
line 4 reads
unsigned char pwm_width;
Why is it char shouldn't it be int ?
Fri Apr 18 2014, 04:59 pm
PWM width I kept can only range from 0 to 255 and 8 bit variable is enough.
Do not confuse int/char/long types used in traditional c. In embedded c you look them from prospective of their data size. In 8051
char is 8 bit
Int is 16 bit
Long is 32 bit
Hope it helps.
Do not confuse int/char/long types used in traditional c. In embedded c you look them from prospective of their data size. In 8051
char is 8 bit
Int is 16 bit
Long is 32 bit
Hope it helps.
Powered by e107 Forum System