11#include <zephyr.h>
22#include <device.h>
3- #include <pwm.h>
43#include <sys_clock.h>
54#include <misc/byteorder.h>
65#include <adc.h>
1312#define PRINT printk
1413#endif
1514
16- #define IO3_RED "PWM_0"
17- #define IO5_GREEN "PWM_1"
18- #define IO6_BLUE "PWM_2"
15+ #define SLEEPTICKS SECONDS(1)
1916
2017#define ADC_DEVICE_NAME "ADC_0"
2118
2926 * A4 Channel 14
3027 */
3128#define A0 10
32- #define BUFFER_SIZE 40
29+ #define BUFFER_SIZE 4
3330
3431static uint8_t buffer [BUFFER_SIZE ];
3532
@@ -45,46 +42,46 @@ static struct adc_seq_table table = {
4542 .num_entries = 1 ,
4643};
4744
48- #define SLEEPTICKS SECONDS(1)
45+ uint8_t tmp36_read (void )
46+ {
47+ uint8_t * buf = buffer ;
48+
49+ // ADC only works on ARC: https://www.zephyrproject.org/doc/board/arduino_101.html
50+ // below two lines is testing code:
51+ uint32_t * set = (uint32_t * ) buf ;
52+ set [0 ] = 150 ;
53+ //if (adc_read(tmp36, &table) == 0) {
54+ uint32_t length = BUFFER_SIZE ;
55+ for (; length > 0 ; length -= 4 , buf += 4 ) {
56+ uint32_t rawValue = * ((uint32_t * ) buf );
57+ printf ("raw temperature value %d\n\n\n" , rawValue );
58+ float voltage = (rawValue / 1024.0 ) * 5.0 ;
59+ float celsius = (voltage - 0.5 ) * 100 ;
60+ return (uint8_t ) celsius + 0.5 ;
61+ }
62+ //}
63+ return 20 ;
64+ }
4965
5066void main (void )
5167{
5268 struct nano_timer timer ;
5369 uint32_t data [2 ] = {0 , 0 };
5470
55- struct device * red , * green , * blue , * tmp36 ;
71+ struct device * tmp36 ;
5672
5773 PRINT ("Zephyr WebBluetooth demo\n" );
5874
59- red = device_get_binding (IO3_RED );
60- green = device_get_binding (IO5_GREEN );
61- blue = device_get_binding (IO6_BLUE );
62-
63- if (!red || !green || !blue ) {
64- PRINT ("Cannot find LED connected to pin 3 (red), 5 (green) and 6 (blue).\n" );
65- }
66-
67- tmp36 = device_get_binding (ADC_DEVICE_NAME );
68- if (!tmp36 ) {
69- PRINT ("Cannot find the TMP36 connected to pin A0.\n" );
75+ if ((tmp36 = device_get_binding (ADC_DEVICE_NAME )) == NULL ) {
76+ printk ("device_get_binding: failed for ADC\n" );
77+ printk ("Temperature (celsius): %d\n\n\n" , tmp36_read ());
7078 }
7179
7280 nano_timer_init (& timer , data );
7381 adc_enable (tmp36 );
7482
7583 while (1 ) {
76- pwm_pin_set_values (red , 1024 , SLEEPTICKS , 0 );
77-
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- }
84+ tmp36_read ();
8885
8986 nano_timer_start (& timer , SLEEPTICKS );
9087 nano_timer_test (& timer , TICKS_UNLIMITED );
0 commit comments