Marlon Fulla / Mbed 2 deprecated 2014FEB4_Vibrometro_Xbee

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers analogin_api.c Source File

analogin_api.c

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 #include "analogin_api.h"
00017 
00018 #include "cmsis.h"
00019 #include "pinmap.h"
00020 #include "error.h"
00021 
00022 static const PinMap PinMap_ADC[] = {
00023     {PTE20, ADC0_SE0,  0},
00024     {PTE22, ADC0_SE3,  0},
00025     {PTE29, ADC0_SE4b, 0},
00026     {PTE30, ADC0_SE23, 0},
00027     {PTB0,  ADC0_SE8,  0},
00028     {PTB1,  ADC0_SE9,  0},
00029     {PTB2,  ADC0_SE12, 0},
00030     {PTB3,  ADC0_SE13, 0},
00031     {PTC0,  ADC0_SE14, 0},
00032     {PTC1,  ADC0_SE15, 0},
00033     {PTC2,  ADC0_SE11, 0},
00034     {PTD1,  ADC0_SE5b, 0},
00035     {PTD5,  ADC0_SE6b, 0},
00036     {PTD6,  ADC0_SE7b, 0},
00037     {NC,    NC,        0}
00038 };
00039 
00040 void analogin_init(analogin_t *obj, PinName pin) {
00041     obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
00042     if (obj->adc == (uint32_t)NC) {
00043         error("ADC pin mapping failed");
00044     }
00045 
00046     SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;
00047 
00048     uint32_t port = (uint32_t)pin >> PORT_SHIFT;
00049     SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
00050 
00051     ADC0->SC1[1] = ADC_SC1_ADCH(obj->adc);
00052 
00053     ADC0->CFG1 = ADC_CFG1_ADIV(0)       // Clock Divide Select: (Input Clock)
00054                | ADC_CFG1_ADLSMP_MASK   // Long Sample Time
00055                | ADC_CFG1_MODE(3)       // (16)bits Resolution
00056                | ADC_CFG1_ADICLK(0);    // Input Clock: (Bus Clock)
00057 
00058     ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK   // ADxxb channels are selected
00059                | ADC_CFG2_ADACKEN_MASK  // Asynchronous Clock Output Enable
00060                | ADC_CFG2_ADHSC_MASK    // High-Speed Configuration
00061                | ADC_CFG2_ADLSTS(3);    // Short Sample Time Select
00062 
00063     ADC0->SC2 = ADC_SC2_REFSEL(0);      // Default Voltage Reference
00064 
00065     pinmap_pinout(pin, PinMap_ADC);
00066 }
00067 
00068 uint16_t analogin_read_u16(analogin_t *obj) {
00069     // start conversion
00070     ADC0->SC1[0] = ADC_SC1_ADCH(obj->adc);
00071 
00072     // Wait Conversion Complete
00073     while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK);
00074 
00075     // Return value
00076     return (uint16_t)ADC0->R[0];
00077 }
00078 
00079 float analogin_read(analogin_t *obj) {
00080     uint16_t value = analogin_read_u16(obj);
00081     return (float)value * (1.0f / (float)0xFFFF);
00082 }
00083