Wednesday, May 23, 2012

How to interafce 16x2 LCD with AVR microcontroller (ATmega16)


In this project LCD is working in 8-bit mode i.e., the data transferred to the LCD must be in 8-bit data form. The PortA of ATmega16 is connected to data pins of LCD and is defined as LCD_DATA. PortB is defined as control pins (Rs, R/W and En).

Conceptually, interfacing LCD with AVR microcontroller is similar to that of interfacing it with any other microcontroller.
The following steps explain in detail how LCD can be interfaced with ATMEGA 16:

Step1: To initialize LCD
The following instructions are used to initialize LCD.
·       0x38 to initialize LCD in 8-bit mode
·       0x01 to clear LCD
·       0x0E to make cursor ON
·       0x80 to go to first line and 0th position in LCD

 Delay of around 50 micro second is necessary b/w two commands for execution of instructions. 
void init_LCD(void)
{
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0x01); // clear LCD
_delay_ms(1);
LCD_cmd(0x0E); // cursor ON
_delay_ms(1);
LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position
delay_ms(1);
return;
}
Step2: Define command function
To send any command to LCD
·         Transfer the command to LCD_DATA port
·         Set RS and RW bit to go LOW and enable as HIGH
·         Give a small delay
·         Set enable to LOW 
void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en); // RS and RW as LOW and EN as HIGH
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en); // RS, RW , LOW and EN as LOW
_delay_ms(50);
return;
}
Step3: Define write function
To display any data on LCD
·         Transfer the data (Ascii value of the character to be sent) to LCD_DATA port.
·         Make RW as LOW and set RS and enable as HIGH.
·         Give a small delay.
·         Make enable pin LOW

void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en); // RW as LOW and RS, EN as HIGH
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en); // EN and RW as LOW and RS HIGH
_delay_ms(50); // delay to get things executed
return ;
}

Friday, May 18, 2012

Simple 12 Volt Day Night Switch


At times it’s really very annoying to see the street lights kept switched ON even during broad day light. The present circuit of an automatic night light can very effectively put an end to this problem. Read here how to build it.
We all know how important it has become to save electricity in today’s circumstance where it is getting difficult to fulfill the power requirements for our cities. Today everybody along with the politicians and the official authorities are busy putting forth their own opinions regarding the issue. But ironically at times we find the street lights kept switched on even during broad daylight, and this clearly shows how irresponsible these men can be. So instead of depending on these officials, why not take some help from electronics and find a solution to get the work done automatically? A simple circuit of an automatic night light described in this article can very accurately switch ON a load (street lights for example) when darkness falls and switch it OFF when dawn breaks.

How the Circuit Functions

A single NAND gate, a PNP transistor and few other passive components are the only things needed to construct this useful gadget. The circuit description can be understood from the following explanation:
  • As shown in the diagram a single NAND gate N1 from the IC 4093 is configured as an inverter and a voltage monitor.
  • A reference voltage can be set at its input with the help of VR1. This adjustment will set the level of darkness at which the system will change state.
  • An LDR (Light Dependent Resistor) which is also connected at the input of N1 is used to sense a difference in light levels. A LDR is in fact a resistor which changes its value with a change in the intensity of light falling on it.
  • In the absence of light or when its dark, the LDR offers an infinite resistance and thus the input of N1 is kept at logic high due to the voltage received through VR1. This means that at this instant the output of N1 is logic low, the relay is activated through T1 and the lights (load) connected to the relay contacts are switched ON.
  • With an increase in the ambient light the resistance of the LDR will gradually fall and after a certain level the input of N1 will become logic low. Immediately its output will go high switching OFF the transistor, the relay and the lights.
  • Capacitor C1 has been kept to avoid the relay from chattering during twilight threshold levels.

How to Build and Install the Unit

The entire circuit of this 12 volt day night switch can be built over a small piece of general PCB and the whole assembly along with the transformer may be housed inside a good looking ABS plastic enclosure. Only the LDR has to be fixed over the box so that it can sense the ambient daylight.
Take due care to position the LDR in such way that no other stray light or the light which it’s controlling is able to be incident on it, or else it may produce false switching and may start oscillating. The best position would be to install the unit at a point much higher than the lights which are controlled by it.
This automatic night light system may also be appropriately used to control building porch lights, neon signs, large advertising displays, gallery lights and also as automatic house interior decorative lights. Thus this simple inexpensive circuit should not only be able to relieve you from the headache of timely switching the particular lights but also will result in quite an economical way of using them.

Automatic LED Day, Night Light Circuit Diagram

Automatic LED Day, Night Light Circuit Diagram


