Sei sulla pagina 1di 10

~: Keil interfacing programs for 8051 :~

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. 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++) { P0=0x00; P1=0x00; P2=0x00; P3=0x00 Delay(); P0=0xFF; P1=0xFF;

// ON-OFF effect

P2=0xFF; P3=0xFF; Delay(); } for(j=0;j<50;j++) { P0=0x55; P1=0x55; P2=0x55; P3=0x55 Delay(); P0=0xAA; P1=0xAA; P2=0xAA; P3=0xAA; Delay(); } for(k=0;k<50;k++) { 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; // alternate blinking effect

// ON-OFF effect for port lower and upper nibble

} for(l=0;l<50;l++) { P0=0x00 Delay(); P0=0xFF; P1=0x00; Delay(); P1=0xFF; P2=0x00 Delay(); P2=0xFF; P3=0x00; Delay(); P3=0xFF; }

// alternate switching of ports one by one

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: 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: 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 7segment 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 rd = 0; d=P1; rd=1; a = (d & 0x0F); b = d >> 4; EA=1; // first disable any other interrupt // send read signal // read the data from port // saperate upper and lower // nibbles // 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; wr = 0; wr = 1; while(1); } DAC interfacing:

// enable external and timer interrupts // send write signal // wait in continuous loop

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(); void delay2(); void sqrarwave(); void triwave(); void stairwave(); void delay1() { int a,b; for(a=0;a<100;a++) for(b=0;b<1000;b++); } void delay2() { int c; for(c=0;c<1000;c++); } void squarwave() { while(P3==0xFF) { P2=0xFF; Delay2(); P2=0x00; //key debounce delay // delay for frequency // function to generate square wave // function to generate triangular wave // function to generate staircase wave // approx. 100 ms delay

// approx 1 ms delay

// till any button is not pressed // first send all high to P2 // then send all low to P2

Delay2(); } } void triwave() { unsigned char d; while(P3==0xFF) { for(d=0x00;d<0xFF;d++) { P2=d; } for(d=0xFF;d<0x00;d--) { P2=d; } } void stairwave() { while(P3==0xFF) { P2=0x00; Delay2(); P2=0x20; Delay2(); P2=0x40; Delay2(); P2=0x80; Delay2(); } } void main() { P2=0x00; P3=0xFF; While(P3=0xFF); Switch(P3) {

// send values 00 to FF // one by one for positive ramp // send values FF to 00 // one by one for negative ramp

// step by step increment // values sent to P2

// P2 as output port // P3 as input port // wait till any button is pressed

case 0xFE: delay1(); P3=0xFF; squarwave(); break; case 0xFD: delay1(); P3=0xFF; triwave(); break; case 0xFB: delay1(); P3=0xFF; stairwave(); break; } }

// for first button // key debounce delay // send all 1s again to P3 // generate square wave

Potrebbero piacerti anche