aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Harrington <kb0iic@berzerkula.org>2019-07-31 23:15:39 -0500
committerWilliam Harrington <kb0iic@berzerkula.org>2019-07-31 23:15:39 -0500
commit6679708c8dc5c9fa119f692a97321fa1ec715f31 (patch)
tree2ba18dbd4407dfd350ccaa41d6a9e7b8282e7c8a
parentdc4ed0a6b74e1b787fe7efa2ba3eab9fb38dece6 (diff)
Add Adc_Init and redo Adc_Read. Rework comments.
-rw-r--r--adc.c33
-rw-r--r--adc.h3
2 files changed, 26 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
diff --git a/adc.h b/adc.h
index 39bc940..d214580 100644
--- a/adc.h
+++ b/adc.h
@@ -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