]> kolegite.com Git - vmks.git/commitdiff
Updated Timer Interrupt Examples
authorIvan Stefanov <istefanov@elsys-bg.org>
Tue, 24 Nov 2020 14:10:17 +0000 (16:10 +0200)
committerIvan Stefanov <istefanov@elsys-bg.org>
Tue, 24 Nov 2020 14:10:17 +0000 (16:10 +0200)
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.

Examples/Timer_1Hz_with_external_enable/Timer_1Hz with external enable.png [deleted file]
Examples/Timer_1Hz_with_external_enable/Timer_1Hz_with_external_enable.ino [deleted file]
Examples/Timer_1_10kHz_ISR/Timer_1_10kHz_ISR.ino [deleted file]
Examples/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino [new file with mode: 0644]
Examples/Timer_1_COMPA_and_OVF_10kHz_Software-PWM/Timer_1_COMPA_and_OVF_10kHz_Software-PWM.ino [new file with mode: 0644]
Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.ino [new file with mode: 0644]
Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.png [new file with mode: 0644]
Examples/Timer_ISR/Timer_ISR.ino [deleted file]
Examples/UART-Send/UART-Send.ino

diff --git a/Examples/Timer_1Hz_with_external_enable/Timer_1Hz with external enable.png b/Examples/Timer_1Hz_with_external_enable/Timer_1Hz with external enable.png
deleted file mode 100644 (file)
index fc72730..0000000
Binary files a/Examples/Timer_1Hz_with_external_enable/Timer_1Hz with external enable.png and /dev/null differ
diff --git a/Examples/Timer_1Hz_with_external_enable/Timer_1Hz_with_external_enable.ino b/Examples/Timer_1Hz_with_external_enable/Timer_1Hz_with_external_enable.ino
deleted file mode 100644 (file)
index 895f612..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-#define BTN_PIN   2
-
-void button_pressed_ISR(void);
-
-volatile uint8_t timer_en = 0;
-
-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
-  ICR1 = 15625;
-  OCR1A = 10000;
-
-  // Enable interrupts
-  interrupts();
-}
-
-void loop()
-{}
-
-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);
-    }
-}
diff --git a/Examples/Timer_1_10kHz_ISR/Timer_1_10kHz_ISR.ino b/Examples/Timer_1_10kHz_ISR/Timer_1_10kHz_ISR.ino
deleted file mode 100644 (file)
index b1ae812..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-void setup() {
-  pinMode(13, OUTPUT);
-  noInterrupts(); //cli(); // Stop reception of interrupts
-
-  // 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;
-  // Enable compare match interrupt
-  TIMSK1 |= (1 << OCIE1A);
-  TIMSK1 |= (1 << TOIE1);
-  // Set prescaler to 8 and starts PWM
-  TCCR1B |= (1 << CS11);
-
-  interrupts(); //sei();
-}
-
-void loop() {
-  // Empty
-}
-
-//Timer1 Compare Match Interrupt turns OFF pin 13 (LED)
-ISR(TIMER1_COMPA_vect) {
-  digitalWrite(13, LOW);
-}
-
-//Timer1 Overflow Interrupt turns ON pin 13 (LED)
-ISR(TIMER1_OVF_vect) {
-  digitalWrite(13, HIGH);
-}
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 (file)
index 0000000..d51c257
--- /dev/null
@@ -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_COMPA_and_OVF_10kHz_Software-PWM/Timer_1_COMPA_and_OVF_10kHz_Software-PWM.ino b/Examples/Timer_1_COMPA_and_OVF_10kHz_Software-PWM/Timer_1_COMPA_and_OVF_10kHz_Software-PWM.ino
new file mode 100644 (file)
index 0000000..a592e20
--- /dev/null
@@ -0,0 +1,54 @@
+void setup() {
+  // Set pin 13 to output
+  pinMode(13, OUTPUT);
+  
+  // 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;   // 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();
+}
+
+void loop() {
+  // Empty
+}
+
+//Timer1 Compare Match Interrupt turns OFF pin 13 (LED)
+ISR(TIMER1_COMPA_vect) {
+  digitalWrite(13, LOW);
+}
+
+//Timer1 Overflow Interrupt turns ON pin 13 (LED)
+ISR(TIMER1_OVF_vect) {
+  digitalWrite(13, HIGH);
+}
diff --git a/Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_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
new file mode 100644 (file)
index 0000000..e6bb3ef
--- /dev/null
@@ -0,0 +1,61 @@
+#define BTN_PIN   2
+
+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 1Hz
+  ICR1 = 15625;
+  OCR1A = 10000;
+
+  // Enable interrupts
+  interrupts();
+}
+
+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);
+  }
+}
diff --git a/Examples/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_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
new file mode 100644 (file)
index 0000000..fc72730
Binary files /dev/null and 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 differ
diff --git a/Examples/Timer_ISR/Timer_ISR.ino b/Examples/Timer_ISR/Timer_ISR.ino
deleted file mode 100644 (file)
index 8d11733..0000000
+++ /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;
-  }
-}
index d6b3048785b1323c50926c93bdbcba2870b78633..9470a171d055f147eb5321ab89dad63c28920dd9 100644 (file)
@@ -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);
 }