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.

P89V51RD2 Frequency to PWM

Status
Not open for further replies.

Allan Bautista

Newbie level 2
Joined
Mar 29, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,297
Hi everybody,

I am new in 8051 Microcontroller. How can I make a frequency controlled variable PWM output? For example my input signal is 1 khz and it suppose to generate let's say 10% PWM, when the frequency increases the PWM Duty Cycle increases too. Please help. My MCU is P89V51RD2.
 

Its quite simple... Here is the sample code follows...

Timer Setup for PWM in c;

//Global variables and definition

#define PWMPIN P1_0

unsigned char pwm_width;
bit pwm_flag = 0;

void pwm_setup()
{
EA = 0;
TMOD = 0;
pwm_width = 160;
TR0 = 1;
ET0 = 1;
EA = 1;
}

Interrupt Service Routine:
void timer0() interrupt 1
{
if(!pwm_flag) // Start of High Level
{
pwm_flag = 1; // Set Flag
PWMPIN = 1; // Set PWM o/p pin
TH0 = pwm_width; // Load Timer
TF0 = 0; // Clear interrupt flag
return;
}
else // Start of low level
{
pwm_flag = 0; // clear flag
PWMPIN = 0; // clear PWM o/p pin
TH0 = 255 - pwm_width; // Load timer
TF0 = 0; // Clear interrupt flag
return; // return
}
}

In your main program you need to call this pwm_setup() routine and your controller will have a PWM output.. Timer ISR will take care of PWM in the background... The width of PWM can be changed by changing the value of pwm_width variable... In above example i am using 160, you can choose any value from 0 to 255...

You can also make use of Timer1 if you want.. And the output pin can be changed to whatever pin you want...
 
Thanks kandhu26! But how can I control the PWM Duty Cyle using the external pulse/frequency source? I have this external frequency source with (ex. 1Khz) and I want to have let's say 10% PWM Duty Cycle. Then if the external pulse/frequency increases (ex. 2khz) the PWM output must have let's say 20% PWM Duty Cycle. Please help me.
 

u cant define port pin in this manner(#define PWMPIN P1_0). this simply define portP1 not Port pin

if u want to set port1 then it is ok...if u want set port pin i.e P1.0 then put sbit PWMPIN =P1^0;
 

Thanks kandhu26! But how can I control the PWM Duty Cyle using the external pulse/frequency source? I have this external frequency source with (ex. 1Khz) and I want to have let's say 10% PWM Duty Cycle. Then if the external pulse/frequency increases (ex. 2khz) the PWM output must have let's say 20% PWM Duty Cycle. Please help me.

For that you need to detect your frequency using edge triggered Interrupt with 2nd priority.
Increment a counter whenever the interrupt comes, make one another timer interrupt having a count value of 1 sec (need to use nested loop).
with 1st Priority.

After the timer Interrupt, Interrupts the controller you need to check your counter value (edge triggered counter value).
This counter value is nothing but your frequency.

Now using the functions shown by Kandhu26 you need to vary the value of PWM Width which should depends on your counter value (Frequency).

Hope you get this.
All the best :)
 

Hi,
You can also use a timer in Input Capture mode to determinate period of your input frequency,
and then derive the value to feed in timer pwm module.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top