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);
}
--- /dev/null
+https://www.tinkercad.com/things/iVX5kSRkIeb-analogserialsend-ldr
\ No newline at end of file
* 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);
}
--- /dev/null
+https://www.tinkercad.com/things/4fzzU2QRVqM-uart-send
\ No newline at end of file
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;
}
digitalWrite(EN_L, HIGH);
*/
- //aservo.write(90);
+ aservo.write(90);
- /*
for (uint8_t i = 0; i < 4; i++)
{
forward(255);
}
digitalWrite(EN_L, LOW);
digitalWrite(EN_R, LOW);
- */
}
void forward(uint8_t speed)
void setup()
{
- pinMode(DRV_IN1, OUTPUT);
+ pinMode(DRV_IN1, OUTPUT);
pinMode(DRV_IN2, OUTPUT);
pinMode(DRV_EN, OUTPUT);
pinMode(POT, INPUT);
#include <inttypes.h>
-#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
#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
#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
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);
-}\r
+ 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);
+}
#include <inttypes.h>
-#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
#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
#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
void setup()
{
- stepper_init();
- timer1_init();
+ stepper_init();
+ timer1_init();
}
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);
}
-\r
+
/*
- 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;
+ }
}
--- /dev/null
+https://www.tinkercad.com/things/5oJQkL6MRSs-timer1compatogglepin13at1hz
\ No newline at end of file
-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);
}
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()
-{
-
-}
+{}
--- /dev/null
+https://www.tinkercad.com/things/jzFefsghwuC-timer1compmatchainterrupt
\ No newline at end of file
-#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);
+ }
}
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
}
// 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()
+{}
--- /dev/null
+https://www.tinkercad.com/things/7nyoEOqUo4Y-timerpwm10khzd25
\ No newline at end of file
// 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()
+{}
--- /dev/null
+https://www.tinkercad.com/things/9pjZJJ6DwZ5-timerpwm10khzd50
\ No newline at end of file
// 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()
+{}
--- /dev/null
+https://www.tinkercad.com/things/iQ6EdHcVRLC-timerpwm2hzd25
\ No newline at end of file
// 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()
+{}
--- /dev/null
+https://www.tinkercad.com/things/c215UduVTvv-timerpwm2hzd80
\ No newline at end of file