int read_a2d ( char channel ) // Read specified channel { ADCON0 = (ADCON0 & 0b11000011) | ((channel << 2) & 0b00111100); ADCON0bits.GO = 1; // Start conversion while ( adc_busy() == 1 ); // While conversion is not finished return adc_read(); // Return 10 bit value }
/* * Do an analog to digital conversion. * use this when you want to be notified as soon as the conversion is done. * The process will be polled when conversion is done. */ void adc_get_poll(uint8_t adc_ch, uint16_t *buf, struct process *p) { if(buf == NULL) { /* unnecessary to run the ADC as the result will be dropped. */ return; } state = POLL; calling_process = p; dest = buf; /* wait for already running ADC to finish */ while (adc_busy()) {;} /* set up ports and pins */ if(adc_ch <= A7) { /* all analog in are on port 1 on 2553 */ init_pininput(1 << adc_ch); } ADC10CTL1 |= adc_ch << 12; /* set up, start ADC */ ADC10CTL0 |= ADC10IE; ADC10CTL0 |= ADC10SC | ENC; }
unsigned int io_read_analog(unsigned char port) { unsigned int result; unsigned char channel; #ifndef SDCC static unsigned char inputs[] = { ADC_CH0, ADC_CH1, ADC_CH2, ADC_CH3, ADC_CH4, ADC_CH5, ADC_CH6, ADC_CH7, ADC_CH8, ADC_CH9, ADC_CH10, ADC_CH11, ADC_CH12, ADC_CH13, ADC_CH14, ADC_CH15 }; #endif /* Make sure port does not exceed current Analog_ports */ if ( ! VALID_ANALOG_PORT(port) ) return OV_BAD_PARAM; #ifdef SDCC result = 0; channel = port - 1; /* * SDCC generic adc_open() doesn't work for the 8520. Lots of * stuff missing. */ adc_open8520(channel); /* Allow settling time before starting a conversion */ delay10tcy(10); adc_conv(); while ( adc_busy() ) ; result = adc_read(); adc_close(); #else /* MCC18 */ channel = inputs[port - 1]; OpenADC(ADC_FOSC_RC & ADC_RIGHT_JUST & Analog_ports_const, channel & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS); Delay10TCYx(10); ConvertADC(); while (BusyADC()) ; ReadADC(); CloseADC(); result = (unsigned int)ADRESH << 8 | ADRESL; #endif return result; }
/* * Do an analog to digital conversion. * use this when you need the result as soon as it is done; it will block until * it is finished with the conversion (ca 50 us) */ uint16_t adc_get(uint8_t adc_ch) { /* wait for already running ADC to finish */ while (adc_busy()) {;} state = SYNCH; /* set up ports and pins */ if(adc_ch <= A7) { /* all analog in are on port 1 on 2553 */ init_pininput(1 << adc_ch); } ADC10CTL1 |= adc_ch << 12; /* set up, start ADC */ ADC10CTL0 |= ADC10SC | ENC; /* wait for ADC to finish, return result */ while (adc_busy()) {;} state = OFF; return ADC10MEM; }
uint16_t analog_get(index_t index) { if (IX_ANALOG(1) <= index && index <= IX_ANALOG(USER_CT_ANALOG) && NUM_ANALOG_VALID(USER_CT_ANALOG)) { /* Read ADC (0b10000111 = 0x87). */ uint8_t chan = 0x87 | (IX_ANALOG_INV(index) << 3); adc_setchannel(chan); delay10tcy(5); /* Wait for capacitor to charge */ adc_conv(); while(adc_busy()); return adc_read(); } else { WARN_IX(index); return 0; } }
/* * Do an analog to digital conversion. * use this when you want to be notified as soon as the conversion is done. * The process p will be sent an event when conversion is done. * adc_get_event(A7, PROCESS_CURRENT()); * PROCESS_WAIT_EVENT_UNTIL(ev == adc_event); * */ void adc_get_event(uint8_t adc_ch, struct process *p) { /* wait for already running ADC to finish */ while (adc_busy()) {;} state = EVENT; calling_process = p; /* set up ports and pins */ if(adc_ch <= A7) { /* all analog in are on port 1 on 2553 */ init_pininput(1 << adc_ch); } ADC10CTL1 |= adc_ch << 12; /* set up, start ADC */ ADC10CTL0 |= ADC10IE; ADC10CTL0 |= ADC10SC | ENC; }
/* * Do an analog to digital conversion. * use this when you plan on using the conversion a bit later (>50 us later) * and it is not that important. Will not block while converting. * val will contain the result when done. */ void adc_get_noblock(uint8_t adc_ch, uint16_t *val) { if(val == NULL) { /* unnecessary to run the ADC as the result will be dropped. */ return; } dest = val; /* wait for already running ADC to finish */ while (adc_busy()) {;} state = ASYNCH; /* set up ports and pins */ if(adc_ch <= A7) { init_pininput(1 << adc_ch); } ADC10CTL1 |= adc_ch << 12; /* this needs irq */ ADC10CTL0 |= ADC10IE; ADC10CTL0 |= ADC10SC | ENC; }