Hello! I have a timing critical project, and so have most interrupts disabled most of the time, only enabling the main interrupt flag at times when I can spare it to read single bytes at a time over serial (using Timing Serial.available and Serial.read), and set a bool that one is available, before disabling once again.
with my initial test code, I thought I had something like 137 cycles at worst for this:
void setup() {
Serial.begin(115200);
noInterrupts();
TIMSK0 = 0;
TIMSK1 = 0;
TIMSK2 = 0;
EIMSK = 0;
PCMSK0 = 0;
PCMSK1 = 0;
PCMSK2 = 0;
ADCSRA &= ~bit(ADIE);
ACSR &= ~bit(ACIE);
TCCR1A = 0;
TCCR1B = bit(CS10);//no divider
}
uint16_t cycles = 0;
uint16_t worstcycles = 0;
void loop() {
TCNT1 = 0;
interrupts();
if(Serial.available()) {//137 cycles
//if (UCSR0A & bit(RXC0)) {//this method a bit faster.. 111, but we're gonna miss available ones in buffer?
// TCNT1 = 0;
// interrupts();
uint8_t readit = Serial.read();
noInterrupts();
cycles = TCNT1;
if(cycles>worstcycles){
worstcycles = cycles;
}
if(readit == '*'){
printit();
}
}
}
void printit(){
interrupts();
Serial.print("worst number of cycles seen: ");
Serial.println(worstcycles);//seeing 137/111 cycles at worst... can this be made better, or split up into smaller chunks?
TCNT1 = 0;
while(TCNT1 < 50000){
worstcycles = 0;//something, while we waste time
}
noInterrupts();
}
However, in my project, I'm now measuring more like 360 cycles at worst, which is too long (137 would be fine). Is there any easy way to make this take less cycles? break the work up into multiple parts, or use a different method entirely to read the serial?
Cheers!