0% found this document useful (0 votes)
102 views14 pages

Keil 8051 Program

The document contains 8 programs demonstrating various microcontroller applications: 1. Blinking an LED 2. Serial communication printing "Hello World" 3. Interfacing a keyboard and LCD display 4. Programming an LCD in 8-bit and 4-bit modes 5. Interfacing 7-segment LED display 6. Driving a DC motor in both directions 7. Driving a stepper motor using wave drive 8. Driving a stepper motor using full wave and half wave drive

Uploaded by

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

Keil 8051 Program

The document contains 8 programs demonstrating various microcontroller applications: 1. Blinking an LED 2. Serial communication printing "Hello World" 3. Interfacing a keyboard and LCD display 4. Programming an LCD in 8-bit and 4-bit modes 5. Interfacing 7-segment LED display 6. Driving a DC motor in both directions 7. Driving a stepper motor using wave drive 8. Driving a stepper motor using full wave and half wave drive

Uploaded by

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

Program No 1 : LED Blinking

#include<reg52.h>
// special function register declarations
// for the intended 8051 derivative
sbit LED = P2^0;

// Defining LED pin

void Delay(void);

// Function prototype declaration

void main (void)


{
while(1)
{
LED = 0;
Delay();
LED = 1;
Delay();
}
}

// infinite loop
// LED ON
// LED OFF

void Delay(void)
{
int j;
int i;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}
Program2 :Serial Communication
/*-----------------------------------------------------------------------------HELLO.C
------------------------------------------------------------------------------*/
#include <reg52.h>

/* special function register declarations */


/* for the intended 8051 derivative
*/

#include <stdio.h>

void Delay();

/* prototype declarations for I/O functions */

//advance declaration of function

/*-----------------------------------------------The main C function. Program execution starts


here after stack initialization.
------------------------------------------------*/
void main (void) {
/*-----------------------------------------------Setup the serial port for 2400 baud at 11.0592MHz.
------------------------------------------------*/
SCON = 0x50;
/* SCON: mode 1, 8-bit UART, enable rcvr
*/
TMOD |= 0x20;
/* TMOD: timer 1, mode 2, 8-bit reload
*/
TH1 = 244;
/* TH1: reload value for 2400 baud @ 11.0592MHz */
TR1 = 1;
/* TR1: timer 1 run
*/
TI = 1;
/* TI: set TI to send first char of UART */

/*-----------------------------------------------Note that an embedded program never exits (because


there is no operating system to return to). It
must loop and execute forever.
------------------------------------------------*/
while (1) {
printf ("Hello World\n"); /* Print "Hello World" */
Delay();
Delay();
Delay();
}
}
void Delay(void)
{

int j;
for(j=0;j<10000;j++)
{
}
}
Program 3 : KeyBoard and LCD Interfacing
#include<reg52.h> //including sfr registers for ports of the controller
#include<lcd.h>
//LCD Module Connections
sbit RS = P0^0;
sbit EN = P0^1;
sbit D0 = P2^0;
sbit D1 = P2^1;
sbit D2 = P2^2;
sbit D3 = P2^3;
sbit D4 = P2^4;
sbit D5 = P2^5;
sbit D6 = P2^6;
sbit D7 = P2^7;
//End LCD Module Connections
//Keypad Connections
sbit R1 = P1^0;
sbit R2 = P1^1;
sbit R3 = P1^2;
sbit R4 = P1^3;
sbit C1 = P1^4;
sbit C2 = P1^5;
sbit C3 = P1^6;
sbit C4 = P1^7;
//End Keypad Connections
void Delay(int a)
{
int j;
int i;

for(i=0;i<a;i++)
{
for(j=0;j<100;j++)
{
}
}
}
char Read_Keypad()
{
C1=1;
C2=1;
C3=1;
C4=1;
R1=0;
R2=1;
R3=1;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '7';}
if(C2==0){Delay(100);while(C2==0);return '8';}
if(C3==0){Delay(100);while(C3==0);return '9';}
if(C4==0){Delay(100);while(C4==0);return '/';}
R1=1;
R2=0;
R3=1;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '4';}
if(C2==0){Delay(100);while(C2==0);return '5';}
if(C3==0){Delay(100);while(C3==0);return '6';}
if(C4==0){Delay(100);while(C4==0);return 'X';}
R1=1;
R2=1;
R3=0;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '1';}
if(C2==0){Delay(100);while(C2==0);return '2';}
if(C3==0){Delay(100);while(C3==0);return '3';}
if(C4==0){Delay(100);while(C4==0);return '-';}
R1=1;

R2=1;
R3=1;
R4=0;
if(C1==0){Delay(100);while(C1==0);return 'C';}
if(C2==0){Delay(100);while(C2==0);return '0';}
if(C3==0){Delay(100);while(C3==0);return '=';}
if(C4==0){Delay(100);while(C4==0);return '+';}
return 0;
}
void main()
{
int i=0;
char c,p;
Lcd8_Init();
while(1)
{
Lcd8_Set_Cursor(1,1);
Lcd8_Write_String("Keys Pressed:");
Lcd8_Set_Cursor(2,1);
Lcd8_Write_String("Times:");
while(!(c = Read_Keypad()));
p=c;
while(p==c)
{
i++;
Lcd8_Set_Cursor(1,14);
Lcd8_Write_Char(c);
Lcd8_Set_Cursor(2,7);
Lcd8_Write_Char(i+48);
Delay(100);
while(!(c = Read_Keypad()));
}
i=0;
Lcd8_Clear();
}
}
Program 4 : LCD Programming 8 bit

#include<reg52.h> //including sfr registers for ports of the controller


