Advertisement
Guest User

Raspberry Pi picoでSPI接続の気温センサーADT7310を使う

a guest
Feb 8th, 2021
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. 以下、CMakeLists.txt
  2.  
  3.  
  4.  
  5. cmake_minimum_required(VERSION 3.12)
  6.  
  7. # Pull in PICO SDK (must be before project)
  8. include(/d/pico-sdk/pico/pico-sdk/external/pico_sdk_import.cmake)
  9.  
  10. project(test_adt7310_usb01_project)
  11. set(CMAKE_C_STANDARD 11)
  12. set(CMAKE_CXX_STANDARD 17)
  13.  
  14. # Initialize the SDK
  15. pico_sdk_init()
  16. add_executable(test_adt7310_usb01 test_adt7310_usb01.c)
  17. target_link_libraries(test_adt7310_usb01 pico_stdlib hardware_spi)
  18.  
  19. pico_enable_stdio_usb(test_adt7310_usb01 1)
  20. pico_enable_stdio_uart(test_adt7310_usb01 0)
  21.  
  22. pico_add_extra_outputs(test_adt7310_usb01)
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. 以下、test_adt7310_usb01.c
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "pico/stdlib.h"
  35. #include "pico/time.h"
  36. #include "hardware/spi.h"
  37. #include "hardware/gpio.h"
  38.  
  39. #define PIN_MISO 16 // GPIO16 (21番ピン)
  40. #define PIN_CS 17 // GPIO17 (22番ピン)
  41. #define PIN_SCK 18 // GPIO18 (24番ピン)
  42. #define PIN_MOSI 19 // GPIO19 (25番ピン)
  43.  
  44. #define SPI_PORT spi0
  45.  
  46. static inline void cs_select();
  47. static inline void cs_deselect();
  48. void write_register(uint8_t reg, uint8_t data);
  49. void read_registers(uint8_t reg, uint8_t *buf, uint16_t len);
  50. void loop(void);
  51.  
  52. int main() {
  53. stdio_init_all();
  54.  
  55. // This example will use SPI0 at 0.5MHz.
  56. spi_init(SPI_PORT, 125 * 1000);
  57. gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
  58. gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
  59. gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
  60.  
  61. // Chip select is active-low, so we'll initialise it to a driven-high state
  62. gpio_init(PIN_CS);
  63. gpio_set_dir(PIN_CS, GPIO_OUT);
  64. gpio_put(PIN_CS, 1);
  65.  
  66. spi_set_format(SPI_PORT, 8, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST); // SPI MODE3, MSBFIRST
  67.  
  68. // ADT7310 ソフトウェアリセット
  69. uint8_t buf01[4] = {0xff, 0xff, 0xff, 0xff};
  70. cs_select();
  71. spi_write_blocking(SPI_PORT, buf01, 4);
  72. cs_deselect();
  73. sleep_ms(100);
  74.  
  75. while (1) {
  76. loop();
  77. }
  78.  
  79. return 0;
  80. }
  81.  
  82. void loop(void) {
  83. double temp = 0.0;
  84. int32_t temp_raw = 0;
  85. uint8_t val[2];
  86.  
  87. // 16bit 1-shot
  88. uint8_t data01 = 0xa0;
  89.  
  90. //// 13bit 1-shot
  91. //uint8_t data01 = 0x20;
  92.  
  93. write_register((uint8_t)0x08, data01);
  94.  
  95. long long read_wait_time01 = time_us_64();
  96. sleep_ms(250);
  97.  
  98. uint8_t status_reg = 0;
  99. int count001 = 0;
  100.  
  101. while (1) {
  102. // 10ms待機
  103. sleep_ms(10);
  104.  
  105. // ステータスレジスタの読み込み
  106. read_registers((uint8_t)0x40, &status_reg, (uint16_t)1);
  107.  
  108. if (count001 >= 500) {
  109. break;
  110. }
  111. if ((((int)status_reg >> 7) & 1) == 0) {
  112. break;
  113. }
  114. count001++;
  115. }
  116.  
  117. // ADT7410のステータスレジスタの値の表示
  118. //printf("status=0x%02x, count001=%d, %lld ms\r\n", (int)status_reg, count001, (time_us_64() - read_wait_time01) / 1000LL);
  119.  
  120. sleep_ms(10);
  121.  
  122. read_registers((uint8_t)0x50, val, 2);
  123. temp_raw = (int32_t)(((uint32_t)val[0] << 8) | (uint32_t)val[1]);
  124.  
  125. //printf("temp_raw = 0x%08x\r\n", temp_raw);
  126.  
  127. // 16bitの計算
  128. if (temp_raw >= 32768) {
  129. temp_raw -= 65536;
  130. }
  131. temp = temp_raw / 128.0;
  132.  
  133. //// 13bitの計算
  134. //temp_raw = temp_raw >> 3;
  135. //if (temp_raw >= 4096) {
  136. // temp_raw -= 8192;
  137. //}
  138. //temp = temp_raw / 16.0;
  139.  
  140. printf("Temp : %.4f degC\r\n", temp);
  141.  
  142. sleep_ms(5000);
  143. }
  144.  
  145. static inline void cs_select() {
  146. asm volatile("nop \n nop \n nop");
  147. gpio_put(PIN_CS, 0); // Active low
  148. asm volatile("nop \n nop \n nop");
  149. }
  150.  
  151. static inline void cs_deselect() {
  152. asm volatile("nop \n nop \n nop");
  153. gpio_put(PIN_CS, 1);
  154. asm volatile("nop \n nop \n nop");
  155. }
  156.  
  157. void write_register(uint8_t reg, uint8_t data) {
  158. uint8_t buf[2];
  159. buf[0] = reg;
  160. buf[1] = data;
  161. cs_select();
  162. spi_write_blocking(SPI_PORT, buf, 2);
  163. cs_deselect();
  164. }
  165.  
  166. void read_registers(uint8_t reg, uint8_t *buf, uint16_t len) {
  167. cs_select();
  168. spi_write_blocking(SPI_PORT, &reg, 1);
  169. spi_read_blocking(SPI_PORT, 0, buf, len);
  170. cs_deselect();
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement