Microcontroller › 8051 › GENERATING VARIABLE PWM
- This topic has 17 replies, 4 voices, and was last updated 11 years, 1 month ago by
AJISH ALFRED.
-
AuthorPosts
-
July 9, 2012 at 8:00 pm #4777
Varun Srivastava
ParticipantI am working on a simple circuit consisting of AT89S52 and two push buttons as input. The output is PWM wave to be fed to a load.
I am stuck at the programing part.
1. I want to generate PWM output of frequency 3KHz.
2. When the circuit will be switched on the duty cycle of PWM will be 100%.
3. If we press increase then the duty cycle of the PWM will increase by 10% of the previous value. (MAX Value of duty cycle is 100% i.e ON condition)
4. If we press decrease then the duty cycle of the PWM will decrease by 10% of the previous value. (MIN value is 0% i.e OFF condition)
5. The PWM output should be available at all the time even if we release the button.The Hardware arrangement i have made is shown in the picture.
Any help regarding this project is deeply appreciated. Expecting support from all of u.
July 9, 2012 at 9:07 pm #8229AJISH ALFRED
ParticipantAre you using timers to generate pwm?
July 9, 2012 at 9:19 pm #8230Varun Srivastava
Participantpresently i am using for loop for generating delay but i have no problem in using timers if the condition demands it.
July 11, 2012 at 3:35 pm #8241Amrith
ParticipantHi Varun,
Better use timers for accuracy.
July 14, 2012 at 3:27 pm #8255Varun Srivastava
ParticipantHi Amrith,
The problem i m facing is not with the timers but i want to vary the duty cycle with push of the buttons this is where i m stuck.
If u can provide any help in logical building of the program that how to vary the duty cycle with single push of the button.It will be appreciated very much.
thnx.
July 17, 2012 at 1:39 pm #8279Amrith
ParticipantHi Varun,
Regret for delay in response.
When ever you press the pust button increment the value of duty cycle by a value. Before this you must provide a logic to calculate duty cycle & assign a variable to it.
July 17, 2012 at 4:10 pm #8284Anonymous
Guesthi
July 23, 2012 at 7:04 am #8306Varun Srivastava
Participant88 views but only 6 replies…does no one has any idea abt it…
I thought every1 out here is gonna help me out…!!July 23, 2012 at 5:45 pm #8310AJISH ALFRED
ParticipantHi Varun,
You have already got a fiew suggessions, right? You know why there is no other reply? Because other people can see that you’ve already got answer to your problem as a reply to your post.
Now you should try out the things suggested by them and update the result, then only others can help you more.
Have you tried out the suggession posted by Amrith yet?
I hope you understand, and will take it in good sense.
Kindly update the current status and your further doubts.
July 24, 2012 at 8:55 am #8319Varun Srivastava
ParticipantHii Ajish,
The Programing part i have done yet is using 4 push buttons to change d value of PWM output. Now this is the next step in my project design dat i want to do it using only two push buttons to either increase or decrease the duty cycle of the PWM.
My present program is….
#include <reg51.h>
sbit ONE=P2^7;
sbit TWO=P2^6;
sbit THREE=P2^5;
sbit FOUR=P2^4;
sbit FIVE=P2^3;
sbit SIX=P2^2;
sbit SEVEN=P2^1;
sbit EIGHT=P2^0;
sbit MTR=P1^0;
void MSDelay(int value);
void main()
{
unsigned int on,off;
ONE=1;
TWO=1;
THREE=1;
FOUR=1;
FIVE=1;
SIX=1;
SEVEN=1;
EIGHT=1;
while(1)
{
if (ONE==0)
{
FIVE=0;
SIX=1;
SEVEN=1;
EIGHT=1;
while(FIVE==0 && TWO==1 && THREE==1 && FOUR==1)
{
on=2;
off=8;
MTR=1;
MSDelay(on);
MTR=0;
MSDelay(off);
}
}
else if (TWO==0)
{
FIVE=1;
SIX=0;
SEVEN=1;
EIGHT=1;
while(SIX==0 && ONE==1 && THREE==1 && FOUR==1)
{
on=3;
off=7;
MTR=1;
MSDelay(on);
MTR=0;
MSDelay(off);
}
}
else if (THREE==0)
{
FIVE=1;
SIX=1;
SEVEN=0;
EIGHT=1;
while(SEVEN==0 && ONE==1 && TWO==1 && FOUR==1)
{
on=5;
off=5;
MTR=1;
MSDelay(on);
MTR=0;
MSDelay(off);
}
}
else if (FOUR==0)
{
FIVE=1;
SIX=1;
SEVEN=1;
EIGHT=0;
while(EIGHT==0 && ONE==1 && TWO==1 && THREE==1)
{
MTR=1;
}
else
{
MTR=1;
}
}
}
void MSDelay(int value)
{
int x;
for(x=0;x<=value;x++);
}
If u cn help me where to change d logic in dis present program…looking forward for it…!!
July 25, 2012 at 5:22 pm #8330AJISH ALFRED
ParticipantHi Varun,
Try the following code logic. Modifications might be required.
/*
Assume key1 for increasing on time and key2 for decreasing.
*/
on=1; //initial value of ON time delay
off=10; //initial value of OFF time delay
while (1)
{
while(ONE==1 && TWO==1) //when both keys are open
{//—-generating PWM
//
MTR=1;
MSDelay(on); //applying ON time delay
MTR=0;
MSDelay(off); //applying OFF time delay//
//
}//—when any of the key is down, exit from while loop and check which key is down–//
if(ONE==0) //if key on is down
{//—-increment ON time delay–//
on++;
off–;//
//
}
else
{//—-decrement ON time delay-//
on–;
off++;//
//
}/*put some delay waiting here, if required*/
}Kindly update the result asap. And next time when you post a code, make sure that it is properly commented like the one above.Also give a hint about hardware connections, like what would be voltage reading when the key is pressed etc.
July 26, 2012 at 12:57 pm #8332Varun Srivastava
ParticipantHi Ajish,
First of all thnx for ur quick assistance. I implemented the logic provided by you, I am very close to final result just now i have brought up few changes in the program and simulated it via MULTISIM.
Although the simulation seems to be what i want but i still want to check it through hardware. Also i m trying to implement TIMER in MODE 0 instead of FOR LOOP as somebody told me it will be more accurate.
I will put up the changes here soon….also if u can provide any assistance in how to implement TIMER in MODE 0…pls guide me..I m reffering to Mazidi for the programming part.
Thnx.
July 26, 2012 at 4:32 pm #8335AJISH ALFRED
ParticipantHi,
Thats good news.
Mazidi is a good text book for beginners. Also refer the following links to learn more.
http://www.8052.com/tuttimer.php
http://www.cecs.csulb.edu/~rallison/ppt/285ppt/Chapter_11.ppsx
August 6, 2012 at 6:47 am #8406Varun Srivastava
ParticipantHi Ajish,
Here is the bad news…i tried on implementing TIMERS btu didnt succeeded…nd then came back again to FOR LOOPs…did a very little change to the logic provided by….
The Program is lyk dis…
#include <reg51.h>
sbit INCREASE = P2^6;
sbit DECREASE = P2^7;
sbit MTR = P1^0;
void MSDelay(int value);
void main()
{
unsigned int on,off;
on=20; //initial value of ON time delay
off=0; //initial value of OFF time delay
while (1)
{
while(ONE==1 && TWO==1) //when both keys are open
{//—-generating PWM
//
MTR=1;
MSDelay(on); //applying ON time delay
MTR=0;
MSDelay(off); //applying OFF time delay
//
//
}//—when any of the key is down, exit from while loop and check which key is down–//
if(INCREASE==0 && on<20) //if key increase is down check whether it has max value or not
{on++;
off–; //—-increment ON time delay if it is not maximum–////
//
}
else if (TWO==0 && on>0) // if key decrease is down check whether it ON time delay >0//
{on–;
off++; //—-decrement ON time delay if it is not zero—-////
//
}
}}
void MSDelay(int value) // Delay function //
{
int x;
for(x=0;x<=value;x++);
}Now the above thing works very fine in Multisim but when i tried with my hardware….i m getting only two states either my load goes fully on or fully off….no in between states…
Now i dnt knw where the problem is…is it possible that the case may be of switch debounce…!!!
expecting your reply soon…thnx
August 10, 2012 at 5:59 pm #8437AJISH ALFRED
ParticipantJust add a small delay as the last statement of your infinite loop.
like, MSDelay(1000);
-
AuthorPosts
- You must be logged in to reply to this topic.