Вы находитесь на странице: 1из 10

#include <REGX51.H> #define adc_port P2 //ADC Port #define rd P1_0 //Read signal P1.

0 #define wr P1_1 //Write signal P1.1 #define cs P1_2 //Chip Select P1.2 #define intr P1_3 //INTR signal P1.3 void conv(); void read(); //Start of conversion function //Read ADC function

unsigned char adc_val; void main(){ while(1){ //Forever loop conv(); //Start conversion read(); //Read ADC P3 = adc_val; //Send the read value to P3 } } void conv(){ cs = 0; //Make CS low wr = 0; //Make WR low wr = 1; //Make WR high cs = 1; //Make CS high while(intr); //Wait for INTR to go low } void read(){ cs = 0; //Make CS low rd = 0; //Make RD low adc_val = adc_port; //Read ADC port rd = 1; //Make RD high cs = 1; //Make CS high }

//Using LM35 Tempreture Sensor interface with 8051 including expansion port for data logger 002 003 //Pre-processor commands 004 #include <reg51.h> 005 #include <absacc.h> 006 007 //Additional Pre-processor commands 008 #define PPIA 0x4000 009 #define PPIB 010 #define PPIC 0x4001 0x4002 //Port A //Port B //Port C //8255 Control Register //ADC Port (output)

011 #define PPICNTRL 0x4003 012 #define VOLT1ADC P2 013 014 //Single Bit Definitions 015 sbit SENS1=P1^0; 016 sbit SENS2=P1^1; 017 //sbit VOLTM1=P1^2; 018 //sbit VOLTM2=P1^3; 019 020 sbit readAD=P3^7; 021 sbit write=P3^6; 022 sbit chipsel=P3^4; 023 sbit intr=P3^3; 024

025 //Prototype Functions in use 026 void disp_mess(unsigned char mymess[],length); 027 void PPI_data(unsigned char value1); 028 void PPI_cmd(unsigned char value1); 029 void PPI_data2(unsigned char value2); 030 void PPI_cmd2(unsigned char value2); 031 void PPI_data3(unsigned char voltage); 032 void PPI_cmd3(unsigned char voltage); 033 void LCDDelay(unsigned char ustime); 034 void CHARDelay(unsigned int mstime);

//no return parameters

//Enable Pulse

035 void disp(unsigned char value1,length); //in between Characters 036 void TEMPDelay(unsigned int time); //in between Tempreture readings 037 void INITSER(); 038 void conv();//A to D Conversion 039 void read();// Read from A/D port 040

041 unsigned char tempconv1(void); 042 unsigned char tempconv2(void); 043 void volt1conv(unsigned char voltage,length); //Voltage Conversion to ASCII 044 045 unsigned int A2DM; 046 unsigned int ADC; 047 048 049 //Main Function Block 050 void main(void) 051 { 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 PPI_cmd2(0xC0); disp_mess("Sens 2: ", 0x0C); { temp2=tempconv2(); disp(temp2,1); disp(0xDF,1); disp_mess("C",1); // Command To Display 0x2A (*) //change cursor pos //display message on line 2 } //LCD messages for TEMPERATURE disp_mess("Sens 1: ",0x0C); { temp1=tempconv1(); disp(temp1,1); disp(0xDF,1); disp_mess("C",1); //display message on line 1 bits PPI_cmd(0x0E); PPI_cmd(0x01); PPI_cmd(0x80); //LCD on, cursor on //Clear LCD screen //change cursor pos while(1) { //infinite loop //Control word ALL Ports o/p //init. LCD 2 lines, 5x7 matrix, 8unsigned char temp1; unsigned char temp2; unsigned char volt1; unsigned char i;

XBYTE[PPICNTRL]=0x80; //LCD commands PPI_cmd(0x38);

// Command To Display 0x2A (*)

082 083 084 085 086 087 088 089 090 091 value { {

} PPI_cmd(0x01);

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

//10 loops to achieve correct

conv();

//Start conversion //Read ADC //insert value into array adc_avg

read(); A2DM += ADC; 092 and increament 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 } 112 113 114 } // PPI_data3('V'); } TEMPDelay(50); exit: goto exit; A2DM = A2DM/10; //PPI_cmd3(0x80); disp_mess("Volt 1: ", 0x0C); ADC = A2DM * 19; }

//Value is Units

volt1conv((unsigned char)(ADC/1000)); PPI_data3('.'); ADC = ADC%1000; //decimal point //Remainder is 2 dp

volt1conv((unsigned char)(ADC/10));

//micro stay here

115 //Tempreture Convertion Value for SENSOR 1 116 unsigned char tempconv1() 117 { 118 119 120 input1=SENS1; //read input from Port 1.0 unsigned char input1,val1,val1A,val1B,val1C;

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 } 139 140 //Tempreture Convertion Value for SENSOR 2 141 unsigned char tempconv2() 142 { 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 PPI_cmd2(0xC7); PPI_data2(val2C); PPI_cmd2(0xC8); PPI_data2(val2B); PPI_cmd2(0xC9); PPI_data2('.'); //LCD Position val2A=val2A|0x30; val2B=val2B|0x30; val2C=val2C|0x30; //ASCII Convertions val2=input2/100; val2A=input2%10; val2B=val2A%10; val2C=val2B%10; //divide by 10 //remainder is Units digit //remainder of second division is Tens digit input2=SENS2; //read input from Port 1.1 unsigned char input2,val2,val2A,val2B,val2C; PPI_cmd(0x87); PPI_data(val1C); PPI_cmd(0x88); PPI_data(val1B); PPI_cmd(0x89); PPI_data('.'); PPI_data(val1A); //LCD Position val1A=val1A|0x30; val1B=val1B|0x30; val1C=val1C|0x30; //ASCII Convertions val1=input1/1000; val1A=input1%10; val1B=val1A%10; val1C=val1B%10; //remainder is Units digit //remainder of second division is Tens digit

