arduino / ArduinoCore-avr

The Official Arduino AVR core
https://www.arduino.cc
1.22k stars 1.04k forks source link

Arduino nano SPI & INT0 #135

Open chjchoi opened 7 years ago

chjchoi commented 7 years ago

I have experienced some problem. On the Arduino nano Atmega328, INT0 and SPI can't do their jobs concurrently.

1) I have made the toggle wave (8.3333mSec Period) externally and input it into the pin 2

2) I have made a test code for that as belows if I have blocked the code line //SPCR |= _BV(SPE); then INT0 interrupt handler "void PCI_ZC1()" called and 8.333mSec period LED Toggling can be shown by oscilloscope. but if // is removed.. There is no LED Toggling on Oscilloscope...

Does SPE bit of SPCR determs the INT0 or BUILTIN LED or function digitalWrite()?

-------------------------------------
#define _sclk 13
#define _miso 12
#define _mosi 11
#define _cs 10
#define _dc 9
#define _rst 8
#define ZC1         2
#define ZC2         3
 //uint8_t  _cs, _dc, _rst, _mosi, _miso, _sclk, 
 //uint8_t mosipinmask, clkpinmask, cspinmask, dcpinmask;
//  volatile uint8_t *mosiport, *clkport, *dcport, *rsport, *csport;
unsigned char state;
//--------------------------------------------------
void INT0_Handler()
{
   state=!state;
   digitalWrite(LED_BUILTIN,state);

}
//----------------------------------------------
void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN,OUTPUT);
  pinMode(ZC1,INPUT); //no pullup
  attachInterrupt(digitalPinToInterrupt(ZC1),INT0_Handler,CHANGE);
  //
  pinMode(_rst, OUTPUT);
  digitalWrite(_rst, LOW);
  pinMode(_dc, OUTPUT);
  pinMode(_cs, OUTPUT);
  //csport    = portOutputRegister(digitalPinToPort(_cs));
  //dcport    = portOutputRegister(digitalPinToPort(_dc));
  //cspinmask = digitalPinToBitMask(_cs);
  //dcpinmask = digitalPinToBitMask(_dc);
  SPCR |= _BV(MSTR); 
  //SPCR |= _BV(SPE);
}

void loop() {
  // put your main code here, to run repeatedly:

}
aentinger commented 4 years ago

Hi @chjchoi :wave: Do you still experience this issue? In any case SPI and INT handling should not interfere with one another. One thing that's problematic for sure with your code is that a) you do not initialize state, e.g. state = 0. and b) you use a boolean ! operator on a unsigned char type. I'd suggest to modify your code like that

boolean state = false;
...
state=!state;
digitalWrite(LED_BUILTIN,state);
...