<< | Home | Stuff | Download | Link | >>
Program
The Microcontroller AT89x51 Using C to Generate Pulse Width Modulation
Pulse Width Modulation (PWM) is essential in a smooth controlling. PWM can be generate by using MCS-51 microcontroller.
Pulse Width Modulation

Pwm is an asimetric square / triangle wave which the wide/duration of high pulse is different with the wide/duration of low pulse. By varying the high pulse Thigh and T is constant you will have an Voltage or Current which is proportional with duty cycle.
Duty cycle (D) is formed by Thigh, Tlow, and T. Duty cycle is a term used to describe the output pulse. It is given as a percentage.
D = Thigh / (Thigh + Tlow) = Thigh / T
MCS-51 TIMER
To generate PWM you may use TIMER feature. MCS-51 has 2 timers, timer 0 and timer1. Each timer can be employed in 4 mode timer.
mode 0 : 13 bit timer
mode 1 : 16 bit timer
mode 2 : 8 bit reloaded timer
mode 3 : mix reloaded timer
For further information on the timers’ operation, refer to the Atmel Web site ( http://www.atmel.com ). From the home page, select "Products", then "Microcontrollers", then "8051-Architecture", then "Documentation", and "Other Documents". Open the Adobe Acrobat file "AT89 Series Hardware Description".
In most of my experiment which involving microcontroller, I always using AT89Sxxxx, especially AT89S51. Because its cost and easy to upload the hex file into. You may read from AT89S Up-loader.
Program
This listing program using C language and modified for Small Device C Compiler (SDCC). SDCC is made by Mr. Sandeep Dutta (sandeep@users.sourceforge.net). SDCC is a Freeware, retargettable, optimizing ANSI - C compiler that targets the Intel 8051, Maxim 80DS390 and the Zilog Z80 based MCUs. Work is in progress on supporting the Motorola 68HC08 as well as Microchip PIC16 and PIC18 series. The entire source code for the compiler is distributed under GPL. You can download SDCC from http://sdcc.sourceforge.net.
Or you may see my article about Easy Interface Microcontroller Programming by using SDCC and Jens File Editor and find download section to download SDCC 2.3.0 (It is older version but work very good in AT89X family)
Here the listing program :
//
---------------------------------------------------
// DAC 1 bit using Pulse Width Modulation
// Push on Button P3.6 and P3.7 for adjusting the output voltage
// Output at P2.0.
// Pwmgen.C
// ---------------------------------------------------
#include <at89x51.h>
// header file including AT89x51.h will initiate the MCS-51 function, you may
use reg51.h
unsigned char k, m;
// data type
void ISR_Timer0() interrupt 1 { //
Interrupt Service Routine
TR0 = 0;
P2_0 = !P2_0;
if (P2_0) {
k = m;
} else {
k = 100-m;
}
TH0 = 0xFF;
TL0 = 0xFF-k;
TR0 = 1;
}
main() {
IE = 0;
// disable all Interrupt
EA = 1;
ET0 = 1;
// activate timer 0
TMOD = 0x01;
// timer 0 mode 1
TH0 = 0xFF;
// set the timer duration
TL0 = 0xFA;
m = 50;
// set the starting value duty cycle = 50 %
TR0 = 1;
do {
// loop program ; check the push on button
if (!P3_7) {
if (m<=95) {
m++;
}
}
if (!P3_6) {
if (m>=5) {
m--;
}
}
} while (1);
}
The Hardware Connection and Testing
You may use AT89C51 or AT89S51 or other MCS-51 (IS89C51, 89C51 etc).

As you can see above you must connect 2 switches push button which connected between P3.6, P3.7 and ground. If you push switch which connected to P3.7 the duty cycle will increase, and if you push switch which connected to P3.6 the duty cycle will decrease.
You can see the changing of duty cycle at P2.0 by using Oscilloscope. Or if you don't have an oscilloscope you may use this:
Use DCmeter or Voltmeter to measure the VDC+ and GND. It should increase as the duty cycle increased. And vice versa
Download Section :
Literatures :
Book : "Workshop Dasar Aplikasi mikrokontroler menggunakan bahasa C" 2003, Physics Engineering UGM by Ir. Balza Achmad, M.ScE & Ir. Agus Arif, MT.
Web : www.atmel.com , sdcc.sourceforge.net ,
Comment :
Any suggestions, comments, etc. E-mail me: fridiant@yahoo.com
_____________________________________________________________________________________________________________________
<< | Home | Stuff | Download | Link | >>
_______________________________________________________________________________________________________
Tiar Fridianto @ 2004 : fridiant@yahoo.com