0% found this document useful (0 votes)
3 views5 pages

microcontroller

The document contains various code snippets for controlling hardware components using the 8051 microcontroller, including timers, traffic lights, seven-segment displays, LCD displays, DC motors, and stepper motors. Each section provides specific code implementations for initializing and controlling these devices. The code examples demonstrate basic operations such as delays, state changes, and output to ports.

Uploaded by

levinjosh82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

microcontroller

The document contains various code snippets for controlling hardware components using the 8051 microcontroller, including timers, traffic lights, seven-segment displays, LCD displays, DC motors, and stepper motors. Each section provides specific code implementations for initializing and controlling these devices. The code examples demonstrate basic operations such as delays, state changes, and output to ports.

Uploaded by

levinjosh82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

· I/O ports:

· Timer:
#include <reg51.h>
void timer_delay();
void main()
{
unsigned char segment_code[]=
{
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char i,j;
while(1){
for(i=0;i<10;i++)
{
P0=segment_code[i];
for(j=0;j<1000;j++)
{
timer_delay();
}}}
}
void timer_delay(){
TMOD=0x01;
TL0=0x66;
TH0=0xFC;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;}

· Traffic Light:

#include<reg51.h>
#define ON 1
#define OFF 0
sbit red=P0^0;
sbit yel=P0^1;
sbit gre=P0^2;
sbit red1=P0^3;
sbit yel1=P0^4;
sbit gre1=P0^5;
sbit red2=P1^0;
sbit yel2=P1^1;
sbit gre2=P1^2;
sbit red3=P1^3;
sbit yel3=P1^4;
sbit gre3=P1^5;
void delay(int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1000;j++)
{}}}
void main(){
while(1){
P0=P1=0;
gre=red1=red2=red3=1;
delay(50);
P0=P1=0;
yel=yel1=red2=red3=1;
delay(30);
P0=P1=0;
red=gre1=red2=red3=1;
delay(50);
P0=P1=0;
red=yel1=yel2=red3=1;
delay(30);
P0=P1=0;
red=red1=gre2=red3=1;
delay(50);
P0=P1=0;
red=red1=yel2=yel3=1;
delay(30);
P0=P1=0;
red=red1=red2=gre3=1;
delay(50);
P0=P1=0;
yel=red1=red2=yel3=1;
delay(30);
}}

· Relays-bulb:

· Seven-Segment Display:

1. #include <reg51.h>
void timer_delay();
void main()
{
unsigned char segment_code[]=
{
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char i,j;
while(1){
for(i=0;i<10;i++)
{
P0=segment_code[i];
for(j=0;j<1000;j++)
{
timer_delay();
}
}
}
}
void timer_delay(){
TMOD=0x01;
TL0=0x66;
TH0=0xFC;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;}

2. #include <reg51.h>
void delay();
unsigned int i=0;
void delay(unsigned int ms_count){
unsigned int i;
for(i=0;i<ms_count;i++){
TMOD = 0x01;
TH0=0xFC;
TL0=0x66;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}}
int main(){
char c[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
char N=0;
while(1){
for(N=0x00;N<=0x09;N++){
P0=c[N];
delay(5000);
}}}

· LCD display:

#include <reg51.h>
#define LCD P1
sbit RS=P3^0;
sbit EN=P3^1;
unsigned char txt[25]="aMAL JYOTHI COLLEGE";
void delay(){
int k;
for(k=0;k<=1000;k++);
}
void lcd(char a,short int b){
LCD=a;
RS=b;
EN=1;
delay();
EN=0;
}
void main(){
int i=0;
lcd (0x38,0);
lcd(0x0f,0);
lcd(0x01,0);
lcd(0x02,0);
lcd(0x80,0);
for (i=0;i<=18;i++){
for (i=0;i<=15;i++){
lcd(txt[i],1);
}
lcd(0xC0,0);
for (i=16;i<=18;i++){
lcd(txt[i],1);
}}
while(1);
}

· DC motor:

#include <reg51.h>
#define ON 1
#define OFF 0
sbit SW1=P2^0;
sbit SW2=P2^1;
sbit FW=P3^0;
sbit BW=P3^1;
void delay(){
unsigned int k,j;
for(k=0;k<1000;k++);{
for(j=0;j<=1000;j++);}
}
void main(){
while(1){
if (SW1==1){
FW=1;
BW=0;
delay();
FW=0;
BW=0;
} else if(SW2==1){
FW=0;
BW=1;
delay();
FW=0;
BW=0;
}
else{
FW=0;
BW=0;
}}}

· Stepper:

#include<reg51.h>
#define STEPPER P2
void delay(int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1000;j++){
}}}
void main(){
int rot_angle[]={0x01,0x02,0x04,0x08};
int i,j;
for(j=0;j<512;j++){
for(i=0;i<4;i++){
P2=rot_angle[i];
delay(3);
}}}

You might also like