
Skeleton program for Federico's 4YP project.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of IoT_Ex by
source/ADC.cpp@5:0c7d131e6089, 2016-10-06 (annotated)
- Committer:
- defrost
- Date:
- Thu Oct 06 07:58:31 2016 +0000
- Revision:
- 5:0c7d131e6089
- Parent:
- 3:f20e114eb2ee
- Child:
- 6:424e225d2a91
- Added comments, cleaned up code
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 | 1:4403f2ed1c1f | 43 | |
defrost | 1:4403f2ed1c1f | 44 | |
defrost | 1:4403f2ed1c1f | 45 | INFO("ADC configuration complete!"); |
defrost | 5:0c7d131e6089 | 46 | DBG("ADC Registers:"); |
defrost | 5:0c7d131e6089 | 47 | DBG("The SR Register reads: %d", ADC1->SR); |
defrost | 5:0c7d131e6089 | 48 | DBG("The CR1 Register reads: %d", ADC1->CR1); |
defrost | 5:0c7d131e6089 | 49 | DBG("The CR2 Register reads: %d", ADC1->CR2); |
defrost | 5:0c7d131e6089 | 50 | DBG("The JSQR Register reads: %d", ADC1->JSQR); |
defrost | 1:4403f2ed1c1f | 51 | |
defrost | 1:4403f2ed1c1f | 52 | return; |
defrost | 1:4403f2ed1c1f | 53 | } |