0% found this document useful (0 votes)
18 views23 pages

Gagandeep Singh, Assignment 3.doxc

The document contains 6 questions about programming LEDs, 7-segment displays, and LCD displays in C. For each question, it provides the code to control these devices with delays and shows sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

Gagandeep Singh, Assignment 3.doxc

The document contains 6 questions about programming LEDs, 7-segment displays, and LCD displays in C. For each question, it provides the code to control these devices with delays and shows sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Q1) Write a C program to switching ON and OFF individual LED’s

with software delay of 10ms.


Code:
#include<reg52.h> // special function register declarations
// for the intended 8051 derivative

sbit LED = P1^0;


sbit LED = P1^1;
sbit LED = P1^2;
sbit LED = P1^3;
sbit LED = P1^4;
sbit LED = P1^5;
sbit LED = P1^6;
sbit LED = P1^7;
// Defining LED pin

void Delay(void); // Function prototype declaration

void main (void)


{
while(1) // infinite loop
{
LED = 0; // LED ON
Delay();
LED = 1; // LED OFF
Delay();
}
}

void Delay(void)
{
int j;
int i;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}

Output:
Q2) Write a C program to blink individual LED’s and make the
Blinking led to move left to right and right to left continuously.
Code:
#include <REGX51.H>
sbit Led1=P1^0;
sbit Led2=P1^1;
sbit Led3=P1^2;
sbit Led4=P1^3;
sbit Led5=P1^4;
sbit Led6=P1^5;
sbit Led7=P1^6;
sbit Led8=P1^7;
void delay();
void main()
{
P1=0x00;
while(1){
Led1=~Led1;
delay();
Led2=~Led2;
delay();
Led3=~Led3;
delay();
Led4=~Led4;
delay();
Led5=~Led5;
delay();
Led6=~Led6;
delay();
Led7=~Led7;
delay();
Led8=~Led8;
delay();
Led8=~Led8;
delay();
Led7=~Led7;
delay();
Led6=~Led6;
delay();
Led5=~Led5;
delay();
Led4=~Led4;
delay();
Led3=~Led3;
delay();
Led2=~Led2;
delay();
Led1=~Led1;
delay();

}
}
void delay()
{
unsigned int i;
for (i=0;i<10000;i++){
}
}

Output:
Q3) Write a C program to display 00 to 99 on common Anode (CA)
type 7-seg displays. (Use the 2 digit)
Code:
#include <REGX51.H>
void delay();
void main(){
unsigned int x[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned int i,j;
P0=0xC0;
P2=0xC0;
while(1){
for(i=0;i<10;i++){
P0=x[i];
delay();
for(j=0;j<10;j++){
P2=x[j];
delay();
}
}
}
}
void delay(){
unsigned int i;
for (i=0;i<10000;i++){
}
}
Output:
Q4) Write a C program to count 000 to 999 (Use 3-digit multiplex CA
displays.
Code:

Output:
Q5) Write a C program to display “Embedded Lab” on 16x2 LCD.
Code:
#include <REGX51.H>
#define LCD_DATA P3
sbit EN=P2^2;
sbit RS=P2^0;

void delay();
void lcd(unsigned char m);
void lcd_data(unsigned char m);

void main()
{
lcd(0x38);
lcd(0x0E);
while(1){
lcd(0x80);
lcd_data('E');
delay();
lcd_data('M');
delay();
lcd_data('B');
delay();
lcd_data('E');
delay();
lcd_data('D');
delay();
lcd_data('D');
delay();
lcd_data('E');
delay();
lcd_data('D');
delay();
lcd_data(' ');
delay();
lcd_data('L');
delay();
lcd_data('A');
delay();
lcd_data('B');
}

}
void lcd_data(unsigned char m)
{
LCD_DATA=m;
RS=1;
EN=1;
delay();
EN=0;
}
void lcd(unsigned char m)
{
LCD_DATA=m;
RS=0;
EN=1;
delay();
EN=0;
}
void delay(){
unsigned int j;
for (j=0;j<3000;j++){
}
}

Output:
Q6) Write a C program to display digital clock on 16x2 LCD display.
(Hr: mm: ss)
Code:
#include <reg51.h>
int scan_key();
int scan_key1();
void display();
void start();
void tochar(unsigned int value);
void onemintdelay();
sbit rs=P3^5; //Register select (RS)
sbit en=P3^6; //Enable (EN) pin
sbit r0=P1^0;
sbit r1=P1^1;
sbit r2=P1^2;
sbit r3=P1^3;
sbit c0=P1^4;
sbit c1=P1^5;
sbit c2=P1^6;
sbit c3=P1^7;
char clock[]={"CLOCK!"},hour[]="HOUR 01 to 12",mint[]="MINT 01
to 60",set[]={"RESET 1"};
int count=0,H,M,S=0,hour1[1],mint1[1];

void delay(unsigned int time) //Time delay function


{
unsigned int i,j;
for(i=0;i< time;i++)
for(j=0;j< 5;j++);
}

void lcdcmd(unsigned char value) //Function for sending values to


