In: Computer Science
Q2. Write an 8051 C program that uses 3 temperature
sensors (TS1, TS2, TS3), 3 LEDs (GreenLED, BlueLED and
RedLED) and 1 buzzer (BUZZ). Monitor the temperature sensors such
that whenever any of the sensor goes high, the buzzer will produce
a sound and at the same time, its corresponding LED blinks 3 times.
You may use any of the I/O Port Pins.
SOLUTION:
C PROGRAM
Here our task is to write a program which continously monitor the three sensor pins and blink the coressponding LED if any of it goes high ,
step1) we have to define 3 input pins for sensors and 4 output pins for Buzzer and leds
step2) check whether any of the sensor is high using if statement
step3) blink the corresponding led and turn on buzzer with help of delay function
This can be easily implemented in C as below
CODE
(Please read all comments for better understanding of the program)
I am also attaching the text version of the code in case you need to copy paste
#include<reg51.h> //calling reg51 header file from library
sbit TS1 = P2^1; //Defining sensor TS1 as pin1 at port 2
sbit TS2 = P2^2; //Defining sensor TS2 as pin2 at port 2
sbit TS3 = P2^3; //Defining sensor TS3 as pin3 at port 2
sbit LEDgreen = P3^1; //Defining LEDgreen as pin 1 port 3
sbit LEDblue = P3^2; //Defining LEDblue as pin 2 port 3
sbit LEDred = P3^3; //Defining LEDred as pin 1 port 3
sbit Buzzer = P3^4; //Defining Buzzer as pin 1 port 3
void Delay() //defining a delay function
{
int j;
inti;
for(i=0;i<100;i++) // by continous looping it will create a
small
// delay in the runninmg of 8051
{
for(j=0;j<100;j++)
{
}
int main ()
{
P3 = 0x00; //P3 port is used as output
P2 = 0xFF; //P2 port is used as input
while(1) //craeting a loop that will run as long as 8051 is powered up
{
if(TS1 == 0) // this condition will be true when sensor TS1 id
high
{
Buzzer=1; // turn on the Buzzer
for (int k=0;k<3;k++ ){ // loop which run 3 times
LEDgreen = 1 ; // turn on the green led
Delay(); //calling dely function to create a small delay
LEDgreen = 0 ; //turn on green led
}
Buzzer=0; //turn of buzzer
}
}
if(TS2 == 0) // this condition will be tru when sensor TS2 id
high
{
Buzzer=1; // turn on the Buzzer
for (int l=0;l<3;l++ ){ // loop which run 3 times
LEDblue = 1 ; // turn on the blue led
Delay(); //calling dely function to create a small delay
LEDblue = 0 ; //turn on blue led
}
Buzzer=0; //turn of buzzer
}
}
if(TS3 == 0) // this condition will be true when sensor TS3 id high
{
Buzzer=1; // turn on the Buzzer
for (int m=0;m<3;m++ ){ // loop which run 3 times
LEDred = 1 ; // turn on the red led
Delay(); //calling dely function to create a small delay
LEDred = 0 ; //turn on red led
}
Buzzer=0; //turn of buzzer
}
}
}
}