Amazon Store

Friday, 1 May 2015


Simple 100W Inverter Circuit


Inverter is a small circuit which will convert the direct current (DC) to alternating current (AC). The power of a battery is converted in to’ main voltages’ or AC power. This power can be used for electronic appliances like television, mobile phones, computer etc. the main function of the inverter is to convert DC to AC and step-up transformer is used to create main voltages from resulting AC
.
Block Diagram of Inverter:

In the block diagram battery supply is given to the MOSFET driver where it will convert DC to AC and the resulting AC is given to the step up transformer from the step up transformer we will the get the original voltage
.
Main Components
:
CD4047: CD4047 is a multi vibrator with very low power consumption designed by TEXAS INSTRUMENTS.it can operate in monostable multivibrator and also astable multivibrator.in the astable multivibrator mode it can operate in free running or gatable modes and also provides good astable frequency stability. It can generate 50% duty cycle which will create a pulse, which can be applied for inverter circuit. This is mainly used in frequency discriminators, timing circuits frequency divisions etc.

IRF540: IRF540 is a N-channel enhanced mode silicon gate field effect transistor (MOSFET).they are mainly used in switching regulators, switching converters relay drivers etc. the reason for using them in the INVERTER circuit is the because it is a high switching transistor , can work in very low gate drive power and have high input impedance.

IRF540 Symbol:
Simple 100W Inverter Circuit Diagram:
Explanation:
  • In the circuit diagram we can observe that 12V battery is connecter to the diode LED and also connected to the pin8 of the IC 4047 which is VCC or power supply pin and also to pin 4 and 5 which are astable and  complement astable of  the IC. Diode in the circuit will help not give any reverse current, LED will work as a indicator to the battery is working or not.
  •  IC CD4047 will work in the astable multivibrator mode. To work it in astable multivibrator mode we need an external capacitor which should be connected between the pin1 and pin3. Pin2 is connected by the resistor and a variable resistor to change the change the output frequency of the IC. Remaining pins are grounded .The pins 10 and 11 are connected to the gate of the mosfets IRF540. The pin 10 and 11 are Q and ~Q from these pins the output frequencies is generated with 50% duty cycle.
  • The output frequency is connected to the mosfets through resistor which will help to prevent to the loading of the mosfets. The main AC current is generated by the two mosfets which will act as a two electronic switches. The battery current is made to flow upper half or positive half of the primary coil of transformer through Q1 this is done when the pin 10 becomes high and lower half or negative half is done by opposite current flow through the primary coil of transformer, this is done when pin 11 is high. By switching the two mosfets current is generated.
  • This AC is given to the step up transformer of the secondary coil from this coil only we will get the increased AC voltage , this AC voltage is so high; from step up transformer we will get the max voltage. Zenor diode will help avoid the reverse current.
 The generated AC is not equal to the normal AC mains or house hold current. You cannot use this voltage for pure electric appliances like heater, electric cooker etc. Because of the fast switching of mosfets heat is dissipated which will effect the efficiency, use heat sink to remove this problem.

Tuesday, 24 March 2015

How Does Your Car's Airbag System Work

The airbag system is one of the most important parts of your vehicle's safety components. Proper airbag deployment can ensure that you and your passengers survive a crash that you may have otherwise experienced serious injury or death as a result of. Airbag deployment has saved thousands of lives through the years. But how exactly do they work? The airbag system is extremely complex and needs to activate within milliseconds after a crash occurs to ensure the protection of the driver and passengers.

What Are Airbags?
Airbags are stretchable fabrics or other materials that are tightly packed in various locations throughout your vehicle. There are airbags at the front of the dashboard in most cars, and many vehicles have airbags along the side of the car as well. These bags are compressed and kept in a small area. When there is an accident, the airbags fill up with air very quickly to provide a cushioning system for the people in the car so that they are not thrown around in the event of a crash. While this does not necessarily prevent total injury or death, it can be very helpful in cushioning the passengers in a car in many cases.

