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.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of BatteryModelTester by
source/ADC.cpp@6:424e225d2a91, 2016-10-06 (annotated)
- Committer:
- defrost
- Date:
- Thu Oct 06 08:04:34 2016 +0000
- Revision:
- 6:424e225d2a91
- Parent:
- 5:0c7d131e6089
- Updated comments
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| defrost | 6:424e225d2a91 | 1 | /** |
| defrost | 6:424e225d2a91 | 2 | * @author Damien Frost |
| defrost | 6:424e225d2a91 | 3 | * |
| defrost | 6:424e225d2a91 | 4 | * @section LICENSE |
| defrost | 6:424e225d2a91 | 5 | * |
| defrost | 6:424e225d2a91 | 6 | * Copyright (c) 2016 Damien Frost |
| defrost | 6:424e225d2a91 | 7 | * |
| defrost | 6:424e225d2a91 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| defrost | 6:424e225d2a91 | 9 | * of this software and associated documentation files (the "Software"), to deal |
| defrost | 6:424e225d2a91 | 10 | * in the Software without restriction, including without limitation the rights |
| defrost | 6:424e225d2a91 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| defrost | 6:424e225d2a91 | 12 | * copies of the Software, and to permit persons to whom the Software is |
| defrost | 6:424e225d2a91 | 13 | * furnished to do so, subject to the following conditions: |
| defrost | 6:424e225d2a91 | 14 | * |
| defrost | 6:424e225d2a91 | 15 | * The above copyright notice and this permission notice shall be included in |
| defrost | 6:424e225d2a91 | 16 | * all copies or substantial portions of the Software. |
| defrost | 6:424e225d2a91 | 17 | * |
| defrost | 6:424e225d2a91 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| defrost | 6:424e225d2a91 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| defrost | 6:424e225d2a91 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| defrost | 6:424e225d2a91 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| defrost | 6:424e225d2a91 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| defrost | 6:424e225d2a91 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| defrost | 6:424e225d2a91 | 24 | * THE SOFTWARE. |
| defrost | 6:424e225d2a91 | 25 | * |
| defrost | 6:424e225d2a91 | 26 | * @file "ADC.cpp" |
| defrost | 6:424e225d2a91 | 27 | * |
| defrost | 6:424e225d2a91 | 28 | * @section DESCRIPTION |
| defrost | 6:424e225d2a91 | 29 | * Configuration of the ADC for the IoT example. |
| defrost | 6:424e225d2a91 | 30 | * |
| defrost | 6:424e225d2a91 | 31 | */ |
| defrost | 1:4403f2ed1c1f | 32 | |
| defrost | 1:4403f2ed1c1f | 33 | #include "ADC.h" |
| defrost | 1:4403f2ed1c1f | 34 | #include "globals.h" |
| defrost | 1:4403f2ed1c1f | 35 | #include "mbed.h" |
| defrost | 1:4403f2ed1c1f | 36 | |
| defrost | 1:4403f2ed1c1f | 37 | |
| defrost | 1:4403f2ed1c1f | 38 | //#define DEBUG |
| defrost | 1:4403f2ed1c1f | 39 | #define INFOMESSAGES |
| defrost | 1:4403f2ed1c1f | 40 | #define WARNMESSAGES |
| defrost | 1:4403f2ed1c1f | 41 | #define ERRMESSAGES |
| defrost | 1:4403f2ed1c1f | 42 | #define FUNCNAME "ADC" |
| defrost | 1:4403f2ed1c1f | 43 | #include "messages.h" |
| defrost | 1:4403f2ed1c1f | 44 | |
| defrost | 1:4403f2ed1c1f | 45 | |
| defrost | 1:4403f2ed1c1f | 46 | void ConfigureADC(void){ |
| defrost | 1:4403f2ed1c1f | 47 | |
| defrost | 1:4403f2ed1c1f | 48 | unsigned int value; |
| defrost | 1:4403f2ed1c1f | 49 | |
| defrost | 1:4403f2ed1c1f | 50 | // ensure power is turned on |
| defrost | 1:4403f2ed1c1f | 51 | // Grabbed from lines 54-57 of analogin_api.c |
| defrost | 1:4403f2ed1c1f | 52 | // This turns on the clock to Ports A, B, and C |
| defrost | 1:4403f2ed1c1f | 53 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN; |
| defrost | 1:4403f2ed1c1f | 54 | // This turns on the clock to the ADC: |
| defrost | 1:4403f2ed1c1f | 55 | RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; |
| defrost | 1:4403f2ed1c1f | 56 | |
| defrost | 1:4403f2ed1c1f | 57 | |
| defrost | 1:4403f2ed1c1f | 58 | // Turn on the ADC: |
| defrost | 1:4403f2ed1c1f | 59 | value = ADC_CR2_ADON; |
| defrost | 1:4403f2ed1c1f | 60 | ADC1->CR2 = value; |
| defrost | 1:4403f2ed1c1f | 61 | wait_us(100); |
| defrost | 1:4403f2ed1c1f | 62 | |
| defrost | 2:7abdaa5a9209 | 63 | // Set the EOC flag at the end of every regular conversion: |
| defrost | 2:7abdaa5a9209 | 64 | ADC1->CR2 |= ADC_CR2_EOCS; |
| defrost | 2:7abdaa5a9209 | 65 | |
| defrost | 1:4403f2ed1c1f | 66 | // Turn on the internal temperature sensor: |
| defrost | 1:4403f2ed1c1f | 67 | ADC->CCR |= ADC_CCR_TSVREFE; |
| defrost | 1:4403f2ed1c1f | 68 | |
| defrost | 2:7abdaa5a9209 | 69 | // Set the first (and only channel) to convert to CH16, the internal temperature sensor: |
| defrost | 2:7abdaa5a9209 | 70 | ADC1->SQR3 |= ADC_SQR3_SQ1_4; |
| defrost | 1:4403f2ed1c1f | 71 | |
| defrost | 1:4403f2ed1c1f | 72 | // Set the sample numbers (making this bigger samples more slowly): |
| defrost | 2:7abdaa5a9209 | 73 | ADC1->SMPR2 = ADC_SMPR1_SMP16_1 | ADC_SMPR1_SMP16_2; // Set for 144 ADC clock cycles |
| defrost | 1:4403f2ed1c1f | 74 | |
| defrost | 1:4403f2ed1c1f | 75 | |
| defrost | 1:4403f2ed1c1f | 76 | INFO("ADC configuration complete!"); |
| defrost | 5:0c7d131e6089 | 77 | DBG("ADC Registers:"); |
| defrost | 5:0c7d131e6089 | 78 | DBG("The SR Register reads: %d", ADC1->SR); |
| defrost | 5:0c7d131e6089 | 79 | DBG("The CR1 Register reads: %d", ADC1->CR1); |
| defrost | 5:0c7d131e6089 | 80 | DBG("The CR2 Register reads: %d", ADC1->CR2); |
| defrost | 5:0c7d131e6089 | 81 | DBG("The JSQR Register reads: %d", ADC1->JSQR); |
| defrost | 1:4403f2ed1c1f | 82 | |
| defrost | 1:4403f2ed1c1f | 83 | return; |
| defrost | 1:4403f2ed1c1f | 84 | } |
