void InitADC() { PCONP |= 1<<12; //set ADC power clock //ADC0-3 enabled PINSEL1 |= 0x55 << 14 ; //0b01010101 PINSEL1 &= ~(0xAA<<14); //0b10101010 AD0CR= 0x00200500 ; } void ADC_stopCnv (void) { AD0CR &= ~0x01000000; /* stop conversion */ } void read_adc() { unsigned char adc_input; for (adc_input=0; adc_input<4; adc_input++) { AD0CR &= 0xFFFFFF00; AD0CR|=adc_input; delay_us(10); ADC_stopCnv(); AD0CR |= (1<<24); //start delay_us(10); //x2=AD0GDR; while (!(AD0GDR & (1UL<<31))); //AnalogValues[adc_input] = (AD0GDR>>6 & 0x3FF); //this line wasn't working properly so I used AD0DRx ADC_stopCnv(); //send data to RS232 port if(adc_input==0) { AnalogValues[adc_input]=AD0DR0>>6; sendchar(0); sendchar(AnalogValues[adc_input]>>8); sendchar(AnalogValues[adc_input]); delay_ms(400); } else if(adc_input==1) { AnalogValues[adc_input]=AD0DR1>>6; sendchar(1); sendchar(AnalogValues[adc_input]>>8); sendchar(AnalogValues[adc_input]); delay_ms(400); } else if(adc_input==2) { AnalogValues[adc_input]=AD0DR2>>6; sendchar(2); sendchar(AnalogValues[adc_input]>>8); sendchar(AnalogValues[adc_input]); delay_ms(400); } else if(adc_input==3) { AnalogValues[adc_input]=AD0DR3>>6; sendchar(3); sendchar(AnalogValues[adc_input]>>8); sendchar(AnalogValues[adc_input]); delay_ms(400); } } } |