Crash Sensors
The most important parts of the success of the airbag system are the crash sensors. These small pieces of electronics are designed to tell when the vehicle has been damaged in an accident. They respond to several different sets of stimuli, including sudden stopping, increased pressure as pieces of the car are moved due to the force of the collision, and more.

Different types of sensors measuring wheel speed, seat occupant status, brake pressure and impact, and other vehicle status indicators are monitored by the airbag control unit located in the front portion of the cabin. The sensors relay signals to the airbag control unit, which analyzes the data and can orchestrate safety features like seat belt lock, automatic door locks, as well as airbag deployment.

Two types of airbag sensors used in cars are electrical and mechanical. Electrical sensors vary in design. Some use an electromechanical "ball and tube" mechanism, which basically consists of a small tube containing a circuit switch and ball that's held together by a small magnet. If a collision occurs, the ball is dislodged from the magnet and rolls forward in the tube, hitting a switch that completes the electrical circuit. Other electrical designs are similar in principle, using a metal roller or spring loaded weight instead of a ball, or in newer cars, an accelerometer to trip the sensor. Mechanical sensors work independent of the electrical system and respond similarly to the electrical sensors, with a design that actuates a firing pin triggering a small explosion after a crash. Since a mechanical sensor does not require a power source, it cannot be deactivated like an electrical sensor can when the battery is disconnected.

The success of the airbag system relies upon the crash sensors working not only accurately but also extremely quickly, so the most expensive and technologically advanced part of the airbag system are here.

Inflator
Once the control unit determines there is an accident, it sends a signal to the inflator system. The inflator sets off a chemical charge, producing an explosion of nitrogen gas, filling up the airbag. As the airbag fills up, it bursts through the paneling that contains it and enters into the space of the car in order to protect you.

This all happens in an instant, usually within 25 or 50 milliseconds. That translates to almost 200 miles per hour. The airbag then will deflate itself on its own once it deploys.

Related Questions and Answers

How Fast Does an Airbag Deploy?

An airbag can deploy in about 55 milliseconds, according to engineering study. This is about the same amount of time it takes you to blink your eyes or sneeze. In other words, airbag deployment is very quick. Authorities have noted that if you are out of position when an airbag deploys, you can be injured. How can you get out of position? Airbags should be used in conjunction with seatbelts, and if you drive without your seatbelt fastened, then it is possible for the airbag to fire, and for you to slide under the airbag just far enough so that you can receive a nasty blow to the face or head.

Which Airbag Sensors are Better, Electrical or Mechanical?

The jury is still out on the best airbag sensors system. For example, Toyota and Jaguar use electromechanical airbag igniters in which a small switch senses a crash force. If it is great enough, an igniter pin drives into the sodium azide packets used to inflate airbags. Other automakers use small tubes that break over a certain pressure, freeing a gold ball that completes the circuit, igniting the airbag. A third type--piezoelectric sensors--are used by several automakers. These electrical systems rely on switches located outside and inside the vehicle. If the sensors agree that there is enough force, then the airbag inflates.

How Much Does an Airbag Cost?

A common question asked is, 'How much does an airbag cost?'. If you need to replace or repair an airbag in your vehicle, the cost will vary depending on the type of car you are driving, as well as which airbag needs to be replaced. In most cases, budget between $400-800 for a driver's side airbag. A passenger side airbag can cost up to $1,000. Curtain airbags tend to be a bit cheaper, but are still fairly expensive. Replacing an airbag is a complicated repair, and it must be done by a professional. The labor costs are what makes up the majority of the bill when it comes to replacing an airbag.

How Do I Know If My Car Has Faulty Airbags?

If your vehicle has an airbag light, and it is lit up, this could indicate faulty airbags or a problem with the airbag system. If the vehicle does not have an airbag indicator light there is no real way of knowing if the vehicle has a faulty airbag. If the airbag light comes on, it is best to have the vehicle checked out by a professional. Airbags and the sensors that monitor them are complicated and must be repaired by a professional. This light comes on if the system senses a fault or problem with the airbag system. As the airbag system may save your life, it is best to get it checked out as soon as possible.

