Unit - 6
Applications
Q1) What are LEDs?
A1)
Light Emitting Diodes are the semi- conductor light sources. Commonly used LEDs will have a cut-off voltage of 1.7V and current of 10mA. When an LED is applied with its required voltage and current it glows with full intensity.
The Light Emitting Diode works similar to normal PN- diode but it emits energy in the form of light. The color of light depends on the band gap of the semiconductor.
Q2) Explain the interfacing of LEDs?
A2)
Figure 1. LED
LED is connected to AT89C51 microcontroller with the help of a current limiting resistor. The value of this resistor is calculated using the following formula.
R= (V-1.7)/10mA, where V is the input voltage.
The maximum output voltage of microcontrollers is 5V. Thus, the value of resistor calculated for this is 330 Ohms. The resistor can be connected to either the cathode or the anode of the LED.
- Initially, burn the code into the microcontroller.
- Connect the LEDs to the Port0 of the microcontroller.
- Switch on the circuit.
- The LEDs start glowing.
- Now, switch off the circuit.
Algorithm
- Initially, include the “reg51.h” header file in your code.
- Now write a function to produce delay using for loop.
- Start the main function.
- Inside the while loop write the condition to port pin for making it logic high or low.
- Initially, make it high for some delay of 1000 microseconds.
- Now make the port pin low.
- Again, provide some delay of 1000 microseconds.
- Repeat this for 8 times using for loop.
- In another loop, try to represent the binary equivalent of the first 255 number using LEDs.
- Now close the while loop and end main.
Program:
#include<reg51.h>
#define led P0
Unsigned char i=0;
Void delay (int);
Void delay (int d)
{
Unsigned char i=0;
For(;d>0;d--)
{
For(i=250;i>0;i--);
for(i=248;i>0;i--);
}
}
Void main()
{
While(1)//// led blink
{
Led=0xff;
Delay(1000);
Led=0x00;
Delay(1000);
++i;
If(i==7)
{
i=0;
Break;
}
}
While(1)//// binary equivalent representation of 1byte data
{
Led=i++;
If(i==256)
{
i=0;
Break;
}
Delay(500);
}
While(1);
}
Q3) What are the applications of LED?
A3)
- LEDs are widely used in many applications like in seven segments.
- They are used in dot matrix displays.
- They can be used for streetlights.
- They are used as indicators.
- They can be used in traffic lights.
- They are used in emergency lights
- They can used to make electronic designs.
Q4) What is LCD?
A4)
LCD is one of the most used display unit. 16x2 LCD means that there are two rows in which 16 characters can be displayed per line, and each character takes 5X7 matrix space on LCD.
We can divide it in five categories, Power Pins, contrast pin, Control Pins, Data pins and Backlight pins.
RS: RS is the register select pin. If set it to 1 we are sending some data to be displayed on LCD. If set it to 0 some command instruction like clear the screen (hex code 01).
RW: This is Read/write pin, If set it to 0, we write some data on LCD. And if set to 1 we read from LCD module. Generally, this is set to 0, because we do need to read data from LCD.
E: This pin is used to enable the module when a high to low pulse is given to it. That transition from HIGH to LOW makes the module ENABLE.
Some important command instructions are given below:
Q5) Explain the interfacing of LCD?
A5)
Figure 2. LCD Interfacing
LCD interfacing with 8051 microcontrollers is shown in the above figure.
The data pins (D0-D7) of LCD to the Port 2 (P2_0 – P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3) of microcontroller, respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1 (VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected to voltage supply and Ground.
Program:
/ Program for LCD Interfacing with 8051 Microcontroller (AT89S52)
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
Sbit rs = P3^2; //RS pin connected to pin 2 of port 3
Sbit rw = P3^3; // RW pin connected to pin 3 of port 3
Sbit e = P3^4; //E pin connected to pin 4 of port 3
Void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
Void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1
msdelay(10);
}
Void main()
{
unsigned char a[15]="CIRCUIT DIGEST"; //string of 14 characters with a null terminator.
int l=0;
lcd_init();
while(a[l] != '\0') // searching the null terminator in the sentence
{
lcd_data(a[l]);
l++;
msdelay(50);
}
}
Q6) Explain the applications of LCD?
A6)
- The LCDs are commonly used in all the digital wrist watches for displaying time.
- The LCD (liquid crystal display) is used in aircraft cockpit displays.
- It is used for displaying images used in digital cameras.
- It is used in instruments panel where all the lab instruments uses LCD screens for display.
- It is used as a display screen in calculators.
- The television is main applications of LCD.
- Mostly the computer monitor is made up of LCDs.
Q7) Explain Keyboard Interfacing with 8051?
A7)
Matrix keyboard is used for interfacing. It is designed with a rows and columns. These rows and columns are connected to the microcontroller through its ports of 8051.
Whenever a key is pressed, a row and a column get shorted through that pressed key and all the other keys are left open. The bit in the port goes high which indicates the microcontroller that the key is pressed. By this high on the bit key in the corresponding column is identified.
Once we are sure that one of key in the keyboard is pressed next our aim is to identify that key. To do this we firstly check particular row and then check the corresponding column on the keyboard.
In order to check the row of the pressed key, one of the rows is made high by making one of bit in the output port of 8051 high. This is done until the row is found out. Once we get the row the next task is to detect the column of the pressed key. The column is detected by contents in the input ports with the help of a counter. The content of the input port is rotated with carry until the carry bit is set.
The contents of the counter is compared and displayed in the display. This display is seven segment display and a BCD to seven segment decoder IC 7447.
The BCD equivalent to the number of counter is sent through output part of 8051 displays the number of pressed key.
Figure. Keyboard interfacing with 8051
Q8) Write a program for keyboard interfacing?
A8)
Start: mov a,#00h
mov p1,a ;making all rows of port p1 zero
mov a,#0fh
mov p1,a ;making all rows of port p1 high
press: mov a,p2
jz press ;check until any key is pressed
After making sure that any key is pressed
mov a,#01h ;make one row high at a time
mov r4,a
mov r3,#00h ;initiating counter
next: mov a,r4
mov p1,a ;making one row high at a time
mov a,p2 ;taking input from port A
jnz colscan ;after getting the row jump to check
column
mov a,r4
rl a ;rotate left to check next row
mov r4,a
mov a,r3
add a,#08h ;increment counter by 08 count
mov r3,a
sjmp next ;jump to check next row
After identifying the row to check the column following steps are followed
colscan: mov r5,#00h
in: rrc a ;rotate right with carry until get the carry
jc out ;jump on getting carry
inc r3 ;increment one count
jmp in
out: mov a,r3
da a ;decimal adjust the contents of counter
before display
mov p2,a
jmp start ;repeat for check next key.
Q9) Explain stepper motor interfacing?
A9)
Figure. Interfacing Stepper Motor with 8051
Program:
#include<reg51.h>
Sbit LED_pin = P2^0; //set the LED pin as P2.0
Void delay(int ms){
unsigned int i, j;
for(i = 0; i<ms; i++){ // Outer for loop for given milliseconds value
for(j = 0; j< 1275; j++){ //execute in each milliseconds
;
}
}
}
Void main(){
int rot_angle[] = {0x0C,0x06,0x03,0x09};
int i;
while(1){ //infinite loop for LED blinking
for(i = 0; i<4; i++){
P0 = rot_angle[i];
delay(100);
}
}
}
Q10) Explain DC motor Interfacing?
A10)
DC motor converts electrical energy in the form of Direct Current into mechanical energy.
Figure 6. DC Motor Interfacing
- As shown in the above figure we have connected two toggle switches on P1.0 and P1.1 pin of the AT89S52 microcontroller to change the speed of the DC motor by 10%.
- One toggle switch at pin P1.2 controls the motor rotating direction.
- P1.6 and P1.7 pins are used as output direction control pins. It provides control to motor1 input pins of L293D motor driver which rotate the motor clockwise and anticlockwise by changing their terminal polarity.
- And Speed of the DC Motor is varied through PWM Out pin P2.0.
- The timer of AT89S52 is used to generate PWM.
Program
#include <reg52.h>
#include <intrins.h>
/* Define value to be loaded in timer for PWM period of 20 milli second */
#define PWM_Period 0xB7FE
Sbit PWM_Out_Pin = P2^0;/* PWM Out Pin for speed control */
Sbit Speed_Inc = P1^0;/* Speed Increment switch pin */
Sbit Speed_Dec = P1^1;/* Speed Decrement switch pin */
Sbit Change_Dir= P1^2;/* Rotation direction change switch pin */
Sbit M1_Pin1= P1^6;/* Motor Pin 1 */
Sbit M1_Pin2= P1^7;/* Motor Pin 2 */
Unsigned int ON_Period, OFF_Period, DutyCycle, Speed;
/* Function to provide delay of 1ms at 11.0592 MHz */
Void delay(unsigned int count)
{
int i,j;
for(i=0; i<count; i++)
For(j=0; j<112; j++);
}
Void Timer_init()
{
TMOD = 0x01;/* Timer0 mode1 */
TH0 = (PWM_Period >> 8);/* 20ms timer value */
TL0 = PWM_Period;
TR0 = 1;/* Start timer0 */
}
/* Timer0 interrupt service routine (ISR) */
Void Timer0_ISR() interrupt 1
{
PWM_Out_Pin = !PWM_Out_Pin;
If(PWM_Out_Pin)
{
TH0 = (ON_Period >> 8);
TL0 = ON_Period;
}
Else
{
TH0 = (OFF_Period >> 8);
TL0 = OFF_Period;
}
}
/* Calculate ON & OFF period from PWM period & duty cycle */
Void Set_DutyCycle_To(float duty_cycle)
{
Float period = 65535 - PWM_Period;
ON_Period = ((period/100.0) * duty_cycle);
OFF_Period = (period - ON_Period);
ON_Period = 65535 - ON_Period;
OFF_Period = 65535 - OFF_Period;
}
/* Initially Motor Speed & Duty cycle is zero and in either direction */
Void Motor_Init()
{
Speed = 0;
M1_Pin1 = 1;
M1_Pin2 = 0;
Set_DutyCycle_To(Speed);
}
Int main()
{
EA = 1;/* Enable global interrupt */
ET0 = 1; /* Enable timer0 interrupt */
Timer_init();
Motor_Init();
while(1)
{
/* Increment Duty cycle i.e. speed by 10% for Speed_Inc Switch */
If(Speed_Inc == 1)
{
If(Speed < 100)
Speed += 10;
Set_DutyCycle_To(Speed);
While(Speed_Inc == 1);
Delay(200);
}
/* Decrement Duty cycle i.e. speed by 10% for Speed_Dec Switch */
If(Speed_Dec == 1)
{
If(Speed > 0)
Speed -= 10;
Set_DutyCycle_To(Speed);
While(Speed_Dec == 1);
Delay(200);
}
/* Change rotation direction for Change_Dir Switch */
If(Change_Dir == 1)
{
M1_Pin1 = !M1_Pin1;
M1_Pin2 = !M1_Pin2;
While(Change_Dir == 1);
Delay(200);
}
}
}
Q11) Explain sensor interfacing?
A11)
DHT11 is a single wire digital humidity and temperature sensor, which provides humidity and temperature values serially.
Figure . Sensor Interfacing
- The above circuit diagram shows the interfacing of 8051 with the DHT11 sensor.
- In that, a DHT11 sensor is connected to P2.1(Pin No 22) of the microcontroller.
Programming Steps
- First, initialize the LCD16x2_4bit.h library.
- Define pin no. To interface DHT11 sensor, in our program we define P2.1 (Pin no.22)
- Send the start pulse to the DHT11 sensor by making low to high on the data pin.
- Receive the response pulse from the DHT11 sensor.
- After receiving the response, receive 40-bit data serially from the DHT11 sensor.
- Display this received data on LCD16x2 along with error indication.
#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"
Sbit DHT11=P2^1;/* Connect DHT11 output Pin to P2.1 Pin */
Int I_RH,D_RH,I_Temp,D_Temp,CheckSum;
Void timer_delay20ms()/* Timer0 delay function */
{
TMOD = 0x01;
TH0 = 0xB8;/* Load higher 8-bit in TH0 */
TL0 = 0x0C;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void timer_delay30us()/* Timer0 delay function */
{
TMOD = 0x01;/* Timer0 mode1 (16-bit timer mode) */
TH0 = 0xFF;/* Load higher 8-bit in TH0 */
TL0 = 0xF1;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void Request()/* Microcontroller send request */
{
DHT11 = 0;/* set to low pin */
Timer_delay20ms();/* wait for 20ms */
DHT11 = 1;/* set to high pin */
}
Void Response()/* Receive response from DHT11 */
{
While(DHT11==1);
While(DHT11==0);
While(DHT11==1);
}
Int Receive_data()/* Receive data */
{
Int q,c=0;
For (q=0; q<8; q++)
{
While(DHT11==0);/* check received bit 0 or 1 */
Timer_delay30us();
If(DHT11 == 1)/* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */
Else/* otherwise its logic LOW */
c = (c<<1);
While(DHT11==1);
}
Return c;
}
Void main()
{
Unsigned char dat[20];
LCD_Init();/* initialize LCD */
While(1)
{
Request();/* send start pulse */
Response();/* receive response */
I_RH=Receive_data();/* store first eight bit in I_RH */
D_RH=Receive_data();/* store next eight bit in D_RH */
I_Temp=Receive_data();/* store next eight bit in I_Temp */
D_Temp=Receive_data();/* store next eight bit in D_Temp */
CheckSum=Receive_data();/* store next eight bit in CheckSum */
If ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
LCD_String_xy(0,0,"Error");
}
Else
{
Sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
Sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
Memset(dat,0,20);
Sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
Delay(100);
}
}
Unit - 6
Applications
Q1) What are LEDs?
A1)
Light Emitting Diodes are the semi- conductor light sources. Commonly used LEDs will have a cut-off voltage of 1.7V and current of 10mA. When an LED is applied with its required voltage and current it glows with full intensity.
The Light Emitting Diode works similar to normal PN- diode but it emits energy in the form of light. The color of light depends on the band gap of the semiconductor.
Q2) Explain the interfacing of LEDs?
A2)
Figure 1. LED
LED is connected to AT89C51 microcontroller with the help of a current limiting resistor. The value of this resistor is calculated using the following formula.
R= (V-1.7)/10mA, where V is the input voltage.
The maximum output voltage of microcontrollers is 5V. Thus, the value of resistor calculated for this is 330 Ohms. The resistor can be connected to either the cathode or the anode of the LED.
- Initially, burn the code into the microcontroller.
- Connect the LEDs to the Port0 of the microcontroller.
- Switch on the circuit.
- The LEDs start glowing.
- Now, switch off the circuit.
Algorithm
- Initially, include the “reg51.h” header file in your code.
- Now write a function to produce delay using for loop.
- Start the main function.
- Inside the while loop write the condition to port pin for making it logic high or low.
- Initially, make it high for some delay of 1000 microseconds.
- Now make the port pin low.
- Again, provide some delay of 1000 microseconds.
- Repeat this for 8 times using for loop.
- In another loop, try to represent the binary equivalent of the first 255 number using LEDs.
- Now close the while loop and end main.
Program:
#include<reg51.h>
#define led P0
Unsigned char i=0;
Void delay (int);
Void delay (int d)
{
Unsigned char i=0;
For(;d>0;d--)
{
For(i=250;i>0;i--);
for(i=248;i>0;i--);
}
}
Void main()
{
While(1)//// led blink
{
Led=0xff;
Delay(1000);
Led=0x00;
Delay(1000);
++i;
If(i==7)
{
i=0;
Break;
}
}
While(1)//// binary equivalent representation of 1byte data
{
Led=i++;
If(i==256)
{
i=0;
Break;
}
Delay(500);
}
While(1);
}
Q3) What are the applications of LED?
A3)
- LEDs are widely used in many applications like in seven segments.
- They are used in dot matrix displays.
- They can be used for streetlights.
- They are used as indicators.
- They can be used in traffic lights.
- They are used in emergency lights
- They can used to make electronic designs.
Q4) What is LCD?
A4)
LCD is one of the most used display unit. 16x2 LCD means that there are two rows in which 16 characters can be displayed per line, and each character takes 5X7 matrix space on LCD.
We can divide it in five categories, Power Pins, contrast pin, Control Pins, Data pins and Backlight pins.
RS: RS is the register select pin. If set it to 1 we are sending some data to be displayed on LCD. If set it to 0 some command instruction like clear the screen (hex code 01).
RW: This is Read/write pin, If set it to 0, we write some data on LCD. And if set to 1 we read from LCD module. Generally, this is set to 0, because we do need to read data from LCD.
E: This pin is used to enable the module when a high to low pulse is given to it. That transition from HIGH to LOW makes the module ENABLE.
Some important command instructions are given below:
Q5) Explain the interfacing of LCD?
A5)
Figure 2. LCD Interfacing
LCD interfacing with 8051 microcontrollers is shown in the above figure.
The data pins (D0-D7) of LCD to the Port 2 (P2_0 – P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3) of microcontroller, respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1 (VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected to voltage supply and Ground.
Program:
/ Program for LCD Interfacing with 8051 Microcontroller (AT89S52)
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
Sbit rs = P3^2; //RS pin connected to pin 2 of port 3
Sbit rw = P3^3; // RW pin connected to pin 3 of port 3
Sbit e = P3^4; //E pin connected to pin 4 of port 3
Void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
Void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1
msdelay(10);
}
Void main()
{
unsigned char a[15]="CIRCUIT DIGEST"; //string of 14 characters with a null terminator.
int l=0;
lcd_init();
while(a[l] != '\0') // searching the null terminator in the sentence
{
lcd_data(a[l]);
l++;
msdelay(50);
}
}
Q6) Explain the applications of LCD?
A6)
- The LCDs are commonly used in all the digital wrist watches for displaying time.
- The LCD (liquid crystal display) is used in aircraft cockpit displays.
- It is used for displaying images used in digital cameras.
- It is used in instruments panel where all the lab instruments uses LCD screens for display.
- It is used as a display screen in calculators.
- The television is main applications of LCD.
- Mostly the computer monitor is made up of LCDs.
Q7) Explain Keyboard Interfacing with 8051?
A7)
Matrix keyboard is used for interfacing. It is designed with a rows and columns. These rows and columns are connected to the microcontroller through its ports of 8051.
Whenever a key is pressed, a row and a column get shorted through that pressed key and all the other keys are left open. The bit in the port goes high which indicates the microcontroller that the key is pressed. By this high on the bit key in the corresponding column is identified.
Once we are sure that one of key in the keyboard is pressed next our aim is to identify that key. To do this we firstly check particular row and then check the corresponding column on the keyboard.
In order to check the row of the pressed key, one of the rows is made high by making one of bit in the output port of 8051 high. This is done until the row is found out. Once we get the row the next task is to detect the column of the pressed key. The column is detected by contents in the input ports with the help of a counter. The content of the input port is rotated with carry until the carry bit is set.
The contents of the counter is compared and displayed in the display. This display is seven segment display and a BCD to seven segment decoder IC 7447.
The BCD equivalent to the number of counter is sent through output part of 8051 displays the number of pressed key.
Figure. Keyboard interfacing with 8051
Q8) Write a program for keyboard interfacing?
A8)
Start: mov a,#00h
mov p1,a ;making all rows of port p1 zero
mov a,#0fh
mov p1,a ;making all rows of port p1 high
press: mov a,p2
jz press ;check until any key is pressed
After making sure that any key is pressed
mov a,#01h ;make one row high at a time
mov r4,a
mov r3,#00h ;initiating counter
next: mov a,r4
mov p1,a ;making one row high at a time
mov a,p2 ;taking input from port A
jnz colscan ;after getting the row jump to check
column
mov a,r4
rl a ;rotate left to check next row
mov r4,a
mov a,r3
add a,#08h ;increment counter by 08 count
mov r3,a
sjmp next ;jump to check next row
After identifying the row to check the column following steps are followed
colscan: mov r5,#00h
in: rrc a ;rotate right with carry until get the carry
jc out ;jump on getting carry
inc r3 ;increment one count
jmp in
out: mov a,r3
da a ;decimal adjust the contents of counter
before display
mov p2,a
jmp start ;repeat for check next key.
Q9) Explain stepper motor interfacing?
A9)
Figure. Interfacing Stepper Motor with 8051
Program:
#include<reg51.h>
Sbit LED_pin = P2^0; //set the LED pin as P2.0
Void delay(int ms){
unsigned int i, j;
for(i = 0; i<ms; i++){ // Outer for loop for given milliseconds value
for(j = 0; j< 1275; j++){ //execute in each milliseconds
;
}
}
}
Void main(){
int rot_angle[] = {0x0C,0x06,0x03,0x09};
int i;
while(1){ //infinite loop for LED blinking
for(i = 0; i<4; i++){
P0 = rot_angle[i];
delay(100);
}
}
}
Q10) Explain DC motor Interfacing?
A10)
DC motor converts electrical energy in the form of Direct Current into mechanical energy.
Figure 6. DC Motor Interfacing
- As shown in the above figure we have connected two toggle switches on P1.0 and P1.1 pin of the AT89S52 microcontroller to change the speed of the DC motor by 10%.
- One toggle switch at pin P1.2 controls the motor rotating direction.
- P1.6 and P1.7 pins are used as output direction control pins. It provides control to motor1 input pins of L293D motor driver which rotate the motor clockwise and anticlockwise by changing their terminal polarity.
- And Speed of the DC Motor is varied through PWM Out pin P2.0.
- The timer of AT89S52 is used to generate PWM.
Program
#include <reg52.h>
#include <intrins.h>
/* Define value to be loaded in timer for PWM period of 20 milli second */
#define PWM_Period 0xB7FE
Sbit PWM_Out_Pin = P2^0;/* PWM Out Pin for speed control */
Sbit Speed_Inc = P1^0;/* Speed Increment switch pin */
Sbit Speed_Dec = P1^1;/* Speed Decrement switch pin */
Sbit Change_Dir= P1^2;/* Rotation direction change switch pin */
Sbit M1_Pin1= P1^6;/* Motor Pin 1 */
Sbit M1_Pin2= P1^7;/* Motor Pin 2 */
Unsigned int ON_Period, OFF_Period, DutyCycle, Speed;
/* Function to provide delay of 1ms at 11.0592 MHz */
Void delay(unsigned int count)
{
int i,j;
for(i=0; i<count; i++)
For(j=0; j<112; j++);
}
Void Timer_init()
{
TMOD = 0x01;/* Timer0 mode1 */
TH0 = (PWM_Period >> 8);/* 20ms timer value */
TL0 = PWM_Period;
TR0 = 1;/* Start timer0 */
}
/* Timer0 interrupt service routine (ISR) */
Void Timer0_ISR() interrupt 1
{
PWM_Out_Pin = !PWM_Out_Pin;
If(PWM_Out_Pin)
{
TH0 = (ON_Period >> 8);
TL0 = ON_Period;
}
Else
{
TH0 = (OFF_Period >> 8);
TL0 = OFF_Period;
}
}
/* Calculate ON & OFF period from PWM period & duty cycle */
Void Set_DutyCycle_To(float duty_cycle)
{
Float period = 65535 - PWM_Period;
ON_Period = ((period/100.0) * duty_cycle);
OFF_Period = (period - ON_Period);
ON_Period = 65535 - ON_Period;
OFF_Period = 65535 - OFF_Period;
}
/* Initially Motor Speed & Duty cycle is zero and in either direction */
Void Motor_Init()
{
Speed = 0;
M1_Pin1 = 1;
M1_Pin2 = 0;
Set_DutyCycle_To(Speed);
}
Int main()
{
EA = 1;/* Enable global interrupt */
ET0 = 1; /* Enable timer0 interrupt */
Timer_init();
Motor_Init();
while(1)
{
/* Increment Duty cycle i.e. speed by 10% for Speed_Inc Switch */
If(Speed_Inc == 1)
{
If(Speed < 100)
Speed += 10;
Set_DutyCycle_To(Speed);
While(Speed_Inc == 1);
Delay(200);
}
/* Decrement Duty cycle i.e. speed by 10% for Speed_Dec Switch */
If(Speed_Dec == 1)
{
If(Speed > 0)
Speed -= 10;
Set_DutyCycle_To(Speed);
While(Speed_Dec == 1);
Delay(200);
}
/* Change rotation direction for Change_Dir Switch */
If(Change_Dir == 1)
{
M1_Pin1 = !M1_Pin1;
M1_Pin2 = !M1_Pin2;
While(Change_Dir == 1);
Delay(200);
}
}
}
Q11) Explain sensor interfacing?
A11)
DHT11 is a single wire digital humidity and temperature sensor, which provides humidity and temperature values serially.
Figure . Sensor Interfacing
- The above circuit diagram shows the interfacing of 8051 with the DHT11 sensor.
- In that, a DHT11 sensor is connected to P2.1(Pin No 22) of the microcontroller.
Programming Steps
- First, initialize the LCD16x2_4bit.h library.
- Define pin no. To interface DHT11 sensor, in our program we define P2.1 (Pin no.22)
- Send the start pulse to the DHT11 sensor by making low to high on the data pin.
- Receive the response pulse from the DHT11 sensor.
- After receiving the response, receive 40-bit data serially from the DHT11 sensor.
- Display this received data on LCD16x2 along with error indication.
#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"
Sbit DHT11=P2^1;/* Connect DHT11 output Pin to P2.1 Pin */
Int I_RH,D_RH,I_Temp,D_Temp,CheckSum;
Void timer_delay20ms()/* Timer0 delay function */
{
TMOD = 0x01;
TH0 = 0xB8;/* Load higher 8-bit in TH0 */
TL0 = 0x0C;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void timer_delay30us()/* Timer0 delay function */
{
TMOD = 0x01;/* Timer0 mode1 (16-bit timer mode) */
TH0 = 0xFF;/* Load higher 8-bit in TH0 */
TL0 = 0xF1;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void Request()/* Microcontroller send request */
{
DHT11 = 0;/* set to low pin */
Timer_delay20ms();/* wait for 20ms */
DHT11 = 1;/* set to high pin */
}
Void Response()/* Receive response from DHT11 */
{
While(DHT11==1);
While(DHT11==0);
While(DHT11==1);
}
Int Receive_data()/* Receive data */
{
Int q,c=0;
For (q=0; q<8; q++)
{
While(DHT11==0);/* check received bit 0 or 1 */
Timer_delay30us();
If(DHT11 == 1)/* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */
Else/* otherwise its logic LOW */
c = (c<<1);
While(DHT11==1);
}
Return c;
}
Void main()
{
Unsigned char dat[20];
LCD_Init();/* initialize LCD */
While(1)
{
Request();/* send start pulse */
Response();/* receive response */
I_RH=Receive_data();/* store first eight bit in I_RH */
D_RH=Receive_data();/* store next eight bit in D_RH */
I_Temp=Receive_data();/* store next eight bit in I_Temp */
D_Temp=Receive_data();/* store next eight bit in D_Temp */
CheckSum=Receive_data();/* store next eight bit in CheckSum */
If ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
LCD_String_xy(0,0,"Error");
}
Else
{
Sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
Sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
Memset(dat,0,20);
Sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
Delay(100);
}
}
Unit - 6
Applications
Q1) What are LEDs?
A1)
Light Emitting Diodes are the semi- conductor light sources. Commonly used LEDs will have a cut-off voltage of 1.7V and current of 10mA. When an LED is applied with its required voltage and current it glows with full intensity.
The Light Emitting Diode works similar to normal PN- diode but it emits energy in the form of light. The color of light depends on the band gap of the semiconductor.
Q2) Explain the interfacing of LEDs?
A2)
Figure 1. LED
LED is connected to AT89C51 microcontroller with the help of a current limiting resistor. The value of this resistor is calculated using the following formula.
R= (V-1.7)/10mA, where V is the input voltage.
The maximum output voltage of microcontrollers is 5V. Thus, the value of resistor calculated for this is 330 Ohms. The resistor can be connected to either the cathode or the anode of the LED.
- Initially, burn the code into the microcontroller.
- Connect the LEDs to the Port0 of the microcontroller.
- Switch on the circuit.
- The LEDs start glowing.
- Now, switch off the circuit.
Algorithm
- Initially, include the “reg51.h” header file in your code.
- Now write a function to produce delay using for loop.
- Start the main function.
- Inside the while loop write the condition to port pin for making it logic high or low.
- Initially, make it high for some delay of 1000 microseconds.
- Now make the port pin low.
- Again, provide some delay of 1000 microseconds.
- Repeat this for 8 times using for loop.
- In another loop, try to represent the binary equivalent of the first 255 number using LEDs.
- Now close the while loop and end main.
Program:
#include<reg51.h>
#define led P0
Unsigned char i=0;
Void delay (int);
Void delay (int d)
{
Unsigned char i=0;
For(;d>0;d--)
{
For(i=250;i>0;i--);
for(i=248;i>0;i--);
}
}
Void main()
{
While(1)//// led blink
{
Led=0xff;
Delay(1000);
Led=0x00;
Delay(1000);
++i;
If(i==7)
{
i=0;
Break;
}
}
While(1)//// binary equivalent representation of 1byte data
{
Led=i++;
If(i==256)
{
i=0;
Break;
}
Delay(500);
}
While(1);
}
Q3) What are the applications of LED?
A3)
- LEDs are widely used in many applications like in seven segments.
- They are used in dot matrix displays.
- They can be used for streetlights.
- They are used as indicators.
- They can be used in traffic lights.
- They are used in emergency lights
- They can used to make electronic designs.
Q4) What is LCD?
A4)
LCD is one of the most used display unit. 16x2 LCD means that there are two rows in which 16 characters can be displayed per line, and each character takes 5X7 matrix space on LCD.
We can divide it in five categories, Power Pins, contrast pin, Control Pins, Data pins and Backlight pins.
RS: RS is the register select pin. If set it to 1 we are sending some data to be displayed on LCD. If set it to 0 some command instruction like clear the screen (hex code 01).
RW: This is Read/write pin, If set it to 0, we write some data on LCD. And if set to 1 we read from LCD module. Generally, this is set to 0, because we do need to read data from LCD.
E: This pin is used to enable the module when a high to low pulse is given to it. That transition from HIGH to LOW makes the module ENABLE.
Some important command instructions are given below:
Q5) Explain the interfacing of LCD?
A5)
Figure 2. LCD Interfacing
LCD interfacing with 8051 microcontrollers is shown in the above figure.
The data pins (D0-D7) of LCD to the Port 2 (P2_0 – P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3) of microcontroller, respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1 (VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected to voltage supply and Ground.
Program:
/ Program for LCD Interfacing with 8051 Microcontroller (AT89S52)
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
Sbit rs = P3^2; //RS pin connected to pin 2 of port 3
Sbit rw = P3^3; // RW pin connected to pin 3 of port 3
Sbit e = P3^4; //E pin connected to pin 4 of port 3
Void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
Void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1
msdelay(10);
}
Void main()
{
unsigned char a[15]="CIRCUIT DIGEST"; //string of 14 characters with a null terminator.
int l=0;
lcd_init();
while(a[l] != '\0') // searching the null terminator in the sentence
{
lcd_data(a[l]);
l++;
msdelay(50);
}
}
Q6) Explain the applications of LCD?
A6)
- The LCDs are commonly used in all the digital wrist watches for displaying time.
- The LCD (liquid crystal display) is used in aircraft cockpit displays.
- It is used for displaying images used in digital cameras.
- It is used in instruments panel where all the lab instruments uses LCD screens for display.
- It is used as a display screen in calculators.
- The television is main applications of LCD.
- Mostly the computer monitor is made up of LCDs.
Q7) Explain Keyboard Interfacing with 8051?
A7)
Matrix keyboard is used for interfacing. It is designed with a rows and columns. These rows and columns are connected to the microcontroller through its ports of 8051.
Whenever a key is pressed, a row and a column get shorted through that pressed key and all the other keys are left open. The bit in the port goes high which indicates the microcontroller that the key is pressed. By this high on the bit key in the corresponding column is identified.
Once we are sure that one of key in the keyboard is pressed next our aim is to identify that key. To do this we firstly check particular row and then check the corresponding column on the keyboard.
In order to check the row of the pressed key, one of the rows is made high by making one of bit in the output port of 8051 high. This is done until the row is found out. Once we get the row the next task is to detect the column of the pressed key. The column is detected by contents in the input ports with the help of a counter. The content of the input port is rotated with carry until the carry bit is set.
The contents of the counter is compared and displayed in the display. This display is seven segment display and a BCD to seven segment decoder IC 7447.
The BCD equivalent to the number of counter is sent through output part of 8051 displays the number of pressed key.
Figure. Keyboard interfacing with 8051
Q8) Write a program for keyboard interfacing?
A8)
Start: mov a,#00h
mov p1,a ;making all rows of port p1 zero
mov a,#0fh
mov p1,a ;making all rows of port p1 high
press: mov a,p2
jz press ;check until any key is pressed
After making sure that any key is pressed
mov a,#01h ;make one row high at a time
mov r4,a
mov r3,#00h ;initiating counter
next: mov a,r4
mov p1,a ;making one row high at a time
mov a,p2 ;taking input from port A
jnz colscan ;after getting the row jump to check
column
mov a,r4
rl a ;rotate left to check next row
mov r4,a
mov a,r3
add a,#08h ;increment counter by 08 count
mov r3,a
sjmp next ;jump to check next row
After identifying the row to check the column following steps are followed
colscan: mov r5,#00h
in: rrc a ;rotate right with carry until get the carry
jc out ;jump on getting carry
inc r3 ;increment one count
jmp in
out: mov a,r3
da a ;decimal adjust the contents of counter
before display
mov p2,a
jmp start ;repeat for check next key.
Q9) Explain stepper motor interfacing?
A9)
Figure. Interfacing Stepper Motor with 8051
Program:
#include<reg51.h>
Sbit LED_pin = P2^0; //set the LED pin as P2.0
Void delay(int ms){
unsigned int i, j;
for(i = 0; i<ms; i++){ // Outer for loop for given milliseconds value
for(j = 0; j< 1275; j++){ //execute in each milliseconds
;
}
}
}
Void main(){
int rot_angle[] = {0x0C,0x06,0x03,0x09};
int i;
while(1){ //infinite loop for LED blinking
for(i = 0; i<4; i++){
P0 = rot_angle[i];
delay(100);
}
}
}
Q10) Explain DC motor Interfacing?
A10)
DC motor converts electrical energy in the form of Direct Current into mechanical energy.
Figure 6. DC Motor Interfacing
- As shown in the above figure we have connected two toggle switches on P1.0 and P1.1 pin of the AT89S52 microcontroller to change the speed of the DC motor by 10%.
- One toggle switch at pin P1.2 controls the motor rotating direction.
- P1.6 and P1.7 pins are used as output direction control pins. It provides control to motor1 input pins of L293D motor driver which rotate the motor clockwise and anticlockwise by changing their terminal polarity.
- And Speed of the DC Motor is varied through PWM Out pin P2.0.
- The timer of AT89S52 is used to generate PWM.
Program
#include <reg52.h>
#include <intrins.h>
/* Define value to be loaded in timer for PWM period of 20 milli second */
#define PWM_Period 0xB7FE
Sbit PWM_Out_Pin = P2^0;/* PWM Out Pin for speed control */
Sbit Speed_Inc = P1^0;/* Speed Increment switch pin */
Sbit Speed_Dec = P1^1;/* Speed Decrement switch pin */
Sbit Change_Dir= P1^2;/* Rotation direction change switch pin */
Sbit M1_Pin1= P1^6;/* Motor Pin 1 */
Sbit M1_Pin2= P1^7;/* Motor Pin 2 */
Unsigned int ON_Period, OFF_Period, DutyCycle, Speed;
/* Function to provide delay of 1ms at 11.0592 MHz */
Void delay(unsigned int count)
{
int i,j;
for(i=0; i<count; i++)
For(j=0; j<112; j++);
}
Void Timer_init()
{
TMOD = 0x01;/* Timer0 mode1 */
TH0 = (PWM_Period >> 8);/* 20ms timer value */
TL0 = PWM_Period;
TR0 = 1;/* Start timer0 */
}
/* Timer0 interrupt service routine (ISR) */
Void Timer0_ISR() interrupt 1
{
PWM_Out_Pin = !PWM_Out_Pin;
If(PWM_Out_Pin)
{
TH0 = (ON_Period >> 8);
TL0 = ON_Period;
}
Else
{
TH0 = (OFF_Period >> 8);
TL0 = OFF_Period;
}
}
/* Calculate ON & OFF period from PWM period & duty cycle */
Void Set_DutyCycle_To(float duty_cycle)
{
Float period = 65535 - PWM_Period;
ON_Period = ((period/100.0) * duty_cycle);
OFF_Period = (period - ON_Period);
ON_Period = 65535 - ON_Period;
OFF_Period = 65535 - OFF_Period;
}
/* Initially Motor Speed & Duty cycle is zero and in either direction */
Void Motor_Init()
{
Speed = 0;
M1_Pin1 = 1;
M1_Pin2 = 0;
Set_DutyCycle_To(Speed);
}
Int main()
{
EA = 1;/* Enable global interrupt */
ET0 = 1; /* Enable timer0 interrupt */
Timer_init();
Motor_Init();
while(1)
{
/* Increment Duty cycle i.e. speed by 10% for Speed_Inc Switch */
If(Speed_Inc == 1)
{
If(Speed < 100)
Speed += 10;
Set_DutyCycle_To(Speed);
While(Speed_Inc == 1);
Delay(200);
}
/* Decrement Duty cycle i.e. speed by 10% for Speed_Dec Switch */
If(Speed_Dec == 1)
{
If(Speed > 0)
Speed -= 10;
Set_DutyCycle_To(Speed);
While(Speed_Dec == 1);
Delay(200);
}
/* Change rotation direction for Change_Dir Switch */
If(Change_Dir == 1)
{
M1_Pin1 = !M1_Pin1;
M1_Pin2 = !M1_Pin2;
While(Change_Dir == 1);
Delay(200);
}
}
}
Q11) Explain sensor interfacing?
A11)
DHT11 is a single wire digital humidity and temperature sensor, which provides humidity and temperature values serially.
Figure . Sensor Interfacing
- The above circuit diagram shows the interfacing of 8051 with the DHT11 sensor.
- In that, a DHT11 sensor is connected to P2.1(Pin No 22) of the microcontroller.
Programming Steps
- First, initialize the LCD16x2_4bit.h library.
- Define pin no. To interface DHT11 sensor, in our program we define P2.1 (Pin no.22)
- Send the start pulse to the DHT11 sensor by making low to high on the data pin.
- Receive the response pulse from the DHT11 sensor.
- After receiving the response, receive 40-bit data serially from the DHT11 sensor.
- Display this received data on LCD16x2 along with error indication.
#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"
Sbit DHT11=P2^1;/* Connect DHT11 output Pin to P2.1 Pin */
Int I_RH,D_RH,I_Temp,D_Temp,CheckSum;
Void timer_delay20ms()/* Timer0 delay function */
{
TMOD = 0x01;
TH0 = 0xB8;/* Load higher 8-bit in TH0 */
TL0 = 0x0C;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void timer_delay30us()/* Timer0 delay function */
{
TMOD = 0x01;/* Timer0 mode1 (16-bit timer mode) */
TH0 = 0xFF;/* Load higher 8-bit in TH0 */
TL0 = 0xF1;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void Request()/* Microcontroller send request */
{
DHT11 = 0;/* set to low pin */
Timer_delay20ms();/* wait for 20ms */
DHT11 = 1;/* set to high pin */
}
Void Response()/* Receive response from DHT11 */
{
While(DHT11==1);
While(DHT11==0);
While(DHT11==1);
}
Int Receive_data()/* Receive data */
{
Int q,c=0;
For (q=0; q<8; q++)
{
While(DHT11==0);/* check received bit 0 or 1 */
Timer_delay30us();
If(DHT11 == 1)/* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */
Else/* otherwise its logic LOW */
c = (c<<1);
While(DHT11==1);
}
Return c;
}
Void main()
{
Unsigned char dat[20];
LCD_Init();/* initialize LCD */
While(1)
{
Request();/* send start pulse */
Response();/* receive response */
I_RH=Receive_data();/* store first eight bit in I_RH */
D_RH=Receive_data();/* store next eight bit in D_RH */
I_Temp=Receive_data();/* store next eight bit in I_Temp */
D_Temp=Receive_data();/* store next eight bit in D_Temp */
CheckSum=Receive_data();/* store next eight bit in CheckSum */
If ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
LCD_String_xy(0,0,"Error");
}
Else
{
Sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
Sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
Memset(dat,0,20);
Sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
Delay(100);
}
}
Unit - 6
Applications
Q1) What are LEDs?
A1)
Light Emitting Diodes are the semi- conductor light sources. Commonly used LEDs will have a cut-off voltage of 1.7V and current of 10mA. When an LED is applied with its required voltage and current it glows with full intensity.
The Light Emitting Diode works similar to normal PN- diode but it emits energy in the form of light. The color of light depends on the band gap of the semiconductor.
Q2) Explain the interfacing of LEDs?
A2)
Figure 1. LED
LED is connected to AT89C51 microcontroller with the help of a current limiting resistor. The value of this resistor is calculated using the following formula.
R= (V-1.7)/10mA, where V is the input voltage.
The maximum output voltage of microcontrollers is 5V. Thus, the value of resistor calculated for this is 330 Ohms. The resistor can be connected to either the cathode or the anode of the LED.
- Initially, burn the code into the microcontroller.
- Connect the LEDs to the Port0 of the microcontroller.
- Switch on the circuit.
- The LEDs start glowing.
- Now, switch off the circuit.
Algorithm
- Initially, include the “reg51.h” header file in your code.
- Now write a function to produce delay using for loop.
- Start the main function.
- Inside the while loop write the condition to port pin for making it logic high or low.
- Initially, make it high for some delay of 1000 microseconds.
- Now make the port pin low.
- Again, provide some delay of 1000 microseconds.
- Repeat this for 8 times using for loop.
- In another loop, try to represent the binary equivalent of the first 255 number using LEDs.
- Now close the while loop and end main.
Program:
#include<reg51.h>
#define led P0
Unsigned char i=0;
Void delay (int);
Void delay (int d)
{
Unsigned char i=0;
For(;d>0;d--)
{
For(i=250;i>0;i--);
for(i=248;i>0;i--);
}
}
Void main()
{
While(1)//// led blink
{
Led=0xff;
Delay(1000);
Led=0x00;
Delay(1000);
++i;
If(i==7)
{
i=0;
Break;
}
}
While(1)//// binary equivalent representation of 1byte data
{
Led=i++;
If(i==256)
{
i=0;
Break;
}
Delay(500);
}
While(1);
}
Q3) What are the applications of LED?
A3)
- LEDs are widely used in many applications like in seven segments.
- They are used in dot matrix displays.
- They can be used for streetlights.
- They are used as indicators.
- They can be used in traffic lights.
- They are used in emergency lights
- They can used to make electronic designs.
Q4) What is LCD?
A4)
LCD is one of the most used display unit. 16x2 LCD means that there are two rows in which 16 characters can be displayed per line, and each character takes 5X7 matrix space on LCD.
We can divide it in five categories, Power Pins, contrast pin, Control Pins, Data pins and Backlight pins.
RS: RS is the register select pin. If set it to 1 we are sending some data to be displayed on LCD. If set it to 0 some command instruction like clear the screen (hex code 01).
RW: This is Read/write pin, If set it to 0, we write some data on LCD. And if set to 1 we read from LCD module. Generally, this is set to 0, because we do need to read data from LCD.
E: This pin is used to enable the module when a high to low pulse is given to it. That transition from HIGH to LOW makes the module ENABLE.
Some important command instructions are given below:
Q5) Explain the interfacing of LCD?
A5)
Figure 2. LCD Interfacing
LCD interfacing with 8051 microcontrollers is shown in the above figure.
The data pins (D0-D7) of LCD to the Port 2 (P2_0 – P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3) of microcontroller, respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1 (VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected to voltage supply and Ground.
Program:
/ Program for LCD Interfacing with 8051 Microcontroller (AT89S52)
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
Sbit rs = P3^2; //RS pin connected to pin 2 of port 3
Sbit rw = P3^3; // RW pin connected to pin 3 of port 3
Sbit e = P3^4; //E pin connected to pin 4 of port 3
Void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
Void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1
msdelay(10);
}
Void main()
{
unsigned char a[15]="CIRCUIT DIGEST"; //string of 14 characters with a null terminator.
int l=0;
lcd_init();
while(a[l] != '\0') // searching the null terminator in the sentence
{
lcd_data(a[l]);
l++;
msdelay(50);
}
}
Q6) Explain the applications of LCD?
A6)
- The LCDs are commonly used in all the digital wrist watches for displaying time.
- The LCD (liquid crystal display) is used in aircraft cockpit displays.
- It is used for displaying images used in digital cameras.
- It is used in instruments panel where all the lab instruments uses LCD screens for display.
- It is used as a display screen in calculators.
- The television is main applications of LCD.
- Mostly the computer monitor is made up of LCDs.
Q7) Explain Keyboard Interfacing with 8051?
A7)
Matrix keyboard is used for interfacing. It is designed with a rows and columns. These rows and columns are connected to the microcontroller through its ports of 8051.
Whenever a key is pressed, a row and a column get shorted through that pressed key and all the other keys are left open. The bit in the port goes high which indicates the microcontroller that the key is pressed. By this high on the bit key in the corresponding column is identified.
Once we are sure that one of key in the keyboard is pressed next our aim is to identify that key. To do this we firstly check particular row and then check the corresponding column on the keyboard.
In order to check the row of the pressed key, one of the rows is made high by making one of bit in the output port of 8051 high. This is done until the row is found out. Once we get the row the next task is to detect the column of the pressed key. The column is detected by contents in the input ports with the help of a counter. The content of the input port is rotated with carry until the carry bit is set.
The contents of the counter is compared and displayed in the display. This display is seven segment display and a BCD to seven segment decoder IC 7447.
The BCD equivalent to the number of counter is sent through output part of 8051 displays the number of pressed key.
Figure. Keyboard interfacing with 8051
Q8) Write a program for keyboard interfacing?
A8)
Start: mov a,#00h
mov p1,a ;making all rows of port p1 zero
mov a,#0fh
mov p1,a ;making all rows of port p1 high
press: mov a,p2
jz press ;check until any key is pressed
After making sure that any key is pressed
mov a,#01h ;make one row high at a time
mov r4,a
mov r3,#00h ;initiating counter
next: mov a,r4
mov p1,a ;making one row high at a time
mov a,p2 ;taking input from port A
jnz colscan ;after getting the row jump to check
column
mov a,r4
rl a ;rotate left to check next row
mov r4,a
mov a,r3
add a,#08h ;increment counter by 08 count
mov r3,a
sjmp next ;jump to check next row
After identifying the row to check the column following steps are followed
colscan: mov r5,#00h
in: rrc a ;rotate right with carry until get the carry
jc out ;jump on getting carry
inc r3 ;increment one count
jmp in
out: mov a,r3
da a ;decimal adjust the contents of counter
before display
mov p2,a
jmp start ;repeat for check next key.
Q9) Explain stepper motor interfacing?
A9)
Figure. Interfacing Stepper Motor with 8051
Program:
#include<reg51.h>
Sbit LED_pin = P2^0; //set the LED pin as P2.0
Void delay(int ms){
unsigned int i, j;
for(i = 0; i<ms; i++){ // Outer for loop for given milliseconds value
for(j = 0; j< 1275; j++){ //execute in each milliseconds
;
}
}
}
Void main(){
int rot_angle[] = {0x0C,0x06,0x03,0x09};
int i;
while(1){ //infinite loop for LED blinking
for(i = 0; i<4; i++){
P0 = rot_angle[i];
delay(100);
}
}
}
Q10) Explain DC motor Interfacing?
A10)
DC motor converts electrical energy in the form of Direct Current into mechanical energy.
Figure 6. DC Motor Interfacing
- As shown in the above figure we have connected two toggle switches on P1.0 and P1.1 pin of the AT89S52 microcontroller to change the speed of the DC motor by 10%.
- One toggle switch at pin P1.2 controls the motor rotating direction.
- P1.6 and P1.7 pins are used as output direction control pins. It provides control to motor1 input pins of L293D motor driver which rotate the motor clockwise and anticlockwise by changing their terminal polarity.
- And Speed of the DC Motor is varied through PWM Out pin P2.0.
- The timer of AT89S52 is used to generate PWM.
Program
#include <reg52.h>
#include <intrins.h>
/* Define value to be loaded in timer for PWM period of 20 milli second */
#define PWM_Period 0xB7FE
Sbit PWM_Out_Pin = P2^0;/* PWM Out Pin for speed control */
Sbit Speed_Inc = P1^0;/* Speed Increment switch pin */
Sbit Speed_Dec = P1^1;/* Speed Decrement switch pin */
Sbit Change_Dir= P1^2;/* Rotation direction change switch pin */
Sbit M1_Pin1= P1^6;/* Motor Pin 1 */
Sbit M1_Pin2= P1^7;/* Motor Pin 2 */
Unsigned int ON_Period, OFF_Period, DutyCycle, Speed;
/* Function to provide delay of 1ms at 11.0592 MHz */
Void delay(unsigned int count)
{
int i,j;
for(i=0; i<count; i++)
For(j=0; j<112; j++);
}
Void Timer_init()
{
TMOD = 0x01;/* Timer0 mode1 */
TH0 = (PWM_Period >> 8);/* 20ms timer value */
TL0 = PWM_Period;
TR0 = 1;/* Start timer0 */
}
/* Timer0 interrupt service routine (ISR) */
Void Timer0_ISR() interrupt 1
{
PWM_Out_Pin = !PWM_Out_Pin;
If(PWM_Out_Pin)
{
TH0 = (ON_Period >> 8);
TL0 = ON_Period;
}
Else
{
TH0 = (OFF_Period >> 8);
TL0 = OFF_Period;
}
}
/* Calculate ON & OFF period from PWM period & duty cycle */
Void Set_DutyCycle_To(float duty_cycle)
{
Float period = 65535 - PWM_Period;
ON_Period = ((period/100.0) * duty_cycle);
OFF_Period = (period - ON_Period);
ON_Period = 65535 - ON_Period;
OFF_Period = 65535 - OFF_Period;
}
/* Initially Motor Speed & Duty cycle is zero and in either direction */
Void Motor_Init()
{
Speed = 0;
M1_Pin1 = 1;
M1_Pin2 = 0;
Set_DutyCycle_To(Speed);
}
Int main()
{
EA = 1;/* Enable global interrupt */
ET0 = 1; /* Enable timer0 interrupt */
Timer_init();
Motor_Init();
while(1)
{
/* Increment Duty cycle i.e. speed by 10% for Speed_Inc Switch */
If(Speed_Inc == 1)
{
If(Speed < 100)
Speed += 10;
Set_DutyCycle_To(Speed);
While(Speed_Inc == 1);
Delay(200);
}
/* Decrement Duty cycle i.e. speed by 10% for Speed_Dec Switch */
If(Speed_Dec == 1)
{
If(Speed > 0)
Speed -= 10;
Set_DutyCycle_To(Speed);
While(Speed_Dec == 1);
Delay(200);
}
/* Change rotation direction for Change_Dir Switch */
If(Change_Dir == 1)
{
M1_Pin1 = !M1_Pin1;
M1_Pin2 = !M1_Pin2;
While(Change_Dir == 1);
Delay(200);
}
}
}
Q11) Explain sensor interfacing?
A11)
DHT11 is a single wire digital humidity and temperature sensor, which provides humidity and temperature values serially.
Figure . Sensor Interfacing
- The above circuit diagram shows the interfacing of 8051 with the DHT11 sensor.
- In that, a DHT11 sensor is connected to P2.1(Pin No 22) of the microcontroller.
Programming Steps
- First, initialize the LCD16x2_4bit.h library.
- Define pin no. To interface DHT11 sensor, in our program we define P2.1 (Pin no.22)
- Send the start pulse to the DHT11 sensor by making low to high on the data pin.
- Receive the response pulse from the DHT11 sensor.
- After receiving the response, receive 40-bit data serially from the DHT11 sensor.
- Display this received data on LCD16x2 along with error indication.
#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"
Sbit DHT11=P2^1;/* Connect DHT11 output Pin to P2.1 Pin */
Int I_RH,D_RH,I_Temp,D_Temp,CheckSum;
Void timer_delay20ms()/* Timer0 delay function */
{
TMOD = 0x01;
TH0 = 0xB8;/* Load higher 8-bit in TH0 */
TL0 = 0x0C;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void timer_delay30us()/* Timer0 delay function */
{
TMOD = 0x01;/* Timer0 mode1 (16-bit timer mode) */
TH0 = 0xFF;/* Load higher 8-bit in TH0 */
TL0 = 0xF1;/* Load lower 8-bit in TL0 */
TR0 = 1;/* Start timer0 */
While(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;/* Stop timer0 */
TF0 = 0;/* Clear timer0 flag */
}
Void Request()/* Microcontroller send request */
{
DHT11 = 0;/* set to low pin */
Timer_delay20ms();/* wait for 20ms */
DHT11 = 1;/* set to high pin */
}
Void Response()/* Receive response from DHT11 */
{
While(DHT11==1);
While(DHT11==0);
While(DHT11==1);
}
Int Receive_data()/* Receive data */
{
Int q,c=0;
For (q=0; q<8; q++)
{
While(DHT11==0);/* check received bit 0 or 1 */
Timer_delay30us();
If(DHT11 == 1)/* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */
Else/* otherwise its logic LOW */
c = (c<<1);
While(DHT11==1);
}
Return c;
}
Void main()
{
Unsigned char dat[20];
LCD_Init();/* initialize LCD */
While(1)
{
Request();/* send start pulse */
Response();/* receive response */
I_RH=Receive_data();/* store first eight bit in I_RH */
D_RH=Receive_data();/* store next eight bit in D_RH */
I_Temp=Receive_data();/* store next eight bit in I_Temp */
D_Temp=Receive_data();/* store next eight bit in D_Temp */
CheckSum=Receive_data();/* store next eight bit in CheckSum */
If ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
{
LCD_String_xy(0,0,"Error");
}
Else
{
Sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
Sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
Memset(dat,0,20);
Sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
Delay(100);
}
}