#include<lcd.h> // Can be download from bottom of this article
//LCD Module Connections
sbit RS = P0^0;
sbit EN = P0^1;
sbit D0 = P2^0;
sbit D1 = P2^1;
sbit D2 = P2^2;
sbit D3 = P2^3;
sbit D4 = P2^4;
sbit D5 = P2^5;
sbit D6 = P2^6;
sbit D7 = P2^7;
//End LCD Module Connections
void Delay(int a)
{
int j;
int i;
for(i=0;i<a;i++)
{
for(j=0;j<100;j++)
{
}
}
}
void main()
{
int i;
Lcd8_Init();
while(1)
{
Lcd8_Set_Cursor(1,1);
Lcd8_Write_String("electroSome LCD Hello World");
for(i=0;i<15;i++)
{
Delay(1000);
Lcd8_Shift_Left();
}

for(i=0;i<15;i++)
{
Delay(1000);
Lcd8_Shift_Right();
}
Lcd8_Clear();
Lcd8_Write_Char('e');
Lcd8_Write_Char('S');
Delay(3000);
}
}
LCD Programming 4bit
#include<reg52.h> //including sfr registers for ports of the controller
#include<lcd.h> //Can be download from bottom of this article
//LCD Module Connections
sbit RS = P0^0;
sbit EN = P0^1;
sbit D4 = P2^4;
sbit D5 = P2^5;
sbit D6 = P2^6;
sbit D7 = P2^7;
//End LCD Module Connections
void Delay(int a)
{
int j;
int i;
for(i=0;i<a;i++)
{
for(j=0;j<100;j++)
{
}
}
}
void main()
{
int i;
Lcd4_Init();

while(1)
{
Lcd4_Set_Cursor(1,1);
Lcd4_Write_String("electroSome LCD Hello World");
for(i=0;i<15;i++)
{
Delay(1000);
Lcd4_Shift_Left();
}
for(i=0;i<15;i++)
{
Delay(1000);
Lcd4_Shift_Right();
}
Lcd4_Clear();
Lcd4_Set_Cursor(2,1);
Lcd4_Write_Char('e');
Lcd4_Write_Char('S');
Delay(2000);
}
}
Program 5 : LED Interfacing
#include<reg51.h>
void cct_init(void);
void delay(int);
void DisplayOn7Segment(char);
int main(void)
{
char ch = '0';
cct_init();

// Character to be displayed on 7seg


// Make all ports zero

while(1)
{
DisplayOn7Segment(ch);
delay(30000);

// Display ch on 7seg
// About 1 sec delay

switch(ch)
displayed
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':

// Update ch with new value to be

ch = '1';
ch = '2';
ch = '3';
ch = '4';
ch = '5';
ch = '6';
ch = '7';
ch = '8';
ch = '9';
ch = '0';

break;
break;
break;
break;
break;
break;
break;
break;
break;
break;

default: ch = '0'; break;


}
}
}
void cct_init(void)
{
P0 = 0x00;
P1 = 0x00;
P2 = 0x00;
P3 = 0x00;
}
void delay(int a)
{
int i;
for(i=0;i<a;i++); //null statement
}
void DisplayOn7Segment(char ch) // ch can have a value from '0' to 'F' only
{
switch(ch)
{

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':

P2 = 0x3F; break;
P2 = 0x06; break;
P2 = 0x5B; break;
P2 = 0x4F; break;
P2 = 0x66; break;
P2 = 0x6D; break;
P2 = 0x7D; break;
P2 = 0x07; break;
P2 = 0x7F; break;
P2 = 0x6F; break;

default: P2 = 0x3F; break;


}
}
Program 6 :
#include<reg52.h>
#include<stdio.h>
void delay(void);
sbit motor_pin_1 = P2^0;
sbit motor_pin_2 = P2^1;
void main()
{
do
{
motor_pin_1 = 1;
motor_pin_2 = 0; //Rotates Motor Anit Clockwise
delay();
motor_pin_1 = 1;
motor_pin_2 = 1; //Stops Motor
delay();
motor_pin_1 = 0;
motor_pin_2 = 1; //Rotates Motor Clockwise
delay();
motor_pin_1 = 0;

motor_pin_2 = 0; //Stops Motor


delay();
}while(1);
}
void delay()
{
int i,j;
for(i=0;i<1000;i++)
{
for(j=0;j<1000;j++)
{
}
}
}

Program 7 : Wave Drive Stepper Motor


#include<reg52.h>
#include<stdio.h>
void delay(int);
void main()
{
do
{
P2=0x01; //0001
delay(1000);
P2=0x02; //0010
delay(1000);
P2=0x04; //0100
delay(1000);
P2=0x08; //1000
delay(1000);
}
while(1);
}

void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}
}
}
Program 8 :Stepper Motor Full wave Drive
#include<reg52.h>
#include<stdio.h>
void delay(int);
void main()
{
do
{
P2 = 0x03; //0011
delay(1000);
P2 = 0x06; //0110
delay(1000);
P2 = 0x0C; //1100
delay(1000);
P2 = 0x09; //1001
delay(1000);
}
while(1);
}
void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}

}
}
Program 7 :Stepper Motor Half wave Drive
#include<reg52.h>
#include<stdio.h>
void delay(int);
void main()
{
do
{
P2=0x01; //0001
delay(1000);
P2=0x03; //0011
delay(1000);
P2=0x02; //0010
delay(1000);
P2=0x06; //0110
delay(1000);
P2=0x04; //0100
delay(1000);
P2=0x0C; //1100
delay(1000);
P2=0x08; //1000
delay(1000);
P2=0x09; //1001
delay(1000);
} while(1);
}
void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}

}
}

You might also like