Where Can I Buy Used Airbags for Sale?

There are a number of places that have used airbags for sale. Used airbags are much cheaper than brand new ones, and if they have never been deployed, they are just as safe as new ones. Used airbags are removed from dead vehicles. Airbags can be salvaged and refurbished if necessary. It is best to check with your insurance company before having used airbags put in. There are policies that void coverage if used airbags are installed. Check with local junkyards and part recyclers. Doing a Google search for used airbags brings up a number of listings. Airbag Center is one of the leading providers of used and new airbags.

What is the Speed of an Airbag Deployment?

The speed of an airbag deployment will vary depending on the vehicle and the seriousness of the accident. In most cases, the airbag will come out at a speed of between 100 to 220 miles per hour. At this speed, an airbag can cause significant damage to a person, so it is important to be at least 10 inches away from the airbag when it deploys. This is why wearing a seatbelt is extremely important. Even when driving a car equipped with airbags. There have been cases of children being killed by a deploying airbag, so it is vital that the airbag be turned off if a child is in the front seat.

Sleeping Security - Smart Keypad Lock using AtMega16

This project is just a smart version of any keypad lock. What's smart about it is that it can detect whether it is needed by the user or not and accordingly switches itself to take a sleep. Making a microcontroller to sleep reduces power consumption as well as increases its usage span.

Many of you must be wondering that would it be secure enough to make a lock go to sleep, isn't it. Don't worry, when a microcontroller goes off to sleep it puts a hold on what it was doing before sleeping. In my project I have made it to sleep only when the user has finished entering the password stuff and microcontroller has done its entire job.

Sleeping Security - Smart Keypad Lock using AtMega16

This was really necessary because I didn't find it logical to make the microcontroller keep active all the time even when it's not needed.

So after entering the sleep mode it can be woken up by any interrupt source. Here what I have done is that I have used 4 diodes in such a way that when any of the keypad buttons is pressed they generate a low level pulse at the external interrupt INT0. INT0 is set to call its ISR routine function when such a thing happens. In short these diodes are working as an OR gate to detect any key pressed.

After waking up, the microcontroller resumes from the position where it went to sleep, thus no risk of data mishandling.

 

For further references you can see the coding part as well.

The Keypad is connected to PORTC and the LCD works on 4-bit mode with PORTB.

I have used a self-made header file for the keypad interfacing which you can find in this folder itself. 

A great thing about this lock is that it can remember your changed password. Now the question that arises is how to change the password? It can be done when the safe is open; you just need to enter the new password that you want. Note this password won't be hidden, as it is at the unlocking time.

 

What I want to say is that at the time of unlocking the LCD screen displays "*" for each time user presses a button. This is done to secure the password. But at the time of renewing it, LCD displays the same character pressed on the keypad just to ensure user about the password. Once the password has been changed everything becomes hidden again.

 

One more feature about this lock that would excite some of you is that you can even erase and rewrite your password. For erasing purposes, "<-" this button needs to be pressed.   

In my project I have used a LED as an indication about the lock status. When LED lights up it means unlocked status and when LED switches off it indicates the locked status.

 

In the coding part variable "i" plays an important role in deciding whether the microcontroller will go to sleep or not. A while loop runs till i<15000. "i" is incremented every time by one when no button is pressed but as soon as any button is pressed its value reaches to 0 and then again the whole process works accordingly.

Requirements

·         Keypad 4X4

·         Diode (1N4007)-4

·         AtMega16

·         LCD (16X2)

·         Locking Mechanism (optional)

Circuit Diagram


Code:

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

void function()
{
LCDclr();
LCDGotoXY(0,0);
LCDdisplay("ENTER PASSWORD");
LCDGotoXY(1,0);
}

