/*============================================================================== analog.h optimized for IDE for PIC18F devices: pic18f2455, pic18f2550,pic18f4455, pic18f4550 Written by Ervin Jung (2010) -------------------------------------------------------------------------------- This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. In other words, you are welcome to use, share and improve this program. You are forbidden to forbid anyone else to use, share and improve what you give them. Help stamp out software-hoarding! As a special exception, if you link this library with other files, some of which are compiled with SDCC, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. -------------------------------------------------------------------------------- example: adc_init(ADC_VCFG_VDD_VSS | ADC_CFG_13A, ADC_FRM_RJUST | ADC_ACQT_20 | ADC_FOSC_64); printf("ADC0 = %u\r\n", adc_read(ADC_CHN_0)); ==============================================================================*/ #ifndef __ANALOG_H__ #define __ANALOG_H__ /* interrupt on/off flag */ #define ADC_INT_OFF 0x00 #define ADC_INT_ON 0x01 /* output format */ #define ADC_FRM_RJUST 0x80 #define ADC_FRM_LJUST 0x00 /* reference voltage configuration */ #define ADC_VCFG_VDD_VSS 0x00 #define ADC_VCFG_AN3_VSS 0x10 #define ADC_VCFG_VDD_AN2 0x20 #define ADC_VCFG_AN3_AN2 0x30 /* oscillator frequency */ #define ADC_FOSC_2 0x00 #define ADC_FOSC_4 0x04 #define ADC_FOSC_8 0x01 #define ADC_FOSC_16 0x05 #define ADC_FOSC_32 0x02 #define ADC_FOSC_64 0x06 #define ADC_FOSC_RC 0x07 /* acquisition time */ #define ADC_ACQT_0 (0x00 << 3) #define ADC_ACQT_2 (0x01 << 3) #define ADC_ACQT_4 (0x02 << 3) #define ADC_ACQT_6 (0x03 << 3) #define ADC_ACQT_8 (0x04 << 3) #define ADC_ACQT_12 (0x05 << 3) #define ADC_ACQT_16 (0x06 << 3) #define ADC_ACQT_20 (0x07 << 3) #if defined(pic18f2455) || defined(pic18f2550) || defined(pic18f4455) || defined(pic18f4550) /* analog channels */ #define ADC_CHN_0 0x00 #define ADC_CHN_1 0x01 #define ADC_CHN_2 0x02 #define ADC_CHN_3 0x03 #define ADC_CHN_4 0x04 #define ADC_CHN_5 0x05 #define ADC_CHN_6 0x06 #define ADC_CHN_7 0x07 #define ADC_CHN_8 0x08 #define ADC_CHN_9 0x09 #define ADC_CHN_10 0x0a #define ADC_CHN_11 0x0b #define ADC_CHN_12 0x0c #define ADC_CHN_13 0x0d #define ADC_CHN_14 0x0e #define ADC_CHN_15 0x0f // analog ports #define ADC_CFG_15A 0x00 #define ADC_CFG_14A 0x01 #define ADC_CFG_13A 0x02 #define ADC_CFG_12A 0x03 #define ADC_CFG_11A 0x04 #define ADC_CFG_10A 0x05 #define ADC_CFG_9A 0x06 #define ADC_CFG_8A 0x07 #define ADC_CFG_7A 0x08 #define ADC_CFG_6A 0x09 #define ADC_CFG_5A 0x0a #define ADC_CFG_4A 0x0b #define ADC_CFG_3A 0x0c #define ADC_CFG_2A 0x0d #define ADC_CFG_1A 0x0e #define ADC_CFG_0A 0x0f #else /* unknown device */ #error Device ADC style is unknown! #endif void adc_init(char conf1, char conf2) { ADCON1 = conf1; ADCON2 = conf2; } void adc_disable(void) { ADCON0bits.ADON = 0; } unsigned int adc_read(unsigned char channel) { ADCON0 = (channel << 2) | 3; while(ADCON0bits.GO); return (ADRESH << 8) | ADRESL; } #endif //__ANALOG_H__