質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

IoT

IoT(Internet of Things)とは、インターネットがコンピュータなどの情報・通信機器のネットワークだけでなく、世の中のある様々なモノに接続されて自動認識・自動制御・遠隔計測などの能力を備えることです。「モノのインターネット」と一般的にいわれます。

Q&A

解決済

1回答

855閲覧

picマイコンでシリアル受信割込みが機能しません

退会済みユーザー

退会済みユーザー

総合スコア0

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

IoT

IoT(Internet of Things)とは、インターネットがコンピュータなどの情報・通信機器のネットワークだけでなく、世の中のある様々なモノに接続されて自動認識・自動制御・遠隔計測などの能力を備えることです。「モノのインターネット」と一般的にいわれます。

0グッド

0クリップ

投稿2018/08/23 01:34

前提・実現したいこと

・c言語、picマイコン初心者です。
android端末からarduinoに値を送信し、その値をシリアル通信でpicマイコンに送ります。
ここでpicマイコンが値(A○○ or B○○ ○○は2桁の数字)を判定し、信号を作り出してモータコントローラに送信するプログラムです。

発生している問題・エラーメッセージ

arduinoから値が送信されていることはシリアルモニタで確認済みですが、picマイコンが信号を作り出すことができていません。書き込みは完了したと出ているので書き込めていると思います。また、arduinoとpicマイコンの接続はオシロスコープで確認済みです。

該当のソースコード

c言語 (picマイコン)