int i=0,j=0;
char k=0,arr[16]="NUL",pass[16]="1234";

void main()
{
DDRA=1;
PORTA=0;
LCDinit();
LCDcursorOFF();
while(1)
{
if(PINA&1)
{
  LCDdisplay("SAFE OPEN");
  LCDGotoXY(1,0);
}
else
  function();
  while(i<15000)
{
  if(!k)
  i++;
  else
  {
   if(k!='#' && k!='E')
   {
   arr[j++]=k;
   if(PINA&1)
   LCDsendChar(k);
   else
   LCDsendChar('*');
   }
  else if(k=='E' && j!=0) 
   {
   j--;
   LCDcursorLeft(1);
   LCDsendChar(' ');
   LCDcursorLeft(1);
   }
  else if(k!='E')   
   {
   if(!strcmp(arr,"NUL"))
   {
    PORTA=0;
    LCDclr();
    LCDdisplay("SAFE LOCKED");
    _delay_ms(1000);
    function();
   }
   else 
    {
    arr[j]='\0';
    j=0;
    LCDclr();
    if(PINA&1)
    {
     if(strlen(arr)>=4)
    {
     strcpy(pass,arr);
     PORTA=0;//lock the safe
     LCDclr();
     LCDdisplay("SAFE LOCKED");
     _delay_ms(1000);
     function();
    }
    else
    {
     LCDdisplay("ERROR");
     LCDGotoXY(1,0);
     LCDdisplay("WEAK PASSWORD");
     _delay_ms(1000);
     LCDclr();
     LCDdisplay("SAFE OPEN");
     LCDGotoXY(1,0);
    }
    }
    else
{
     if(!strcmp(arr,pass))
     {
      LCDdisplay("SAFE OPEN");
      LCDGotoXY(1,0);
      PORTA=1;//open the safe
     }
     else
     {
      LCDdisplay("ERROR");
      LCDGotoXY(1,0);
      LCDdisplay("WRONG PASSWORD");
      _delay_ms(1000);
      function();
     }
    }
    strcpy(arr,"NUL");
   }
  }
  i=0;
}
k=Read_Keypad();
}
LCDclr();
DDRC=0x0f;
PORTC=0x00;
SREG|=(1<<7); //initializes the global interrupt i.e. sei();
DDRD&=~(1<<2); //declares INT0 as input pin
PORTD|=(1<<2); //pull-up enabled
GICR|=(1<<INT0); //enables INT0
GIFR|=(1<<INTF0); //enables INT0 flag for ISR recognition
MCUCR|=(1<<SM0)|(1<<SM1)|(1<<SE); //sleep mode enabled
MCUCR&=~(1<<SE); //sleep mode disabled
}
}

ISR(INT0_vect)
{
k=Read_Keypad();
GICR&=~(1<<INT0);
GIFR&=~(1<<INTF0);
i=0;j=0;strcpy(arr,"NUL");
}

Stepper Motor Angle Control using AVR Microcontroller

There are many applications in which it is required to set the position of an object at a desire angle. Some of the examples are

1.      Satellite Dish Antenna positioning 

The Satellite Dish Antenna should be in straight alignment with Satellite in Space to receive signal with maximum strength. So it's required to position the Dish Antenna at a certain angle

2.      Aileron, Elevator and Rudder positioning in Aircraft

In an Aircraft it is required to position Aileron, Elevator and Rudder at a desired angle to increase or decrease the lift, to change direction and to increase or decrease the speed

3.      Rudder Positioning in Ship

To move ship in a specific direction it is required to set an angle of Rudder located in front of the Propeller

4.      Tank Gun or Howitzer Positioning

To hit the bull's eye (Target) the Tank Gun (or Howitzer) must be positioned to specific angle 

 

