Archive for March, 2011

Led blinking program with ATMEGA32 and AVR Studio 5.0

The first “Hello World!” project I prefer for Microcontroller is LED Blinking. I have used AVR  microcontroller .  Here,All pins of PORTB declared as output.

Source Code

Here,i have  written code  in C using AVR Studio 5.0.

/*
* ledblinking.c
*
* Created: 3/30/2011 9:36:30 AM
*  Author: sfg
*/

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRB=0xFF; //all pins of PORTB declared as output
PORTB=0x00;
while(1)
{
//TODO:: Please write your application code
PORTB=0xFF;  //High State
_delay_ms(200); //delay
PORTB=0x00; //low state
_delay_ms(200); //delay
}
}

Circuit

Led blinking With Atmega32

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

 

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

Stepper Motor Interfacing With Microcontroller

►Introduction

A stepper motor is a brushless, synchronous electric motor that converts electrical  pulses into mechanical movement. Every revolution of the stepper motor is divided into a discrete number of steps, and the motor must be sent a separate pulse for each step. The stepper motor can only take one step at a time and each step is the same size. Since each pulse causes the motor to rotate a precise angle, the motor’s position can be controlled without any feedback mechanism. As the electrical pulses increase in frequency, the step movement changes into continuous rotation, with the speed of rotation directly proportional to the frequency of the pulses. Step motors are used every day in both industrial and commercial applications because of their low cost, high reliability, high torque at low speeds and a simple, rugged construction that operates in almost any environment.

►Unipolar stepper motor
The unipolar stepper motor has five or six wires and four coils (actually two coils divided by center connections on each coil). The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole.

Unipolar Stepper Motor Windings

►Bipolar stepper motor
The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common center connection. They have two independent sets of coils instead. You can distinguish them from unipolar steppers by measuring the resistance between the wires. You should find two pairs of wires with equal resistance. If you’ve got the leads of your meter connected to two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance (or no continuity).

Bipolar Stepper Motor windings

Stepper Motor interfacing with Microcontrollers

Stepper motors can be used in various areas of microcontroller projects such as making robots, robotic arm, automatic door lock system etc.Here,I will discuss different controlling types (Half step and Full step), Interfacing Techniques (using L293D or ULN2003) to control stepper motor.

Step Sequence

Stepper motors can be driven in two different patterns or sqeunces. namely,

  • Full Step Sequence
  • Half Step Sequence

►Full Step Sequence

In the full step sequence, two coils are energized at the same time and motor shaft rotates. The order in which coils has to be energized is given in the table below.

Full Step Sequence

►Half Step Sequence
In Half mode step sequence, motor step angle reduces to half the angle in full mode. So the angualar resolution is also increased i.e. it becomes double the angular resolution in full mode. Also in half mode sequence the number of steps gets doubled as that of full mode. Half mode is usually preffered over full mode. Table below shows the pattern of energizing the coils.

Half Step Sequence

►Step Angle

Step angle of the stepper motor is defined as the angle traversed by the motor in one step. To calculate step angle,simply divide 360 by number of steps a motor takes to complete one revolution. As we have seen that in half mode, the number of steps taken by the motor to complete one revolution gets doubled, so step angle reduces to half.

As in above examples, Stepper Motor rotating in full mode takes 4 steps to complete a revolution, So step angle can be calculated as…

Step Angle ø = 360° / 4 = 90°

and in case of half mode step angle gets half so 45°.

So this way we can calculate step angle for any stepper motor. Usually step angle is given in the spec sheet of the stepper motor you are using. Knowing stepper motor’s step angle helps you calibrate the rotation of motor also to helps you move the motor to correct angular position.


►Connecting Unipolar Stepper Motor with Microcontroller(PIC16F887) using ULN2003

Stepper Motor Interfacing with microcontroller Using ULN2003

►Connecting Unipolar Stepper Motor with Microcontroller(PIC16F887) using L293D

Stepper Motor Interfacing With microcontroller Using L293D

Source Code

Here,I  have used PIC16F887  Microcntroller  and Code is written in C using mikroC PRO for PIC.Adjusting the delay will increase or decrease the speed of the motor. Here just for demonstration i have taken some delay, you can change it as you want.

►Programming Full step Sequence

void main() {ANSEL  = 0;                // Configure AN pins as digital I/O
ANSELH = 0;
PORTD = 0;
TRISD = 0;                 // Configure PORTD as output

while(1){
PORTD=0x09;
Delay_ms(500);
PORTD=0x0C;
Delay_ms(500);
PORTD=0x06;
Delay_ms(500);
PORTD=0x03;
Delay_ms(500);
}
}

►Programming Half step Sequence

void main() {

ANSEL  = 0;                // Configure AN pins as digital I/O
ANSELH = 0;
PORTD = 0;
TRISD = 0;                 // Configure PORTD as output

while(1){
PORTD=0x08;

Delay_ms(500);
PORTD=0x0C;
Delay_ms(500);
PORTD=0x04;
Delay_ms(500);
PORTD=0x06;
Delay_ms(500);
PORTD=0x02;
Delay_ms(500);
PORTD=0x03;
Delay_ms(500);
PORTD=0x01;
Delay_ms(500);
PORTD=0x09;
Delay_ms(500);

}
}


