#INCLUDE <16C505.h> #FUSES INTRC, RB4, NOWDT, NOPROTECT, NOMCLR #USE DELAY (CLOCK=4000000) #define AD_CS pin_C0 #define AD_CK pin_C1 #define AD_DO pin_C2 int adVal; //This variable holds result from A/D conversion. /* This function initiates a conversion from the ADC0831 A/D convertor, then reads the value from the A/D convertor and puts the result into the variable adVal. */ void AD_Read() { int i; output_low(AD_CS); //Select chip (initiate conversion. for (i=0; i<2; i++) { output_high(AD_CK); //cycle clock twice (while converstion starts. delay_us(10); output_low(AD_CK); delay_us(10); } for (i=0;i<8;i++) { //Read in value from A/D convertor output_high(AD_CK); delay_us(10); shift_left(&adVal, 1, input(AD_DO)); output_low(AD_CK); delay_us(10); } output_high(AD_CK); //cycle clock once. delay_us(10); output_low(AD_CK); delay_us(10); output_high(AD_CS); //Deselect chip. } /* Main routine - Read A/D convertor 10 times per second void main() { output_low(AD_CK); //Initialize A/D output_high(AD_CS); setup_counters(RTCC_INTERNAL,RTCC_DIV_256); while(1) { output_low(pin_b0); //Turn off all LEDS output_low(pin_b1); output_low(pin_b2); output_low(pin_b4); AD_Read(); //Read A/D convertor if (adVal>51) output_high(pin_b0); //LED1 for Vin>1 volt if (adVal>102) output_high(pin_b1); //LED2 for Vin>2 volt if (adVal>153) output_high(pin_b2); //LED3 for Vin>3 volt if (adVal>204) output_high(pin_b4); //LED4 for Vin>4 volt delay_ms(100); //Wait 0.1 seconds. } }