22#include <device.h>
33#include <pwm.h>
44#include <sys_clock.h>
5+ #include <misc/byteorder.h>
6+ #include <adc.h>
57
68#if defined(CONFIG_STDOUT_CONSOLE )
79#include <stdio.h>
1517#define IO5_GREEN "PWM_1"
1618#define IO6_BLUE "PWM_2"
1719
20+ #define ADC_DEVICE_NAME "ADC_0"
21+
22+ /*
23+ * The analog input pin and channel number mapping
24+ * for Arduino 101 board.
25+ * A0 Channel 10
26+ * A1 Channel 11
27+ * A2 Channel 12
28+ * A3 Channel 13
29+ * A4 Channel 14
30+ */
31+ #define A0 10
32+ #define BUFFER_SIZE 40
33+
34+ static uint8_t buffer [BUFFER_SIZE ];
35+
36+ static struct adc_seq_entry sample = {
37+ .sampling_delay = 12 ,
38+ .channel_id = A0 ,
39+ .buffer = buffer ,
40+ .buffer_length = BUFFER_SIZE ,
41+ };
42+
43+ static struct adc_seq_table table = {
44+ .entries = & sample ,
45+ .num_entries = 1 ,
46+ };
47+
1848#define SLEEPTICKS SECONDS(1)
1949
2050void main (void )
2151{
2252 struct nano_timer timer ;
23- void * timer_data [1 ];
24-
25- struct device * red , * green , * blue ;
53+ uint32_t data [2 ] = {0 , 0 };
2654
27- nano_timer_init ( & timer , timer_data ) ;
55+ struct device * red , * green , * blue , * tmp36 ;
2856
2957 PRINT ("Zephyr WebBluetooth demo\n" );
3058
@@ -33,13 +61,34 @@ void main(void)
3361 blue = device_get_binding (IO6_BLUE );
3462
3563 if (!red || !green || !blue ) {
36- PRINT ("Cannot find LED connected to pin 3 (red), 5 (green) and 6 (blue)! \n" );
64+ PRINT ("Cannot find LED connected to pin 3 (red), 5 (green) and 6 (blue). \n" );
3765 }
3866
67+ tmp36 = device_get_binding (ADC_DEVICE_NAME );
68+ if (!tmp36 ) {
69+ PRINT ("Cannot find the TMP36 connected to pin A0.\n" );
70+ }
71+
72+ nano_timer_init (& timer , data );
73+ adc_enable (tmp36 );
74+
3975 while (1 ) {
4076 pwm_pin_set_values (red , 1024 , SLEEPTICKS , 0 );
4177
78+ if (adc_read (tmp36 , & table ) == 0 ) {
79+ uint32_t length = BUFFER_SIZE ;
80+ uint8_t * buf = buffer ;
81+ for (; length > 0 ; length -= 4 , buf += 4 ) {
82+ uint32_t rawValue = * ((uint32_t * ) buf );
83+ float voltage = (rawValue / 1024.0 ) * 5.0 ;
84+ float celsius = (voltage - 0.5 ) * 100 ;
85+ PRINT ("Celsius %f\n" , celsius );
86+ }
87+ }
88+
4289 nano_timer_start (& timer , SLEEPTICKS );
4390 nano_timer_test (& timer , TICKS_UNLIMITED );
4491 }
92+
93+ adc_disable (tmp36 );
4594}
0 commit comments