Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 以下、CMakeLists.txt
- cmake_minimum_required(VERSION 3.12)
- # Pull in PICO SDK (must be before project)
- include(/d/pico-sdk/pico/pico-sdk/external/pico_sdk_import.cmake)
- project(test_adt7310_usb01_project)
- set(CMAKE_C_STANDARD 11)
- set(CMAKE_CXX_STANDARD 17)
- # Initialize the SDK
- pico_sdk_init()
- add_executable(test_adt7310_usb01 test_adt7310_usb01.c)
- target_link_libraries(test_adt7310_usb01 pico_stdlib hardware_spi)
- pico_enable_stdio_usb(test_adt7310_usb01 1)
- pico_enable_stdio_uart(test_adt7310_usb01 0)
- pico_add_extra_outputs(test_adt7310_usb01)
- 以下、test_adt7310_usb01.c
- #include <stdio.h>
- #include <string.h>
- #include "pico/stdlib.h"
- #include "pico/time.h"
- #include "hardware/spi.h"
- #include "hardware/gpio.h"
- #define PIN_MISO 16 // GPIO16 (21番ピン)
- #define PIN_CS 17 // GPIO17 (22番ピン)
- #define PIN_SCK 18 // GPIO18 (24番ピン)
- #define PIN_MOSI 19 // GPIO19 (25番ピン)
- #define SPI_PORT spi0
- static inline void cs_select();
- static inline void cs_deselect();
- void write_register(uint8_t reg, uint8_t data);
- void read_registers(uint8_t reg, uint8_t *buf, uint16_t len);
- void loop(void);
- int main() {
- stdio_init_all();
- // This example will use SPI0 at 0.5MHz.
- spi_init(SPI_PORT, 125 * 1000);
- gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
- gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
- gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
- // Chip select is active-low, so we'll initialise it to a driven-high state
- gpio_init(PIN_CS);
- gpio_set_dir(PIN_CS, GPIO_OUT);
- gpio_put(PIN_CS, 1);
- spi_set_format(SPI_PORT, 8, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST); // SPI MODE3, MSBFIRST
- // ADT7310 ソフトウェアリセット
- uint8_t buf01[4] = {0xff, 0xff, 0xff, 0xff};
- cs_select();
- spi_write_blocking(SPI_PORT, buf01, 4);
- cs_deselect();
- sleep_ms(100);
- while (1) {
- loop();
- }
- return 0;
- }
- void loop(void) {
- double temp = 0.0;
- int32_t temp_raw = 0;
- uint8_t val[2];
- // 16bit 1-shot
- uint8_t data01 = 0xa0;
- //// 13bit 1-shot
- //uint8_t data01 = 0x20;
- write_register((uint8_t)0x08, data01);
- long long read_wait_time01 = time_us_64();
- sleep_ms(250);
- uint8_t status_reg = 0;
- int count001 = 0;
- while (1) {
- // 10ms待機
- sleep_ms(10);
- // ステータスレジスタの読み込み
- read_registers((uint8_t)0x40, &status_reg, (uint16_t)1);
- if (count001 >= 500) {
- break;
- }
- if ((((int)status_reg >> 7) & 1) == 0) {
- break;
- }
- count001++;
- }
- // ADT7410のステータスレジスタの値の表示
- //printf("status=0x%02x, count001=%d, %lld ms\r\n", (int)status_reg, count001, (time_us_64() - read_wait_time01) / 1000LL);
- sleep_ms(10);
- read_registers((uint8_t)0x50, val, 2);
- temp_raw = (int32_t)(((uint32_t)val[0] << 8) | (uint32_t)val[1]);
- //printf("temp_raw = 0x%08x\r\n", temp_raw);
- // 16bitの計算
- if (temp_raw >= 32768) {
- temp_raw -= 65536;
- }
- temp = temp_raw / 128.0;
- //// 13bitの計算
- //temp_raw = temp_raw >> 3;
- //if (temp_raw >= 4096) {
- // temp_raw -= 8192;
- //}
- //temp = temp_raw / 16.0;
- printf("Temp : %.4f degC\r\n", temp);
- sleep_ms(5000);
- }
- static inline void cs_select() {
- asm volatile("nop \n nop \n nop");
- gpio_put(PIN_CS, 0); // Active low
- asm volatile("nop \n nop \n nop");
- }
- static inline void cs_deselect() {
- asm volatile("nop \n nop \n nop");
- gpio_put(PIN_CS, 1);
- asm volatile("nop \n nop \n nop");
- }
- void write_register(uint8_t reg, uint8_t data) {
- uint8_t buf[2];
- buf[0] = reg;
- buf[1] = data;
- cs_select();
- spi_write_blocking(SPI_PORT, buf, 2);
- cs_deselect();
- }
- void read_registers(uint8_t reg, uint8_t *buf, uint16_t len) {
- cs_select();
- spi_write_blocking(SPI_PORT, ®, 1);
- spi_read_blocking(SPI_PORT, 0, buf, len);
- cs_deselect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement