Showing posts with label tutoriols. Show all posts
Showing posts with label tutoriols. Show all posts

Wednesday, 16 October 2013

4 DIGIT COUNT DOWN TIMER USING LCD


                   Countdown Timer Using LCD

A countdown timer is a down counter that counts from a specified time to zero. It is used in many devices such as washing machine, televisions, microwave ovens, etc. This countdown timer has three states: the running state where it counts down, the pause state where it displays the paused time and the reset state to set the countdown.

In previous post two digit count down timer was described.In this post it modified for 4 digit count down timer.I have developed this count down timer as part of my another project.This  count down timer count from 9999 to zero. This can be used as count down timer in hours or minutes or seconds by adjusting the delay value.For count down in minutes delay value will be 6000.Also the maximum value of count down can be change in program.The reset switch will reset the count down timer.Main parts of the project are microcontroller AT89C51and LCD,5v Regulated power supply.



Microcontroller AT89C51:

Microcontroller AT89C51 is at the heart of the circuit. It is a low power, high performance, 8-bit microcontroller with 4 kB of flash programmable and erasable read-only memory (PEROM) used as on-chip program memory, 128 bytes of RAM used as internal data memory, 32 individually programmable input/output (I/O) lines divided into four 8-bit ports, two 16-bit programmable timers/counters, a five-vector two-level interrupt architecture, on-chip oscillator and clock circuitry. A 11.0592MHz crystal (XTAL1) is used to provide basic clock frequency for the microcontroller. Capacitor C3 and resistor R3 form the power-on reset circuit, while push-to-on switch S20 is used for manual reset.


Liquid Crystal Display (LCD):

Liquid crystal display a type of display used in digital watches and many portable computers.LCD displays utilize two sheets of polarizing material with a liquid crystal solution between them. An electric current passed through the liquid causes the crystals to align so that light cannot pass through them. Each crystal, therefore, is like a shutter, either allowing light to pass through or blocking the light.

The liquid crystals can be manipulated through an applied electric voltage so that light is allowed to pass or is blocked. By carefully controlling where and what wavelength (color) of light is allowed to pass, the LCD monitor is able to display images. A back light provides LCD monitor’s brightness.Other advances have allowed LCD’s to greatly reduce liquid crystal cell response times. Response time is basically the amount of time it takes for a pixel to “change colors”. In reality response time is the amount of time it takes a liquid crystal cell to go from being active to inactive. Here the LCD is used at both the Transmitter as well as the receiver side. The input which we give to the microcontroller is displayed on the LCD of the transmitter side and the message sent is received at the receiver side which displays at the receiver end of the LCD and the corresponding operation is performed.

They make complicated equipment easier to operate. LCDs come in many shapes and sizes but the most common is the 16 character x 4 line display with no backlight. It requires only 11 connections – eight bits for data (which can be reduced to four if necessary) and three control lines (we have only used two here). It runs off a 5V DC supply and only needs about 1mA of current. The display contrast can be varied by changing the voltage into pin 3 of the display.

 Power Supply Unit

The power supply unit is used to provide constant 5 V dc supply to the peripherals. The 230 V ac is converted into 9 V ac by using a transformer and then a bridge rectifier rectifies it to a 9 V dc with ac ripples. This is then filtered by electrolytic capacitors used across the rectifier output. LM7805 regulator is employed to obtain a constant 5 V dc at the output.

 Count Down Timer Using LCD Program:



#include<reg51.h>
#define cmdport P3
#define dataport P2
sbit rs = cmdport^0;  //register select pin
sbit rw = cmdport^1;  // read write pin
sbit e = cmdport^6;  //enable pin
void delay(unsigned int msec)  // Function to provide time delay in msec.
{
int i,j ;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcdcmd(unsigned char item)  //Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
}
void lcddata(unsigned char disp)  //Function to send data to LCD
{
dataport = disp;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
}          
void main() 
{  
  while(1)
  {
    unsigned int i,s,test_final,test_final_1,test_final_2,test_final_3,test_final_4,test_final_5;
    lcdcmd(0x38);  // for using 8-bit 2 row mode of LCD
     delay(100);
      lcdcmd(0x0E);  // turn display ON for cursor blinking
       delay(100); 
        lcdcmd(0x80);              
          delay(25);
          for(i=9959;i>0;i--)
             {    
                  lcdcmd(0x01);       
                 test_final=i;               
                 s=test_final/10000;                                                
                  if(s!=0)
                      {
                           lcddata(s+48);
                          delay(6000);
                      }
               else
                     {                            
                            s=test_final/1000;
                            test_final_1=test_final%1000;
                             test_final_2=test_final_1/100;
                             test_final_3=test_final_1%100;
                             test_final_4=test_final_3/10;
                             test_final_5=test_final_3%10;
                            lcddata(s+48);
                             lcddata(test_final_2+48);
                              lcddata(test_final_4+48);
                              lcddata(test_final_5+48);                           
                             delay(6000);
                      }                                
            }
   }
               
}       