This project demonstrates how any device or object can be positioned to desired angle. The circuit presented here demonstrates how to position Stepper Motor at a specific Angle using AVR microcontroller. The desired Angle Position is entered by user and when He presses the button to rotate motor, the motor starts rotating and rotates till it reaches that angle. The angle can be entered in step of 15o between 0o to 360o. User can increment or decrement angle value in step of 15o and set the desire angle. Based on set angle motor rotates forward (CCW) or reverse (CW). Like if current motor angle is 60o and user enters 90o then motor rotates CCW and if user enters 30o motor rotates CW.

 

System Circuit Diagram

As shown in figure the circuit is build using AVR microcontroller ATMega32, Current Driver Chip ULN2003A, LCD and few push buttons. 3 push buttons (SW1, SW2 and SW3) are connected to PORTA pins PA0 – PA2 such that when any buttons is pressed logic 0 is given as input to that pin. Normally all 3 pins are given logic 1 by connecting them to Vcc through pull-up resistors. PORTB pins PB0 – PB3 are connected to input pins of ULN2003A. These pins drive stepper motor. The output of ULN drives four coils of unipolar stepper motor. The common terminal of motor coils and ULN chip is connected to Vcc. PORTC drives data pins of LCD and two LCD control pins RS and En are driven by PORTD pins PD0 and PD1 respectively. Control pin RW is connected to ground. A 1K pot is connected to brightness control pin Vee (3) as shown. It can be used to adjust LCD brightness. A 16 MHz crystal is connected to crystal input pins of ATMega32 along with two 22pf capacitors as shown.

 

System Operation

·         When circuit is switched on the LCD displays set angle as 0 degree. The motor also rests at 0 degree

·         By pressing SW1 the angle can be increased in step of 15 degree. Similarly to decrease angle press SW2.

·         After selecting desire angle when SW3 is pressed, the stepper motor rotates till desire angle and waits for next angle value

·         The angle value can be increased or decreased by pressing SW1 or SW2

·         If new selected angle value is less than previous angle value then motor rotate reverse. E.g. if motor is at an angle 75and now new set angle value is 30othen motor rotates reverse

·         But if new selected angle value is higher, then motor rotates forward

Complete Circuit Operation is due to the software program embedded into AVR microcontroller. First let us see the logic behind the program.


Code:

#include <avr/io.h>

#include <util/delay.h>

#include <string.h>


//////////////// variable declarations////////////////////////////


unsigned int angle=0,new_angle,set_angle=0,num_of_pulses, rotate_reverse_flag=0;


///////////// function to send data to LCD//////////////////////////


void senddata(unsigned char data)

{

  _delay_ms(2);   // wait for LCD to be ready

PORTD=(1<<PD0);   // for data rs=1

PORTC=data;    // put data byte on data-bus

PORTD=(1<<PD0)|(1<<PD1); // give strobe to En pin

PORTD=(1<<PD0)|(0<<PD1);

}


//////////////////// function to send command to LCD////////////////


void sendcmd(unsigned char cmd)

{

_delay_ms(2);   // wait for LCD to be ready

PORTD=(0<<PD0);   // for data rs=0

PORTC=cmd;    // put command byte on data-bus

PORTD=(1<<PD1);   // give strobe to En pin

PORTD=(0<<PD1);

}


///////////// function to send string to LCD //////////////////////

void printstr(unsigned char *s)

{

uint8_t l,i;

l = strlen(s);   // get the length of string

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

{

  senddata(*s);  // write every char one by one

  s++; 

}

}


///////////////// function to display angle value on LCD///////////////


void display_value(unsigned int t)

  {

   unsigned int t1,a;

unsigned char asci[3];

if(t>=100)    // for angle in 3 digit

      {

    a=2;

    while(t>=10)  // separate all 3 digits

    { 

      t1=t%10;  // one by one

      asci[a]=t1+0x30; // convert them into ASCII

      t=t/10;  // store them in array

      a--;

      }

    asci[0]=t+0x30;  // first digit of angle

      }

  else     // for angle in2 digits

      {

    t1=t%10;   // separate both digits

    asci[2]=t1+0x30;  // convert them in to ASCII

    t=t/10;   

    asci[1]=t+0x30;

    asci[0]=0x20;  // take first digit as space

      }

sendcmd(0xC0);

senddata(asci[0]);  // send all digits one by one to

  senddata(asci[1]);  // LCD

  senddata(asci[2]);

printstr(" deg "); 

  }


