After going through sample programs in keil now, you should be aware of how to write simple codes in C language for 8051. So now, let us see some more programs in C language for 8051. These programs are interfacing programs means different peripheral devices are connected with 8051 and we have to write the C program to properly interface these devices with it. different devices are push buttons, keypad, LEDs, 7-segment display, LCD, ADC, DAC etc. here I am not giving the schematic diagrams of interfacing but just a short description of hardware connection is given that tells how the device is connected with 8051.
8051 project development cycle: – these are the steps to develop 8051 project using keil
- Create source files in C or assembly.
- Compile or assemble source files.
- Correct errors in source files.
- Link object files from compiler and assembler.
- Test linked application.
KEIL can be used to interface several peripherals to an 8051 micro controllers with the help of simple coding. This tutorial will detail you about using the compiler to interface LED, LCD, 7-segment, push button, ADC and DAC. While the working of these peripherals is mandatory here, it is also recommended for the programmer to make sure that the hardware is also properly assembled for better execution of the program.
LED Interfacing
LED Interfacing:
Hardware:- total 32 LEDs are connected one with each i/o line. Anodes of all 32 LEDs are tied to Vcc and cathode is connected with port pin. Here is the program to generate different chasing effects one after another continuously.
#include<reg51.h>
void delay(void);
void delay()
{
int a,b;
for(a=0;a<100;a++)
for(b=0;b<1000;b++);
}
void main()
{
int i, j, k, l;
for(i=0;i<50;i++) // ON-OFF effect
{
P0=0x00;
P1=0x00;
P2=0x00;
P3=0x00
Delay();
P0=0xFF;
P1=0xFF;
P2=0xFF;
P3=0xFF;
Delay();
}
for(j=0;j<50;j++) // alternate blinking effect
{
P0=0x55;
P1=0x55;
P2=0x55;
P3=0x55
Delay();
P0=0xAA;
P1=0xAA;
P2=0xAA;
P3=0xAA;
Delay();
}
for(k=0;k<50;k++) // ON-OFF effect for port lower and upper nibble
{
P0=0xF0;
Delay();
P0=0x0F;
Delay();
P0=0xFF
P1=0xF0;
Delay();
P1=0x0F;
Delay();
P1=0xFF
P2=0xF0;
Delay();
P2=0x0F;
Delay();
P2=0xFF
P3=0xF0;
Delay();
P3=0x0F;
Delay();
P3=0xFF;
}
for(l=0;l<50;l++) // alternate switching of ports one by one
{
P0=0x00
Delay();
P0=0xFF;
P1=0x00;
Delay();
P1=0xFF;
P2=0x00
Delay();
P2=0xFF;
P3=0x00;
Delay();
P3=0xFF;
}
Push button keypad and 7-segment interfacing
Push button keypad and 7-segment interfacing:
Hardware:-8 push buttons are connected with P2 with one terminal as common ground. A common anode type 7-segment display is connected to P0. the program displays number 1 to 8 on 7-segment depending upon the key is pressed
#include<reg51.h>
void main(void)
{
loop:P2=0xFF; // send all 1’s to P1
while(P21==0xFF); // remain within loop till key is not pressed
switch(P1) // when key pressed detect is
{
case 0xFE:
P0=0xF9; // and display digit from 1 to 8
break;
case 0xFD:
P0=0xA4;
break;
case 0xFB:
P0=0xB0;
break;
case 0xF7:
P0=0x99;
break;
case 0xEF:
P0=0x92;
break;
case 0xDF:
P0=0x82;
break;
case 0xBF:
P0=0xF8;
break;
case 0x7F:
P0=0x80;
break;
}
goto loop;
}
LCD interfacing
LCD interfacing:
Hardware:- normally all types of text LCDs have 8 data pins and 3 control signals. here data pins are connected with P0 and three control pins RS, R/W and EN are connected with P2.7, P2.6 & P2.5 respectively. Its a 16X2 LCD.
#include <reg51.h>
#include <string.h>
sbit rs = P2^7; // declare P2.7 as rs pin
sbit en = P2^5; // declare p2.5 as enable pin
sbit rw = P2^6; // declare p2.6 as read/write pin
sbit b = P0^7; // busy flag
void writecmd(unsigned char a); // function to send command to LCD
void writedat(unsigned char b); // function to send data to LCD
void busy(); // function to check LCD is busy or not
void writestr(unsigned char *s); // function to write string on LCD
void writecmd(unsigned char a)
{
busy(); // check for LCD is busy or not
rs = 0; // clear rs pin for command
rw = 0; // clear rw pin to write
P0 = a; // send command character
en = 1; // strob LCD
en = 0;
}
void writedat(unsigned char b)
{
busy(); // check for LCD is busy or not
rs = 1; // set rs pin for data
rw = 0; // clear rw pin to write
P0 = b; // send data character
en = 1; // strob LCD
en = 0;
}
void busy()
{
en = 0; // disable display
P0 = 0xFF; // configur P0 as input
rs = 0; // clear rs pin for command
rw = 1; // set rw pin to read
while(b==1)
{
en=0; // strob LCD till P0.7 is 1
en=1;
}
en=0;
}
void writestr(unsigned char *s)
{
unsigned char l,i;
l = strlen(s); // get the length of string
for(i=1;i<l;i++)
{
writedat(*s); // write every char one by one
s++;
}
}
main()
{
P0=0x00; // P0 and P0 as output ports
P2=0x00;
writecmd(0x3C); // initialize LCD
writecmd(0x0E);
writecmd(0x01); // clear memory and home cursor
writestr(“Wel-Come to LCD”); // write message in first line
writecmd(0xC4); // move cursor to second line 4th pos
writestr(“Program”);
while(1) // continuous loop
}
ADC interfacing
ADC interfacing:
Hardware:- here I have interfaced 8 bit ADC 0804 with 8051 and given program displays digital equivalent value (HEX) of any analog input on 2-digit multiplex seven segment. Usually ADC has four control signals. CS (chip select-active low), WR (SOC-start of conversion-active low), RD(OE-o/p enable-active low)and INT. from these four only INT is o/p form ADC all other are inputs. The CS is connected with ground. WR, RD and INT signals are connected with P3.0, P3.1 and P3.2 respectively. 8 data pins are connected with P1. Multiplex 7-segment has 8 data pins and 2 display select pins. data pins are connected with P0 and display select pins are connected to P2.7 and P2.6 respectively.
#include <reg51.h>
#include <string.h>
sbit wr = P3^0; // ADC write enable
sbit rd = P3^1; // ADC read enable
sbit d1 = P2^7 // display 1 select bit
sbit d2 = P2^6 // display 2 select bit
unsigned char data d;
unsigned int a=0,b=0,c=0;
void int1(void) interrupt 1 //external interrupt 1 subroutine
{
EA=0 // first disable any other interrupt
rd = 0; // send read signal
d=P1; // read the data from port
rd=1;
a = (d & 0x0F); // saperate upper and lower
b = d >> 4; // nibbles
EA=1; // again enable interrupts
}
void tmr1(void) interrupt 3 // timer 1 overflow interrupt
{
c++; // count no of timer 1 interrupts
TH1=0xEC; // reload timer
TL1=0x77;
TF1=0; // clear timer 1 overflow flag
if((c%2)==0) // check interrupt has arrived even
{ // times or odd times
d1=1; // alternatively change display
d2=0; // for even times
P0=b;
}
else // and odd times
{
d1=0;
d2=1;
P0=a;
}
}
main()
{
P0=0x00; // P0 output ports
P1=0xFF; // P1 input port
TMOD=0x90; // initialize T1 as 16 bit timer
TH1=0xEC; // load count 60535(=EC77h)
TL1=0x77; // so it will overflow after 5 ms
TR1=1; // start timer
d1=0;
d2=1; //enable first display
P0=a; //display 0 initially
IE=0x89; // enable external and timer interrupts
wr = 0; // send write signal
wr = 1;
while(1); // wait in continuous loop
}
DAC interfacing
DAC interfacing:
Hardware:-a 8 bit DAC is connected to P2. Its two control signals WR and CS are tied to ground. Here I am writing a program to generate three different types of waveforms square, triangular and staircase type. To select desire waveform three push buttons are connected at P3.0 to P3.2.
#include<reg51.h>
void delay1(); //key debounce delay
void delay2(); // delay for frequency
void sqrarwave(); // function to generate square wave
void triwave(); // function to generate triangular wave
void stairwave(); // function to generate staircase wave
void delay1() // approx. 100 ms delay
{
int a,b;
for(a=0;a<100;a++)
for(b=0;b<1000;b++);
}
void delay2() // approx 1 ms delay
{
int c;
for(c=0;c<1000;c++);
}
void squarwave()
{
while(P3==0xFF) // till any button is not pressed
{
P2=0xFF; // first send all high to P2
Delay2();
P2=0x00; // then send all low to P2
Delay2();
}
}
void triwave()
{
unsigned char d;
while(P3==0xFF)
{
for(d=0x00;d<0xFF;d++) // send values 00 to FF
{
P2=d; // one by one for positive ramp
}
for(d=0xFF;d<0x00;d–) // send values FF to 00
{
P2=d; // one by one for negative ramp
}
}
void stairwave()
{
while(P3==0xFF)
{
P2=0x00; // step by step increment
Delay2();
P2=0x20; // values sent to P2
Delay2();
P2=0x40;
Delay2();
P2=0x80;
Delay2();
}
}
void main()
{
P2=0x00; // P2 as output port
P3=0xFF; // P3 as input port
While(P3=0xFF); // wait till any button is pressed
Switch(P3)
{
case 0xFE: // for first button
delay1(); // key debounce delay
P3=0xFF; // send all 1’s again to P3
squarwave(); // generate square wave
break;
case 0xFD:
delay1();
P3=0xFF;
triwave();
break;
case 0xFB:
delay1();
P3=0xFF;
stairwave();
break;
}
}
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.