WindowsパソコンとRaspberry Pi PicoをUSB接続しました。Raspberry Pi Picoで読み取った値はC言語のprintfでTeratermに出力することが出来たのですが、Teratermで値を入力して、Raspberry Pi Picoで読み取るのはscanfですることが出来ませんでした。USB通信で送られた値を読み取るRaspberry Pi Pico/C言語のライブラリはありますか?
#include <stdio.h> #include "pico/stdlib.h" #include "hardware/spi.h" #include "hardware/gpio.h" #include "hardware/adc.h" #define PIN_SCK 2 #define PIN_MOSI 3 #define PIN_MISO 4 #define PIN_CS 5 #define SPI_PORT spi0 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 setup_SPI(){ // This example will use SPI0 at 50kHz. spi_init(SPI_PORT, 500000); 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); } int readADC(){ uint16_t writeData = 0x0000; uint8_t buffer[2]; cs_deselect(); sleep_us(1); cs_select(); sleep_us(1); cs_deselect(); sleep_us(10); cs_select(); spi_read_blocking(SPI_PORT, writeData, buffer, 2); return (buffer[0]<<8 | buffer[1]); } int main() { stdio_init_all(); setup_SPI(); sleep_ms(5000); printf("\nHello, AD7685 Reading raw data from registers via SPI...\n"); sleep_ms(10000); printf("n =\n"); sleep_ms(5000); int i; int n; float nAdc[300]; float sum1, ave1; scanf("%d",&n); sleep_ms(5000); sum1 =0; for (int i =0; i<n;i++) { nAdc[i] = 5.0 / 65535.0 *readADC(); printf("%5.9lfV\n", (double)(nAdc[i])); sum1 = sum1 + nAdc[i]; sleep_ms(10); sleep_ms(250); } ave1 = sum1/n; printf("average = %.9f\n", ave1); return 0; }
あなたの回答
tips
プレビュー