A basic Library for the 16 bit Differential A/D in the K64F Freedom platform

Dependents:   K64F_DIFF_A2D

This Library cn use either A2D0 or 1. Note that when using A2D0 you must use pins ADC0_DP1 and ADC0_DM1. For A2D1 you must use pins ADC1_DP1 and ADC1_DM1.

Revision:
1:7b36e4381d83
Parent:
0:0f6f4be28e21
diff -r 0f6f4be28e21 -r 7b36e4381d83 AnalohIn_Diff.cpp
--- a/AnalohIn_Diff.cpp	Tue May 20 00:58:33 2014 +0000
+++ b/AnalohIn_Diff.cpp	Thu May 22 17:40:21 2014 +0000
@@ -2,30 +2,29 @@
 #include "AnalogIn_Diff.h"
 
 
-AnalogIn_Diff::AnalogIn_Diff(int channel) : ch(channel) {
-    if(ch) BW_SIM_SCGC3_ADC1(1);
+AnalogIn_Diff::AnalogIn_Diff(int a2d_number) : ch(a2d_number) {
+    if(ch) BW_SIM_SCGC3_ADC1(1); // Turn on clock as needed
     else BW_SIM_SCGC6_ADC0(1);
-    BW_ADC_SC1n_ADCH(ch, 0, 0x01);
-    BW_ADC_SC1n_DIFF(ch, 0, 1);
-    BW_ADC_CFG1_ADICLK(ch, 0);
-    BW_ADC_CFG1_MODE(ch, 3);
-    BW_ADC_CFG1_ADLSMP(ch, 0); // 
-    BW_ADC_CFG1_ADIV(ch, 3);
-    BW_ADC_CFG1_ADLPC(ch, 0);
+    BW_ADC_SC1n_DIFF(ch, 0, 1);     //  Differential Mode
+    BW_ADC_CFG1_ADICLK(ch, 0);      //  Bus Clock
+    BW_ADC_CFG1_MODE(ch, 3);        //  16Bit differential mode
+    BW_ADC_CFG1_ADLSMP(ch, 0);      //  Short Sample Window 
+    BW_ADC_CFG1_ADIV(ch, 3);        //  Clock / 8
+    BW_ADC_CFG1_ADLPC(ch, 0);       //  Normal Power Mode
 }
 
 AnalogIn_Diff::~AnalogIn_Diff() { }
 
-int16_t AnalogIn_Diff::read_16(void) {
-    BW_ADC_SC1n_ADCH(ch, 0, 0x01);
-    while(!BR_ADC_SC1n_COCO(ch, 0));
-    return(BR_ADC_Rn_D(ch, 0));
+int16_t AnalogIn_Diff::read_16(int channel) {  // Returns a 16bit signed integer
+    BW_ADC_SC1n_ADCH(ch, 0, channel);      //  Trigger Conversion
+    while(!BR_ADC_SC1n_COCO(ch, 0));    //  Wait for conversion to finish
+    return(BR_ADC_Rn_D(ch, 0));         //  Return the result
 }
 
-float AnalogIn_Diff::read(void) {
+float AnalogIn_Diff::read(int channel) {
     int16_t i;
     float t;
-    BW_ADC_SC1n_ADCH(ch, 0, 0x01);
+    BW_ADC_SC1n_ADCH(ch, 0, channel);
     while(!BR_ADC_SC1n_COCO(ch, 0));
     i =  BR_ADC_Rn_D(ch, 0);
     t = ((float) i);