8051 LCD interfacing Command Codes

Before you interface an LCD with any microcontroller , say 8051 or AVR, you should have a knowledge of basic LCD commands. These commands or codes are used to get the LCD perform some particular task, for example, clearing the LCD display screen ornshifting entire data to the right etc. I have gathered here some the commands that might prove for you an important data, because once I was doing 8051 LCD interfacing it really helped me a lot. Here are some of it :-
  1. 01H : Clear Display Screen
  2. 02H : Return Home
  3. 04H: Decrement Cursor
  4. 06H : Increment Cursor
  5. 05H : Shift Dis[;ay Right
  6. 07H : Shift Display Left
  7. 08H : Display off, Cursor off
  8. AH : Display off, Cursor on
  9. CH : Display on, Cursor off
  10. EH : Display on, Cursor Blinking
  11. FH : Display on, Cursor Blinkinf
  12. 10H: Shift Cursor position to left
  13. 14H: Shift Cursor position to right
  14. 18H: Shift the entire display to left
  15. 1CH: Shift the entire display to right
  16. 80H: Force cursor to begin from the Ist line
  17. C0H:Force cursor to begin from the 2nd line
  18. 38H: 2 lines and 5 x 7 matrix
More to folow for 8051 LCD interfacing

15:49 | Posted in , | Read More »

220V zero crossing detection using AVR microcontroller with minimum components

Well, it seems strange that zero crossing being done directly using microcontroller with the help of two resistors only. AVR  application note 182 verifies that zero crossing of 220 AC is possible using two resistors only. Here is how it looks like:

There is no rocket science in the above arrangement. 220AC through 1M ohm resistor, being fed directly to the AVR microcontroller will not do any harm to it. This is because the microcontrollers have internal clamping diodes that limits the high input voltage to microcontrollers operating voltage. The resistors here are used to convert a high voltage AC sinusoid to low voltage square wave. Thereafter using interrupts of the microcontroller, one can easily do the zero crossing detection.

However, there are some issues regarding the above arrangement. First of all EMI (Electromagnetic Compatibility) issue can arise. This is due to the fact that there is no isolation between the Hot Line and the microcontroller and the induced noise may affect the functionality both in the inter or intra system.  Secondly, the resistor connected with AC lines act as a RC filter and thereby generating a minute phase delay. But this minute phase delay can be neglected in most of the applications. This method for the zero crossing detection is still considered very efficient. 

Care should also be taken that high AC voltage may not damage the other components. There have been cases, I personally know, that people have burned out their microcontroller just because of little carelessness.

Also refer to the following posts:

RMS Voltage Control Circuit with MOC3021 and BT136

AVR C code for RMS voltage control using BT-136 and MOC-3021



For any queries, contact elprojects@ymail.com or fill up the contact form.

More to Follow!

01:42 | Posted in , , , , , | Read More »

AVR C code for RMS voltage control using BT-136 and MOC-3021

To accomplish the firing angle (RMS) firing angle control of an AC load, please refer to the hardware arrangement explained here. Here is the schematic taken from the application notes of MOC3021 :


There is a need of zero crossing detector, that will provide the reference point. Just after the microcontroller detects the zero crossing of 220V AC, it will turn on the BJT C828 (shown above) with some delay. This delay will determine the RMS voltage across the AC load. Say for example on 50 Hz AC,  the time period comes out to be 20ms. That means the microcontroller will detect the zero crossing every 10 ms. So the range of delay, after which the microcontroller will trigger C828, comes out to be 0-10ms. At delay of 0ms, whole sinusoid will pass through the AC load. Similarly at 5ms i-e firing angle of 90', the RMS voltage across the AC load will be 110V. Therefore we can say that by varying the delay from 0-10ms, we get varying RMS voltage from 220V to 0V.

I have tested the above circuitry using AVR ATmega16L. What microcontroller needs to do is to generate an interrupt on zero crossing and then call the delay routine. How zero crossing of 220V AC is accomplished on microcontroller, will be discussed later. Here is the C code for AVR studio, to test the firing angle control of AC load:

#include <avr/interrupt.h>
#include <avr/iom16.h>
#include <util/delay.h>


volatile int int_flag=0;


ISR( INT0_vect )
{
int_flag=1;
}


void int0_init( void )
{
    MCUCR = (0<<ISC01)|(1<<ISC00);  // enable any level change interrupt
    GICR = (1<<INT0);               // enable INT0
}


int main( void )
{
        DDRB   = 0xFF;     // PORTB as output
DDRA   = 0xFF;     
DDRC   = 0xFF;


  
    int0_init();                    // configure INT0

    sei();                          // enable global interrupts

    while (1)                       // loop forever, 
        
{
asm("nop");

if(int_flag==1)
{

PORTB=0x00;
PORTA=0x00;
PORTC=0x00;
PORTC=0xFF;
PORTB|=(1<<PB0);
_delay_ms(0.625);
PORTB=(1<<PB1)|(1<<PB0);
_delay_ms(0.625);
PORTB=(1<<PB2)|(1<<PB1)|(1<<PB0);
_delay_ms(0.625);
PORTB=(1<<PB3)|(1<<PB2)|(1<<PB1)|(1<<PB0);
_delay_ms(0.625);
PORTB=(1<<PB4)|(1<<PB3)|(1<<PB2)|(1<<PB1)|(1<<PB0);
_delay_ms(0.625);
PORTB=(1<<PB5)|(1<<PB4)|(1<<PB3)|(1<<PB2)|(1<<PB1)|(1<<PB0);
_delay_ms(0.625);
PORTB=(1<<PB6)|(1<<PB5)|(1<<PB4)|(1<<PB3)|(1<<PB2)|(1<<PB1)|(1<<PB0);
_delay_ms(0.625);
PORTB=(1<<PB7)|(1<<PB6)|(1<<PB5)|(1<<PB4)|(1<<PB3)|(1<<PB2)|(1<<PB1)|(1<<PB0);
_delay_ms(0.625);
PORTA=(1<<PA0);
_delay_ms(0.625);
PORTA=(1<<PA1)|(1<<PA0);
_delay_ms(0.625);
PORTA=(1<<PA2)|(1<<PA1)|(1<<PA0);
_delay_ms(0.625);
PORTA=(1<<PA3)|(1<<PA2)|(1<<PA1)|(1<<PA0);
_delay_ms(0.625);
PORTA=(1<<PA4)|(1<<PA3)|(1<<PA2)|(1<<PA1)|(1<<PA0);
_delay_ms(0.625);
PORTA=(1<<PA5)|(1<<PA4)|(1<<PA3)|(1<<PA2)|(1<<PA1)|(1<<PA0);
_delay_ms(0.625);
PORTA=(1<<PA6)|(1<<PA5)|(1<<PA4)|(1<<PA3)|(1<<PA2)|(1<<PA1)|(1<<PA0);
_delay_ms(0.625);
PORTA=(1<<PA7)|(1<<PA6)|(1<<PA5)|(1<<PA4)|(1<<PA3)|(1<<PA2)|(1<<PA1)|(1<<PA0);
int_flag=0;

}

}
    return(0);
}

What is happening in the code? The two ports of AVR ATmega16 is dedicated to test the RMS voltage control of an AC load. Any pin of PORTA and PORTB can be connected to the BJT. Pin 0 of PORTB will trigger the transistor at a delay of 0ms, Pin 1 at a delay of 0.625 ms. In fact the the firing angle is divided into 16 steps (as their are 16 pins dedicated), therefore as you move from PINB0 to PINB7 and then from PINA0 to PINA7, you get an increment of 0.625ms each time you move from one pin to another. If the BJT is connected across PINB0, you get RMS voltage 220 across the load. This RMS voltage will decrease as you move to PINB1 and further till PINA7, as the delay is increasing. 

The above code has been tested successfully. Contact elprojects@ymail.com for any queries.

22:49 | Posted in , , , | Read More »

Theory of 8051/AT89S52 based SMOKE DETECTOR and FIRE ALARM SYSTEM

OVERALL THEORY OF THE PROJECT
Report and Coding of above project can be found here.
The project over all consist of the following components:
1.   Microcontroller ATMEL 89S52.
2.   Temperature sensor LM-35.
3.   Smoke sensor TGS-308.
4.   16x2 LCD.
5.   Operational amplifier IC LM-324.
6.   12 Volts Buzzer.
7.   6 Volts Relay.
8.   Potentiometers 10K.
9.   5 Volts Voltage regulator 7805.
10. BJT C-1383 NPN-type
11. Crystal oscillator 11.0592 Mhz.
12. LED’s.
13. Push button.
14. Capacitors.
15. DB-25 Connector.
16. ISP Programmer.
17. KEIL UVision-2.
18. 9 Volts Adapter.
19. Weiroboard.
20. Wiring and miscellaneous.


             Step wise execution of the project is very necessary to fulfill the task. For this purpose first of all selection and purchase of all the components was done. Then the project was first designed on a breadboard. For this purpose first the microcontroller was connected with the crystal oscillator that produces 11.0592 MHz frequency to drive the microcontroller and to provide the clock frequency. After successful production of the frequency fixed 5 volts supply for the microcontroller was to be designed for which voltage regulator 7805 was used with capacitors circuitry. Output of the regulator was fed into the microcontroller’s Vcc. Until this task which is common for every microcontroller based project everything worked all well and fine. Coming to the main stream now the heat sensor LM-35 was interfaced with a comparator that gives an output voltage level for certain temperature. According to the fire environment the output of the heat sensor compares 0.5 Volts DC already set on the comparator IC, hence any increase in the temperature correspondingly increases the output of the sensor and hence the comparator IC was generating the high output thereby giving positive indication of fire.

            
            Similarly smoke sensor TGS-308 was interfaced with the comparator. As the sensor gives certain voltage level upon positive detection of smoke so the comparator was fed with 3 Volts DC to compare with the output of the smoke sensor, as soon as the output of the smoke sensor increases beyond 3 Volts the comparator passes high logic on its output hence indicating positive presence of smoke.


            The output of both these sensors is fed into the microcontroller through pins 2.0 and 2.2. The microcontroller continuously examines these pins and performs AND operation and high output of the AND operation confirms fire, there by sending the operation condition to the buzzer and displaying the location of fire on the LCD through parallel connections through port 1.



            Similarly three more smoke and heat sensors’ output were also being fed into the microcontroller which deals these inputs similarly like the one stated above and hence alarming the buzzer as an output and displaying the presence and the location of the fire on LCD display. Hence giving us a flexibility to interface various sensors at a time with the microcontroller and displaying the exact location of the fire. The output current of the microcontroller is very low, as low as a few microamperes, so buzzer can not be directly driven by the microcontroller as the buzzer must need at least a few milliamperes to sound efficiently and loudly. For this purpose need for a relay to be used was felt that was energized on logic high reception from the microcontroller and in turn was activating the buzzer. Continuous examining of the respective pins of the sensors makes this project highly useful to be used in sensitive and high risk areas.



            Programming of the project needs continuous examining of the pins connected to the output of the sensors and on positive indication sending data to LCD indicating the position of the fire and sounding of the alarm.


More Details see Project Report and Coding

10:25 | Posted in , | Read More »

8051/AT89S52 based SMOKE DETECTOR and FIRE ALARM SYSTEM

Download Project Report

       
       
8051 BASED FIRE ALARM SYSTEM
ownership

We, Mr. Hasban Mehmood, S/LT Arslan, S/LT Tauseef and S/LT Sikandar, bearing ROLL/P No. El-614, 7400, 7407, 7410 respectively of national university of science and technology, take an oath and state that the project FIRE ALARM SYSTEM is entirely and devotedly made by us. Previously stated project is owned by us and bears no belonging to anybody else. All the work and efforts in this project has been done only and only by the above mentioned group members. All the work from selection of project to the completion has been done by ourselves, although we have referred to some seniors and instructors of electronics department whenever we faced some problems which happened very often. 


Importance
In any modern structure or building of the world, safety has the highest priority and therefore fire detection system is one of the basic components of the structure. Timely information of fire not only helps save lives but also makes it easier to put out fire. 

Objectives: 
Our objective is to design a Fire Alarm System that would fulfill the following objectives:    
 
  • Indicate the room in which fire erupted
  • Sound the alarm if fire occurs
  •  False Alarm occurrence should be kept minimum
  •  The system should be flexible enough to be easily modified in case if new rooms are added to the building
  •  The system should also provide the flexibility to adjust the temperature and smoke sensitivity levels as per the operating environment
  • The system should never be in any ambiguous state. Under normal conditions the system should indicate the state of the room as ‘NORMAL’ in order to avoid any confusion.

REASONS FOR CHOOSING DIGITAL SOLUTION:
Conventional fire alarm systems having hard wire layout and normally opened warning devices like heat detector and smoke detector in general specifications, have a good view in low price, but have a bad view in

1. Low efficiency to warn and to communicate to human
2. Difficulty of maintenance
3. Hard expansion and transformation of working
4. No record and no database to develop in the future

Our proposed fire alarm system is designed and built to solve these problems. The connections between warning devices are dealt with microcontroller.

GENERAL WORKING OF THE PROPOSED SYSTEM:
This paper proposes the design and construction of fire alarm system which is controlled by 89S52 microcontroller. LCD is used to indicate situations of the system composed of 2 modes of working state, regular working mode and fire mode. Graphic output states are displayed on LCD by using software ‘Keil uVision2’, interfacing with microcontroller. Input signal coming from the normally opened warning devices such as heat detector and smoke detector is sent into a detectable instrument to separate the mode of working state. After that, output signal is dispatched to 89S52 microcontroller for analysis. This microcontroller can transfer data and display the situation of detector in 4 zones. Thus, the environment of the zones is exposed on LCD. In case of fire mode, the data will be sent to LCD and to Buzzer simultaneously.

HOW I WILL GO ABOUT IMPLEMENTING MY SOLUTION:
To make our lives easier and to make maximum use of time available to us, we plan to use the modular design concept to implement our system. The entire system is to be divided into different modules, which not only makes troubleshooting easier but is also an effective approach for system modification.

Our system will consist of the following modules:
• HEAT DETECTION CIRCUIT
• SMOKE DETECTION CIRCUIT
• MICROCONTROLLER PROGRAMMING
• INTERFACING OF BUZZER
• LCD INTERFACING

We first plan to implement each of our modules on breadboard. Once the circuit starts to function properly on the breadboard it will be implemented on the weiroboard. In the next step all these modules will be linked together via microcontroller. The software will then be embedded onto the hardware to perform the desired control operations.


HEAT DETECTION CIRCUIT:
This is the first module of our project in which we have used LM 35 sensor and a comparator. LM 35 has three pins a, b and c. Pin a is connected to Vcc, pin b is connected with ground while the output is from pin c .The output of this sensor is going to a comparator .As per our requirement we need to send a high signal to the microcontroller when the temperature is 50 degree centigrade As the output of sensor gives 10mv change with a change of 1 degree centigrade in temperature . So at 50 degree centigrade it will give 0.5 volts as output has a linear relationship with temperature .so to meet our requirement we have set 0.49 volts on the negative input of comparator so that when the output of sensor will give 0.5 volts the comparator will pass high signal to microcontroller. So when the temperature of external environment will be 50 degree centigrade, a high signal will be passed on to the microcontroller through comparator.

SMOKE DETECTION CIRCUIT:
The second module of our project is to detect smoke from the environment which is inevitable outcome of fire. So to fulfill this requirement we have used smoke sensor TGS 308 and a comparator. The sensor output is almost zero when there is no smoke in environment .On the contrary when there is a smoke the sensor will give a output voltage according to the intensity of smoke .As soon as the smoke vanishes its output again comes to zero .The internal circuitry of TGS-308 contains a heater .as the smoke particles will pass through , the heater will ionize the smoke particles and they will act as a charge carriers so a voltage will be built at output .TGS-308 has four pins Pin A, B, C and D. out of these A and B are connected to Vcc, pin D is connected with ground and pin C is the output pin. The output pin is connected with the positive input of comparator while at its negative input there is constant 3 volts .so as the smoke will produce, there will be voltage at output of sensor. As the smoke intensity increase and the sensor output will reach 3.1 volts the comparator will pass a high signal to microcontroller. As this sensor is quite sensitive to smoke so we have kept 3 volts as a standard at comparator input so that it should detect real fire and do not activate on fake signals just like if someone is smoking.

MICROCONTROLLER PROGRAMMING AND INTERFACING:
We have used 89S52 microcontroller. It has 40 pins .pin no 40 is connected to Vcc while pin no 20 with ground. 

LCD INTERFACING:
The LCD we used was having 16 pins out of which 8 pins are directly going to microcontroller pin number 1 to 8. Two pins are connected with Vcc one with ground and one with the potentiometer to set the resolution of LCD. Three control pins of LCD are also connected to microcontroller pin no 2.5, 2.6, and 2.7 .They are used for enabling LCD, performing read or write operations and to select command or data register .

BUZZER:
It has two pins; one is connected with the supply and the other one with the microcontroller pin no 2.4. When microcontroller will provide low signal, the circuit will be completed and the buzzer will start alarming.

SOFTWARE DESIGN
CONDITIONS:
  • In case if both the bits of a particular room are high (i.e. the smoke and heat levels are above  
    normal conditions), the program sets the pin connected to the buzzer, turning it ON. 
    Simultaneously the LCD displays the room number of the room where fire has erupted. 
  • In case if one of the bits of a particular room is set (i.e. either the smoke sensor or h
    is operating above normal conditions), the program takes this situation as ambiguous and  displays “NORMAL” on the LCD.

  • If both the bits are at low logic levels (i.e. both the heat and smoke sensors are operating under normal conditions), the program simply displays “NORMAL” on the LCD and the buzzer also remains OFF.
  • The programming approach to display output on the LCD includes character by character transfer of the entire string with an appropriate delay. The LCD is initialized only once and only the top line of the LCD is being used for display purpose.
SOURCE CODE:

;********************************************************************

 LCD_RS    BIT     P2.7
 LCD_RW    BIT     P2.6
 LCD_EN    BIT     P2.5

 LCD       EQU     P1


;********************************************************************
ORG     000H
      
        CALL    INIT_LCD
       
      
                 
again:
        CALL    GET_SENSOR_DATA
       
        MOV               A,#81H  
                CALL        WR_LCD_COMMAND
                CALL DELAY

                JNC HERE
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#'F'
                CALL        WR_LCD_CHR
                MOV       A,#'I'
                CALL        WR_LCD_CHR
                MOV       A,#'R'
                CALL        WR_LCD_CHR
                MOV       A,#'E'
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#'A'
                CALL        WR_LCD_CHR
                MOV       A,#'L'
                CALL        WR_LCD_CHR
                MOV       A,#'A'
                CALL        WR_LCD_CHR
                MOV       A,#'R'
                CALL        WR_LCD_CHR
                MOV       A,#'M'
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#'#'
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
       
        CLR     P2.4

       
        MOV               A,#31H
                CALL        WR_LCD_CHR

        JMP    AGAIN

HERE:   MOV          A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#'N'
                CALL        WR_LCD_CHR
                MOV       A,#'O'
                CALL        WR_LCD_CHR
                MOV       A,#'R'
                CALL        WR_LCD_CHR
                MOV       A,#'M'
                CALL        WR_LCD_CHR
                MOV       A,#'A'
                CALL        WR_LCD_CHR
                MOV       A,#'L'
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR
                MOV       A,#' '
                CALL        WR_LCD_CHR

        SETB    P2.4
              
        JMP AGAIN

;************************************************************************

INIT_LCD:
        MOV      A,#38H                ;2 line 5x7
        CALL    WR_LCD_COMMAND   
        MOV      A,#0CH                ;LCD on cursor on
        CALL    WR_LCD_COMMAND
        MOV      A,#01H                ;clear LCD
        CALL    WR_LCD_COMMAND
        MOV      A,#06H                ;shift cursor right
        CALL    WR_LCD_COMMAND
DISP:       MOV       A, #81H  ; Curser at LINE # 1, Position # 3
                CALL        WR_LCD_COMMAND
RET

;**************************************************************************

WR_LCD_COMMAND:
                CALL DELAY
                MOV  LCD,A
        CLR  LCD_RS
        CLR  LCD_RW
        SETB LCD_EN
        CLR  LCD_EN
RET

;***************************************************************************
WR_LCD_CHR:
                CALL DELAY
                MOV  LCD,A
        SETB LCD_RS
        CLR  LCD_RW
        SETB LCD_EN
        CLR  LCD_EN
        
RET
;***************************************************************************
DELAY:
                mov        r5, #0ffh                 ; delay loop instead of checking bit-7
                djnz        r5, $
RET
;****************************************************************************
GET_SENSOR_DATA:

        MOV C, P2.0
        ANL C, P2.2
RET
END

Feel free to contact us at elprojects@ymail.com.

20:49 | Posted in , | Read More »

Labels

Recently Commented

Recently Added