the command register of LCD
{
P2=value; //sending commands on port 2 Means
to the lcd command register
P3=0x40;
delay(50);
en=0;
delay(50);
return;
}

void lcddata(unsigned char value) //Function for sending values to


the data register of LCD
{
P2=value; //sending data on port 2 Means to the
lcd data register
P3=0x60;
delay(50);
en=0;
delay(50);
return;
}
void lcdint(void)
{
P1=0xFF;
P2=0x00;
P3=0x00;
delay(15000);
lcddata(0x30);
delay(4500);
lcddata(0x30);
delay(300);
lcddata(0x30);
delay(650);
lcdcmd(0x38);
delay(50);
lcdcmd(0x0C);
delay(50);
lcdcmd(0x01);
delay(50);
lcdcmd(0x06);
delay(50);
lcdcmd(0x80);
delay(50);
}

void settime()
{
lcdcmd(0x01);
lcdcmd(0x80);
while(hour[count]!='\0'){
lcddata(hour[count]);
count++;
}
count=0;
lcdcmd(0xC8);
while(count!=2){
hour1[count]=scan_key();
count++;
}
H=(hour1[0]*10)+hour1[1];
count=0;
lcdcmd(0x01);
lcdcmd(0x80);
while(mint[count]!='\0'){
lcddata(mint[count]);
count++;
}
count=0;
lcdcmd(0xC8);
while(count!=2){
mint1[count]=scan_key();
count++;
}
M=(mint1[0]*10)+mint1[1];
count=0;
delay(10000);
lcdcmd(0x01);
lcdcmd(0x80);
}

void onemintdelay()
{
int count1=0,sec,check;
xyz:
S=2;
for(sec=0;sec<59;sec++){
count1=0;
while(count1!=500)
{
TMOD=0x01;
TH0=0xF8;
TL0=0xCC;
TR0=1;
while(!TF0);
TR0 = 0;
TF0 = 0;
count1++;
}
S++;
lcdcmd(0xCB);
tochar(S);

check=scan_key1();
if(check==1)
{
start();
goto xyz;
}
}
display();
}

void main()
{
lcdint();
start();
while(1){
onemintdelay();}
}
void start()
{
settime();
delay(100);
count=0;
lcdcmd(0x01);
lcdcmd(0x80);
while(set[count]!='\0'){
lcddata(set[count]);
count++;
}
count=0;
lcdcmd(0x89);
while(clock[count]!='\0'){
lcddata(clock[count]);
count++;
}
count=0;
lcdcmd(0xC4);
tochar(H);
lcddata(':');
tochar(M);
lcdcmd(0xCB);
tochar(S);

void display(){
count=0;
lcdcmd(0x01);
lcdcmd(0x80);
while(set[count]!='\0'){
lcddata(set[count]);
count++;
}
count=0;
lcdcmd(0x89);
while(clock[count]!='\0'){
lcddata(clock[count]);
count++;
}
count=0;

if(H==12 && M>=60 && S>=60)


{S=1;H=1;M=1;}
if(M>=60)
{H++;M=1;S=1;}
if(S>=60)
{M++;S=1;}

lcdcmd(0xC4);
tochar(H);
lcddata(':');
tochar(M);
lcdcmd(0xCB);
tochar(S);
}
int scan_key()
{
unsigned int c;
c=22;
while(!(c==0 && c==1 && c==2 && c==3 && c==4 && c==5 &&
c==6 && c==7 && c==8 && c==9 ))
{
r0=0;r1=1;r2=1;r3=1;
if(c0==0 && r0==0 )
{lcddata('1');delay(100000);P1=0xFF;return c=1;}
if(c1==0 && r0==0)
{ lcddata('2');delay(100000);P1=0xFF;return c=2;}
if(c2==0 && r0==0)
{ lcddata('3');delay(100000);P1=0xFF;return c=3;}
if(c3==0 && r0==0)
{ lcddata('4');delay(100000);P1=0xFF;return c=4;}

r0=1;r1=0;r2=1;r3=1;

if(c0==0 && r1==0)


{ lcddata('5');delay(100000);P1=0xFF;return c=5;}
if(c1==0 && r1==0)
{ lcddata('6');delay(100000);P1=0xFF;return c=6;}
if(c2==0 && r1==0)
{ lcddata('7');delay(100000);P1=0xFF;return c=7;}
if(c3==0 && r1==0)
{ lcddata('8');delay(100000);P1=0xFF;return c=8;}

r0=1;r1=1;r2=0;r3=1;

if(c0==0 && r2==0)


{ lcddata('9');delay(100000);P1=0xFF;return c=9;}
if(c1==0 && r2==0)
{ lcddata('0');delay(100000);P1=0xFF;return c=0;}
}
P1=0xFF;
return 0;
}
void tochar(unsigned int value)
{
char tendigit,unitdigit;
tendigit=value/10;
lcddata(tendigit+48);
unitdigit=value%10;
lcddata(unitdigit+48);
}
int scan_key1()
{
int c;
r0=0;r1=1;r2=1;r3=1;
if(c0==0 && r0==0 )
{c=1;}
else c=0;
P1=0xFF;
return c;
}
● Output

You might also like