From 908407353a98d96523392a7bac1f9a6bf3fc9c6b Mon Sep 17 00:00:00 2001 From: Ivan Stefanov Date: Tue, 24 Nov 2020 16:10:17 +0200 Subject: [PATCH] Updated Timer Interrupt Examples Updated names to show interrupt source used and the main parameters of the timer setup. Also added a little bit description in the beginning of some of the projects describing the code and added some comments. --- .../Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino | 54 ++++++++++++++++++ ...er_1_COMPA_and_OVF_10kHz_Software-PWM.ino} | 17 +++++- ...ink_pin_9_at_1Hz_with_external_enable.ino} | 49 ++++++++-------- ...ink_pin_9_at_1Hz_with_external_enable.png} | Bin Examples/Timer_ISR/Timer_ISR.ino | 38 ------------ Examples/UART-Send/UART-Send.ino | 17 ++++-- 6 files changed, 105 insertions(+), 70 deletions(-) create mode 100644 Examples/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino rename Examples/{Timer_1_10kHz_ISR/Timer_1_10kHz_ISR.ino => Timer_1_COMPA_and_OVF_10kHz_Software-PWM/Timer_1_COMPA_and_OVF_10kHz_Software-PWM.ino} (76%) rename Examples/{Timer_1Hz_with_external_enable/Timer_1Hz_with_external_enable.ino => Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.ino} (65%) rename Examples/{Timer_1Hz_with_external_enable/Timer_1Hz with external enable.png => Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.png} (100%) delete mode 100644 Examples/Timer_ISR/Timer_ISR.ino diff --git a/Examples/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino b/Examples/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino new file mode 100644 index 0000000..d51c257 --- /dev/null +++ b/Examples/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino @@ -0,0 +1,54 @@ +/* + Timer 1 is set to generate Compare Match Interrupt with frequency 1Hz + The counter counts with frequency 16MHz/1024 = 15.625kHz + For 1s the counter will reach value of [15.625kHz*1s - 1] = [15625 - 1] = 15624 + This value is set as compare value so that when counter reaches it + it will generate Interrupt with Period exactly 1s => Frequency 1Hz + LED attached on pin 13 is TOGGLED with this frequency of 1Hz so it stays 1s on and 1s off +*/ + +volatile boolean toggle = 0; + +void setup() { + pinMode(13, OUTPUT); // Set pin 13 as output + noInterrupts(); //cli(); // Stop reception of interrupts + + // Set Timer 1 Control registers and counter to 0 + TCCR1A = 0; + TCCR1B = 0; + TIMSK1 = 0; + TCNT1 = 0; + + // Set compare match value = Fclk_io / (Fout * Prescaler) - 1 + OCR1A = 15624; // = (16MHz) / (1 * 1024) - 1 + TIMSK1 |= (1 << OCIE1A); // Enable compare match interrupt + + // Set Timer 1 Mode of operation to Clear Timer on Compare Match (CTC -> Mode 4) + TCCR1B |= (1 << WGM12); // Mode 4 + + // Set Timer 1 Clock prescaler and source so that the timer starts counting + TCCR1B |= (1 << CS12) | (1 << CS10); // Set prescaler to 1024 (=> enable the clock) + + interrupts(); //sei(); // Allow reception of interrupts +} + +void loop() { + // put your main code here, to run repeatedly: +} + +// Timer1 Interrupt Service Routine (ISR) +// Toggle pin 13 (LED) every 1s +ISR(TIMER1_COMPA_vect) { + /* + * Generates pulse wave of frequency 1Hz/2 = 0.5Hz + * Takes two cycles for full wave - toggle high then after 1s toggle low after 1s repeat + */ + if (toggle == 1) { + digitalWrite(13, HIGH); + toggle = 0; + } + else { + digitalWrite(13, LOW); + toggle = 1; + } +} diff --git a/Examples/Timer_1_10kHz_ISR/Timer_1_10kHz_ISR.ino b/Examples/Timer_1_COMPA_and_OVF_10kHz_Software-PWM/Timer_1_COMPA_and_OVF_10kHz_Software-PWM.ino similarity index 76% rename from Examples/Timer_1_10kHz_ISR/Timer_1_10kHz_ISR.ino rename to Examples/Timer_1_COMPA_and_OVF_10kHz_Software-PWM/Timer_1_COMPA_and_OVF_10kHz_Software-PWM.ino index b1ae812..a592e20 100644 --- a/Examples/Timer_1_10kHz_ISR/Timer_1_10kHz_ISR.ino +++ b/Examples/Timer_1_COMPA_and_OVF_10kHz_Software-PWM/Timer_1_COMPA_and_OVF_10kHz_Software-PWM.ino @@ -1,30 +1,41 @@ void setup() { + // Set pin 13 to output pinMode(13, OUTPUT); - noInterrupts(); //cli(); // Stop reception of interrupts + + // Stop reception of interrupts + noInterrupts(); //cli(); // Set PB1 to be an output (Pin9 Arduino UNO) DDRB |= (1 << PB1); + // Clear Timer/Counter Control Registers TCCR1A = 0; TCCR1B = 0; TIMSK1 = 0; + // Set non-inverting mode - Table 15-3 (page 108) TCCR1A |= (1 << COM1A1); + // Set Fast-PWM Mode (Mode 14) - Table 15-5 (page 109) TCCR1A |= (1 << WGM11); TCCR1B |= (1 << WGM12); TCCR1B |= (1 << WGM13); + // Clear Timer 1 Counter TCNT1 = 0; + // Set PWM frequency/top value - Output PWM 10kHz - ICR1 = 199; - OCR1A = 180; + ICR1 = 199; // Fclk_io / (Fout * Prescaler) - 1 + OCR1A = 100; // Output OC1A will be ON for [OCR1A/(ICR1+1)]% of the time -> 100/(199+1) = 50% + // Enable compare match interrupt TIMSK1 |= (1 << OCIE1A); TIMSK1 |= (1 << TOIE1); + // Set prescaler to 8 and starts PWM TCCR1B |= (1 << CS11); + // Enables interrupts interrupts(); //sei(); } diff --git a/Examples/Timer_1Hz_with_external_enable/Timer_1Hz_with_external_enable.ino b/Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.ino similarity index 65% rename from Examples/Timer_1Hz_with_external_enable/Timer_1Hz_with_external_enable.ino rename to Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.ino index 895f612..e6bb3ef 100644 --- a/Examples/Timer_1Hz_with_external_enable/Timer_1Hz_with_external_enable.ino +++ b/Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.ino @@ -1,37 +1,37 @@ #define BTN_PIN 2 -void button_pressed_ISR(void); - volatile uint8_t timer_en = 0; +void button_pressed_ISR(void); + void setup() { // Stop reception of interrupts noInterrupts(); //cli(); - + pinMode(BTN_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed_ISR, FALLING); // Set PB1 to be an output (Pin9 Arduino UNO) DDRB |= (1 << PB1); - + // Clear Timer/Counter Control Registers TCCR1A = 0; TCCR1B = 0; TIMSK1 = 0; - + // Set non-inverting mode - Table 15-3 (page 108) TCCR1A |= (1 << COM1A1); - + // Set Fast-PWM Mode (Mode 14) - Table 15-5 (page 109) TCCR1A |= (1 << WGM11); TCCR1B |= (1 << WGM12); TCCR1B |= (1 << WGM13); - + // Clear Timer 1 Counter TCNT1 = 0; - - // Set PWM frequency/top value - Output PWM 10kHz + + // Set PWM frequency/top value - Output PWM 1Hz ICR1 = 15625; OCR1A = 10000; @@ -39,22 +39,23 @@ void setup() interrupts(); } -void loop() -{} +void loop() { + // some useless code here + } void button_pressed_ISR(void) { - if (timer_en) - { - timer_en = 0; - // Disable the timer by selecting "none" as the clock source - TCCR1B &= ~((1 << CS11) | (1 << CS12) | (1 << CS10)); - } - else - { - timer_en = 1; - // Enable the timer - TCCR1B |= (1 << CS12) | (1 << CS10); - TCCR1B &= ~(1 << CS11); - } + if (timer_en) + { + timer_en = 0; + // Disable the timer by selecting "none" as the clock source + TCCR1B &= ~((1 << CS11) | (1 << CS12) | (1 << CS10)); + } + else + { + timer_en = 1; + // Enable the timer + TCCR1B |= (1 << CS12) | (1 << CS10); + TCCR1B &= ~(1 << CS11); + } } diff --git a/Examples/Timer_1Hz_with_external_enable/Timer_1Hz with external enable.png b/Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.png similarity index 100% rename from Examples/Timer_1Hz_with_external_enable/Timer_1Hz with external enable.png rename to Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.png diff --git a/Examples/Timer_ISR/Timer_ISR.ino b/Examples/Timer_ISR/Timer_ISR.ino deleted file mode 100644 index 8d11733..0000000 --- a/Examples/Timer_ISR/Timer_ISR.ino +++ /dev/null @@ -1,38 +0,0 @@ -volatile boolean toggle1 = 0; - -void setup() { - pinMode(13, OUTPUT); - noInterrupts(); //cli(); // Stop reception of interrupts - - // Set Timer 1 Control registers and counter to 0 - TCCR1A = 0; - TCCR1B = 0; - TCNT1 = 0; - - // Set compare match value = Fclk_io / (Fout * Prescaler) - 1 - OCR1A = 15624; // = (16MHz) / (1 * 1024) - 1 - TIMSK1 |= (1 << OCIE1A); // Enable compare match interrupt - - // Set Timer 1 Mode of operation and clock prescalers - TCCR1B |= (1 << WGM12); // Mode 4 - TCCR1B |= (1 << CS12) | (1 << CS10); // Set prescaler to 1024 (=> enable the clock) - - - interrupts(); //sei(); -} - -void loop() { - // put your main code here, to run repeatedly: -} - -ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED) -//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low) - if (toggle1){ - digitalWrite(13,HIGH); - toggle1 = 0; - } - else{ - digitalWrite(13,LOW); - toggle1 = 1; - } -} diff --git a/Examples/UART-Send/UART-Send.ino b/Examples/UART-Send/UART-Send.ino index d6b3048..9470a17 100644 --- a/Examples/UART-Send/UART-Send.ino +++ b/Examples/UART-Send/UART-Send.ino @@ -1,3 +1,10 @@ +/* + * ASCII => Char + * 0x56 => V + * 0x0A => [LINE FEED] + * 0x0D => [CARRIAGE RETURN] + */ + void setup() { // put your setup code here, to run once: Serial.begin(9600); @@ -5,13 +12,13 @@ void setup() { void loop() { // put your main code here, to run repeatedly: - Serial.print('V'); - Serial.println('V'); - - Serial.write(0x56); + Serial.print('V'); // Sends 0x56 +// Serial.println('V'); // Sends 0x56 0x0D 0x0A +// Serial.write(0x56); // Sends 0x56 + while(Serial.available() > 0) { Serial.read(); } - delay(5000); + delay(500); } -- 2.39.2