]> kolegite.com Git - vmks.git/commitdiff
Upload Timer1 Configuration example
authorIvan Stefanov <istefanov@elsys-bg.org>
Tue, 12 Jan 2021 17:18:55 +0000 (19:18 +0200)
committerIvan Stefanov <istefanov@elsys-bg.org>
Tue, 12 Jan 2021 17:18:55 +0000 (19:18 +0200)
Example for configuring Timer 1 with Output Compare Match A Interrupt

Examples/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino [new file with mode: 0644]

diff --git a/Examples/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino b/Examples/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino
new file mode 100644 (file)
index 0000000..99a5bfc
--- /dev/null
@@ -0,0 +1,52 @@
+volatile uint16_t var = 0;
+
+void setup()
+{
+  noInterrupts(); // Забраняваме глобално прекъсванията //cli();
+  
+  // Зануляване на конфигурацията
+  TCCR1A = 0;
+  TCCR1B = 0;
+  TCCR1C = 0;
+  TCNT1 = 0;
+  OCR1A = 0;
+  OCR1B = 0;
+  ICR1 = 0;
+  TIMSK1 = 0;
+  TIFR1 = 0;
+  
+  // Режим на таймера - 14 Fast-PWM  
+  TCCR1A = TCCR1A | (1 << WGM11);
+  TCCR1B = TCCR1B | (1 << WGM12);
+  TCCR1B = TCCR1B | (1 << WGM13);
+  // TCCR1A |= (1 << WGM11);
+  // TCCR1B = TCCR1B | (1 << WGM12) | (1 << WGM13);
+  
+  // Режим на изводите - неинв.
+  TCCR1A = TCCR1A | (1 << COM1A1) | (1 << COM1B1);
+  
+  // Честота - 500Hz
+  // Избираме Prescaler = 1
+  ICR1 = 31999; // TOP = 16MHZ/(1*500Hz)-1 = 31999
+  
+  // Коеф. на запълване - 75%
+  OCR1A = 23999; // DUTY = TOP * 75% = 31999*75% = 23999
+  
+  // Прекъсване - CM A
+  TIMSK1 |= (1 << OCIE1A);
+  
+  // Пускане на таймера
+  TCCR1B |= (1 << CS10);
+  
+  interrupts(); // Разрешаваме глобално прекъсванията //sei();
+}
+
+ISR(TIMER1_COMPA_vect)
+{
+  var++;
+}
+
+void loop()
+{
+
+}