Example program to create IoT devices for a local network, which connect to a local server.
Dependencies: WebSocketClient WiflyInterface mbed messages
This code is used in the second part of my Internet of Things (IoT) blog post available here. The code is fairly simple, but its real value is in its reliability. I have worked hard to try to make the wireless connection as reliable, and as fast, as possible. There are a few lines of code that must be modified before it will work correctly, and those are described in the following Wiki pages.
It is designed to work with a Python WebSocket Server running on a PC, the source code of which is available here.
Once operating with the server, each microcontroller, or IoT device, will broadcast a counter and its internal temperature to your WebSocket Server.
source/ADC.cpp@1:4403f2ed1c1f, 2016-10-04 (annotated)
- Committer:
- defrost
- Date:
- Tue Oct 04 13:19:19 2016 +0000
- Revision:
- 1:4403f2ed1c1f
- Child:
- 2:7abdaa5a9209
- Temperature sensor with injected channel conversion
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 | 1:4403f2ed1c1f | 32 | // Turn on the internal temperature sensor: |
defrost | 1:4403f2ed1c1f | 33 | ADC->CCR |= ADC_CCR_TSVREFE; |
defrost | 1:4403f2ed1c1f | 34 | |
defrost | 1:4403f2ed1c1f | 35 | // *** Control Register 1: CR1 *** |
defrost | 1:4403f2ed1c1f | 36 | value = 0; |
defrost | 1:4403f2ed1c1f | 37 | // [8] Set the SCAN Mode bit, to convert all registers when trigered: |
defrost | 1:4403f2ed1c1f | 38 | value |= ADC_CR1_SCAN; |
defrost | 1:4403f2ed1c1f | 39 | // [5] Set the JEOCIE bit, to trigger an interrupt when conversion ends. |
defrost | 1:4403f2ed1c1f | 40 | value |= ADC_CR1_JEOCIE; |
defrost | 1:4403f2ed1c1f | 41 | // Set the register: |
defrost | 1:4403f2ed1c1f | 42 | ADC1->CR1 = value; |
defrost | 1:4403f2ed1c1f | 43 | |
defrost | 1:4403f2ed1c1f | 44 | // *** Control Register 2: CR2 *** |
defrost | 1:4403f2ed1c1f | 45 | value = 0; |
defrost | 1:4403f2ed1c1f | 46 | // [21:20] Set to 1, External trigger of injected channels triggered by positive edge. |
defrost | 1:4403f2ed1c1f | 47 | value |= ADC_CR2_JEXTEN_0; |
defrost | 1:4403f2ed1c1f | 48 | // Select the TIM1 TRGO and the external trigger: |
defrost | 1:4403f2ed1c1f | 49 | value |= ADC_CR2_JEXTSEL_0; |
defrost | 1:4403f2ed1c1f | 50 | // Set the register: |
defrost | 1:4403f2ed1c1f | 51 | ADC1->CR2 |= value; |
defrost | 1:4403f2ed1c1f | 52 | |
defrost | 1:4403f2ed1c1f | 53 | // *** ADC injected sequence register: JSQR *** |
defrost | 1:4403f2ed1c1f | 54 | value = 0; |
defrost | 1:4403f2ed1c1f | 55 | // [21:20] JL bits, set to 0 for 1 total conversions to take place |
defrost | 1:4403f2ed1c1f | 56 | // |
defrost | 1:4403f2ed1c1f | 57 | // [19:15] Convert CH16 first: |
defrost | 1:4403f2ed1c1f | 58 | value |= ADC_JSQR_JSQ1_4; |
defrost | 1:4403f2ed1c1f | 59 | // Save the register: |
defrost | 1:4403f2ed1c1f | 60 | ADC1->JSQR = value; |
defrost | 1:4403f2ed1c1f | 61 | |
defrost | 1:4403f2ed1c1f | 62 | // Set the sample numbers (making this bigger samples more slowly): |
defrost | 1:4403f2ed1c1f | 63 | ADC1->SMPR2 = ADC_SMPR2_SMP1_1 | ADC_SMPR2_SMP1_2; |
defrost | 1:4403f2ed1c1f | 64 | |
defrost | 1:4403f2ed1c1f | 65 | |
defrost | 1:4403f2ed1c1f | 66 | INFO("ADC configuration complete!"); |
defrost | 1:4403f2ed1c1f | 67 | DBG("ADC Registers:\n\r"); |
defrost | 1:4403f2ed1c1f | 68 | DBG("The SR Register reads: %d\n\r", ADC1->SR); |
defrost | 1:4403f2ed1c1f | 69 | DBG("The CR1 Register reads: %d\n\r", ADC1->CR1); |
defrost | 1:4403f2ed1c1f | 70 | DBG("The CR2 Register reads: %d\n\r", ADC1->CR2); |
defrost | 1:4403f2ed1c1f | 71 | DBG("The JSQR Register reads: %d\n\r", ADC1->JSQR); |
defrost | 1:4403f2ed1c1f | 72 | |
defrost | 1:4403f2ed1c1f | 73 | return; |
defrost | 1:4403f2ed1c1f | 74 | } |