<問題>
arduinoからPicマイコンへデータを送信しているのですが、Picマイコンで上手く受け取れていません。
Tera Tarm で確認したところ、送信した文字が、"9" → "1"に、"B" → "F"に変換されてしまっているようです。文字コードや通信速度も確認しましたが怪しいところが見つかりませんでした。
<現状>
picマイコンは "16F886" を使っています。
MPLABのバージョンもいくつか変更して試しましたが駄目でした。
Tera Tarmを使用していない時もオシロスコープでデータが渡されていることは確認しています。
考えられる可能性をなんでもいいので教えて頂けると助かります。
pic マイコン(割り込み処理と関係する部分のみ)
C
1// Constants 2#define RXBUFSIZE 96 // Serial buffer size 3#define TXBUFSIZE 32 // Serial buffer size 4// Work vars 5char Servo1; // Servo1 position (0 to 99) 6char Servo2; // Servo2 position (0 to 99) 7char RxBuf[RXBUFSIZE]; // Recieve queue 8char TxBuf[TXBUFSIZE]; // Transmit queue 9char Digit[8]; // Digit buffer 10char Reply[16]; // Reply data buffer 11char Index; // Result store index 12 13char PortB; // PORTB image copy for asynchronous bit operation 14volatile char Tick; // PORTB copy for bit operation 15volatile char RxIn; // Receive queue entry pointer 16volatile char RxOut; // Receive queue output pointer 17volatile char TxIn; // Receive queue entry pointer 18volatile char TxOut; // Receive queue output pointer 19ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 20char RxSense(void) 21{ 22 signed char i; 23 i = RxIn - RxOut; // Check queued number 24 if(i < 0) // Negative? (may wraped queue index) 25 i += RXBUFSIZE; // Make it plus 26 return i; // Return with queued number 27} 28ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 29char GetChar(void) 30{ 31 char i; 32 while(!RxSense()); // Wait untill receive data has come 33 i = RxBuf[RxOut]; // Get queued data 34 if(RxOut == RXBUFSIZE - 1) // Reach to the end of the queue? 35 RxOut = 0; // Go to top again (ring buffer) 36 else 37 RxOut++; // Advance get pointer 38 return i; // Return with received data 39} 40ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 41// Main 42// 43main() 44{ 45 char i; 46 while(!(OSCCON & 0x04)); // Wait untill HFINTOSC is stabled 47 48// Clock setup 49 OSCCON = 0x71; // Set prescaler 8MHz and use internal clock 50 51// Set port direction & purpose 52 ANSEL = 0x00; // Set PA0 & PA1 is analog input 0x03 53 PORTA = 0x00; // Clear PORTA 54 PortB = 0x30; // Clear PORTB & set servo ports 55 PORTB = PortB; // Set PORT B data 56 PORTC = 0x00; // Clear PORTC 57 TRISA = 0x0f; // Set PA4,5,6,7 output 58 TRISB = 0xc0; // Set PB0,1,2,3,4,5 output 59 TRISC = 0xc0; // Set PC0,1,2,3,4,5 output 60 61// Setup variables 62 Servo1 = 99; // Set default servo1 position 63 Servo2 = 50; // Set default servo2 position 64 RxIn = 0; RxOut = 0; // Clear receive queue pointers 65 TxIn = 0; TxOut = 0; // Clear tansmit queue pointers 66 67// Setup serial port 68 SPBRG = 12; // Set 9600bps of 8MHz clock 69 RCSTA = 0x80; // Enable TX & RX pin 70 TXEN = 1; // Enable TX 71 CREN = 1; // Enable RX 72 RCIE = 1; // Enable serial receive interrupts 73 74// Set A/D converter 75 ADCON0 = 0x81; // A/D clock source is FOSC/32 & enable A/D logic 76 ADCON1 = 0x80; // Set A/D result format is right justified 77 78// Setup TIMER 79 OPTION_REG = 0x84; // Prescaler = 64 TIMER0 tick will 16uS OSC 8MHz/4/32 80 TMR1ON = 1; // Enable TIMER1 81 TMR1IE = 1; // Enable TIMER1 interrupts 82 T2CON = 0x0f; // Enable TIMER2 & prescaler 1:4 & postscaler 1:8 83 84// Enable interrupts 85 PEIE = 1; // Enable peripheral interrupts 86 GIE = 1; // Enable global interrupts 87 88// Start main loop 89 for(;;) 90 { 91 92// Receive process 93 if(RxSense()) 94 { 95 if(RXBUFSIZE != 0){ 96 // Serial data has come from WiFi module 97 i = GetChar(); // Get one byte 98 switch(i) // Only ACT A,B,C 99 { 100 case 'A': // Servo 1? 101 Servo1 = GetValue(); // Get servo 1 value 102 break; 103 case 'B': // Servo 1? 104 Servo2 = GetValue(); // Get servo 2 value 105 break; 106 case 'D': // Temperture ? 107 case 'E': // Variable register?? 108 GetValue(); // Get value and skip 109 break; 110 111 } 112 } 113 else{ 114 Servo1=99; 115 Servo2=50; 116 } 117 } 118 }
回答4件
あなたの回答
tips
プレビュー