質問編集履歴
1
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
PICマイコンで
|
1
|
+
PICマイコンで通信
|
test
CHANGED
@@ -1,407 +1,9 @@
|
|
1
1
|
##やりたいこと
|
2
2
|
|
3
|
-
PICマイコンを使用して、
|
3
|
+
PICマイコンを使用して、USART通信を行う。
|
4
|
-
|
5
|
-
Xbee(親:Cordinator API設定) ⇔ Xbee(子: Router API設定) ⇔PICマイコン (Xbee子とUSART通信)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
API通信にしている理由は、いずれ、子を増やして1対N通信を行いたいからです。
|
10
|
-
|
11
|
-
PICマイコンに接続しているスイッチの状態に応じて、メッセージを親機に送りたいと考えています。
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
スイッチが押されているとき: "test"と送信
|
16
|
-
|
17
|
-
スイッチが押されていないとき: "abcd"と送信
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
親機での受信確認は、XCTUを使用しています。
|
22
4
|
|
23
5
|
|
24
6
|
|
25
7
|
|
26
8
|
|
27
9
|
##今の状況
|
28
|
-
|
29
|
-
次のコードで送信させていますが、親機で何もデータを受信できていません。
|
30
|
-
|
31
|
-
データが受信できない原因について、なにかアドバイス等いただけると助かります。
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
API通信のコードについては、↓のサイトを参考にしました。
|
36
|
-
|
37
|
-
http://zattouka.net/GarageHouse/index.htm
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
```html
|
42
|
-
|
43
|
-
#include <xc.h>
|
44
|
-
|
45
|
-
#include <stdbool.h>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
// CONFIG1
|
50
|
-
|
51
|
-
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
|
52
|
-
|
53
|
-
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
|
54
|
-
|
55
|
-
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
|
56
|
-
|
57
|
-
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
|
58
|
-
|
59
|
-
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
|
60
|
-
|
61
|
-
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
|
62
|
-
|
63
|
-
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
|
64
|
-
|
65
|
-
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
|
66
|
-
|
67
|
-
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
|
68
|
-
|
69
|
-
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
// CONFIG2
|
74
|
-
|
75
|
-
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
|
76
|
-
|
77
|
-
#pragma config PLLEN = OFF // PLL Enable (4x PLL enabled)
|
78
|
-
|
79
|
-
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
|
80
|
-
|
81
|
-
#pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)
|
82
|
-
|
83
|
-
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
/**********************************
|
88
|
-
|
89
|
-
Port Settings
|
90
|
-
|
91
|
-
***********************************/
|
92
|
-
|
93
|
-
#define switch PORTCbits.RC3
|
94
|
-
|
95
|
-
//LED
|
96
|
-
|
97
|
-
#define LED1 LATCbits.LATC0
|
98
|
-
|
99
|
-
#define LED2 LATCbits.LATC1
|
100
|
-
|
101
|
-
#define LED3 LATCbits.LATC2
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
#define _XTAL_FREQ 8000000 //Clock=8MHz
|
106
|
-
|
107
|
-
#define __delay(X) _delay((unsigned long)((X)))
|
108
|
-
|
109
|
-
#define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0))) //usec delay
|
110
|
-
|
111
|
-
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0))) //msec delay
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
/**********************************
|
118
|
-
|
119
|
-
Function
|
120
|
-
|
121
|
-
***********************************/
|
122
|
-
|
123
|
-
void UART_Send(const char *dt,int num) ;
|
124
|
-
|
125
|
-
char XBee_CheckSum_Make(char *packet_dt,int len) ;
|
126
|
-
|
127
|
-
int XBee_MakePacket10(char *packet_dt, const char *adrs, const char *data, int num) ;
|
128
|
-
|
129
|
-
int XBee_SendPacket10(char *packet_dt,int len,int sw) ;
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
/**********************************
|
134
|
-
|
135
|
-
Program Variable
|
136
|
-
|
137
|
-
***********************************/
|
138
|
-
|
139
|
-
int i , x ;
|
140
|
-
|
141
|
-
char dt[36] ;
|
142
|
-
|
143
|
-
char adrs[] = {0x00,0x13,0xA2,0x00,0x40,0x9E,0xB1,0xF3} ; // 相手XBeeのアドレス
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
void main(void) {
|
148
|
-
|
149
|
-
OSCCON=0x72; //PLL Disable/8MHz
|
150
|
-
|
151
|
-
ANSELA=0x04; //RA2 Analog Setting
|
152
|
-
|
153
|
-
ANSELC=0x00; //PORTC Digital Setting
|
154
|
-
|
155
|
-
TRISA=0x04; //RA2 Input
|
156
|
-
|
157
|
-
TRISC=0x28; //RC5, RC3 Input
|
158
|
-
|
159
|
-
LATC=0x00;
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
ADCON0=0x09; //Channel Select: AN2, ADC Enabled
|
164
|
-
|
165
|
-
ADCON1=0xD0; //Result Format: Right justified, FOSC/16=500lHz (2usec)
|
166
|
-
|
167
|
-
__delay_ms(500);
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
//USART設定 非同期で9600bps
|
172
|
-
|
173
|
-
//USART pins
|
174
|
-
|
175
|
-
RXDTSEL = 0; // RC5:RX
|
176
|
-
|
177
|
-
TXCKSEL = 0; // RC4:TX
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
//mode
|
182
|
-
|
183
|
-
TXSTA = 0x24; //非同期/8ビット/パリティなし
|
184
|
-
|
185
|
-
RCSTA = 0x90; //シリアルポート使用/連続受信あり
|
186
|
-
|
187
|
-
BAUDCON = 0x00; // 8bit mode
|
188
|
-
|
189
|
-
//Baud Rate: 9600bps 設定値51
|
190
|
-
|
191
|
-
SPBRGH = 0x00; //上位8bit
|
192
|
-
|
193
|
-
SPBRGL = 0x33; //下位8bit
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
//LED初期設定
|
198
|
-
|
199
|
-
LED1=0; //1: 点灯/0: 消灯
|
200
|
-
|
201
|
-
LED2=0; //1: 点灯/0: 消灯
|
202
|
-
|
203
|
-
LED3=0; //1: 点灯/0: 消灯
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
while(1){
|
208
|
-
|
209
|
-
if(switch == 1) {
|
210
|
-
|
211
|
-
i = XBee_MakePacket10(dt,adrs,"test",4) ;
|
212
|
-
|
213
|
-
x = XBee_SendPacket10(dt,i,1) ;
|
214
|
-
|
215
|
-
LED1=1;
|
216
|
-
|
217
|
-
__delay_ms(100); //100msec
|
218
|
-
|
219
|
-
LED1=0;
|
220
|
-
|
221
|
-
__delay_ms(100); //100msec
|
222
|
-
|
223
|
-
LED2=1;
|
224
|
-
|
225
|
-
__delay_ms(100); //100msec
|
226
|
-
|
227
|
-
LED2=0;
|
228
|
-
|
229
|
-
__delay_ms(100); //100msec
|
230
|
-
|
231
|
-
LED3=1;
|
232
|
-
|
233
|
-
__delay_ms(100); //100msec
|
234
|
-
|
235
|
-
LED3=0;
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
else{
|
240
|
-
|
241
|
-
i = XBee_MakePacket10(dt,adrs,"abcd",4) ;
|
242
|
-
|
243
|
-
x = XBee_SendPacket10(dt,i,1) ;
|
244
|
-
|
245
|
-
LED1=1;
|
246
|
-
|
247
|
-
LED2=1;
|
248
|
-
|
249
|
-
LED3=1;
|
250
|
-
|
251
|
-
__delay_ms(250); //250msec
|
252
|
-
|
253
|
-
LED1=0;
|
254
|
-
|
255
|
-
LED2=0;
|
256
|
-
|
257
|
-
LED3=0;
|
258
|
-
|
259
|
-
__delay_ms(250); //250msec
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
}
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
void UART_Send(const char *dt,int num)
|
272
|
-
|
273
|
-
{
|
274
|
-
|
275
|
-
int i ;
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
// 指定した個数分繰り返す
|
280
|
-
|
281
|
-
for (i=0 ; i<num ; i++) {
|
282
|
-
|
283
|
-
while(TXIF==0) ; // 送信可能になるまで待つ
|
284
|
-
|
285
|
-
TXREG = *dt++ ; // 送信する
|
286
|
-
|
287
|
-
}
|
288
|
-
|
289
|
-
}
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
char XBee_CheckSum_Make(char *packet_dt,int len)
|
294
|
-
|
295
|
-
{
|
296
|
-
|
297
|
-
int i , c , x ;
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
c = len - 4 ;
|
302
|
-
|
303
|
-
x = 0 ;
|
304
|
-
|
305
|
-
packet_dt = packet_dt + 3 ; // 4バイト目から開始
|
306
|
-
|
307
|
-
for (i=0 ; i<c ; i++) {
|
308
|
-
|
309
|
-
x = x + *packet_dt ;
|
310
|
-
|
311
|
-
packet_dt++ ;
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
x = 0xff - (x & 0xff) ;
|
316
|
-
|
317
|
-
return x ; // 計算したチェックサム値を返す
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
int XBee_MakePacket10(char *packet_dt, const char *adrs, const char *data, int num)
|
324
|
-
|
325
|
-
{
|
326
|
-
|
327
|
-
int x , i ;
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
packet_dt[0] = 0x7E ; // 開始コード
|
332
|
-
|
333
|
-
x = num + 14 ;
|
334
|
-
|
335
|
-
packet_dt[1] = x / 256 ; // フレームデータの長さ
|
336
|
-
|
337
|
-
packet_dt[2] = x % 256 ;
|
338
|
-
|
339
|
-
packet_dt[3] = 0x10 ; // フレームタイプ
|
340
|
-
|
341
|
-
packet_dt[4] = 0x00 ; // フレームID
|
342
|
-
|
343
|
-
for (i=5 ; i<13 ; i++) {
|
344
|
-
|
345
|
-
packet_dt[i] = *adrs ; // 送信先の64ビットアドレス
|
346
|
-
|
347
|
-
adrs++ ;
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
packet_dt[13] = 0xFF ; // 送信先の16ビットアドレス
|
352
|
-
|
353
|
-
packet_dt[14] = 0xFE ;
|
354
|
-
|
355
|
-
packet_dt[15] = 0x00 ; // 最大ホップ数
|
356
|
-
|
357
|
-
packet_dt[16] = 0x00 ; // 送信オプション
|
358
|
-
|
359
|
-
for (i=0 ; i<num ; i++) {
|
360
|
-
|
361
|
-
packet_dt[17+i] = *data ; // 送信するデータ
|
362
|
-
|
363
|
-
data++ ;
|
364
|
-
|
365
|
-
}
|
366
|
-
|
367
|
-
i = 17 + num ;
|
368
|
-
|
369
|
-
x = i + 1 ;
|
370
|
-
|
371
|
-
packet_dt[i] =XBee_CheckSum_Make(packet_dt,x) ; // チェックサム
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
return x ; // 作成したパケットの長さを返す
|
376
|
-
|
377
|
-
}
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
int XBee_SendPacket10(char *packet_dt,int len,int sw)
|
384
|
-
|
385
|
-
{
|
386
|
-
|
387
|
-
char dt[36] ;
|
388
|
-
|
389
|
-
int x , ans , c ;
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
ans = 0 ;
|
394
|
-
|
395
|
-
if (sw == 0) {
|
396
|
-
|
397
|
-
// 返答を待たない(送りっぱなし)
|
398
|
-
|
399
|
-
UART_Send(packet_dt,len) ;
|
400
|
-
|
401
|
-
}
|
402
|
-
|
403
|
-
return ans ;
|
404
|
-
|
405
|
-
}
|
406
|
-
|
407
|
-
```
|