Bluetooth Module Interfacing with Microcontroller

Bluetooth® wireless technology is becoming a popular standard in the communication arena, and it is one of the fastest growing fields in the wireless technologies. It is  convenient, easy to use and has the bandwidth to meet most of today’s demands for mobile and personal communications. Bluetooth technology handles the wireless part of the communication channel; it transmits and receives data wirelessly between these devices. It delivers the received data and receives the data to be transmitted to and from a host system through a host controller interface (HCI). The most popular host controller interface today is either a UART or a USB .Here,I will only focus on the UART interface, it can be easily show how a Bluetooth module can be integrated on to a host system through a  UART connection and provide the designer an optimal solution for Bluetooth enabled systems.

Here,I will show two examples of  hardware interface between Bluetooth wireless technology and UART.One example shows an interface between an  Bluetooth module and a PC via UART, and the other example shows an interface between a Bluetooth module and a  Microcontroller via UART.

I have a used WT32 bluetooth module.To know more click here.

WT32 Bluetooth module

Supply voltage at VCC  pin can vary between 1.8 V and 3.3 V. VCC and BTEN  combined to a single 3.3 V supply voltage.

Interface between an  Bluetooth module and a PC via UART

Now connect the PC with Bluetooth module through RS232 over MAX232 or MAX233 level converter.

Bluetooth module Connection with PC

Now,Test  the connection with hyperterminal or any serial port communication software .Here,I have used hyperterminal for test.

Hyperterminal settings

 

– 9600 baud
– no parity
– 8 databits
– no flowcontrol
– 1 stopbit

You can be change this settings via hyperterminal with WT32 bluetooth module command.See more in user guide.

Interface between an  Bluetooth module and a Microcontroller via UART

Connect the bluetooth module with microcontroller.Here,I have used PIC16F887 microcontroller.

Now,Test the Communication between PC and Microcontroller Device.You can use the following code that is written in C using mikroC PRO for PIC.

Source Code

char uart_rd;
void main() {
ANSEL  = 0;                     // Configure AN pins as digital
ANSELH = 0;
UART1_Init(9600);               // Initialize UART module at 9600 bps
Delay_ms(100);                  // Wait for UART module to stabilize
while (1) {                     // Endless loop
UART1_Write_Text(“TEST”);
Delay_ms(2000);                  // Wait

}
}

Output

Now,you can be see the Data “TEST” on Hyperterminal that will send by microcontroller via Bluetooth Module.

Hyperterminal

(more…)

Servo Motor Control Using Microcontroller

A Servo is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. As the coded signal changes, the angular position of the shaft changes. In practice, servos are used in radio controlled airplanes to position control surfaces like the elevators and rudders. They are also used in radio controlled cars, puppets, and of course, robots.

The guts of a servo motor are shown in the picture below. You can see the control circuitry, the motor, a set of gears, and the case. You can also see the 3 wires that connect to the outside world. One is for power (+5volts), ground, and the white wire is the control wire.

Servo motor

So, how does a servo work? The servo motor has some control circuits and a potentiometer (a variable resistor, aka pot) that is connected to the output shaft. In the picture above, the pot can be seen on the right side of the circuit board. This pot allows the control circuitry to monitor the current angle of the servo motor. If the shaft is at the correct angle, then the motor shuts off. If the circuit finds that the angle is not correct, it will turn the motor the correct direction until the angle is correct. The output shaft of the servo is capable of travelling somewhere around 180 degrees. Usually, its somewhere in the 210 degree range, but it varies by manufacturer. A normal servo is used to control an angular motion of between 0 and 180 degrees. A normal servo is mechanically not capable of turning any farther due to a mechanical stop built on to the main output gear.

The amount of power applied to the motor is proportional to the distance it needs to travel. So, if the shaft needs to turn a large distance, the motor will run at full speed. If it needs to turn only a small amount, the motor will run at a slower speed. This is called proportional control.

How do you communicate the angle at which the servo should turn? The control wire is used to communicate the angle. The angle is determined by the duration of a pulse that is applied to the control wire. This is called Pulse Coded Modulation. The servo expects to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the motor turn to the 90 degree position (often called the neutral position). If the pulse is shorter than 1.5 ms, then the motor will turn the shaft to closer to 0 degress. If the pulse is longer than 1.5ms, the shaft turns closer to 180 degress.

Standard time vs. angle

Procedure

If you want to control your servo motor from a microcontroller,then you must follow-

bring high a digital port
wait between 1-2ms
bring low the same digital port
cycle a few dozen times per second


Here,i have used PIC16F887  microcontroller.Code is written in C using mikroC PRO for PIC.

Source Code

void main(){

int i;
ANSEL  = 0;                                    // Configure AN pins as digital I/O
ANSELH = 0;

PORTD=0x00;
TRISD=0x00;

while(1){

//Move Anti Clockwise direction
for(i=1;i<=500; i++){
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(1000); //want servo to move to 0 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);
delay_ms(20);

}

//Move Clockwise Direction
for(i=1;i<=500; i++){
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(2000); //want servo to move to 180 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);
delay_ms(20);

}

}
}

Circuit Diagram