//////////////// function to initialize LCD /////////////////////////

void lcd_init()

  {

    sendcmd(0x3E);   // configure LCD dot pattern

  sendcmd(0x0E);   // LCD screen on cursor on

  sendcmd(0x01);   // clear LCD memory and home cursor

  sendcmd(0x80);   // set cursor to 1st line 1st column

  printstr("Set Motor Angle:");  // display message

  }


//////////////// function to increase angle by 15 //////////////////

void inc_angle()

  {

   if(angle<345) angle+=15; // increase angle till 345 degree

display_value(angle);  // display new angle value

new_angle = angle;  // update new angle value

  }


////////////// function to decrease angle by 15 ////////////////

void dec_angle()

  {

   if(angle>0) angle-=15;  // decrease angle till 0 degree

display_value(angle);  // display new angle value

new_angle = angle;  // update new angle value

  } 


///////////////// function to rotate motor till desire angle //////////

void rotate_motor()

  {

   int i;

if(new_angle>set_angle)  // if new entered angle is larger

   {

  num_of_pulses = (new_angle-set_angle) / 15; // set number of

  rotate_reverse_flag=0; // pulses and clear flag to rotate

      // motor CCW

   }

else     // if new entered angle is smaller

   {

  num_of_pulses = (set_angle-new_angle) / 30; // set number

  rotate_reverse_flag=1; // of pulses and set flag to rotate

      // motor CW

   }

if(rotate_reverse_flag==0) // check if flag is clear

   {

    for(i=0;i<num_of_pulses;i++) // generate pulse sequence to

    {     // rotate motor CCW

     PORTB = 0x01;

   _delay_ms(100);

   PORTB = 0x02;

   _delay_ms(100);

   PORTB = 0x04;

   _delay_ms(100);

   PORTB = 0x08;

   _delay_ms(100);  

    } 

   }

  else      // else if flag is set

   {

    for(i=0;i<num_of_pulses;i++) // generate pulse sequence to

    {

     PORTB = 0x08;  // rotate motor CW

   _delay_ms(100);

   PORTB = 0x04;

   _delay_ms(100);

   PORTB = 0x02;

   _delay_ms(100);

   PORTB = 0x01;

   _delay_ms(100);

    } 

   }  

   set_angle = new_angle;  // save angle value


  }


/////////////////// main function starts here /////////////////////////

int main(void)

{

DDRC=0xFF;    // PORTB, PORTC and PORTD as

DDRD=0xFF;    // an output ports

DDRB=0x0F;

DDRA=0x00;    // PORT A as an input port

PORTC=0x00;

PORTD=0x00;

PORTB=0x0F;

lcd_init();    // initialize LCD

display_value(angle);  // display angle as 0 degree

loop:while(PINA==0xF0);  // loop until no button is pressed

switch(PINA)   // when button is pressed

   {

   case 0xF1:   // if 1st button is pressed

   inc_angle(); // increment angle by 15 degree

   _delay_ms(200); // key debounce delay

   break;  // get out of loop

  case 0xF2:   // same as above

   dec_angle();

   _delay_ms(200);   

   break;

  case 0xF4:   // for 3rd key

   rotate_motor(); // rotate motor   

   break;   

   }       

      goto loop;    // continuous loop

}


Fully Customized Device On/Off Timer

Timers are used in many different applications for example in Industrial Applications, to switch ON or switch OFF any device or a machine load for a specific period of time. In the same way the timers are used in Domestic Appliances like in Air Conditioners, Microwave Ovens, Washing Machines, Food Processors etc.

The applications for the timers could also be seen in Military Applications and Space Applications, to trigger any Bomb or a Missile or to ignite booster rockets for lift offs at a specific time.