// for 60 second delay(1 minute) == ms dealay 6000


                      

HOW CONVERT ARRAY OF ELEMENTS INTO DECIMAL VALUE



Convert Array data to an decimal number
In some programs it is necessary to convert the array elements into an integer value like receive value from keyboard and set the value as input for countdown timer.4x3 or 4x4 matrix keypads will give one key value at one time and LCD Display also display only one number at a time. So  if we need to display keyboard value 5432 you need to convert separate value of 5,4,3,2 into 5432 to set the timer. You can use this program for convert array elements to an integer.




Microcontroller AT89C51:

Microcontroller AT89C51 is at the heart of the circuit. It is a low power, high performance, 8-bit microcontroller with 4 kB of flash programmable and erasable read-only memory (PEROM) used as on-chip program memory, 128 bytes of RAM used as internal data memory, 32 individually programmable input/output (I/O) lines divided into four 8-bit ports, two 16-bit programmable timers/counters, a five-vector two-level interrupt architecture, on-chip oscillator and clock circuitry. A 11.0592MHz crystal (XTAL1) is used to provide basic clock frequency for the microcontroller. Capacitor C3 and resistor R3 form the power-on reset circuit, while push-to-on switch S20 is used for manual reset.


Liquid Crystal Display (LCD):

Liquid crystal display a type of display used in digital watches and many portable computers.LCD displays utilize two sheets of polarizing material with a liquid crystal solution between them. An electric current passed through the liquid causes the crystals to align so that light cannot pass through them. Each crystal, therefore, is like a shutter, either allowing light to pass through or blocking the light.

The liquid crystals can be manipulated through an applied electric voltage so that light is allowed to pass or is blocked. By carefully controlling where and what wavelength (color) of light is allowed to pass, the LCD monitor is able to display images. A back light provides LCD monitor’s brightness.Other advances have allowed LCD’s to greatly reduce liquid crystal cell response times. Response time is basically the amount of time it takes for a pixel to “change colors”. In reality response time is the amount of time it takes a liquid crystal cell to go from being active to inactive. Here the LCD is used at both the Transmitter as well as the receiver side. The input which we give to the microcontroller is displayed on the LCD of the transmitter side and the message sent is received at the receiver side which displays at the receiver end of the LCD and the corresponding operation is performed.

They make complicated equipment easier to operate. LCDs come in many shapes and sizes but the most common is the 16 character x 4 line display with no backlight. It requires only 11 connections – eight bits for data (which can be reduced to four if necessary) and three control lines (we have only used two here). It runs off a 5V DC supply and only needs about 1mA of current. The display contrast can be varied by changing the voltage into pin 3 of the display.

 Power Supply Unit

The power supply unit is used to provide constant 5 V dc supply to the peripherals. The 230 V ac is converted into 9 V ac by using a transformer and then a bridge rectifier rectifies it to a 9 V dc with ac ripples. This is then filtered by electrolytic capacitors used across the rectifier output. LM7805 regulator is employed to obtain a constant 5 V dc at the output.




 Convert Array data to an decimal number Program:


 #include<reg51.h>
sfr lcd_data_pin=0xA0;  // data port P2
sbit rs=P3^0; // Register select pin
sbit rw=P3^1; // Read write pin
sbit en=P3^6; // Enable pin
void delay(unsigned int msec)    //delay function
{
int i,j;                                   
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcd_command(unsigned char comm) // function to send command to LCD
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(1);
en=0;
}
void lcd_data(unsigned char disp)    // function to send data on LCD
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}
void main()
{
 unsigned int i,s,test_final,test_final_1,test_final_2,test_final_3,test_final_4,test_final_5;
unsigned int value[4]={5,4,3,2};
while(1)
{
lcd_command(0x38);  
delay(5);
lcd_command(0x0F);      
delay(5);
lcd_command(0x80);
delay(5);
  i=value[0]*1000+value[1]*100+value[2]*10+value[3];
                  test_final=i;               
                  s=test_final/10000;                                               
                   if(s!=0)
                    {
                      lcd_data(s+48);
                       delay(250);
                    }
                   else
                     {                            
                            s=test_final/1000;
                            test_final_1=test_final%1000;
                             test_final_2=test_final_1/100  
                             test_final_3=test_final_1%100;
                              test_final_4=test_final_3/10;
                             test_final_5=test_final_3%10;
                            lcd_data(s+48);
                             lcd_data(test_final_2+48);
                               lcd_data(test_final_4+48);
                                 lcd_data(test_final_5+48);                           
                                                                  
             }                                

    }
}
                                                                       

 

