PWM With Microcontroller 8051 For SCR or Triac Power Control
PWM With Microcontroller 8051 For SCR or Triac Power Control
Introduction
Pulse width Modulation or PWM is one of the powerful techniques used in control
systems today. They are not only employed in wide range of control application which
includes: speed control, power control, measurement and communication. This tutorial
will take you through the PWM basics and implementation of PWM on 8051 and AVR
microcontrollers.
So you can see from the final equation the output voltage can be
directly varied by varying the Ton value.
If Ton is 0, Vout is also 0.
if Ton is Ttotal then Vout is Vin or say maximum.
This was all about theory behind PWM. Now lets take a look at the
practical implementation of PWM on microcontrollers.
Pulse Width Modulation (PWM): 8051 Code example
value)
CLR TF0
interrupt flag
RETI
where
from
HIGH_DONE:
CLR F0
start of low section
CLR PWMPIN
MOV A, #0FFH
CLR C
so it does
subtraction
SUBB A, R7
255 - R7.
MOV TH0, A
TH0 + R7 = 255
CLR TF0
interrupt flag
RETI
where
; Subtract R7 from A. A =
; so the value loaded into
; Clear the Timer 0
; Return from Interrupt to
; the program came from
In your main program you need to call this PWM_SETUP routine and your controller
will have a PWM output. Timer Interrupt service routine will take care of PWM in the
background. The width of PWM can be changed by changing the value of R7 register. In
above example I am using 160, you can choose any value from 0 to 255. R7 = 0 will give
you o/p 0V approx and R7 = 255 will give you 5V approx.
You can also make use of Timer1 if you want. And the output pin can be changed to
whatever pin you want.
C Code Example
Timer setup for PWM in C
CODE:
//Global variables and definition
#define PWMPIN P1_0
unsigned char pwm_width;
bit pwm_flag = 0;
void pwm_setup(){
TMOD = 0;
pwm_width = 160;
EA = 1;
ET0 = 1;
TR0 = 1;
}
The code of program for PWM with microcontroller 8051 for SCR or triac power control
is written c lanuage using keil compiler. The c-code for PWM is as follows.
/* This project is develop to generate two seperate channel PWM signals at 50HZ
frequency
The duty cycle of the pulses is variable subject to the input on Port1 and port2.
The output PWM signal can be used to drive SCR or triacs to control the phase angle and
power of any load with suitable hardware interface.
Please not that in this project the OPAMP are used to convert the pulses to 0 - 5 V, means
you can use it in as two channel DAC also.
But for triac or SCR interface the OPAMP circuit will be re-placed with proper
optocoupler like moc3020 and scrs.89c51 as pwm controller
*/
#include<at89x52.h>
unsigned char ch_count[2];// two channel counts array
unsigned char ch_duty_level[2];// level of the duty cycle
bit int_10ms;// this is a flag wich is set every 10msec time period
sbit PWM1 = P3^6;
sbit PWM2 = P3^7;
sbit indicator = P3^5;
void external_interrupt (void) interrupt 0
// external interrupt for zero crossing dectection
{
ch_count[0]=ch_duty_level[0];
// on zero crossing detection, counts are updated for both PWM channels
ch_count[1]=ch_duty_level[1];
PWM1=0; // the output is active low
PWM2 =0;//89c51 as pwm controller
int_10ms = 1; // 10msec flag is set
}
void timer_interrupt (void) interrupt 1
// The timer 0 is used for the duty cycle adjustement of two channel PWM
{
TL0 = 0XE2; //reset timer LSB
if(--ch_count[0]==0) PWM1 = 1;
// each time the timer interrupt occur the counts are decreased by one
and on reaching zero
if(--ch_count[1]==0) PWM2 = 1;
// the outputs are turned off, as the out put is active low
}
void main (void)
{
ET0 = 1; // enables the Timer 0 interrupt of 8051
TMOD = 0x02;
// timer mode register, timer 0 is used in mode 2
TL0 = 0XE2; // Initialize timer0 LSB
TR0 = 1; // timer 0 is activated
EX0 = 1; // external inturpt is activated
IT0 = 1;
EA = 1;
ch_duty_level[0] = 30; // dummy value for duty cycle for PWM 1
ch_duty_level[1] = 60; // dummy value for duty cycle for PWM 2
while (1) // endless loop
{
if( int_10ms){
int_10ms = 0;
ch_duty_level[0] = P1;
// take the input value of duty cycle for PWM 1 fromport1 of the 8051
ch_duty_level[1] = P2;
// take the input value of duty cycle for PWM 2 fromport1 of the 8051
indicator =~ indicator;
}
}
}