Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of AnalogIn_Diff by
AnalohIn_Diff.cpp@1:7b36e4381d83, 2014-05-22 (annotated)
- Committer:
- JimCarver
- Date:
- Thu May 22 17:40:21 2014 +0000
- Revision:
- 1:7b36e4381d83
- Parent:
- 0:0f6f4be28e21
- Child:
- 2:ea5a4c22bd53
Now supports either Channel 0 or 1 for both ADC0 & ADC1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| JimCarver | 0:0f6f4be28e21 | 1 | #include "mbed.h" |
| JimCarver | 0:0f6f4be28e21 | 2 | #include "AnalogIn_Diff.h" |
| JimCarver | 0:0f6f4be28e21 | 3 | |
| JimCarver | 0:0f6f4be28e21 | 4 | |
| JimCarver | 1:7b36e4381d83 | 5 | AnalogIn_Diff::AnalogIn_Diff(int a2d_number) : ch(a2d_number) { |
| JimCarver | 1:7b36e4381d83 | 6 | if(ch) BW_SIM_SCGC3_ADC1(1); // Turn on clock as needed |
| JimCarver | 0:0f6f4be28e21 | 7 | else BW_SIM_SCGC6_ADC0(1); |
| JimCarver | 1:7b36e4381d83 | 8 | BW_ADC_SC1n_DIFF(ch, 0, 1); // Differential Mode |
| JimCarver | 1:7b36e4381d83 | 9 | BW_ADC_CFG1_ADICLK(ch, 0); // Bus Clock |
| JimCarver | 1:7b36e4381d83 | 10 | BW_ADC_CFG1_MODE(ch, 3); // 16Bit differential mode |
| JimCarver | 1:7b36e4381d83 | 11 | BW_ADC_CFG1_ADLSMP(ch, 0); // Short Sample Window |
| JimCarver | 1:7b36e4381d83 | 12 | BW_ADC_CFG1_ADIV(ch, 3); // Clock / 8 |
| JimCarver | 1:7b36e4381d83 | 13 | BW_ADC_CFG1_ADLPC(ch, 0); // Normal Power Mode |
| JimCarver | 0:0f6f4be28e21 | 14 | } |
| JimCarver | 0:0f6f4be28e21 | 15 | |
| JimCarver | 0:0f6f4be28e21 | 16 | AnalogIn_Diff::~AnalogIn_Diff() { } |
| JimCarver | 0:0f6f4be28e21 | 17 | |
| JimCarver | 1:7b36e4381d83 | 18 | int16_t AnalogIn_Diff::read_16(int channel) { // Returns a 16bit signed integer |
| JimCarver | 1:7b36e4381d83 | 19 | BW_ADC_SC1n_ADCH(ch, 0, channel); // Trigger Conversion |
| JimCarver | 1:7b36e4381d83 | 20 | while(!BR_ADC_SC1n_COCO(ch, 0)); // Wait for conversion to finish |
| JimCarver | 1:7b36e4381d83 | 21 | return(BR_ADC_Rn_D(ch, 0)); // Return the result |
| JimCarver | 0:0f6f4be28e21 | 22 | } |
| JimCarver | 0:0f6f4be28e21 | 23 | |
| JimCarver | 1:7b36e4381d83 | 24 | float AnalogIn_Diff::read(int channel) { |
| JimCarver | 0:0f6f4be28e21 | 25 | int16_t i; |
| JimCarver | 0:0f6f4be28e21 | 26 | float t; |
| JimCarver | 1:7b36e4381d83 | 27 | BW_ADC_SC1n_ADCH(ch, 0, channel); |
| JimCarver | 0:0f6f4be28e21 | 28 | while(!BR_ADC_SC1n_COCO(ch, 0)); |
| JimCarver | 0:0f6f4be28e21 | 29 | i = BR_ADC_Rn_D(ch, 0); |
| JimCarver | 0:0f6f4be28e21 | 30 | t = ((float) i); |
| JimCarver | 0:0f6f4be28e21 | 31 | t = t / 32768.0f; |
| JimCarver | 0:0f6f4be28e21 | 32 | return(t); |
| JimCarver | 0:0f6f4be28e21 | 33 | } |

ADC DIFF K64F