HOW TO INTERFACE A 4X3 MATRIX KEYPAD TO MICROCONTROLLER 8051(AT89C51) WITH C PROGRAM


Matrix Keyboard interfacing with Micro controller 8051  


      

Keyboards and LCDs are the most widely used input/output devices of the 8051, and a basic understanding of them is essential. In this tutorial the keyboard fundamentals, along with key press and key detection mechanisms was discussed. Interfacing a keyboard with microcontroller 8051 also described here. Keypad is a widely used input device with lots of application in our everyday life. From a simple telephone to keyboard of a computer, ATM, electronic lock, etc., 

keypad is used to take input from the user for further processing. In this article we are interfacing keypad with the MCU AT89C51 and displaying the corresponding number on LCD.  This module can be further used in a number of systems to interfaced keypad with microcontroller and other processors to get desired output. The program to interface keypad with controller is written in C language which is very easy to understand. 

Keypad is organized as a matrix of switches in rows and column. The article uses a 4X3 matrix keypad and a 16x2 LCD for displaying the output of keypad. The circuit diagram shows the connection of keypad with the controller.Port P1 of the microcontroller is used to send the data for displaying on the LCD. P2^1, P2^2, P2^3 pins of microcontroller is connected to RS, RW, EN pins of LCD respectively. Port P3 is used to scan input from the keypad (refer circuit diagram for connection).


      Fig.1 Interfacing matrix keypad with microcontroller 8051

Interfacing the Keyboard to the 8051 :

        At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and column through ports; therefore, with two 8-bit ports, an 8*8 matrix of keys can be connected to a microprocessor. When a key pressed, a row and column make a connect; otherwise, there is no connection between row and column. In IBM PC keyboards, a single micro controller (consisting of microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of software and hardware interfacing of keyboard. In such systems it is the function of programs stored in the EPROM of micro controller to scan the keys continuously, identify which one has been activated, and present it to the motherboard. In this section we look at the mechanism by which the 8051 scans and identifies the key.

Scanning and identifying the key:      
       
The following Figure shows a 4*4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1s for all columns since they are all connected to high (Vcc) If all the rows are grounded and a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the micro controller to scan the keyboard continuously to detect and identify the key pressed. How it is done is explained next.

The concept of interfacing keypad with the MCU is simple. Every number is assigned two unique parameters, i.e., row and column number (n(R, C) for example 6 (2, 3)). Hence every time a key is pressed the number is identified by detecting the row and column number of the key pressed.

Initially all the rows are set to zero by the controller and the columns are scanned to check if any key is pressed. In case no key is pressed the output of all the columns will be high. 

Whenever a key is pressed the row and column corresponding to the key will get short, resulting in the output of the corresponding column goes to go low (since we have made all the rows zero). This gives the column number of the pressed key.


Once the column number is detected, the controller set’s all the rows to high. Now one by one each row is set to zero by controller and the earlier detected column is checked if it becomes zero. The row corresponding to which the column gets zero is the row number of the digit.  


The above process is very fast and even if the switch is pressed for a very small duration of time the controller can detect the key which is pressed. The controller displays the number corresponding to the row and column on the LCD.

There are IC chips such as National Semiconductors MM74C923 that incorporate keyboard scanning and decoding all in one chip. Such chips use combinations of counters and logic gates (No micro controller).

 Keypad interfacing Embedded C Program:

#include<reg51.h>
#define port P2
#define dataport P1  // Dataport for lcd
#define sec 100
sbit rs = port^1;
sbit rw = port^2;
sbit en = port^3;

sbit col1=P3^4;
sbit col2=P3^5;
sbit col3=P3^6;
sbit row1=P3^0;
sbit row2=P3^1;
sbit row3=P3^2;
sbit row4=P3^3;
void delay(unsigned int msec) //Time delay function
{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char item) //Function to send command to LCD
{
dataport = item;
en=1;
rs= 0;
rw=0;
delay(1);
en=0;

}
void lcd_data(unsigned char disp) //Funtion to send data on LCD
{
dataport = disp;
en=1;
rs= 1;
rw=0;
delay(1);
en=0;

}

lcd_string(unsigned char *disp) // Function to send string on LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);       
}
}
void lcd_ini()                  //Function to initialize the LCD
  {
    lcd_cmd(0x38);         
    delay(5);
    lcd_cmd(0x0F);       
    delay(5);
          lcd_cmd(0x01);  //clear screen
          delay(5);
    lcd_cmd(0x80);

    delay(5);
   }
