Archive for the ‘8051 Micrcontroller Tutorial’ Category

Servo Motor Control Using 8051 Micrcontroller

The control signals for the rotation of the motor are provided by 8051 microcontroller.For basic concepts and know-how of a servo motor, refer to the article Servo Motor.

Source Code

Here,i have used AT89S51  microcontroller.Code is written in C using  Keil uVision4.
#include<reg51.h>
sbit output=P1^0;          //Output to motor

void timer(int msec)     // Function for timer
{
int i;

for(i=0;i<msec;i++)

{
TMOD=0x20;         // Mode2
TH1=0xFF;
TL1=0xD1;
//TH1= -23;        // 50usec timer
TR1=1;
while(TF1==0);
TF1=0;
TR1=0;
}
}

void main()
{
int i;

output=0;

while(1)
{
//anticlockwise direction
for(i=0;i<500;i++)
{
output=1;
timer(20);
output=0;
timer(360);
}
//clockwise direction
for(i=0;i<500;i++)
{
output=1;
timer(40);
output=0;
timer(360);
}
}
}

Circuit

Servo motor control

 

Advertisements

Micrcontroller Programming Using C With Keil IDE

KEIL uVision is the name of a software dedicated to the development and testing of a family of microcontrollers based on 8051 technology,  which we are going to use along this tutorial. You can can download an evaluation version of KEIL at their website: http://www.keil.com/c51/. Most versions share merely the same interface, this tutorial uses KEIL C51 uVision 4 with the C51 compiler .

To create project,write and test the source code and also create hex file,follow the following steps:

Open Keil and Start a new project:


 

You will prompted to chose a name for your new project,chose  a name and click save.The following window will appear,where you will be asked to select a device for Target ‘Target 1’:

From the list ,select ATMEL and chose your microcntroller .The AT89s52 will be called your ‘Target device’ ,which is the final destination of your source code.You will be asked whether to copy standard 8051 startup code click No.

Click File,New and similar to the following window should appear.The blank space where your code should be written.

Now,You have to click ‘File ‘,Save as and chose a file name for code ending with the extension ‘.c’ and click save.Then you have to add this file to your project work space as shown in the following screen:

After right-clicking on ‘source group 1‘, click on ‘Add files to group…‘, then you will be prompted to browse the file to add to ‘source group 1’, chose the file that you just saved  and add it to the source group. You will notice that the file is added to the project tree at the left.

In some versions of this software you have to turn ON manually the option to generate HEX files. make sure it is turned ON, by right-clicking on target 1, Options for target ‘target 1’, then under the ‘output‘ tab, by checking the box ‘generate HEX file‘. This step is very important as the HEX file is the compiled output of your project that is going to be transferred to the microcontroller.

You can then start to write the source code in the window ‘ then before testing your source code, you have to compile your source code, and correct eventual syntax errors. In KEIL IDE, this step is called ‘rebuild all targets’ .

You can use the output window to track eventual syntax errors, but also to check the FLASH memory occupied by the program (code = 49) as well as the registers occupied in the RAM (data = 9). If after rebuilding the targets, the ‘output window’ shows that there is 0 error, then you are ready to test the performance of your code.

There are many other features to discover in the KEIL IDE. You will easily discover them in first couple hours of practice.

Led blinking program with 8051 Microcontroller and Keil uVision4

The first “Hello World!” project I prefer for Microcontroller is LED Blinking. I have used ATMEL’s 89C51 (40-pins DIP) 8051 architecture microcontroller which is ideal for first time learning MCU Chip.  The program is very simple and straight forward, that uses a delay procedure  loop based software delay.

Source Code

Here,i have  written code  in C using Keil uVision4.

#include <REGX51.H>
void msdelay(unsigned int );
void main(){

P2=0x00;  //all pin of PORT2 declared as output
//infinite loop
while(1){

P2=0xFF;   //all pin high
msdelay(250);    //delay
P2=0x00;   //all pin low
msdelay(250); //delay

}
}

//delay function
void msdelay(unsigned int value){

unsigned int x,y;
for(x=0;x<value;x++)
for(y=0;y<1275;y++);
}

In C programs you cannot be sure of delay, cause  it depends on compiler how it optimize the loops as soon as you make changes in the options the delay changes.

you will have to use Timers for making exact delays….. it does not matters whether u need delays of ms , us or even seconds…

below is a function of 1 second using timers…..

// the idea is to make a 50ms delay and run it 20 times as 20x50ms= 1000ms= 1sec

delay_1s() // timer of 1 sec
{
int d;
for(d=0;d<=20;d++)
{
TMOD=0x01;
TL0=0xFD;
TH0=0x04B;
TR0=1; // start timer.
while(TF0==0); // run until TF turns to 1
TR0=0; // stop timer
TF0=0; // reset the flag
}
}

Circuit Diagram

Led blinking with 8051 microcontroller