Skip to content

Instantly share code, notes, and snippets.

@walidamriou
Last active October 26, 2021 12:20
Show Gist options
  • Save walidamriou/09e83a6581ad298bf6ae57bbcffcb3d7 to your computer and use it in GitHub Desktop.
Save walidamriou/09e83a6581ad298bf6ae57bbcffcb3d7 to your computer and use it in GitHub Desktop.
Timer1 Arduino board by Arduino Language (CPP) output signal of period 18 ms from pin 10
#include <Arduino.h>
#define signal_generator_loop_timer_16us 57 // every 16*57 = 912 generate four signals of four pins (one step of helf step driver control)
#define timer_test_output_pin 10
void timer_init();
uint8_t timer_buffer_loop = 0;
void setup(){
pinMode(timer_test_output_pin, OUTPUT);
timer_init();
}
ISR(TIMER1_COMPA_vect){ // timer compare interrupt service routine
timer_buffer_loop++;
if(timer_buffer_loop==signal_generator_loop_timer_16us){
timer_buffer_loop=0;
digitalWrite(timer_test_output_pin, digitalRead(timer_test_output_pin) ^ 1); // toggle LED pin
}
}
void loop(){
// your program here…
}
void timer_init(){
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 256; // compare match register
// 16MHz/1 = 16MHz
// 1/16MHz = 0.0000000625 s
// 0.0000000625 * 256 = 0.000016 s = 16 us
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS10); // no prescaling (clk/1)
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment