Hi,
I'm trying to stream 1's and 0's using pin 53 at 250KHz on a Mega2560. My goal is to take a character string, break the characters down to binary and stream out the bits. Every 4 microseconds a pulse with a duty cycle of 25% for a 1 and 0% for a zero will be generated. Right now I'm just trying to generate a 1 every 4 microseconds. I have it working but, I am seeing random cluster of spikes in the signal generated by the Arduino. I turned off Timer0, external interrupts and I still can't get rid of the random spikes. I am using a Kingst LA1010 logic analyzer to view the signal generated by the Arduino and the 250KHz reference signal the LA1010 is generating.
I did read Gannon's website on timers and interrupts, and I thought generating 250KHz using an ISR might be too aggressive, so I set the OCR2A to 128 and I still see the spikes. I tried two different Keystudio boards with the same results.
I am looking for suggestions/ideas as to why the spikes are occurring. Thanks!
Here is my code:
#define OFF LOW
#define ON HIGH
#define SIGNAL_PIN 53
void setup() {
Serial.begin(500000);
Serial.println("Trigger signal every 4 us, using timer");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, OFF);
pinMode(SIGNAL_PIN, OUTPUT);
digitalWrite(SIGNAL_PIN, LOW);
noInterrupts();
// clear out external interrupts
EICRA = 0;
EICRB = 0;
EIFR = 0;
// turn off Timer0
TCCR0A = 0;
TCCR0B = 0;
TIMSK0 = 0;
// config Timer2 interrupt
TCCR2A = 0;
TCCR2B = 0;
OCR2A = 0;
OCR2B = 0;
TCNT2 = 0;
OCR2A = 63; // generate interrupt every 64 cycles (zero relative)
// need CTC mode
TCCR2A |= (1 << WGM21);
TCCR2B = 0b00000001; // set to no prescaling, this will start count down
// enable Timer2 interrupt compare
TIMSK2 = (1 << OCIE2A);
// enable general interrupts
interrupts();
}
// create signal
ISR(TIMER2_COMPA_vect) {
TCNT2 = 0;
PORTB |= 1;
asm("nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
"nop \n"
);
PORTB &= 0;
}
void loop() {
}
Here is a screenshot of the spikes, they go away and then will reappear a bit later in the roughly the same number of spikes per cluster.