Here the given project demonstrates the fully featured and customized timer built using AVR Microcontroller ATMega32.

Fully customized device on/off timer using ATMega32


Circuit Diagram:

 

Features of the On/Off Timer

·         Simple user interface using only 4 push buttons

·         LCD for message display

·         LEDs for indications

·         Buzzer for audio notification

·         Can count from 0 to 9999

·         User selectable two counting speeds – 1 count/sec and 10 count/sec

·         User selectable count up or count down

·         Potential free (relay) contacts for device/machine/load on/off

User can connect any device or machine with the timer relay contacts. Set the desire count from 0 to 9999. Set counting direction either up or down and set counting speed either 1 count/sec or 10count/sec. Then when he press start switch to start timer operation, the device/machine will remain ON till the count finishes. As the count finishes, the device will shift to OFF mode and a beep sound is produced for audio notification. The whole counting process is displayed on LCD as count increments of(or) decrements. Two LEDs – one green and one red, are also given for indication of counting process.

 

Circuit Description

The complete circuit is built around AVR Microcontroller ATMega32. It consists of push buttons for user setting, LCD display, LEDs for indication, buzzer for audio notification and relay to switch on/off load or device. For complete Circuit Diagram click on the Circuit Diagram Tab.

·         Four push buttons are connected to PORTA pins PA0 – PA3 such that when button is pressed that pin will get momentarily high logic input

·         PORTC is connected to data pins D0 – D7 of LCD that sends command or data to LCD

·         Two control pins En and Rs of LCD are connected to PORTD pins PD0 and PD1 respectively. Third control pin RW is connected to ground to make LCD write always enable

·         PB0 pin drives 1 C/O (change over) type relay through NPN transistor connected in switch configuration

·         Similarly PB3 pin drives DC buzzer through another NPN transistor connected in switch configuration

·         One green and one red LEDs are connected to PB1 and PB2 pins to indicate counting process is going on or finished

·         A 16 MHz crystal is connected to crystal input pins to provide internal clock for operation

·         Complete circuit works on 5 V supply


code:

#include <avr/io.h>

#include <util/delay.h>

#include <string.h>


#define lcd_databus PORTC

#define lcd_cntr_port PORTD

#define rs PD1

#define en PD0

#define output_port PORTB


unsigned int up_flag=1,dwn_flag=0,up_dwn_flag=0,count=0;

unsigned int count_set_flag=1,speed_flag=0,slow=1,fast=0,counting_rate;


void senddata(unsigned char data)

{

  _delay_ms(2);

lcd_cntr_port=(1<<rs);

lcd_databus=data;

lcd_cntr_port=(1<<rs)|(1<<en);

lcd_cntr_port =(1<<rs)|(0<<en);

}

void sendcmd(unsigned char cmd)

{

  _delay_ms(2);

lcd_cntr_port = (0<<rs);

lcd_databus=cmd;

lcd_cntr_port = (1<<en);

lcd_cntr_port = (0<<en);

}

void printstr(char *s)

{

uint8_t l,i;

l = strlen(s);  // get the length of string

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

{

  senddata(*s);  // write every char one by one

  s++; 

}

}

void display_data(unsigned int value)

  {

   unsigned char ascii_value[4];

unsigned int tmp,t;

if(value>1000)

   {

    t=3;

  while (value>1)

      {

     tmp = value%10;

   ascii_value[t] = tmp+0x30;

   value = value/10;

   t--;

    }

   ascii_value[0] = value+0x30;

   }

else if(value>100)

   {

    tmp = value%10;

  ascii_value[3] = tmp+0x30;

  value = value/10;

  tmp = value%10;

  ascii_value[2] = tmp+0x30;

  value = value/10;

  ascii_value[1] = value+0x30;

  ascii_value[0] = 0x30;

   }

else

   {

    tmp = value%10;

  ascii_value[3] = tmp+0x30;

  value = value/10;

  ascii_value[2] = value+0x30;

  ascii_value[1] = 0x30;

  ascii_value[0] = 0x30;   

   }

  for(t=0;t<4;t++) senddata(ascii_value[t]);    


   }