void display(int a) //Display functon for LCD

{

 lcd_ini();
switch(a)
{
case 1:lcd_string("one 1");
break;
case 2:lcd_string("two 2");
break;
case 3:lcd_string("three 3");
break;
case 4:lcd_string("four 4");
break;
case 5:lcd_string("five 5");
break;
case 6:lcd_string("six 6");
break;
case 7:lcd_string("seven 7");
break;
case 8:lcd_string("eight 8");
break;
case 9:lcd_string("nine 9");
break;
case 10:lcd_string("zero 0");
break;
case 11:lcd_string("*");
break;
case 12:lcd_string("#");
break;
}
}

void check_col1() //Function for checking column one
{
row1=row2=row3=row4=1;
row1=0;
if(col1==0)           
display(1);
row1=1;
row2=0;
if(col1==0)
display(4);
row2=1;
row3=0;
if(col1==0)
display(7);
row3=1;
row4=0;
if(col1==0)
display(11);
row4=1;
}

void check_col2() //Function for checking column two
{
row1=row2=row3=row4=1;
row1=0;
if(col2==0)
display(2);
row1=1;
row2=0;
if(col2==0)
display(5);
row2=1;
row3=0;
if(col2==0)
display(8);
row3=1;
row4=0;
if(col2==0)
display(0);
row4=1;
}

void check_col3() //Function for checking column three
{
row1=row2=row3=row4=1;
row1=0;
if(col3==0)
display(3);
row1=1;
row2=0;
if(col3==0)
display(6);
row2=1;
row3=0;
if(col3==0)
display(9);
row3=1;
row4=0;
if(col3==0)
display(12);  //For #
row4=1;
}          
void keypad()
{
          row1=row2=row3=row4=0;
          if(col1==0)
          check_col1();
          if(col2==0)
          check_col2();
          if(col3==0)
          check_col3();
}

void main()
{
col1=col2=col3=1;
while(1)
{                

keypad();
 }      
}


   

Tuesday, 23 July 2013

SELECTING PROPER MOTORS FROM DIFFERENT TYPES FOR ELECTRONICS APPLICATION PROJECTS



If you want your project to have some form of movements, linking a motor to some mechanism is one effective way of achieving the purpose. Whether it is to rotate a platform, or to swing a door, or to manipulate a lever, a motor can be the power source to operate the mechanism. However, you have to select the correct type of motor to match the job. Using the wrong motor would result in under-performance and in the worse case, the project ending in a complete failure. The following is an introduction to the different types of motor and their applications.

1. DC Motor
   This is the most common type of small battery operated motor that can be used for a variety of projects. The main advantage is their small size and they do not require complicated control. It can be connected directly to the rated battery voltage for simple functions. Straight from the box, a typical DC motor spins in excess of 15,000 rpm (revolution per minute), which is way too fast for most applications. To be useful, it must be geared down to the appropriate speed. Another reason for gearing down a DC Motor is to increase the tuning torque (turning power) which is required to do useful work. 

Applications:
Because of their small sizes and easy control, DC motors find many uses in modern appliances. They can be found cooling power supplies and CPUs in PCs and operating CD ROM drives. Printers use a few of them in each unit to feed paper to the print head. As a result, they can be found easily in used equipment and modified to for use in a project. However, it is important to note that DC motors are not very precise in their operations. Their speed can vary quite a bit even under the same operating condition. This is especially the case when they are under load.

Motor Selection When selecting a DC motor, ask yourself the following questions:

What is the load it is expected to bear? This is the turning torque of the motor and is measured in kg.cm. If the expected load is higher than the motor’s rating, it would heat up very quickly when in use and will soon breakdown. This is a very common cause of motor failure in projects. Always cater for some extra allowance in your selecting the power rating. For example, if your project requires 3kg.cm force, then choose a unit that gives 6kg.cm force. This would prevent motor failure in the event of the mechanism becoming jammed, which is quite common in the development stages.
What speed is the motor expected to turn at? Motor speed is measured in rpm. In applications where the speed is an important factor, it would have to be reduced by a series of gears. Some motors are sold with gear box with user-selectable ratios. These allow the user to select the gear ratio by interchanging the positions of the gears to arrive at a suitable speed for the output shaft. Further speed refinement can be done with an electronic speed controller.

