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
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "AnalogIn_Diff.h" 00018 00019 #if FSL_FEATURE_ADC_HAS_DIFF_MODE 00020 00021 AnalogIn_Diff::~AnalogIn_Diff() { } 00022 00023 AnalogIn_Diff::AnalogIn_Diff(int adc_ch) 00024 { 00025 const uint32_t temp[] = ADC_BASE_ADDRS; 00026 for (int i = 0; i < sizeof(temp) / sizeof(temp[0]); i++) 00027 adc_addrs[i] = temp[i]; 00028 00029 00030 instance=(adc_ch>>1)&1; 00031 chnNum=(adc_ch>>0)&1; 00032 00033 00034 CLOCK_SYS_EnableAdcClock(instance); 00035 00036 uint32_t bus_clock; 00037 CLOCK_SYS_GetFreq(kBusClock, &bus_clock); 00038 uint32_t clkdiv; 00039 for (clkdiv = 0; clkdiv < 4; clkdiv++) { 00040 if ((bus_clock >> clkdiv) <= MAX_FADC) 00041 break; 00042 } 00043 if (clkdiv == 4) { 00044 clkdiv = 0x3; //Set max div 00045 } 00046 00047 /* adc is enabled/triggered when reading. */ 00048 00049 ADC_HAL_Init(adc_addrs[instance]); 00050 ADC_HAL_SetClkSrcMode(adc_addrs[instance], kAdcClkSrcOfBusClk); 00051 ADC_HAL_SetClkDividerMode(adc_addrs[instance], (adc_clk_divider_mode_t)(clkdiv & 0x3)); 00052 ADC_HAL_SetRefVoltSrcMode(adc_addrs[instance], kAdcRefVoltSrcOfVref); 00053 ADC_HAL_SetResolutionMode(adc_addrs[instance], kAdcResolutionBitOfDiffModeAs16); 00054 ADC_HAL_SetContinuousConvCmd(adc_addrs[instance], false); 00055 ADC_HAL_SetHwTriggerCmd(adc_addrs[instance], false); /* sw trigger */ 00056 ADC_HAL_SetHwAverageCmd(adc_addrs[instance], false); 00057 //ADC_HAL_SetHwAverageMode(adc_addrs[instance], kAdcHwAverageCountOf4); 00058 ADC_HAL_SetChnMuxMode(adc_addrs[instance], kAdcChnMuxOfB); /* only B channels are avail */ 00059 00060 00061 } 00062 00063 00064 00065 00066 int16_t AnalogIn_Diff::read_raws16() // Returns a 16bit signed integer 00067 { 00068 /* sw trigger (SC1A) */ 00069 ADC_HAL_ConfigChn(adc_addrs[instance], 0, false, true, chnNum); 00070 while (!ADC_HAL_GetChnConvCompletedCmd(adc_addrs[instance], 0)); 00071 return ADC_HAL_GetChnConvValueRAW(adc_addrs[instance], 0); 00072 } 00073 00074 float AnalogIn_Diff::read() 00075 { 00076 int16_t value = read_raws16(); 00077 return (float)value * (1.0f / (float)0xFFFF); 00078 } 00079 00080 #endif // FSL_FEATURE_ADC_HAS_DIFF_MODE
Generated on Tue Jul 12 2022 14:09:18 by
1.7.2

ADC DIFF K64F