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@2:ea5a4c22bd53, 2014-12-05 (annotated)
- Committer:
- fblanc
- Date:
- Fri Dec 05 14:06:38 2014 +0000
- Revision:
- 2:ea5a4c22bd53
- Parent:
- 1:7b36e4381d83
Compatible hal freescale K64F
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 2:ea5a4c22bd53 | 1 | /* mbed Microcontroller Library |
fblanc | 2:ea5a4c22bd53 | 2 | * Copyright (c) 2006-2013 ARM Limited |
fblanc | 2:ea5a4c22bd53 | 3 | * |
fblanc | 2:ea5a4c22bd53 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
fblanc | 2:ea5a4c22bd53 | 5 | * you may not use this file except in compliance with the License. |
fblanc | 2:ea5a4c22bd53 | 6 | * You may obtain a copy of the License at |
fblanc | 2:ea5a4c22bd53 | 7 | * |
fblanc | 2:ea5a4c22bd53 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
fblanc | 2:ea5a4c22bd53 | 9 | * |
fblanc | 2:ea5a4c22bd53 | 10 | * Unless required by applicable law or agreed to in writing, software |
fblanc | 2:ea5a4c22bd53 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
fblanc | 2:ea5a4c22bd53 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
fblanc | 2:ea5a4c22bd53 | 13 | * See the License for the specific language governing permissions and |
fblanc | 2:ea5a4c22bd53 | 14 | * limitations under the License. |
fblanc | 2:ea5a4c22bd53 | 15 | */ |
fblanc | 2:ea5a4c22bd53 | 16 | |
JimCarver | 0:0f6f4be28e21 | 17 | #include "AnalogIn_Diff.h" |
JimCarver | 0:0f6f4be28e21 | 18 | |
fblanc | 2:ea5a4c22bd53 | 19 | #if FSL_FEATURE_ADC_HAS_DIFF_MODE |
JimCarver | 0:0f6f4be28e21 | 20 | |
JimCarver | 0:0f6f4be28e21 | 21 | AnalogIn_Diff::~AnalogIn_Diff() { } |
JimCarver | 0:0f6f4be28e21 | 22 | |
fblanc | 2:ea5a4c22bd53 | 23 | AnalogIn_Diff::AnalogIn_Diff(int adc_ch) |
fblanc | 2:ea5a4c22bd53 | 24 | { |
fblanc | 2:ea5a4c22bd53 | 25 | const uint32_t temp[] = ADC_BASE_ADDRS; |
fblanc | 2:ea5a4c22bd53 | 26 | for (int i = 0; i < sizeof(temp) / sizeof(temp[0]); i++) |
fblanc | 2:ea5a4c22bd53 | 27 | adc_addrs[i] = temp[i]; |
fblanc | 2:ea5a4c22bd53 | 28 | |
fblanc | 2:ea5a4c22bd53 | 29 | |
fblanc | 2:ea5a4c22bd53 | 30 | instance=(adc_ch>>1)&1; |
fblanc | 2:ea5a4c22bd53 | 31 | chnNum=(adc_ch>>0)&1; |
fblanc | 2:ea5a4c22bd53 | 32 | |
fblanc | 2:ea5a4c22bd53 | 33 | |
fblanc | 2:ea5a4c22bd53 | 34 | CLOCK_SYS_EnableAdcClock(instance); |
fblanc | 2:ea5a4c22bd53 | 35 | |
fblanc | 2:ea5a4c22bd53 | 36 | uint32_t bus_clock; |
fblanc | 2:ea5a4c22bd53 | 37 | CLOCK_SYS_GetFreq(kBusClock, &bus_clock); |
fblanc | 2:ea5a4c22bd53 | 38 | uint32_t clkdiv; |
fblanc | 2:ea5a4c22bd53 | 39 | for (clkdiv = 0; clkdiv < 4; clkdiv++) { |
fblanc | 2:ea5a4c22bd53 | 40 | if ((bus_clock >> clkdiv) <= MAX_FADC) |
fblanc | 2:ea5a4c22bd53 | 41 | break; |
fblanc | 2:ea5a4c22bd53 | 42 | } |
fblanc | 2:ea5a4c22bd53 | 43 | if (clkdiv == 4) { |
fblanc | 2:ea5a4c22bd53 | 44 | clkdiv = 0x3; //Set max div |
fblanc | 2:ea5a4c22bd53 | 45 | } |
fblanc | 2:ea5a4c22bd53 | 46 | |
fblanc | 2:ea5a4c22bd53 | 47 | /* adc is enabled/triggered when reading. */ |
fblanc | 2:ea5a4c22bd53 | 48 | |
fblanc | 2:ea5a4c22bd53 | 49 | ADC_HAL_Init(adc_addrs[instance]); |
fblanc | 2:ea5a4c22bd53 | 50 | ADC_HAL_SetClkSrcMode(adc_addrs[instance], kAdcClkSrcOfBusClk); |
fblanc | 2:ea5a4c22bd53 | 51 | ADC_HAL_SetClkDividerMode(adc_addrs[instance], (adc_clk_divider_mode_t)(clkdiv & 0x3)); |
fblanc | 2:ea5a4c22bd53 | 52 | ADC_HAL_SetRefVoltSrcMode(adc_addrs[instance], kAdcRefVoltSrcOfVref); |
fblanc | 2:ea5a4c22bd53 | 53 | ADC_HAL_SetResolutionMode(adc_addrs[instance], kAdcResolutionBitOfDiffModeAs16); |
fblanc | 2:ea5a4c22bd53 | 54 | ADC_HAL_SetContinuousConvCmd(adc_addrs[instance], false); |
fblanc | 2:ea5a4c22bd53 | 55 | ADC_HAL_SetHwTriggerCmd(adc_addrs[instance], false); /* sw trigger */ |
fblanc | 2:ea5a4c22bd53 | 56 | ADC_HAL_SetHwAverageCmd(adc_addrs[instance], false); |
fblanc | 2:ea5a4c22bd53 | 57 | //ADC_HAL_SetHwAverageMode(adc_addrs[instance], kAdcHwAverageCountOf4); |
fblanc | 2:ea5a4c22bd53 | 58 | ADC_HAL_SetChnMuxMode(adc_addrs[instance], kAdcChnMuxOfB); /* only B channels are avail */ |
fblanc | 2:ea5a4c22bd53 | 59 | |
fblanc | 2:ea5a4c22bd53 | 60 | |
JimCarver | 0:0f6f4be28e21 | 61 | } |
JimCarver | 0:0f6f4be28e21 | 62 | |
fblanc | 2:ea5a4c22bd53 | 63 | |
fblanc | 2:ea5a4c22bd53 | 64 | |
fblanc | 2:ea5a4c22bd53 | 65 | |
fblanc | 2:ea5a4c22bd53 | 66 | int16_t AnalogIn_Diff::read_raws16() // Returns a 16bit signed integer |
fblanc | 2:ea5a4c22bd53 | 67 | { |
fblanc | 2:ea5a4c22bd53 | 68 | /* sw trigger (SC1A) */ |
fblanc | 2:ea5a4c22bd53 | 69 | ADC_HAL_ConfigChn(adc_addrs[instance], 0, false, true, chnNum); |
fblanc | 2:ea5a4c22bd53 | 70 | while (!ADC_HAL_GetChnConvCompletedCmd(adc_addrs[instance], 0)); |
fblanc | 2:ea5a4c22bd53 | 71 | return ADC_HAL_GetChnConvValueRAW(adc_addrs[instance], 0); |
JimCarver | 0:0f6f4be28e21 | 72 | } |
fblanc | 2:ea5a4c22bd53 | 73 | |
fblanc | 2:ea5a4c22bd53 | 74 | float AnalogIn_Diff::read() |
fblanc | 2:ea5a4c22bd53 | 75 | { |
fblanc | 2:ea5a4c22bd53 | 76 | int16_t value = read_raws16(); |
fblanc | 2:ea5a4c22bd53 | 77 | return (float)value * (1.0f / (float)0xFFFF); |
fblanc | 2:ea5a4c22bd53 | 78 | } |
fblanc | 2:ea5a4c22bd53 | 79 | |
fblanc | 2:ea5a4c22bd53 | 80 | #endif // FSL_FEATURE_ADC_HAS_DIFF_MODE |