diff options
author | William Harrington <kb0iic@berzerkula.org> | 2019-07-31 23:15:39 -0500 |
---|---|---|
committer | William Harrington <kb0iic@berzerkula.org> | 2019-07-31 23:15:39 -0500 |
commit | 6679708c8dc5c9fa119f692a97321fa1ec715f31 (patch) | |
tree | 2ba18dbd4407dfd350ccaa41d6a9e7b8282e7c8a | |
parent | dc4ed0a6b74e1b787fe7efa2ba3eab9fb38dece6 (diff) |
Add Adc_Init and redo Adc_Read. Rework comments.
-rw-r--r-- | adc.c | 33 | ||||
-rw-r--r-- | adc.h | 3 |
2 files changed, 26 insertions, 10 deletions
@@ -1,14 +1,27 @@ #include "adc.h" -unsigned int Adc_Read(unsigned int channel) { - ADFM = 1; // results right justified - ADCS0 = 1; //conversion speed = 64*Tosc +/* ADCON0: bit 7 - bit 0 (11-1 pg 111) + * ADCS1:ADCS0:CHS2:CHS1:CHS0:GO/DONE:-:ADON + * + * ADCON1: bit 7 - bit 0 + * ADFM:-:-:-:PCFG3:PCFG2:PCFG1:PCFG0 + */ + +void Adc_Init(void) { + ADCON0 = 0x00; // A/D Off, GO_DONE not in progress, Channel 0, Fosc/2 + ADCS0 = 0; // A/D Conversion Fosc/32 ADCS1:ADCS0 (0b10) (Table 11-1) ADCS1 = 1; - ADCS2 = 1; - ADCON0bits.CHS = channel; - ADON = 1; // Turn on ADC - __delay_ms(1); - GO_DONE = 1; - while (ADCON0bits.GO_DONE == 1); - return ((ADRESH << 8) + ADRESL); // ex 1024 + + //ADCON1 + ADFM = 1; // results right justified + PCFG0 = PCFG1 = PCFG2 = PCFG3 = 0; // AN0-AN7 Analog input, Vref+/- Vdd/Vss +} + +unsigned int Adc_Read(unsigned int channel) { + ADCON0bits.CHS = channel; // Set CHS bits for channel (pg111) + ADCON0bits.ADON = 1; // Turn on ADC + __delay_us(5); // Enough time for A/D acquisition + ADCON0bits.GO_DONE = 1; // A/D Conversion bit set 1 (conversion in progress) + while (ADCON0bits.GO_DONE == 1); // Wait for conversion to finish + return ((ADRESH << 8) + ADRESL); // return combined 10 bits of conversion }
\ No newline at end of file @@ -1,4 +1,7 @@ #include "conf.h" +// Initialize ADC +void Adc_Init(void); + // Get ADC value unsigned int Adc_Read(unsigned int);
\ No newline at end of file |