162 163 } 164

PPI_data2(val2A);

165 //Voltage Conversion Value for VOLTMETER 1 166 void volt1conv(unsigned char voltage,length) 167 { 168 169 char temp1,temp2; // insert value of voltage (hex) into temp1 // clear temp2 array

temp1 = voltage; array 170 temp2=0; 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 } 195 do { temp1 = temp1-10; if(temp1>=0) temp2++; } while(temp1>=0); if(temp2>0) PPI_data3(temp2+0x30); temp2=0; temp1 = temp1+100; } while(temp1>=0); do { temp1 = temp1-100; if(temp1>=0) temp2++;

//decrease value of temp1 by 100 // increament temp2 by 1

//as long as temp1 is greater or equal to 0

//Convert temp2 to ASCII

//as long as temp1 is greater or equal to 0

PPI_data3(temp2+0x30); temp2 = temp1+10; PPI_data3(temp2+0x30);

196 // Prototype Function Decleration To Display SENSOR 1 On LCD 197 void disp(unsigned char value1,length) 198 { 199 200 201 202 unsigned char y,val1; for(y=0;y<length;y++) { val1 =value1; // Variable y For Loop // Loop

203 204 205 206 207 } 208 }

PPI_data(val1); CHARDelay(200);

// LCD Is Defned To Port 0

209 // Prototype Function Decleration To Display SENSOR 2 On LCD 210 void disp2(unsigned char value2,length) 211 { 212 213 214 215 216 217 218 219 220 } 221 222 //LCD Messages for SENSOR 1 223 void disp_mess(unsigned char mymess[],length) 224 { 225 226 227 228 229 230 231 232 233 234 } 235 236 //LCD Command for SENSOR 1 237 void PPI_cmd(unsigned char value1) 238 { 239 240 241 242 243 //strobe the Enable Pin XBYTE[PPIB]=0x04; //E=1 for H-to-L pulse //RS=0 for command XBYTE[PPIA]=value1; //RW=0 for write } for(i=0;i<length;i++) { val1=mymess[i]; PPI_data(val1); CHARDelay(200); //delay for characters unsigned char val1; unsigned char i; } unsigned char y,val2; for(y=0;y<length;y++) { val2 =value2; PPI_data2(val2); CHARDelay(200); // LCD Is Defned To Port 0 // Variable y For Loop // Loop

