+++ /dev/null
-#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);
- }
-}
+++ /dev/null
-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);
-}
--- /dev/null
+/*
+ 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;
+ }
+}
--- /dev/null
+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);
+}
--- /dev/null
+#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);
+ }
+}
+++ /dev/null
-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;
- }
-}
+/*
+ * ASCII => Char
+ * 0x56 => V
+ * 0x0A => [LINE FEED]
+ * 0x0D => [CARRIAGE RETURN]
+ */
+
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
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);
}