Discussion in "Project Help" started by    gaurav7577    Oct 25, 2013.
Fri Oct 25 2013, 06:44 pm
#1
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);
}


[ Edited Fri Oct 25 2013, 06:45 pm ]
Sat Oct 26 2013, 09:18 pm
#2
Use the 'Code' button to help keep the format of your code.

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
#3
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
#4
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();
}
}
Wed Oct 30 2013, 10:28 pm
#5
The first way of doing it is perfect. Timer once started will keep generating pwm output. So setup function only needs to call once.
Thu Oct 31 2013, 12:19 pm
#6
Thanks Ajay & Happy Diwali
Thu Oct 31 2013, 07:54 pm
#7
You're welcome and a Very Very Happy Diwali to you too.
Fri Apr 18 2014, 11:36 am
#8
I have a doubt regarding Timer setup for PWM in http://www.8051projects.net/pulse-width-modulation/8051-pwm-example.php
line 4 reads

unsigned char pwm_width;


Why is it char shouldn't it be int ?
Fri Apr 18 2014, 04:59 pm
#9
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.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am