0% found this document useful (0 votes)
51 views13 pages

ESY All Programs

The document contains code examples for various microcontroller applications including data transfer, arithmetic operations, serial communication, seven segment display, stepper motor control, DAC waveform generation, ADC interfacing, and LCD interfacing. The examples demonstrate basic operations and interfacing of peripherals like timers, ports, serial communication, and I/O devices.

Uploaded by

shridhar sutar
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)
51 views13 pages

ESY All Programs

The document contains code examples for various microcontroller applications including data transfer, arithmetic operations, serial communication, seven segment display, stepper motor control, DAC waveform generation, ADC interfacing, and LCD interfacing. The examples demonstrate basic operations and interfacing of peripherals like timers, ports, serial communication, and I/O devices.

Uploaded by

shridhar sutar
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/ 13

Block Transfer

#include<reg51.h>

void main(void)

unsigned char source[10]={0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20};

unsigned char destination[10];

unsigned char i;

for(i=0;i<10;i++)

destination[i]=source[i];

Block Exchange

#include<reg51.h>

void main(void)

unsigned char source[5]={0x11,0x12,0x13,0x14,0x15};

unsigned char destination[5]={0x01,0x02,0x03,0x04,0x05};

unsigned char i,a,b;

for(i=0;i<5;i++)

a=destination[i];

b=source[i];

destination[i]=b;

source[i]=a;

}
Arithmetic Operations

#include<reg51.h>

void main()

unsigned char p,q,r;

unsigned int s;

P0=0x00;

P1=0x00;

P2=0x00;

P3=0x00;

p=0x04;

q=0x02;

r=p+q;

P0=r;

r=p-q;

P1=r;

r=p/q;

P2=r;

s=p*q;

P3=s;

}
16 bit addition subtraction

#include<reg51.h>

void main()

unsigned int i,j,k,m;

j=0x4567;

k=0x1234;

i=j+k;

m=j-k;

}
Serial Communication to transfer YES
#include<reg51.h>

void serial();

void main()

TMOD=0x20;

TH1=0xFD;

TL1=0xFD;

SCON=0x50;

TR1=1;

while(1)

SBUF='Y';

serial();

SBUF='E';

serial();

SBUF='S';

serial();

SBUF=' ';

serial();

void serial()

while(TI==0);

TI=0;

}
Seven segment display
#include<reg51.h>

void msdelay(unsigned int);

void main()

unsigned char c, arr[10]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98};

P0=0x00;

P1=0xff;

while(1)

for(c=0;c<10;c++)

P0=arr[c];

P1=0xfe;

msdelay(200);

void msdelay(unsigned int t)

unsigned int i,j;

for(i=0;i<t;i++)

for(j=0;j<1275;j++);

}
Stepper Motor
#include<reg51.h>

void msdelay(unsigned int);

void main()

P1=0x00;

while(1)

P1=0x88;

msdelay(50);

P1=0x44;

msdelay(50);

P1=0x22;

msdelay(50);

P1=0x11;

msdelay(50);

void msdelay(unsigned int t)

unsigned int i,j;

for(i=0;i<t;i++)

for(j=0;j<1275;j++);

}
DAC Triangular Waveform
#include<reg51.h>

void delay();

void main(void)

unsigned char x;

P1=0x00;

while(1)

for(x=0x00; x<0xFF; x++)

P1=x;

delay();

for(x=0xFE; x>0x00; x--)

P1=x;

delay();

void delay()

unsigned int j;

for(j=0; j<=256; j++);

}
DAC Sawtooth Waveform
#include<reg51.h>

void delay();

void main(void)

unsigned char x;

P1=0x00;

while(1)

for(x=0x00; x<=0xFF; x++)

P1=x;

delay();

void delay()

unsigned int j;

for(j=0; j<=256; j++);

}
ADC Interfacing
#include<reg51.h>

sbit CLK=P2^3;

sbit ALE=P2^4;

sbit SOC=P2^5;

sbit EOC=P2^6;

sbit OE=P2^7;

void delay();

void main(void)

while(1)

P0=0xff; // input port

P2=0xf8; //

ALE=1;

delay();

ALE=0;

SOC=1;

delay();

SOC=0;

Do {

CLK=1;

delay();

CLK=0;

delay();

}while(EOC!=1);

void delay()

unsigned int j;

for(j=0; j<=256; j++);

}
LCD interfacing 4 bit mode
#include<reg51.h>

sbit RS=P0^0;

sbit E=P0^1;

void lcdcmd (unsigned char);

void lcddta(unsigned char);

void delay(unsigned int);

void main (void)

lcdcmd(0x28); // initialise lcd 4 bit mode

delay(10);

//lcdcmd(0x28);

//delay(10);

//lcdcmd(0x28);

//delay(10);

lcdcmd(0x06); //increment cursor

delay(10);

lcdcmd(0x14); //shift cursor position right

delay(10);

lcdcmd(0x0c); //display on, cursor off

delay(10);

lcdcmd(0x01); // clear display

delay(10);

lcddta('W');

delay(10);

lcddta('E');

delay(10);

lcddta('L');

delay(10);

lcddta('-');

delay(10);

lcddta('C');

delay(10);
lcddta('O');

delay(10);

lcddta('M');

delay(10);

lcddta('E');

delay(10);

lcddta(' ');

delay(10);

lcddta('t');

delay(10);

lcddta('O');

delay(10);

lcddta(' ');

delay(10);

lcddta('E');

delay(10);

lcddta('&');

delay(10);

lcddta('T');

delay(10);

lcddta('C');

delay(10);

lcdcmd(0xc0);

delay(10);

lcddta('D');

delay(10);

lcddta('E');

delay(10);

lcddta('P');

delay(10);

lcddta('A');

delay(10);

lcddta('R');
delay(10);

lcddta('T');

delay(10);

lcddta('M');

delay(10);

lcddta('E');

delay(10);

lcddta('N');

delay(10);

lcddta('T');

delay(10);

lcddta('!');

delay(10);

lcddta('!');

delay(10);

lcddta('!');

delay(10);

while(1);

void delay(unsigned int t)

unsigned int i,j;

for(i=0; i<t; i++)

for(j=0; j<=1275; j++);

void lcdcmd(unsigned char cmd)

unsigned char temp;

RS=0;

temp=cmd;

temp&=0xf0; // Mask Lower 4 Bits

P2&=0x0f; // Make No Affect on 0ther Port Pins

P2|=temp; // Send Higher Nibble to LCDPORT


E=1;

delay(1); //Send Enable Signal to LCD

E=0;

temp=cmd<<4; //Left Shift Byte Four Times

temp&=0xf0; // Mask Higher 4 Bits

P2&=0x0f; // Make No Affect on 0ther Port Pins

P2|=temp; // Send Lower Nibble to LCDPORT

E=1;

delay(1); // Send Enable Signal to LCD

E=0;

void lcddta(unsigned char dta)

unsigned char temp;

RS = 1;

temp=dta;

temp&=0xf0; // Mask Lower 4 Bits

P2&=0x0f; // Make No Affect on 0ther Port Pins

P2|=temp; // Send Higher Nibble to LCDPORT

E=1;

delay(1); //Send Enable Signal to LCD

E=0;

temp=dta<<4; //Left Shift Byte Four Times

temp&=0xf0; // Mask Higher 4 Bits

P2&=0x0f; // Make No Affect on 0ther Port Pins

P2|=temp; // Send Lower Nibble to LCDPORT

E=1;

delay(1); // Send Enable Signal to LCD

E=0;

You might also like