Tuesday, May 15, 2012

Interface LED with AVR Microcontroller


All the four ports can be configured to read an input from some external device or to give output to any external device as per the application. For e.g., a switch is connected to a particular pin, that pin should be configured as input to read the values from the switch (external Device in this case) and if you are connecting a LED to any pin of the port then that particular pin should be configured as output to transmit the signal to the LED (external device in this case). A single port can be configured such that some of the pins of the same port are input and some are output.

Configuring IO Ports:
Every port (PORTx, x = A or B or C or D) of AVR microcontrollers have three registers associated with it:
1.      DDRx: Data direction Register, to set the direction of each pin of PORTx and configuring it to be as input or output.
2.      PORTx: The values which are to be supplied at the output of the port are written in this register. These values acts as input to the device connected at output port of the microcontroller (through PORTx output configured pins).
3.      PINx: This register stores the input value from the external connected hardware, when the port is configured as input port. The input data is read from PINx register.
So the first step in configuring or initializing any of the IO port is to set its direction in data direction register (DDRx) to define the behavior of individual pins as input or output. A high (1) in any bit of the DDRx register means the corresponding pin is set as output and vice versa.
Example: Considering switch-LED scenario, suppose a switch is connected to Pin5 (PORTD.4) of PORTD and LED is connected to Pin8 (PORTD.7) of PORTD. Now we need to initialize the pins accordingly. The other pins of PORTD are not used in this case so they can be ignored as of now.

STEP-1: In order to configure PORTD.4 as input the value of Pin-5 (DDRD.5) in DDRD register is made 0.
DDRD.7
DDRD.6
DDRD.5
DDRD.4
DDRD.3
DDRD.2
DDRD.1
DDRD.0
-
-
0
-
-
-
-
-
Step-2: To initialize PORTD.7 as output the value of Pin-8 (DDRD.7) in DDRD register is made 1.
DDRD.7
DDRD.6
DDRD.5
DDRD.4
DDRD.3
DDRD.2
DDRD.1
DDRD.0
1
-
0
-
-
-
-
-
Step-3: Rest of the pins can have any value as they are not being used in this case. The default values of DDRx register is 0 for each pin i.e., all the ports of AVR microcontrollers are initialized as input.
DDRD.7
DDRD.6
DDRD.5
DDRD.4
DDRD.3
DDRD.2
DDRD.1
DDRD.0
1
0
0
0
0
0
0
0
So the value of DDRD will be initialized as 0x80.
The above command can also be written in this form:
Where, 0b is symbol used to represent binary numbers and following it is the actual 8-bit value.
Setting the pull up value:
All the four ports of Atmega16 are equipped with internal software controlled pull up resistors. Every pin of the ports can be pulled up by setting the values into the register PORTx. Following table illustrates the actual state of the pin with different combination of DDRx and PORTx values.
DDRx
PORTx
I/O State
Pull Up
Description
0
0
Input
No
PORTx pins set as input and pull-ups disabled.
0
1
Input
Yes
PORTx pins set as input with pull up enabled
1
0
Output
No
PORTx pins set as output and pull-ups disabled.
1
1
Output
Yes
PORTx pins set as output with pull ups enabled.
Example: Following commands can be used to disable and enable pull-ups on PORTD.
PORTD = 0xFF;         //PORTD pins pulled high.
PORTD = 0x00;          //Disable pull up registers.
To understand above commands a simple LED blinking experiment is explained below.
Circuit description:
Connect the circuit as shown in the circuit diagram. There is no crystal in the circuit. This project uses the inbuilt crystal of AVR microcontroller, thereby avoiding the need of external crystal. The value of the internal crystal varies from 1MHz to 8MHz, which can be set using the fuse bits. (Readers may connect the external crystal but modify your fuse bits accordingly. Fuse bits are explained in later articles).
Explanation of code: 
#include<avr/io.h>
To include all the input output files for avr microcontroller.
#include<util/delay.h>
WinAvr has in built function for providing delay. To use the delay functions include the above header file.
DDRA=0xFF;
To make port A as output port
PORTA=~PORTA;
Whatever was the value of PORTA compliment it and send back to PORTA.
_delay_ms(1000);
A delay function, gives a delay of 1000 millisecond.
To understand the compilation and execution of the above program, please refer the tutorial Working with AVRstudio.
What is to be observed?
Similarly, a code can be for blinking of LED on all four PORTS. It is observed that all pins are working properly except PC2, PC3, PC4 and PC5 pins. The reason is that the AVR microcontrollers has inbuilt JTAG, which needs to be disabled for using these pins as I/O pins.