UINT16 read_adc(UINT8 channel) { if (channel == A1) AD1CHS = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4; else if (channel == A0) AD1CHS = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2; else return 0; int i; for (i = 0; i < 10; i++); // small delay (may not be needed but w/e) AcquireADC10(); for (i = 0; i < 100; i++); // Wait for sampling to complete ConvertADC10(); while(!BusyADC10()); //AcquireADC10(); return ReadADC10(0); }
// === ADC Thread ============================================= static PT_THREAD (protothread_adc(struct pt *pt)) { PT_BEGIN(pt); myPaddle = (paddle*) malloc(sizeof(paddle)); myPaddle->xc = 4; static int adc_10; while(1) { PT_YIELD_TIME_msec(2); // read the ADC from pin 24 (AN11) adc_10 = ReadADC10(0); // read the result of channel 9 conversion from the idle buffer AcquireADC10(); // not needed if ADC_AUTO_SAMPLING_ON below //READADC10 value is between 0-1020 tft_fillRect(myPaddle->xc, myPaddle->yc, 2, PADDLE_LEN, ILI9340_BLACK); if(adc_10 < 3) adc_10 = 0; else if (adc_10 > 920) adc_10 = 920; myPaddle->vyc = (adc_10 >> 2) - myPaddle->yc; myPaddle->yc = (adc_10 >> 2); tft_fillRect(myPaddle->xc, myPaddle->yc, 2, PADDLE_LEN, ILI9340_WHITE); } // END WHILE(1) PT_END(pt); } // ADC thread