void up_arrow_key()

  {

  

if(count_set_flag==1)

   {

  count++;

  sendcmd(0xC0);

  display_data(count);

   }

else if(up_dwn_flag==1)

   {

    sendcmd(0xC0);

  printstr("up  ");

  up_flag=1;

  dwn_flag=0;

   }

else if(speed_flag==1)

   {

    sendcmd(0xC0);

  printstr("fast(0.1 sec)");

  fast=1;

  slow=0;

   }


   }


void down_arrow_key()

  {

  

if(count_set_flag==1)

   {

  count--;

  sendcmd(0xC0);

  display_data(count);

   }

else if(up_dwn_flag==1)

   {

    sendcmd(0xC0);

     printstr("down");

  up_flag=0;

  dwn_flag=1; 

   }

else if(speed_flag==1)

   {

    sendcmd(0xC0);

  printstr("slow(1 sec)  ");

  fast=0;

  slow=1;

   }


   }

void enter_key()

  {

  

if(count_set_flag==1)

   {

    sendcmd(0x80);

  printstr("count is set to ");

  sendcmd(0xC0);

  display_data(count);

  count_set_flag=0;

  _delay_ms(2000);

  sendcmd(0x01);

  printstr("count up or dwn?");

  sendcmd(0xC0);

  printstr("up");

  up_dwn_flag=1;

   }

   else if(up_dwn_flag==1)

   {

    sendcmd(0x01);

  printstr("selected");

  sendcmd(0xC0);

  if(up_flag==1) printstr("count up");

  else if(dwn_flag==1) printstr("count down");

  up_dwn_flag=0;

  _delay_ms(2000);

  sendcmd(0x01);

  printstr("set countng rate");

  speed_flag=1;

  sendcmd(0xC0);

  printstr("slow(1 sec)");

   }

   else if(speed_flag==1)

     {

   sendcmd(0x01);

   printstr("counting rate is");

   sendcmd(0xC0);

   if(slow==1)

     {

      counting_rate=9;

    printstr("1 sec");

     }

   else if(fast==1)

     {

      counting_rate=1;

    printstr("0.1 sec");

     }

     _delay_ms(2000);

     sendcmd(0x01);

     printstr("press start");

   } 

   }

void delay(int d)

  {

   int j;

for(j=0;j<d;j++) _delay_ms(100);

  }

void start_key()

  {

   unsigned int i;

sendcmd(0x01);

printstr("counting");

output_port |= 0x03;

if(up_flag==1)

   {

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

    {

     sendcmd(0xC0);

   display_data(i);

   delay(counting_rate);

    }

   }

else if(dwn_flag==1)

   {

    for(i=count;i>0;i--)

    {

     sendcmd(0xC0);

   display_data(i);

   delay(counting_rate);

    }

   }

output_port &= 0xFC;

output_port |= 0x0C;

sendcmd(0x01);

printstr("counting finish");

sendcmd(0xC0);

printstr("press reset");

_delay_ms(1000);

output_port &= 0xF7;

void main()

{

 

DDRD=0x03;

PORTD=0x00;

DDRC=0xFF;

PORTC=0x00;

DDRB = 0x0F;

PORTD = 0x00;

    sendcmd(0x3E);

sendcmd(0x0E);

sendcmd(0x01);

printstr("set count");

sendcmd(0xC0);

display_data(count);

while(1)

   {

  while(PINA==0xF0); 

     switch(PINA)

    {

     case 0xF1:

    up_arrow_key();

    break;

    case 0xF2:

    down_arrow_key();

    break;

   case 0xF4:

    enter_key();

    break;

   case 0xF8:

    start_key();

    break;             

        }

  _delay_ms(150);

    }   

  }