1#include "pic.h" 2 3// Setup configure register 4//__CONFIG(FOSC_INTRC_NOCLKOUT & LVP_OFF & DEBUG_ON & WDTE_OFF & BOREN_OFF & PWRTE_OFF & CP_OFF & CPD_OFF ); 5#pragma config FOSC = INTRC_NOCLKOUT 6#pragma config LVP = OFF 7#pragma config DEBUG = ON 8#pragma config WDTE = OFF 9#pragma config BOREN = OFF 10#pragma config PWRTE = OFF 11#pragma config CP = OFF 12#pragma config CPD = OFF 13// Constants 14#define RXBUFSIZE 96 // Serial buffer size 15#define TXBUFSIZE 32 // Serial buffer size 16// Work vars 17char Servo1; // Servo1 position (0 to 99) 18char Servo2; // Servo2 position (0 to 99) 19char RxBuf[RXBUFSIZE]; // Recieve queue 20char TxBuf[TXBUFSIZE]; // Transmit queue 21char Digit[8]; // Digit buffer 22char Reply[16]; // Reply data buffer 23char Index; // Result store index 24 25char PortB; // PORTB image copy for asynchronous bit operation 26volatile char Tick; // PORTB copy for bit operation 27volatile char RxIn; // Receive queue entry pointer 28volatile char RxOut; // Receive queue output pointer 29volatile char TxIn; // Receive queue entry pointer 30volatile char TxOut; // Receive queue output pointer 31 32 33// 34// Common Interrupt handler 35// 36static void interrupt InterruptHandler(void) 37{ 38 char i; 39 signed char j; 40 if(T0IF) // TIMER 0 interrupts? 41 { 42 T0IF = 0; // Clear TIMER0 interrupts 43 T0IE = 0; // Disable TIMER2 interrupts 44 PortB |= 0x10; // Set servo2 port (servo2 signal will be low) 45 PORTB = PortB; // Out port data 46 } 47 if(TMR1IF) // TIMER1 interrupts? 48 { 49 TMR0 = 110 + Servo2; // Set TIMER0 counter again (Range must be 110 - 210) 50 TMR1IF = 0; // Clear TIMER1 interrupt flag 51 T0IF = 0; // Clear TIMER0 interrupts 52 T0IE = 1; // Enable TIMER0 interrupts 53 PR2 = 145 - Servo1; // Set TIMER0 counter again (Range must be 145 - 45) 54 TMR2 = 0; // Clear TIMER2 counter 55 TMR2IF = 0; // Clear TIMER0 interrupts 56 TMR2IE = 1; // Disable TIMER2 interrupts 57 TMR1H = 0x63; // Set TIMER1 interval timer high (interval will be 20mS) 58 TMR1L = 0xc0; // Set TIMER1 interval timer low(interval will be 40000 * 5uS) 59 PortB &= 0x0f; // Clear both servo port(servo signal will be high) 60 PORTB = PortB; // Out port data 61 Tick++; // Advance internal timer 62 } 63 if(TMR2IF) // TIMER 2 interrupts? 64 { 65 TMR2IF = 0; // Clear TIMER2 interrupts 66 TMR2IE = 0; // Disable TIMER2 interrupts 67 PortB |= 0x20; // Ser servo1 port (servo1 signal will be low) 68 PORTB = PortB; // Out port data 69 } 70 if(RCIF) // Serial receive interrupts? 71 { 72 RCIF = 0; // Clear interrupt source 73 i = RCREG; // Get received char 74 j = RxIn - RxOut; // Get queued data count 75 if(j < 0) // If negative number? 76 j += RXBUFSIZE; // Make it plus 77 if(j < RXBUFSIZE - 1) // Dose queue have space to put received data? 78 { 79 RxBuf[RxIn] = i; // Save received char 80 if(RxIn == RXBUFSIZE - 1) // Reach to the end of the queue? 81 RxIn = 0; // Go to top again (ring buffer) 82 else 83 RxIn++; 84 } 85 } 86 if(TXIF) // Serial transmit interrupts? 87 { 88 TXIF = 0; // Clear interrupt source 89 if(TxOut == TxIn) // Check data exists in send queued 90 TXIE = 0; // Disable TX interrupts if no data 91 else // TX data is queued 92 { 93 TXREG = TxBuf[TxOut]; // Set TX data from queue 94 if(TxOut == TXBUFSIZE - 1) // Reach to the end of the queue? 95 TxOut = 0; // Go to top again (ring buffer) 96 else 97 TxOut++; // Advance TX get pointer 98 } 99 } 100} 101 102// 103// Sense incoming serial data 104// 105char RxSense(void) 106{ 107 signed char i; 108 i = RxIn - RxOut; // Check queued number 109 if(i < 0) // Negative? (may wraped queue index) 110 i += RXBUFSIZE; // Make it plus 111 return i; // Return with queued number 112} 113 114// 115// Get serial data from queue 116// 117char GetChar(void) 118{ 119 char i; 120 while(!RxSense()); // Wait untill receive data has come 121 i = RxBuf[RxOut]; // Get queued data 122 if(RxOut == RXBUFSIZE - 1) // Reach to the end of the queue? 123 RxOut = 0; // Go to top again (ring buffer) 124 else 125 RxOut++; // Advance get pointer 126 return i; // Return with received data 127} 128 129// 130// Put serial char data and send 131// 132void PutChar(char ch) 133{ 134 signed char i; 135 i = TxIn - TxOut; // Get queued data count 136 if(i < 0) // If negative number? 137 i += TXBUFSIZE; // Make it plus 138 if(i < TXBUFSIZE - 1) // Dose queue have space to put send data? 139 { 140 TxBuf[TxIn] = ch; // Save received char 141 if(TxIn == TXBUFSIZE - 1) // Reach to the end of the queue? 142 TxIn = 0; // Go to top again (ring buffer) 143 else 144 TxIn++; // Advance Tx input queue pointer 145 TXIE = 1; // Enable interrupts 146 } 147} 148 149// 150// Put serial string data and send 151// 152void PutStr(const unsigned char *str) 153{ 154 char i; 155 for(;;) 156 { 157 i = *str++; // Fetch char 158 if(i == 0) // EOS encountered? 159 break; // Exit string send loop 160 PutChar(i); // Send 1 char 161 } 162} 163 164// 165// Put serial string data and wait CR 166// 167//void PutStrWait(const unsigned char *str) 168//{ 169// char i; 170// for(;;) 171// { 172// i = *str++; // Fetch char 173// if(i == 0) // EOS encountered? 174// break; // Exit string send loop 175// PutChar(i); // Send 1 char 176// } 177// while(GetChar() != '\r'); // Wait CR 178//} 179 180// 181// Sleep timer 182// 183void Sleep(char time) 184{ 185 char i; 186 char ticksave; 187 for(i = 0; i < time; i++) // Repeat for delay time 188 { 189 ticksave = Tick; // Clear tick save 190 while(Tick == ticksave); // Wait until tick change(wait 20mS) 191 } 192} 193 194// 195// Get A/D data 196// 197unsigned int GetAd(char ch) 198{ 199 ADCON0 = 0x83 | (ch << 2); // Specify conversion channel 200 while(ADCON0 & 0x02); // Wait until A/D conversion finish 201 return (ADRESH << 8) | ADRESL; // Get A/D result 202} 203 204// 205// Make reply data 206// 207//void MakeReply(unsigned int val) 208//{ 209// unsigned int k = 10000; // loop 10000, 1000, 100, 10. 1 210// char i = 0; 211// for(;k;) // Repeat untill k has value 212// { 213// Digit[i++] = val / k + '0'; // Create target digit 214// val = val % k; // Get modulo 215// k /= 10; // Get next digit division 216// } 217// for(i = 0; i < 4; i++) // Skip leading 0 218// if(Digit[i] != '0') // Some digit except '0' found? 219// break; // Exit loop 220// for(; i < 5; i++) // Copy digit string to reply buffer 221// Reply[Index++] = Digit[i]; // Copy 1 digit 222// Reply[Index++] = ','; // Insert delimiter 223//} 224// 225//// 226//// Get address data 227//// 228unsigned int GetValue() 229{ 230 unsigned int i = 0; 231 char j; 232 for(;;) 233 { 234 j = GetChar(); //???????? 235 if(j < '0' || j > '9') 236 break; 237 i = i * 10 + j - '0'; //????? 238 } 239 return i; 240} 241 242// 243// Main 244// 245main() 246{ 247 char i; 248 while(!(OSCCON & 0x04)); // Wait untill HFINTOSC is stabled 249 250// Clock setup 251 OSCCON = 0x71; // Set prescaler 8MHz and use internal clock 252 253// Set port direction & purpose 254 ANSEL = 0x00; // Set PA0 & PA1 is analog input 0x03 255 PORTA = 0x00; // Clear PORTA 256 PortB = 0x30; // Clear PORTB & set servo ports 257 PORTB = PortB; // Set PORT B data 258 PORTC = 0x00; // Clear PORTC 259 TRISA = 0x0f; // Set PA4,5,6,7 output 260 TRISB = 0xc0; // Set PB0,1,2,3,4,5 output 261 TRISC = 0xc0; // Set PC0,1,2,3,4,5 output 262 263// Setup variables 264 Servo1 = 50; // Set default servo1 position 265 Servo2 = 50; // Set default servo2 position 266 RxIn = 0; RxOut = 0; // Clear receive queue pointers 267 TxIn = 0; TxOut = 0; // Clear tansmit queue pointers 268 269// Setup serial port 270 SPBRG = 12; // Set 9600bps of 8MHz clock 271 RCSTA = 0x80; // Enable TX & RX pin 272 TXEN = 1; // Enable TX 273 CREN = 1; // Enable RX 274 RCIE = 1; // Enable serial receive interrupts 275 276// Set A/D converter 277 ADCON0 = 0x81; // A/D clock source is FOSC/32 & enable A/D logic 278 ADCON1 = 0x80; // Set A/D result format is right justified 279 280// Setup TIMER 281 OPTION_REG = 0x84; // Prescaler = 64 TIMER0 tick will 16uS OSC 8MHz/4/32 282 TMR1ON = 1; // Enable TIMER1 283 TMR1IE = 1; // Enable TIMER1 interrupts 284 T2CON = 0x0f; // Enable TIMER2 & prescaler 1:4 & postscaler 1:8 285 286// Enable interrupts 287 PEIE = 1; // Enable peripheral interrupts 288 GIE = 1; // Enable global interrupts 289 290// Init XBee WiFi 291// Sleep(200); //Wait Start XBee 292// PutStrWait("+++"); //Send Comand Mode 293// PutStrWait("ATRE\r"); //Send Reset 294// PutStrWait("ATEE 0\r"); //Send Security Mode?0=no security 1=WPA 2=WPA2 3=WEP 295// PutStrWait("ATAH 1\r"); //Send Network Type?0=joiner 1=creator 2=infrastructuer 296// PutStrWait("ATID XBEE\r"); //Send SSID 297// PutStrWait("ATMA 1\r"); //Send IP Addressing Mode 0=DHCP 1=static 298// PutStrWait("ATMY 192.168.0.2\r"); //Send My IP Address 192.168.0.2 299// PutStrWait("ATMK 255.255.255.0\r"); //Send IP Mask 300// PutStrWait("ATDL 192.168.0.20\r"); //Send IP Address 192.168.0.2 301// PutStrWait("ATCH 1\r"); //Send Chanel 302// PutStrWait("ATCN\r"); //Send return 303 304// Start main loop 305 for(;;) 306 { 307 308// Receive process 309 if(RxSense()) 310 { 311 if(RXBUFSIZE != 0){ 312 // Serial data has come from WiFi module 313 i = GetChar(); // Get one byte 314 switch(i) // Only ACT A,B,C 315 { 316 case 'A': // Servo 1? 317 Servo1 = GetValue(); // Get servo 1 value 318 break; 319 case 'B': // Servo 1? 320 Servo2 = GetValue(); // Get servo 2 value 321 break; 322// case 'D': // Temperture ? 323// case 'E': // Variable register?? 324// GetValue(); // Get value and skip 325// break; 326 327 } 328 } 329 } 330 } 331 } 332

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

いきなり受信割り込みを実装しようとせず、まずはポーリングでシリアル受信を実装し、きちんと受信しているのを確認してはどうでしょう。

まずは、どこまで動いているのか、を確認することです

投稿2018/08/23 01:55

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/08/23 02:29

回答ありがとうございます。 ポーリングでシリアル受信を実装する。 試してみたいと思います。恥ずかしながらポーリングのことも知らなかったのでとても助かります。
y_waiwai

2018/08/23 02:37

ポーリング、と言っても、ソフトで受信フルをチェックして受信データ読み出すだけです で、PICマイコンと一口に言っても、PICの型番が違えば各レジスタの割当や動作も変わる場合がありますんで、本当に提示するコードから不具合を見つけてもらおうとするなら、詳しい型番が必要になります #私はそこまでマメではないので解決策だけ
退会済みユーザー

退会済みユーザー

2018/08/23 08:44

わざわざ丁寧にありがとうございます。今後質問を投稿する際に気を付けたいと思います:)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問