Sie sind auf Seite 1von 13

program for ADC 0808 connecting Temperature sensor using 8051

/********************* ADC-0808****************************/ #include<REG51.H> sfr adcdata=0x90; sfr ldata=0x80; sbit rs=P2^7; sbit rw=P2^6; sbit en=P2^5; ` //including the header file registers of 8051 //assigning adc data to port 1 //assigning lcd data to port 0 // assining control pins to port2.5,6,7

// channel selection bits of adc sbit ch_a=P3^2; sbit ch_b=P3^3; sbit ch_c=P3^4; // control pins of adc sbit ALE=P3^5; sbit EOC=P3^6; sbit SC=P3^7; sbit motor=P2^0; sbit LED0=P2^1; sbit LED1=P2^2; sbit LED2=P2^3; sbit LED3=P2^4; void cmd(unsigned char y); void lcddata(unsigned char y); void disp_str(unsigned char *P); void delay(unsigned int k); //void lcdinit(void); unsigned char convertanddispaly(unsigned char value); void disp_loc(unsigned char , unsigned char *); unsigned char value,z,c,TEMP,T,LDR,L; //lcd command function //lcd data function //to display string in lcd //delay function //address latch enble pin // end of converssion pin //select chip pin //assigning motor //assigning leds

unsigned char adc_data(unsigned char);

/**************************convert program*************************/ // the function is used to convert the integer value to ascii value unsigned char convertanddispaly(unsigned char value) { unsigned char x,d1,d2,d3; x=value/10; d1=value%10; d2=x%10; d3=x/10; lcddata(d3+0x30); lcddata(d2+0x30); lcddata(d1+0x30); z=d1*100+d2*10+d3; return(z); } /**********************main program start************************/ void main() { adcdata=0xff; EOC=1; ALE=0; SC=0; lcdinit(); delay(10); cmd(0x01); //initialize the lcd //delay of 10 //lcd command to clear the screen // initial condition values of ADC control pin //move data d3 to lcd //move data d2 to lcd //move data d1 to lcd

disp_loc(0xc0," ADC Project "); delay(100); cmd(0x01); disp_loc(0x80,"TEMP:");

disp_loc(0x89,"LDR:"); while(1) { TEMP=adc_data(1); cmd(0x85); //lcd command to write in the location 85

T=convertanddispaly(TEMP); //convert and display temperature delay(1);//delay of 1 LDR=adc_data(0); cmd(0x8D); L=convertanddispaly(LDR); delay(1); // the adc_data is moved to LDR //lcd command to write in th location 8D //convert and display light

} }

/***********************switch cases******************************/ //The function to select the adc channel. unsigned char adc_data(unsigned char c) { switch(c) { case 0: ch_a=0; ch_b=0; ch_c=0; break; // channel0 selection

case 1: ch_a=1; ch_b=0; ch_c=0; break;

// channel1 selection

case 2: ch_a=0; ch_b=1; ch_c=0; break;

// channel2 selection

case 3: ch_a=1; ch_b=1; ch_c=0; break;

// channel3 selection

case 4: ch_a=0; ch_b=0; ch_c=1; break;

// channel4 selection

case 5: ch_a=1; ch_b=0; ch_c=1; break;

// channel5 selection

case 6: ch_a=0; ch_b=1; ch_c=1; break;

// channel6 selection

default: ch_a=1; ch_b=1; ch_c=1; break; } delay(1); ALE=1; delay(1); SC=1; delay(1); ALE=0; SC=0; while(EOC==1); delay(1); value=adcdata; EOC=1; return(adcdata); } void disp_loc(unsigned char r, unsigned char *s) {

// channel7 selection

//monitoring end of conversion bit until EOC equal to 0

ldata=r; rs=0; rw=0; en=1; delay(2); en=0; while(*s!='\0') lcddata(*s++); } lcdinit() { cmd(0x38); cmd(0x0e); cmd(0x01); cmd(0x06); cmd(0x80); disp_str("welcome"); } void cmd(unsigned char y) { ldata=y; rs=0; rw=0; en=1; delay(10); en=0; } void lcddata(unsigned char y) { ldata=y; // 2 lines and 5x7 matrix //display on, cursor blinking //clear display screen //increment cursor //force cursor to beginning of 1st line //lcd initialize

rs=1; rw=0; en=1; delay(10); en=0; } void disp_str(unsigned char *P) { while(*P!='\0') lcddata(*P++); } void delay(unsigned int k) { unsigned int i,j; for(i=0;i<k;i++) for(j=0;j<1200;j++); }

/Program to display temperature in Celsius and Farenheit scale. #include<reg51.h> #define sec 100 sbit rs = P3^0; sbit rw = P3^1; sbit e = P3^2; sbit wr= P3^3; sbit rd= P3^4; sbit intr= P3^5; int x=0, y=0,z[10],a[3]={0,0,0}; void delay(unsigned int msec )

