I am very new to Arduino programming and I am just trying to count with a frequency of 4000Hz and send the count over serial. Here's the code:
int counter = 0;
void setup() {
Serial.begin(115200);
Serial.println('UP IN HERE');
// put your setup code here, to run once:
noInterrupts();
//set timer1 interrupt at 4000Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 4000hz increments
OCR1A = 3999;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 bit for no prescaling
TCCR1B |= (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts();
}
ISR(Timer1_COMPA_vect){
counter = counter + 1;
Serial.println(counter);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(counter);
}
The main loop never executes so I don't receive anything over the serial port. However, if I comment out the line:
TIMSK1 |= (1 << OCIE1A);
The main loop starts and I receive 0's over serial.