244 245 246 247 } 248

LCDDelay(18); XBYTE[PPIB]=0x00; CHARDelay(200);

//~100us //E=0 //delay for commands

249 //LCD Data for SENSOR 1 250 void PPI_data(unsigned char value1) 251 { 252 253 254 255 256 257 258 } 259 260 //LCD Command for SENSOR 2 261 void PPI_cmd2(unsigned char value2) 262 { 263 264 265 266 267 268 269 270 271 } 272 273 //LCD Data for SENSOR 2 274 void PPI_data2(unsigned char value2) 275 { 276 277 278 279 280 281 282 } 283 284 //ADC Routine for Conversion 285 void conv() //RS=1 for data XBYTE[PPIA]=value2; //RW=0 for write //strobe the Enable pin XBYTE[PPIB]=0x05; LCDDelay(18); XBYTE[PPIB]=0x01; //E=1 for H-to-L pulse //~100us //E=0 //strobe the Enable Pin XBYTE[PPIB]=0x04; LCDDelay(18); XBYTE[PPIB]=0x00; CHARDelay(200); //E=1 for H-to-L pulse //~100us //E=0 //delay for commands //RS=0 for command XBYTE[PPIA]=value2; //RW=0 for write //RS=1 for data XBYTE[PPIA]=value1; //RW=0 for write //strobe the Enable pin XBYTE[PPIB]=0x05; LCDDelay(18); XBYTE[PPIB]=0x01; //E=1 for H-to-L pulse //~100us //E=0

286 { 287 288 289 290 291 292 } 293 294 void read() 295 { 296 297 298 299 300 301 } 302 303 //LCD Command for Voltage 1 304 void PPI_cmd3(unsigned char voltage) 305 { 306 307 308 309 310 311 312 313 314 } 315 316 //LCD Data for Voltage 1 317 void PPI_data3(unsigned char voltage) 318 { 319 320 321 322 323 324 325 } 326 327 //Serial Communication Settings //RS=1 for data XBYTE[PPIA]=voltage; //RW=0 for write //strobe the Enable Pin XBYTE[PPIB]=0x04; //E=1 for H-to-L pulse LCDDelay(18); XBYTE[PPIB]=0x00; CHARDelay(200); //~100us //E=0 //delay for commands //RS=0 for command XBYTE[PPIA]=voltage; //RW=0 for write chipsel = 0; readAD = 0; ADC = VOLT1ADC; readAD = 1; chipsel = 1; //Make CS low //Make RD low //Read ADC port //Make RD high //Make CS high chipsel = 0; write = 0; write = 1; chipsel = 1; while(intr); //Make CS low //Make WR low //Make WR high //Make CS high //Wait for INTR to go low

//strobe the Enable pin XBYTE[PPIB]=0x05; //E=1 for H-to-L pulse LCDDelay(18); XBYTE[PPIB]=0x01; //~100us //E=0

328 void INITSER() 329 { 330 331 332 333 334 335 } 336 337 //LCD Enable Pulse 338 void LCDDelay(unsigned char ustime) 339 { 340 341 342 343 } 344 345 //Delay used for Message Characters 346 void CHARDelay(unsigned int mstime) 347 { 348 349 350 351 352 } 353 354 //Delay between Tempreture readings 355 void TEMPDelay(unsigned int time) 356 { 357 358 359 360 361 362 } unsigned int x,z,y; //nested loops for(x=0;x<time;x++) for(z=0;z<255;z++) for(y=0;y<255;y++); unsigned int i,j; //nested loops for(i=0;i<mstime;i++) for(j=0;j<190;j++); //outer loop //inner loop unsigned char i; //single loop for(i=0;i<ustime;i++); TMOD=0x20; SCON=0x50; TH1=0xFD; TL1=0xFD; TR1=1;