/* ADC.C sample program interfaces LTC1298/MCP3202 12-bit Analog-to-Digital Converter The converted digital data was displayed with onboard LCD 22 Nov 2004 Written by W.SIRICHOTE, kswichit@kmitl.ac.th */ #include <8051io.h> /* include i/o header file */ #include <8051reg.h> #include <8051bit.h> #define Data P1.1 #define CLK P1.2 #define CS P1.3 #include "lcddrv.c" char buffer[20]; int read_ADC(char n) { int k; char i,channel; k=0; clrbit(CS) if(n==0) channel= 0x0d; else channel= 0x0f; // send config nibble dummy read NULL bit after config nibble for(i=0; i<4; i++) { clrbit(CLK) if(channel&8) setbit(Data) else clrbit(Data) setbit(CLK) channel <<=1; } P1 |= 2; // change to input bit clrbit(CLK) // now read 12-bit data for(i=0; i<12; i++) { k<<=1; setbit(CLK) clrbit(CLK) if(P1&2) k |=1; else k &= ~1; } setbit(CS) return k&=0xfff; } void main(void) { InitLcd(); while(1) { P1 ^=0x80; // blink onboard LED sprintf(buffer,"ADC0=%d ",read_ADC(0)); // printf("\n%s",buffer); goto_xy(0,0); Puts(buffer); sprintf(buffer,"ADC1=%d ",read_ADC(1)); // printf(" %s",buffer); goto_xy(0,1); Puts(buffer); // delay(100); } }