aboutsummaryrefslogtreecommitdiffstats
path: root/adc.c
diff options
context:
space:
mode:
Diffstat (limited to 'adc.c')
-rw-r--r--adc.c33
1 files changed, 23 insertions, 10 deletions
diff --git a/adc.c b/adc.c
index 81913ac..801fed0 100644
--- a/adc.c
+++ b/adc.c
@@ -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