diff --git a/platform/micaz/dev/adc.c b/platform/micaz/dev/adc.c new file mode 100755 index 000000000..62986f795 --- /dev/null +++ b/platform/micaz/dev/adc.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2010, University of Colombo School of Computing + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#include +#include "dev/adc.h" + +/*---------------------------------------------------------------------------*/ +void +adc_init() +{ + ADMUX = 0; + /* AVCC with external capacitor at AREF pin. */ + ADMUX |= _BV(REFS0); + /* Disable ADC interrupts. */ + ADCSRA &= ~( _BV(ADIE) | _BV(ADIF) ); + /* Set ADC prescaler to 64 and clear interrupt flag. */ + ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADIE); + +} +/*---------------------------------------------------------------------------*/ +/* Poll based approach. The interrupt based adc is currently not used. + The ADC result is right adjusted. First 8 bits(from left) are in ADCL and + other two bits are in ADCH. See Atmega128 datasheet page 228. */ +uint16_t +get_adc(int channel) +{ + uint16_t reading; + + ADMUX |= (channel & 0x1F); + + /* Disable ADC interrupts. */ + ADCSRA &= ~_BV(ADIE); + /* Clear previous interrupts. */ + ADCSRA |= _BV(ADIF); + /* Enable ADC and start conversion. */ + ADCSRA |= _BV(ADEN) | _BV(ADSC); + /* Wait until conversion is completed. */ + while ( ADCSRA & _BV(ADSC) ); + /* Get first 8 bits. */ + reading = ADCL; + /* Get last two bits. */ + reading |= (ADCH & 3) << 8; + /* Disable ADC. */ + ADCSRA &= ~_BV(ADEN); + return reading; +} +/*---------------------------------------------------------------------------*/ diff --git a/platform/micaz/dev/adc.h b/platform/micaz/dev/adc.h new file mode 100755 index 000000000..473290b86 --- /dev/null +++ b/platform/micaz/dev/adc.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2010, University of Colombo School of Computing + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#ifndef __ADC_H__ +#define __ADC_H__ + +void adc_init(); + +uint16_t get_adc(int channel); + +#endif /* __ADC_H__ */