From: Vladimir Garistov Date: Wed, 9 Nov 2022 14:06:10 +0000 (+0200) Subject: Added tinkercad links and comments; formatting X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=f6abf07e7ab1a44534590b3931e412a6efdb3d8d;p=vmks.git Added tinkercad links and comments; formatting --- diff --git a/Examples/UART/Analog_Serial_Send_-_LDR/Analog_Serial_Send_-_LDR.ino b/Examples/UART/Analog_Serial_Send_-_LDR/Analog_Serial_Send_-_LDR.ino index 6cb7a7b..9459d13 100644 --- a/Examples/UART/Analog_Serial_Send_-_LDR/Analog_Serial_Send_-_LDR.ino +++ b/Examples/UART/Analog_Serial_Send_-_LDR/Analog_Serial_Send_-_LDR.ino @@ -4,13 +4,13 @@ uint16_t value = 0; void setup() { - pinMode(A0, INPUT); - Serial.begin(9600); + pinMode(LDR, INPUT); + Serial.begin(38400, SERIAL_8E2); } void loop() { - value = analogRead(LDR); - Serial.println(value); - delay(100); + value = analogRead(LDR); + Serial.println(value); + delay(100); } diff --git a/Examples/UART/Analog_Serial_Send_-_LDR/tinkercad_link.txt b/Examples/UART/Analog_Serial_Send_-_LDR/tinkercad_link.txt new file mode 100644 index 0000000..5e00277 --- /dev/null +++ b/Examples/UART/Analog_Serial_Send_-_LDR/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/iVX5kSRkIeb-analogserialsend-ldr \ No newline at end of file diff --git a/Examples/UART/UART-Send/UART-Send.ino b/Examples/UART/UART-Send/UART-Send.ino index 9470a17..c8564ed 100644 --- a/Examples/UART/UART-Send/UART-Send.ino +++ b/Examples/UART/UART-Send/UART-Send.ino @@ -5,20 +5,20 @@ * 0x0D => [CARRIAGE RETURN] */ -void setup() { - // put your setup code here, to run once: - Serial.begin(9600); +void setup() +{ + Serial.begin(9600); } -void loop() { - // put your main code here, to run repeatedly: - Serial.print('V'); // Sends 0x56 -// Serial.println('V'); // Sends 0x56 0x0D 0x0A -// Serial.write(0x56); // Sends 0x56 - - while(Serial.available() > 0) - { - Serial.read(); - } - delay(500); +void loop() +{ + Serial.print('V'); // Sends 0x56 +// Serial.println('V'); // Sends 0x56 0x0D 0x0A +// Serial.write(0x56); // Sends 0x56 + + while(Serial.available() > 0) + { + Serial.read(); + } + delay(500); } diff --git a/Examples/UART/UART-Send/UART-Send.png b/Examples/UART/UART-Send/UART-Send.png new file mode 100644 index 0000000..bebc3db Binary files /dev/null and b/Examples/UART/UART-Send/UART-Send.png differ diff --git a/Examples/UART/UART-Send/tinkercad_link.txt b/Examples/UART/UART-Send/tinkercad_link.txt new file mode 100644 index 0000000..5a371a3 --- /dev/null +++ b/Examples/UART/UART-Send/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/4fzzU2QRVqM-uart-send \ No newline at end of file diff --git a/Examples/interrupts/pin_change_demo/pin_change_demo.ino b/Examples/interrupts/pin_change_demo/pin_change_demo.ino index db5f18a..6160d0b 100644 --- a/Examples/interrupts/pin_change_demo/pin_change_demo.ino +++ b/Examples/interrupts/pin_change_demo/pin_change_demo.ino @@ -2,26 +2,31 @@ volatile bool change_detected = false; void setup() { - // put your setup code here, to run once: - DDRC &= ~((1 << DDC0) | (1 << DDC1) | (1 << DDC2) | (1 << DDC3)); - PORTC |= (1 << DDC0) | (1 << DDC1) | (1 << DDC2) | (1 << DDC3); - PCMSK1 |= (1 << PCINT8) | (1 << PCINT9) | (1 << PCINT10) | (1 << PCINT11); - PCICR |= (1 << PCIE1); - Serial.begin(38400, SERIAL_8E2); + // Configure pins PC0, PC1, PC2 and PC3 (A0, A1, A2 and A3) as inputs + DDRC &= ~((1 << DDC0) | (1 << DDC1) | (1 << DDC2) | (1 << DDC3)); + // Enable the built-in pull-up resistors for the same pins + PORTC |= (1 << DDC0) | (1 << DDC1) | (1 << DDC2) | (1 << DDC3); + // Configure these pins to generate group 1 pin change interrupt + PCMSK1 |= (1 << PCINT8) | (1 << PCINT9) | (1 << PCINT10) | (1 << PCINT11); + // Enable the group 1 pin change interrupt + PCICR |= (1 << PCIE1); + Serial.begin(38400, SERIAL_8E2); } void loop() { - if (change_detected) - { - delay(100); - Serial.print("Switch changed: "); - Serial.println(PINC & ((1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3)), BIN); - change_detected = false; - } + if (change_detected) + { + // Software debounce of contact bounce + delay(100); + Serial.print("Switch changed: "); + // Read the DIP switch and send the new value over UART + Serial.println(PINC & ((1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3)), BIN); + change_detected = false; + } } ISR(PCINT1_vect) { - change_detected = true; + change_detected = true; } diff --git a/Examples/motors/DC_motor_and_servo/DC_motor_and_servo.ino b/Examples/motors/DC_motor_and_servo/DC_motor_and_servo.ino index 8f1c9a1..50cfa23 100644 --- a/Examples/motors/DC_motor_and_servo/DC_motor_and_servo.ino +++ b/Examples/motors/DC_motor_and_servo/DC_motor_and_servo.ino @@ -42,9 +42,8 @@ void loop() digitalWrite(EN_L, HIGH); */ - //aservo.write(90); + aservo.write(90); - /* for (uint8_t i = 0; i < 4; i++) { forward(255); @@ -54,7 +53,6 @@ void loop() } digitalWrite(EN_L, LOW); digitalWrite(EN_R, LOW); - */ } void forward(uint8_t speed) diff --git a/Examples/motors/DC_motor_speed_control/DC_motor_speed_control.ino b/Examples/motors/DC_motor_speed_control/DC_motor_speed_control.ino index 086bee0..ac84a5a 100644 --- a/Examples/motors/DC_motor_speed_control/DC_motor_speed_control.ino +++ b/Examples/motors/DC_motor_speed_control/DC_motor_speed_control.ino @@ -5,7 +5,7 @@ void setup() { - pinMode(DRV_IN1, OUTPUT); + pinMode(DRV_IN1, OUTPUT); pinMode(DRV_IN2, OUTPUT); pinMode(DRV_EN, OUTPUT); pinMode(POT, INPUT); diff --git a/Examples/motors/Stepper_demo/stepper_demo_delay/stepper_demo_delay.ino b/Examples/motors/Stepper_demo/stepper_demo_delay/stepper_demo_delay.ino index b7ee2b0..0938633 100644 --- a/Examples/motors/Stepper_demo/stepper_demo_delay/stepper_demo_delay.ino +++ b/Examples/motors/Stepper_demo/stepper_demo_delay/stepper_demo_delay.ino @@ -1,10 +1,10 @@ #include -#define A1_PIN 9 -#define A2_PIN 8 -#define B1_PIN 11 -#define B2_PIN 10 -#define EN_PIN 12 +#define A1_PIN 9 +#define A2_PIN 8 +#define B1_PIN 11 +#define B2_PIN 10 +#define EN_PIN 12 #define STEP_TIME 1000 // ms @@ -15,10 +15,10 @@ #define NUM_STEPS 4 const uint8_t coil_current[NUM_STEPS][4] = { - {1, 0, 0, 1}, - {1, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 1, 1} + {1, 0, 0, 1}, + {1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 1} }; #endif @@ -26,14 +26,14 @@ const uint8_t coil_current[NUM_STEPS][4] = #define NUM_STEPS 8 const uint8_t coil_current[NUM_STEPS][4] = { - {1, 0, 0, 1}, - {1, 0, 0, 0}, - {1, 1, 0, 0}, - {0, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 1, 0}, - {0, 0, 1, 1}, - {0, 0, 0, 1} + {1, 0, 0, 1}, + {1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 1}, + {0, 0, 0, 1} }; #endif @@ -41,28 +41,28 @@ const uint8_t stepper_pins[4] = {A2_PIN, A1_PIN, B1_PIN, B2_PIN}; void setup() { - for (uint8_t i = 0; i < 4; i++) - { - pinMode(stepper_pins[i], OUTPUT); - } - pinMode(EN_PIN, OUTPUT); - digitalWrite(EN_PIN, HIGH); + for (uint8_t i = 0; i < 4; i++) + { + pinMode(stepper_pins[i], OUTPUT); + } + pinMode(EN_PIN, OUTPUT); + digitalWrite(EN_PIN, HIGH); } void loop() { - static uint8_t step = 0; - - for (uint8_t i = 0; i < 4; i++) - { - digitalWrite(stepper_pins[i], coil_current[step][i]); - } - - step++; - if (step >= NUM_STEPS) - { - step = 0; - } - - delay(STEP_TIME); -} + static uint8_t step = 0; + + for (uint8_t i = 0; i < 4; i++) + { + digitalWrite(stepper_pins[i], coil_current[step][i]); + } + + step++; + if (step >= NUM_STEPS) + { + step = 0; + } + + delay(STEP_TIME); +} diff --git a/Examples/motors/Stepper_demo/stepper_demo_timer/stepper_demo_timer.ino b/Examples/motors/Stepper_demo/stepper_demo_timer/stepper_demo_timer.ino index a4e8741..3626e64 100644 --- a/Examples/motors/Stepper_demo/stepper_demo_timer/stepper_demo_timer.ino +++ b/Examples/motors/Stepper_demo/stepper_demo_timer/stepper_demo_timer.ino @@ -1,10 +1,10 @@ #include -#define A1_PIN 9 -#define A2_PIN 8 -#define B1_PIN 11 -#define B2_PIN 10 -#define EN_PIN 12 +#define A1_PIN 9 +#define A2_PIN 8 +#define B1_PIN 11 +#define B2_PIN 10 +#define EN_PIN 12 #define STEP_TIME 200 // ms, max 1000 @@ -15,10 +15,10 @@ #define NUM_STEPS 4 const uint8_t coil_current[NUM_STEPS][4] = { - {1, 0, 0, 1}, - {1, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 1, 1} + {1, 0, 0, 1}, + {1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 1} }; #endif @@ -26,14 +26,14 @@ const uint8_t coil_current[NUM_STEPS][4] = #define NUM_STEPS 8 const uint8_t coil_current[NUM_STEPS][4] = { - {1, 0, 0, 1}, - {1, 0, 0, 0}, - {1, 1, 0, 0}, - {0, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 1, 0}, - {0, 0, 1, 1}, - {0, 0, 0, 1} + {1, 0, 0, 1}, + {1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 1}, + {0, 0, 0, 1} }; #endif @@ -44,8 +44,8 @@ const uint8_t stepper_pins[4] = {A2_PIN, A1_PIN, B1_PIN, B2_PIN}; void setup() { - stepper_init(); - timer1_init(); + stepper_init(); + timer1_init(); } void loop() @@ -53,47 +53,47 @@ void loop() void timer1_init(void) { - // Disable interrupts during timer configuration - noInterrupts(); - // Select CTC mode (4) and set the prescaler to divide the clock by 256 - // resulting in timer frequency of 62.5 kHz - TCCR1A = 0; - TCCR1B = (1 << WGM12) | (1 << CS12); - // Enable interrupt when reaching the maximum value (OCR1A) - TIMSK1 = (1 << OCIE1A); - // Set the timer period to 50 ms (50 * 62.5 = 3125) - OCR1A = (int) STEP_TIME * 62.5; - // Reset OC register B and the input capture register. They are not used - ICR1 = OCR1B = 0; - // Reset the counter register - TCNT1 = 0; - // Enable inerrupts - interrupts(); + // Disable interrupts during timer configuration + noInterrupts(); + // Select CTC mode (4) and set the prescaler to divide the clock by 256 + // resulting in timer frequency of 62.5 kHz + TCCR1A = 0; + TCCR1B = (1 << WGM12) | (1 << CS12); + // Enable interrupt when reaching the maximum value (OCR1A) + TIMSK1 = (1 << OCIE1A); + // Set the timer period to 50 ms (50 * 62.5 = 3125) + OCR1A = (int) STEP_TIME * 62.5; + // Reset OC register B and the input capture register. They are not used + ICR1 = OCR1B = 0; + // Reset the counter register + TCNT1 = 0; + // Enable inerrupts + interrupts(); } ISR(TIMER1_COMPA_vect) { - static volatile uint8_t step = 0; - - for (uint8_t i = 0; i < 4; i++) - { - digitalWrite(stepper_pins[i], coil_current[step][i]); - } - - step++; - if (step >= NUM_STEPS) - { - step = 0; - } + static volatile uint8_t step = 0; + + for (uint8_t i = 0; i < 4; i++) + { + digitalWrite(stepper_pins[i], coil_current[step][i]); + } + + step++; + if (step >= NUM_STEPS) + { + step = 0; + } } void stepper_init(void) { - for (uint8_t i = 0; i < 4; i++) - { - pinMode(stepper_pins[i], OUTPUT); - } - pinMode(EN_PIN, OUTPUT); - digitalWrite(EN_PIN, HIGH); + for (uint8_t i = 0; i < 4; i++) + { + pinMode(stepper_pins[i], OUTPUT); + } + pinMode(EN_PIN, OUTPUT); + digitalWrite(EN_PIN, HIGH); } - + diff --git a/Examples/timer/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino b/Examples/timer/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino index d603aa4..555df64 100644 --- a/Examples/timer/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino +++ b/Examples/timer/Timer_1_COMPA_Toggle_pin_13_at_1Hz/Timer_1_COMPA_Toggle_pin_13_at_1Hz.ino @@ -1,54 +1,63 @@ /* - 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 + 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 setup() +{ + // Set pin 13 as output + pinMode(13, OUTPUT); + // Stop reception of interrupts + noInterrupts(); //cli(); + + // Set Timer 1 Control registers and counter to 0 + TCCR1A = 0; + TCCR1B = 0; + TCNT1 = 0; + OCR1B = 0; + TIMSK1 = 0; + TIFR1 = 0xFF; + + // Set compare match value = Fclk_io / (Fout * Prescaler) - 1 + OCR1A = 15624; // = (16MHz) / (1 * 1024) - 1 + // Enable compare match interrupt + TIMSK1 |= (1 << OCIE1A); + + // 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) + + // Allow reception of interrupts + interrupts(); //sei(); } -void loop() { - // put your main code here, to run repeatedly: -} +void loop() +{} /* - Timer1 Interrupt Service Routine (ISR) - Toggle pin 13 (LED) every 1s - 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 + Timer1 Interrupt Service Routine (ISR) + Toggle pin 13 (LED) every 1s + 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 */ -ISR(TIMER1_COMPA_vect) { - if (toggle == 1) { - digitalWrite(13, HIGH); - toggle = 0; - } - else { - digitalWrite(13, LOW); - toggle = 1; - } +ISR(TIMER1_COMPA_vect) +{ + if (toggle == 1) + { + digitalWrite(13, HIGH); + toggle = 0; + } + else + { + digitalWrite(13, LOW); + toggle = 1; + } } diff --git a/Examples/timer/Timer_1_COMPA_Toggle_pin_13_at_1Hz/tinkercad_link.txt b/Examples/timer/Timer_1_COMPA_Toggle_pin_13_at_1Hz/tinkercad_link.txt new file mode 100644 index 0000000..f9e030b --- /dev/null +++ b/Examples/timer/Timer_1_COMPA_Toggle_pin_13_at_1Hz/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/5oJQkL6MRSs-timer1compatogglepin13at1hz \ No newline at end of file diff --git a/Examples/timer/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino b/Examples/timer/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino index efa2118..f4d4632 100644 --- a/Examples/timer/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino +++ b/Examples/timer/Timer_1_COMPA_and_OVF_10kHz_PWM/Timer_1_COMPA_and_OVF_10kHz_PWM.ino @@ -1,62 +1,70 @@ -void setup() { - // Set pin 13 to output - pinMode(13, OUTPUT); - - // Stop reception of interrupts - noInterrupts(); //cli(); +void setup() +{ + // Set pin 13 to output + pinMode(13, OUTPUT); + + // Stop reception of interrupts + noInterrupts(); //cli(); - // Set PB1 to be an output (Pin-9 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); - TCCR1A |= (1 << COM1B1); - - // 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% - OCR1B = 50; - - // Enable compare match interrupt - TIMSK1 |= (1 << OCIE1A); - TIMSK1 |= (1 << OCIE1B); - TIMSK1 |= (1 << TOIE1); - - // Set prescaler to 8 and starts PWM - TCCR1B |= (1 << CS11); + // Set PB1 to be an output (Pin-9 Arduino UNO; OC1A) + DDRB |= (1 << PB1); + + // Clear Timer/Counter Control Registers + TCCR1A = 0; + TCCR1B = 0; + TIMSK1 = 0; + TIFR1 = 0xFF; + + // 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% + OCR1B = 50; + + // Enable compare match and overflow interrupts + TIMSK1 |= (1 << OCIE1A); + TIMSK1 |= (1 << OCIE1B); + TIMSK1 |= (1 << TOIE1); + + // Set prescaler to 8 and starts PWM + TCCR1B |= (1 << CS11); - // Enables interrupts - interrupts(); //sei(); + // Enables interrupts + interrupts(); //sei(); } -void loop() { - // Empty +void loop() +{ + // Empty } -//Timer1 Compare Match Interrupt turns OFF pin 13 (LED) -ISR(TIMER1_COMPA_vect) { - digitalWrite(13, LOW); +// NOTE: TinkerCAD does not simulate timer interrupts correctly when +// something is connected to the timer pins. + +// Not used, just demonstrating how to define it +ISR(TIMER1_COMPA_vect) +{ + // Empty } //Timer1 Compare Match Interrupt turns OFF pin 13 (LED) -ISR(TIMER1_COMPB_vect) { - //digitalWrite(13, LOW); +ISR(TIMER1_COMPB_vect) +{ + digitalWrite(13, LOW); } //Timer1 Overflow Interrupt turns ON pin 13 (LED) -ISR(TIMER1_OVF_vect) { - digitalWrite(13, HIGH); +ISR(TIMER1_OVF_vect) +{ + digitalWrite(13, HIGH); } diff --git a/Examples/timer/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino b/Examples/timer/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino index 99a5bfc..ae59009 100644 --- a/Examples/timer/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino +++ b/Examples/timer/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.ino @@ -2,51 +2,48 @@ 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(); + // Забраняваме глобално прекъсванията + noInterrupts(); //cli(); + + // Зануляване на конфигурацията + TCCR1A = 0; + TCCR1B = 0; + TCNT1 = 0; + OCR1B = 0; + TIMSK1 = 0; + TIFR1 = 0xFF; + + // Режим на таймера - 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++; + var++; } void loop() -{ - -} +{} diff --git a/Examples/timer/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.png b/Examples/timer/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.png new file mode 100644 index 0000000..bebc3db Binary files /dev/null and b/Examples/timer/Timer_1_CompMatchA_Interrupt/Timer_1_CompMatchA_Interrupt.png differ diff --git a/Examples/timer/Timer_1_CompMatchA_Interrupt/tinkercad_link.txt b/Examples/timer/Timer_1_CompMatchA_Interrupt/tinkercad_link.txt new file mode 100644 index 0000000..7d54c01 --- /dev/null +++ b/Examples/timer/Timer_1_CompMatchA_Interrupt/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/jzFefsghwuC-timer1compmatchainterrupt \ No newline at end of file diff --git a/Examples/timer/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/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.ino index e6bb3ef..7efe474 100644 --- a/Examples/timer/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/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable/Timer_1_OC1A_Blink_pin_9_at_1Hz_with_external_enable.ino @@ -1,61 +1,66 @@ -#define BTN_PIN 2 +#define BTN_PIN 2 volatile uint8_t timer_en = 0; void button_pressed_ISR(void); +// NOTE: TinkerCAD has some issues when simulating timers, it's better to run this on real hardware + void setup() { - // Stop reception of interrupts - noInterrupts(); //cli(); + // Stop reception of interrupts + noInterrupts(); //cli(); - pinMode(BTN_PIN, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed_ISR, FALLING); + 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); + // Set PB1 to be an output (Pin9 Arduino UNO) + DDRB |= (1 << PB1); - // Clear Timer/Counter Control Registers - TCCR1A = 0; - TCCR1B = 0; - TIMSK1 = 0; + // Clear Timer/Counter Control Registers + TCCR1A = 0; + TCCR1B = 0; + OCR1B = 0; + TIMSK1 = 0; + TIFR1 = 0xFF; - // Set non-inverting mode - Table 15-3 (page 108) - TCCR1A |= (1 << COM1A1); + // 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); + // 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; + // Clear Timer 1 Counter + TCNT1 = 0; - // Set PWM frequency/top value - Output PWM 1Hz - ICR1 = 15625; - OCR1A = 10000; + // Set PWM frequency/top value - Output PWM 1Hz + ICR1 = 15625; + OCR1A = 10000; - // Enable interrupts - interrupts(); + // Enable interrupts + interrupts(); } -void loop() { - // some useless code here - } +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/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.ino b/Examples/timer/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.ino index 4f1e4b7..a421d8e 100644 --- a/Examples/timer/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.ino +++ b/Examples/timer/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.ino @@ -8,66 +8,70 @@ void button_ISR(void); -volatile bool led_flag = false; - void setup() { - pinMode(DIP_SW_0, INPUT_PULLUP); - pinMode(DIP_SW_1, INPUT_PULLUP); - pinMode(DIP_SW_2, INPUT_PULLUP); - pinMode(DIP_SW_3, INPUT_PULLUP); - pinMode(BUTTON, INPUT_PULLUP); - pinMode(LED, OUTPUT); - digitalWrite(LED, LOW); + pinMode(DIP_SW_0, INPUT_PULLUP); + pinMode(DIP_SW_1, INPUT_PULLUP); + pinMode(DIP_SW_2, INPUT_PULLUP); + pinMode(DIP_SW_3, INPUT_PULLUP); + pinMode(BUTTON, INPUT_PULLUP); + pinMode(LED, OUTPUT); + digitalWrite(LED, LOW); - attachInterrupt(digitalPinToInterrupt(BUTTON), button_ISR, FALLING); + attachInterrupt(digitalPinToInterrupt(BUTTON), button_ISR, FALLING); - // Disable interrupts during timer configuration - cli(); + // Disable interrupts during timer configuration + cli(); - // Zero the control and counter registers - TCCR1A = TCCR1B = TIMSK1 = 0; - TCNT1 = 0; + // Zero the control and counter registers + TCCR1A = TCCR1B = TIMSK1 = OCR1B = 0; + TCNT1 = 0; + TIFR1 = 0; - // Select mode 15 (Fast PWM, TOP = OCR1A) - TCCR1A |= (1 << WGM10); - TCCR1A |= (1 << WGM11); - TCCR1B |= (1 << WGM12); - TCCR1B |= (1 << WGM13); + // Select mode 15 (Fast PWM, TOP = OCR1A) + TCCR1A |= (1 << WGM10); + TCCR1A |= (1 << WGM11); + TCCR1B |= (1 << WGM12); + TCCR1B |= (1 << WGM13); - // Set max counter value (frequency) - OCR1A = 7811; + // Set max counter value (frequency) + OCR1A = 7811; - // Enable timer overflow interrupt - TIMSK1 |= (1 << TOIE1); + // Enable timer overflow interrupt + TIMSK1 |= (1 << TOIE1); - // Set prescaler to 1024 - TCCR1B |= (1 << CS10) | (1 << CS12); + // Set prescaler to 1024 + TCCR1B |= (1 << CS10) | (1 << CS12); - // Enable interrupts - sei(); + // Enable interrupts + sei(); - Serial.begin(9600); + Serial.begin(9600); } void loop() { - Serial.println(OCR1A); - delay(1000); + Serial.println(OCR1A); + delay(1000); } ISR(TIMER1_OVF_vect) { - digitalWrite(LED, led_flag); - led_flag = !led_flag; + static volatile bool led_flag = false; + + digitalWrite(LED, led_flag); + led_flag = !led_flag; } void button_ISR(void) { - uint8_t f = ~PINB & 0x0F; - - if (f) - { - OCR1A = 16000000 / (1024 * f) - 1; - } + // Read the DIP switch + uint8_t f = ~PINB & 0x0F; + + if (f) + { + // Convert frequency to OCR1A value + OCR1A = 16000000 / (1024 * f) - 1; + } + // Instead of ignoring the case f == 0, you could also disable the timer } diff --git a/Examples/timer/Timer_PWM_10kHz_D25/Timer_PWM_10kHz_D25.ino b/Examples/timer/Timer_PWM_10kHz_D25/Timer_PWM_10kHz_D25.ino index 636d6cc..3209246 100644 --- a/Examples/timer/Timer_PWM_10kHz_D25/Timer_PWM_10kHz_D25.ino +++ b/Examples/timer/Timer_PWM_10kHz_D25/Timer_PWM_10kHz_D25.ino @@ -1,23 +1,26 @@ // Timer 1 - Mode 14 // f = 10kHz ; δ = 25% -void setup() { +void setup() +{ + // Configure the OC1A pin as an output + DDRB |= (1 << PB1); - DDRB |= (1 << PB1); - - // Zero - TCCR1A = 0; - TCCR1B = 0; - - // Set stuff - TCCR1A |= (1 << WGM11) | (1 << COM1A1); - TCCR1B |= (1 << WGM13) | (1 << WGM12); - ICR1 = 1599; // Period (TOP) - OCR1A = 400; // Duty cycle - TCCR1B |= (1 << CS10); + // Zero + TCCR1A = 0; + TCCR1B = 0; + TIMSK1 = 0; + OCR1B = 0; + TIFR1 = 0xFF; + // Set timer mode and output mode + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM13) | (1 << WGM12); + ICR1 = 1599; // Period (TOP) + OCR1A = 400; // Duty cycle + // Activate the timer by selecting the clock source + TCCR1B |= (1 << CS10); } -void loop() { - -} +void loop() +{} diff --git a/Examples/timer/Timer_PWM_10kHz_D25/Timer_PWM_10kHz_D25.png b/Examples/timer/Timer_PWM_10kHz_D25/Timer_PWM_10kHz_D25.png new file mode 100644 index 0000000..36e3627 Binary files /dev/null and b/Examples/timer/Timer_PWM_10kHz_D25/Timer_PWM_10kHz_D25.png differ diff --git a/Examples/timer/Timer_PWM_10kHz_D25/tinkercad_link.txt b/Examples/timer/Timer_PWM_10kHz_D25/tinkercad_link.txt new file mode 100644 index 0000000..3ba3ff2 --- /dev/null +++ b/Examples/timer/Timer_PWM_10kHz_D25/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/7nyoEOqUo4Y-timerpwm10khzd25 \ No newline at end of file diff --git a/Examples/timer/Timer_PWM_10kHz_D50/Timer_PWM_10kHz_D50.ino b/Examples/timer/Timer_PWM_10kHz_D50/Timer_PWM_10kHz_D50.ino index 6b60731..fde4219 100644 --- a/Examples/timer/Timer_PWM_10kHz_D50/Timer_PWM_10kHz_D50.ino +++ b/Examples/timer/Timer_PWM_10kHz_D50/Timer_PWM_10kHz_D50.ino @@ -1,23 +1,26 @@ // Timer 1 - Mode 14 // f = 10kHz ; δ = 50% -void setup() { +void setup() +{ + // Configure the OC1A pin as an output + DDRB |= (1 << PB1); - DDRB |= (1 << PB1); - - // Zero - TCCR1A = 0; - TCCR1B = 0; - - // Set stuff - TCCR1A |= (1 << WGM11) | (1 << COM1A1); - TCCR1B |= (1 << WGM13) | (1 << WGM12); - ICR1 = 1599; // Period (TOP) - OCR1A = 800; // Duty cycle - TCCR1B |= (1 << CS10); + // Zero + TCCR1A = 0; + TCCR1B = 0; + TIMSK1 = 0; + OCR1B = 0; + TIFR1 = 0xFF; + // Set timer mode and output mode + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM13) | (1 << WGM12); + ICR1 = 1599; // Period (TOP) + OCR1A = 800; // Duty cycle + // Activate the timer by selecting the clock source + TCCR1B |= (1 << CS10); } -void loop() { - -} +void loop() +{} diff --git a/Examples/timer/Timer_PWM_10kHz_D50/Timer_PWM_10kHz_D50.png b/Examples/timer/Timer_PWM_10kHz_D50/Timer_PWM_10kHz_D50.png new file mode 100644 index 0000000..36e3627 Binary files /dev/null and b/Examples/timer/Timer_PWM_10kHz_D50/Timer_PWM_10kHz_D50.png differ diff --git a/Examples/timer/Timer_PWM_10kHz_D50/tinkercad_link.txt b/Examples/timer/Timer_PWM_10kHz_D50/tinkercad_link.txt new file mode 100644 index 0000000..2c57a57 --- /dev/null +++ b/Examples/timer/Timer_PWM_10kHz_D50/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/9pjZJJ6DwZ5-timerpwm10khzd50 \ No newline at end of file diff --git a/Examples/timer/Timer_PWM_2Hz_D25/Timer_PWM_2Hz_D25.ino b/Examples/timer/Timer_PWM_2Hz_D25/Timer_PWM_2Hz_D25.ino index ca156da..b14530a 100644 --- a/Examples/timer/Timer_PWM_2Hz_D25/Timer_PWM_2Hz_D25.ino +++ b/Examples/timer/Timer_PWM_2Hz_D25/Timer_PWM_2Hz_D25.ino @@ -1,23 +1,26 @@ // Timer 1 - Mode 14 // f = 2Hz ; δ = 25% -void setup() { +void setup() +{ + // Configure the OC1A pin as an output + DDRB |= (1 << PB1); - DDRB |= (1 << PB1); + // Zero + TCCR1A = 0; + TCCR1B = 0; + TIMSK1 = 0; + OCR1B = 0; + TIFR1 = 0xFF; - // Zero - TCCR1A = 0; - TCCR1B = 0; - - // Set stuff - TCCR1A |= (1 << WGM11) | (1 << COM1A1); - TCCR1B |= (1 << WGM13) | (1 << WGM12); - ICR1 = 31249; // Period (TOP) - OCR1A = 7812; // Duty cycle - TCCR1B |= (1 << CS12); - + // Set timer mode and output mode + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM13) | (1 << WGM12); + ICR1 = 31249; // Period (TOP) + OCR1A = 7812; // Duty cycle + // Activate the timer by selecting the clock source + TCCR1B |= (1 << CS12); } -void loop() { - -} +void loop() +{} diff --git a/Examples/timer/Timer_PWM_2Hz_D25/Timer_PWM_2Hz_D25.png b/Examples/timer/Timer_PWM_2Hz_D25/Timer_PWM_2Hz_D25.png new file mode 100644 index 0000000..36e3627 Binary files /dev/null and b/Examples/timer/Timer_PWM_2Hz_D25/Timer_PWM_2Hz_D25.png differ diff --git a/Examples/timer/Timer_PWM_2Hz_D25/tinkercad_link.txt b/Examples/timer/Timer_PWM_2Hz_D25/tinkercad_link.txt new file mode 100644 index 0000000..449659b --- /dev/null +++ b/Examples/timer/Timer_PWM_2Hz_D25/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/iQ6EdHcVRLC-timerpwm2hzd25 \ No newline at end of file diff --git a/Examples/timer/Timer_PWM_2Hz_D80/Timer_PWM_2Hz_D80.ino b/Examples/timer/Timer_PWM_2Hz_D80/Timer_PWM_2Hz_D80.ino index 4698a58..0d8eb61 100644 --- a/Examples/timer/Timer_PWM_2Hz_D80/Timer_PWM_2Hz_D80.ino +++ b/Examples/timer/Timer_PWM_2Hz_D80/Timer_PWM_2Hz_D80.ino @@ -1,23 +1,26 @@ // Timer 1 - Mode 14 // f = 2Hz ; δ = 80% -void setup() { +void setup() +{ + // Configure the OC1A pin as an output + DDRB |= (1 << PB1); - DDRB |= (1 << PB1); + // Zero + TCCR1A = 0; + TCCR1B = 0; + TIMSK1 = 0; + OCR1B = 0; + TIFR1 = 0xFF; - // Zero - TCCR1A = 0; - TCCR1B = 0; - - // Set stuff - TCCR1A |= (1 << WGM11) | (1 << COM1A1); - TCCR1B |= (1 << WGM13) | (1 << WGM12); - ICR1 = 31249; // Period (TOP) - OCR1A = 25000; // Duty cycle - TCCR1B |= (1 << CS12); - + // Set timer mode and output mode + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM13) | (1 << WGM12); + ICR1 = 31249; // Period (TOP) + OCR1A = 25000; // Duty cycle + // Activate the timer by selecting the clock source + TCCR1B |= (1 << CS12); } -void loop() { - -} +void loop() +{} diff --git a/Examples/timer/Timer_PWM_2Hz_D80/Timer_PWM_2Hz_D80.png b/Examples/timer/Timer_PWM_2Hz_D80/Timer_PWM_2Hz_D80.png new file mode 100644 index 0000000..36e3627 Binary files /dev/null and b/Examples/timer/Timer_PWM_2Hz_D80/Timer_PWM_2Hz_D80.png differ diff --git a/Examples/timer/Timer_PWM_2Hz_D80/tinkercad_link.txt b/Examples/timer/Timer_PWM_2Hz_D80/tinkercad_link.txt new file mode 100644 index 0000000..1bc8af0 --- /dev/null +++ b/Examples/timer/Timer_PWM_2Hz_D80/tinkercad_link.txt @@ -0,0 +1 @@ +https://www.tinkercad.com/things/c215UduVTvv-timerpwm2hzd80 \ No newline at end of file