Interrupts Problem

I'm having a bit of a problem with my Interrupts. This code works perfectly fine:

void sendAddrData(byte address, byte data){
  detachInterrupt(0);
  addrStack.enqueue(address);                              
  dataStack.enqueue(data);
  attachInterrupt(0, writeData, RISING);
}

However, I'm trying to get rid of some timing issues, so I've tried using this to cut back on the cycles needed:

void sendAddrData(byte address, byte data){
  noInterrupts();
  addrStack.enqueue(address);                              
  dataStack.enqueue(data);
  interrupts();
}

Neither this method nor using cli()/sei() work at all. Is there something odd going on?

Thanks.

interrupts() and noInterrupts() are just synonyms for sei() and cli(), so that will make no difference.

Without telling us what kind of problems you have with interrupts, it's very hard to offer useful advice or even to know what your problem is. Also, it would be helpful to know what your magic function writeData is doing.

Korman

With the first example, my code runs just fine, just a little slow.

If I try to use cli() and sei(), nothing happens. Nothing gets popped from the stack and sent along.

I’m trying to fill a FIFO with a few bytes that are read out by a separate microprocessor which needs the data at very specific times. The SPI is being output to a CPLD which does not have enough cells left over for a latch, so I have to do all the buffering in software. Here is the full source:

#include <SimpleFIFO.h>
#include <SPI.h>
#include <digitalWriteFast.h>

SimpleFIFO<byte,32> addrStack;
SimpleFIFO<byte,32> dataStack;

const byte initTable[24] =  {0x30,0x08,0x00,0x00,0x30,0x08,0x00,0x00,0x80,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x40};

const int latchPin = 8;

void writeData(){
  if (addrStack.count() != 0){
    digitalWriteFast(latchPin,HIGH);
    digitalWriteFast(latchPin,LOW);
    SPI.transfer(addrStack.dequeue());
    SPI.transfer(dataStack.dequeue());
  }
}
  
void sendAddrData(byte address, byte data){
  cli();
  addrStack.enqueue(address);                              
  dataStack.enqueue(data);
  sei();
}

void setup() {
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  pinMode(latchPin, OUTPUT);
  pinMode(2,INPUT);
  digitalWriteFast(2,HIGH);
  attachInterrupt(0, writeData, RISING);
  
}

void loop() {
  for (byte n = 0; n < 0x18; n++) sendAddrData(n,initTable[(int)n]);
}

void loop() {
for (byte n = 0; n < 0x18; n++) sendAddrData(n,initTable[(int)n]);
}

This pushes the 24 bytes into the FIFO over-and-over as fast as the processor can. Is this really what you want to happen?

I suppose I should increase the size of the buffer, because the processor that's receiving the data only clocks in at 1MHz. I'll try that.