DC Motor Control:

Some kind of control circuit is required if you need the motor to do more than just spinning. An example is when a motor is used to slide a tray open and close. The motor will need to turn in one direction to open the tray and stop. This is followed by turning in the opposite direction to close the tray. The control circuit would have to accept a signal to turn the motor in one direction and reverses it when a second signal is received to close the tray. The example cited above is relatively easy.
It can be accomplished by means of some switches connected together. For more complicated movements, an intelligent controller would be required. These may include movements that return the output shaft to the original position or one that requires several stops in one revolution.

2. AC Motor

   AC motors are complicated electrical equipment. They are mentioned here just so that you know that there is such a type of motor. Unlike DC motors, they cannot be powered by batteries and requires special connections to enable them to work properly. The most commonly available AC motor in small size is the synchronous motor. It spins at a constant rpm and are found in electric clocks (not battery-operated quartz).

Applications

These days, AC motors are found mostly in heavy industries like machine tools and equipment that require continuous operation like the compressor in refrigerators. Invariably, these tend to be large. Occasionally they may be used in places where speed is not an important factor. A new generation of AC (synchronous) motor has found its way into hobby uses. They provide very high speed power for electric model airplanes.
Because of mass production, they have become quite economical although the complicated electronic controllers are still expensive. AC motors are not useful for most projects unless there is a specific requirement.

3. Servo motor

   These are, strictly speaking not designed as motors like in the above categories. Servos were first used as escapement control to move valves
   and levers in mechanical systems. Ocean-going oil tankers make extensive use of servos to transfer crude oil to and from their bulk tanks. They have a built-in DC motor with reduction gears to increase their operating torque. An electronic circuit controls the movement of the motor precisely. All these are housed in a single casing making servo very compact and easy to use. For application in small projects, hobby servo is an ideal source of power.(photo - servos, inside).


Applications

Hobby servos can be used as they are or with modification. Ordinarily, they are the power houses that push and pull the elevators and ailerons of model airplanes. In recent years, they are used in robotic controls. Modified servos were first used as wheel motors because of their compact size, high power and easy control. A pair of continuous turning servos can easily power a 5 kg mobile robot. There are now purpose-made units for robotic applications. These have higher power output and very precise displacement control. Many find applications in moving limbs and sensors. Hobby servos can be linked to levers or cranks to effect useful movements in a project.(photo – door mech, mobile robot, brat).

Servo Selection

As in DC motor, servos come with different power rating. They range from 1.5 to12 kg.cm, varying also in physical size and battery consumption. Servos are more tolerant to overload because of the built-in reduction gears and sometimes current-limiting capability. The end result of using an underpowered servo is a stalled movement, usually without serious damage to the servo. It is still advisable to use a higher power servo motor for any application to cater for friction and mechanical losses due to sloppy linkages. Servo Control Servo can only be controlled by a servo controller designed for the purpose. 

The use of other types of controller can result in a damaged servo. There is a three-wire cable attached to all hobby servos. This cable must be connected to the controller in the right direction. The standard unmodified servo can move its output disc within limits, usually 100 degrees. The disc can be set to move between different angles and external linkages can be attached to the servo output to activate other mechanisms. Modified servos have their internal mechanical stops removed so that the motor can spin without restriction. In this case, the controller only controls the rotational speed. This is usually within a very limited range.

4. Stepper Motor

   Of all the motors considered so far, the stepper motor offers the greatest precision in term of displacement. However, they are inherently slow and bulky (and heavy) for the same power rating. A series of magnetic windings are placed around the casing (stator) at regular interval. Permanent magnets are attached on the rotor (the central spinning part) with no external electrical contact. The rotor therefore is freewheeling. Electric current is passed through the stator windings sequentially to cause the rotor to spin or to displace a specific angle and hold its position. A stepper motor with an appropriate controller can be expensive. You may want to consider the cost effective before deciding on using one.(photo – stepper, small and big).

Applications:

The stepper motor is the motor of choice in controlling the position of print heads in printers and laser sensor in CD ROM drives. These applications require very precise positioning of mechanical parts within a system. Except in the special areas of robotics, stepper motors are not very useful for general project because of their low torque and complicated control set-up.(photo – CD ROM, printer)


Stepper Motor Control

Special electronic controllers are used to control the movement of steppers. Depending on the motor used, the unit angle of displacement, speed and total displacement can be controlled. Steppers have the unique equality of holding a rotor position without damaged.












Related Posts Plugin for WordPress, Blogger...