|
问题有两种:
1 ARM这边我一次性发送一串字符串过去的话,MANGO收到的数据有时是正确的,有时是错误的,比如,ARM发送‘abcdefg',
MANGO有时收到'abcdefg',有时会收到奇奇怪怪的值
2 ARM 一次只发一个字节,如果有多个字节,就分多次发送,比如:'abc'的话,就分三次发送,依次是'a','b','c',但是MANGO这边收到的数据很奇怪,大部分可以正常收到'abc‘,有时会只收到’a'和'c',‘b'不见了。
这两个问题是什么情况?干扰了?还是设置 不对?
ARM的代码太多就不上了,上ARDUINO的代码:
- #include <SPI.h>
- char buf [64];
- volatile byte pos;
- volatile boolean process_it;
- int PIN_NUM = 7;
- void setup (void)
- {
- pinMode(PIN_NUM, OUTPUT);
-
- Serial.begin (9600); // debugging
-
- // turn on SPI in slave mode
- SPCR |= _BV(SPE);
-
- // get ready for an interrupt
- pos = 0; // buffer empty
- process_it = false;
- // now turn on interrupts
- SPI.attachInterrupt();
- } // end of setup
- void light_on() {
- Serial.println ("going to light on");
- digitalWrite(PIN_NUM, HIGH); // 使Led亮
- delay(1000); // 持续1秒钟
- digitalWrite(PIN_NUM, LOW); // 使Led灭
- delay(2000); // 持续1秒钟。
- }
- // SPI interrupt routine
- ISR (SPI_STC_vect)
- {
- char c = SPDR; // grab byte from SPI Data Register
- Serial.println(c);
-
- // add to buffer if room
- // if (pos < sizeof buf)
- // {
- // buf [pos++] = c;
- // if('a' == c)
- // {
- // light_on();
- // }
-
- // example: newline means time to process buffer
-
- // if(7 == pos)
- // process_it = true;
- // }
-
-
- } // end of interrupt routine SPI_STC_vect
- // main loop - wait for flag set in interrupt routine
- void loop (void)
- {
-
-
- if (process_it)
- {
- buf [pos] = 0;
- Serial.println (buf);
- pos = 0;
- process_it = false;
- } // end of flag set
-
- } // end of loop
复制代码 |
|