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@2:7abdaa5a9209, 2016-10-04 (annotated)
- Committer:
- defrost
- Date:
- Tue Oct 04 13:59:13 2016 +0000
- Revision:
- 2:7abdaa5a9209
- Parent:
- 1:4403f2ed1c1f
- Child:
- 3:f20e114eb2ee
- Internal temperature sensing works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
defrost | 1:4403f2ed1c1f | 1 | |
defrost | 1:4403f2ed1c1f | 2 | #include "ADC.h" |
defrost | 1:4403f2ed1c1f | 3 | #include "globals.h" |
defrost | 1:4403f2ed1c1f | 4 | #include "mbed.h" |
defrost | 1:4403f2ed1c1f | 5 | |
defrost | 1:4403f2ed1c1f | 6 | |
defrost | 1:4403f2ed1c1f | 7 | //#define DEBUG |
defrost | 1:4403f2ed1c1f | 8 | #define INFOMESSAGES |
defrost | 1:4403f2ed1c1f | 9 | #define WARNMESSAGES |
defrost | 1:4403f2ed1c1f | 10 | #define ERRMESSAGES |
defrost | 1:4403f2ed1c1f | 11 | #define FUNCNAME "ADC" |
defrost | 1:4403f2ed1c1f | 12 | #include "messages.h" |
defrost | 1:4403f2ed1c1f | 13 | |
defrost | 1:4403f2ed1c1f | 14 | |
defrost | 1:4403f2ed1c1f | 15 | void ConfigureADC(void){ |
defrost | 1:4403f2ed1c1f | 16 | |
defrost | 1:4403f2ed1c1f | 17 | unsigned int value; |
defrost | 1:4403f2ed1c1f | 18 | |
defrost | 1:4403f2ed1c1f | 19 | // ensure power is turned on |
defrost | 1:4403f2ed1c1f | 20 | // Grabbed from lines 54-57 of analogin_api.c |
defrost | 1:4403f2ed1c1f | 21 | // This turns on the clock to Ports A, B, and C |
defrost | 1:4403f2ed1c1f | 22 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN; |
defrost | 1:4403f2ed1c1f | 23 | // This turns on the clock to the ADC: |
defrost | 1:4403f2ed1c1f | 24 | RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; |
defrost | 1:4403f2ed1c1f | 25 | |
defrost | 1:4403f2ed1c1f | 26 | |
defrost | 1:4403f2ed1c1f | 27 | // Turn on the ADC: |
defrost | 1:4403f2ed1c1f | 28 | value = ADC_CR2_ADON; |
defrost | 1:4403f2ed1c1f | 29 | ADC1->CR2 = value; |
defrost | 1:4403f2ed1c1f | 30 | wait_us(100); |
defrost | 1:4403f2ed1c1f | 31 | |
defrost | 2:7abdaa5a9209 | 32 | // Set the EOC flag at the end of every regular conversion: |
defrost | 2:7abdaa5a9209 | 33 | ADC1->CR2 |= ADC_CR2_EOCS; |
defrost | 2:7abdaa5a9209 | 34 | |
defrost | 1:4403f2ed1c1f | 35 | // Turn on the internal temperature sensor: |
defrost | 1:4403f2ed1c1f | 36 | ADC->CCR |= ADC_CCR_TSVREFE; |
defrost | 1:4403f2ed1c1f | 37 | |
defrost | 2:7abdaa5a9209 | 38 | // Set the first (and only channel) to convert to CH16, the internal temperature sensor: |
defrost | 2:7abdaa5a9209 | 39 | ADC1->SQR3 |= ADC_SQR3_SQ1_4; |
defrost | 1:4403f2ed1c1f | 40 | |
defrost | 1:4403f2ed1c1f | 41 | // Set the sample numbers (making this bigger samples more slowly): |
defrost | 2:7abdaa5a9209 | 42 | ADC1->SMPR2 = ADC_SMPR1_SMP16_1 | ADC_SMPR1_SMP16_2; // Set for 144 ADC clock cycles |
defrost | 2:7abdaa5a9209 | 43 | ADC1->SMPR2 = ADC_SMPR1_SMP18_1 | ADC_SMPR1_SMP18_2; // Set for 144 ADC clock cycles |
defrost | 1:4403f2ed1c1f | 44 | |
defrost | 1:4403f2ed1c1f | 45 | |
defrost | 1:4403f2ed1c1f | 46 | INFO("ADC configuration complete!"); |
defrost | 1:4403f2ed1c1f | 47 | DBG("ADC Registers:\n\r"); |
defrost | 1:4403f2ed1c1f | 48 | DBG("The SR Register reads: %d\n\r", ADC1->SR); |
defrost | 1:4403f2ed1c1f | 49 | DBG("The CR1 Register reads: %d\n\r", ADC1->CR1); |
defrost | 1:4403f2ed1c1f | 50 | DBG("The CR2 Register reads: %d\n\r", ADC1->CR2); |
defrost | 1:4403f2ed1c1f | 51 | DBG("The JSQR Register reads: %d\n\r", ADC1->JSQR); |
defrost | 1:4403f2ed1c1f | 52 | |
defrost | 1:4403f2ed1c1f | 53 | return; |
defrost | 1:4403f2ed1c1f | 54 | } |