質問編集履歴

1

追加

2022/03/27 23:56

投稿

Billiecharlie
Billiecharlie

スコア55

test CHANGED
File without changes
test CHANGED
@@ -1 +1,115 @@
1
1
  WindowsパソコンとRaspberry Pi PicoをUSB接続しました。Raspberry Pi Picoで読み取った値はC言語のprintfでTeratermに出力することが出来たのですが、Teratermで値を入力して、Raspberry Pi Picoで読み取るのはscanfですることが出来ませんでした。USB通信で送られた値を読み取るRaspberry Pi Pico/C言語のライブラリはありますか?
2
+
3
+ ```ここに言語を入力
4
+ #include <stdio.h>
5
+ #include "pico/stdlib.h"
6
+ #include "hardware/spi.h"
7
+ #include "hardware/gpio.h"
8
+ #include "hardware/adc.h"
9
+
10
+
11
+ #define PIN_SCK 2
12
+ #define PIN_MOSI 3
13
+ #define PIN_MISO 4
14
+ #define PIN_CS 5
15
+
16
+ #define SPI_PORT spi0
17
+
18
+
19
+ static inline void cs_select() {
20
+ asm volatile("nop \n nop \n nop");
21
+ gpio_put(PIN_CS, 0); // Active low
22
+ asm volatile("nop \n nop \n nop");
23
+ }
24
+
25
+ static inline void cs_deselect() {
26
+ asm volatile("nop \n nop \n nop");
27
+ gpio_put(PIN_CS, 1);
28
+ asm volatile("nop \n nop \n nop");
29
+ }
30
+
31
+
32
+
33
+ void setup_SPI(){
34
+ // This example will use SPI0 at 50kHz.
35
+ spi_init(SPI_PORT, 500000);
36
+ gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
37
+ gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
38
+ gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
39
+
40
+ // Chip select is active-low, so we'll initialise it to a driven-high state
41
+ gpio_init(PIN_CS);
42
+ gpio_set_dir(PIN_CS, GPIO_OUT);
43
+ gpio_put(PIN_CS, 1);
44
+ }
45
+
46
+ int readADC(){
47
+
48
+ uint16_t writeData = 0x0000;
49
+
50
+ uint8_t buffer[2];
51
+
52
+ cs_deselect();
53
+ sleep_us(1);
54
+ cs_select();
55
+ sleep_us(1);
56
+ cs_deselect();
57
+ sleep_us(10);
58
+ cs_select();
59
+
60
+
61
+ spi_read_blocking(SPI_PORT, writeData, buffer, 2);
62
+
63
+
64
+ return (buffer[0]<<8 | buffer[1]);
65
+ }
66
+
67
+
68
+
69
+ int main() {
70
+ stdio_init_all();
71
+
72
+ setup_SPI();
73
+
74
+ sleep_ms(5000);
75
+
76
+ printf("\nHello, AD7685 Reading raw data from registers via SPI...\n");
77
+
78
+ sleep_ms(10000);
79
+
80
+
81
+ printf("n =\n");
82
+ sleep_ms(5000);
83
+
84
+
85
+
86
+ int i;
87
+ int n;
88
+ float nAdc[300];
89
+ float sum1, ave1;
90
+
91
+ scanf("%d",&n);
92
+
93
+ sleep_ms(5000);
94
+
95
+ sum1 =0;
96
+
97
+ for (int i =0; i<n;i++)
98
+ {
99
+ nAdc[i] = 5.0 / 65535.0 *readADC();
100
+ printf("%5.9lfV\n", (double)(nAdc[i]));
101
+
102
+ sum1 = sum1 + nAdc[i];
103
+ sleep_ms(10);
104
+
105
+ sleep_ms(250);
106
+ }
107
+
108
+ ave1 = sum1/n;
109
+ printf("average = %.9f\n", ave1);
110
+
111
+
112
+
113
+ return 0;
114
+ }
115
+ ```