{ int i ,j ; for(i=0;i<msec;i++) for(j=0; j<1275; j++); } void lcd_cmd(unsigned char item) { P2 = item; rs= 0; rw=0; e=1; delay(1); e=0; return; } void lcd_data(unsigned char item) { P2 = item; rs= 1; rw=0; e=1; delay(1); e=0; return; } // Function to send commands to LCD

// Function to send data to LCD

void lcd_data_string(unsigned char *str) // Function to string to LCD { int i=0; while(str[i]!='\0') { lcd_data(str[i]); i++; delay(10); } return; }

void shape() // Function to make the shape of degree symbol { lcd_cmd(64); lcd_data(2); lcd_data(5); lcd_data(2); lcd_data(0); lcd_data(0); lcd_data(0); lcd_data(0); lcd_data(0); } void convert() // Function to convert the values of ADC into numeric value to be sent to LCD { int s; lcd_cmd(0x81); delay(2); lcd_data_string("TEMP:"); y=(((9*x)/5)+32); s=y/100; y=y%100; lcd_cmd(0x88); if(s!=0) lcd_data(s+48); else lcd_cmd(0x06); s=y/10; y=y%10; lcd_data(s+48); lcd_data(y+48); lcd_data(0); lcd_data('F'); lcd_data(' '); y=x; lcd_cmd(0xc1);

//Setting cursor to first position of first line

delay(2); lcd_data_string("TEMP:"); s=y/100; y=y%100; lcd_cmd(0xc8); if(s!=0) lcd_data(s+48); else lcd_cmd(0x06); s=y/10; y=y%10; lcd_data(s+48); lcd_data(y+48); lcd_data(0); lcd_data('c'); lcd_data(' '); delay(2); } void main() { int i,j; P1=0xff; lcd_cmd(0x38); lcd_cmd(0x0c); delay(2); lcd_cmd(0x01); delay(2);

//2 Line, 5X7 Matrix display //Display On, Cursor blinking // clear screen

while(1) { for(j=0;j<3;j++) { for(i=0;i<10;i++) { delay(1); rd=1; wr=0; delay(1);

wr=1; while(intr==1); rd=0; lcd_cmd(0x88); z[i]=P1/10; delay(1); intr=1; } for(i=0;i<10;i++) a[j]=z[i]+a[j]; } a[0]=a[0]/3; a[1]=a[1]/3; a[2]=a[2]/3; x=a[0]+a[1]+a[2]; shape(); convert(); } }

//Program to make a digital thermometer with display in centigrade scale #include<reg51.h> #define port P3 #define adc_input P1 #define dataport P0 #define sec 100 sbit rs = port^0; sbit rw = port^1; sbit e = port^2; sbit wr= port^3; sbit rd= port^4; sbit intr= port^5; int test_intermediate3=0, test_final=0,test_intermediate1[10],test_intermediate2[3]={0,0,0};

void delay(unsigned int msec ) { int i ,j ; for(i=0;i<msec;i++) for(j=0; j<1275; j++); } void lcd_cmd(unsigned char item) //Function to send command to LCD { dataport = item; rs= 0; rw=0; e=1; delay(1); e=0; return; } void lcd_data(unsigned char item) //Function to send data to LCD { dataport = item; rs= 1; rw=0; e=1; delay(1); e=0; //delay(100); return; } void lcd_data_string(unsigned char *str) // Function to send string to LCD { int i=0; while(str[i]!='\0') { lcd_data(str[i]); i++; delay(10); } return; }

void shape() // Function to create the shape of degree { lcd_cmd(64); lcd_data(2); lcd_data(5); lcd_data(2); lcd_data(0); lcd_data(0); lcd_data(0); lcd_data(0); lcd_data(0); } void convert() // Function to convert the values of ADC into numeric value to be sent to LCD { int s; test_final=test_intermediate3; lcd_cmd(0xc1); delay(2); lcd_data_string("TEMP:"); s=test_final/100; test_final=test_final%100; lcd_cmd(0xc8); if(s!=0) lcd_data(s+48); else lcd_cmd(0x06); s=test_final/10; test_final=test_final%10; lcd_data(s+48); lcd_data(test_final+48); lcd_data(0); lcd_data('c'); lcd_data(' '); delay(2); } void main() { int i,j; adc_input=0xff; lcd_cmd(0x38);

lcd_cmd(0x0c); //Display On, Cursor Blinking delay(2); lcd_cmd(0x01); // Clear Screen delay(2); while(1) { for(j=0;j<3;j++) { for(i=0;i<10;i++) { delay(1); rd=1; wr=0; delay(1); wr=1; while(intr==1); rd=0; lcd_cmd(0x88); test_intermediate1[i]=adc_input/10; delay(1); intr=1; } for(i=0;i<10;i++) test_intermediate2[j]=test_intermediate1[i]+test_intermediate2[j]; } test_intermediate2[0]=test_intermediate2[0]/3; test_intermediate2[1]=test_intermediate2[1]/3; test_intermediate2[2]=test_intermediate2[2]/3; test_intermediate3=test_intermediate2[0]+test_intermediate2[1]+test_intermediate2[2]; shape(); convert(); } } Seek forgiveness with remorse in your heart.

Das könnte Ihnen auch gefallen