From: Vladimir Garistov Date: Fri, 5 Nov 2021 23:44:56 +0000 (+0200) Subject: Added an example for modyfying a timer's frequency while it's running X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=3dcbe842e64c32674efa8deb1104c9a816b24e36;p=vmks.git Added an example for modyfying a timer's frequency while it's running --- diff --git a/Examples/Big_LED_strip/Big_LED_strip.ino b/Examples/Big_LED_strip/Big_LED_strip.ino index 2a84342..60ee8ca 100644 --- a/Examples/Big_LED_strip/Big_LED_strip.ino +++ b/Examples/Big_LED_strip/Big_LED_strip.ino @@ -16,7 +16,7 @@ we must do so directly without allocating a working buffer. Uncomment this to indicate that display_rgb_direct() must be used. */ -#define NEOPIXEL_ATE_MY_RAM +//#define NEOPIXEL_ATE_MY_RAM //#define STM32 #ifdef STM32 @@ -92,7 +92,7 @@ void setup() void loop() { - #ifndef NEOPIXEL_ATE_MY_RAM + #ifdef NEOPIXEL_ATE_MY_RAM uint8_t frame_buffer[ROWS][COLS][3] = {{{0}}}; render_image(frame_buffer, big_image_data); diff --git a/Examples/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.ino b/Examples/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.ino new file mode 100644 index 0000000..4f1e4b7 --- /dev/null +++ b/Examples/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.ino @@ -0,0 +1,73 @@ +#define DIP_SW_0 8 +#define DIP_SW_1 9 +#define DIP_SW_2 10 +#define DIP_SW_3 11 + +#define BUTTON 2 +#define LED 13 + +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); + + attachInterrupt(digitalPinToInterrupt(BUTTON), button_ISR, FALLING); + + // Disable interrupts during timer configuration + cli(); + + // Zero the control and counter registers + TCCR1A = TCCR1B = TIMSK1 = 0; + TCNT1 = 0; + + // 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; + + // Enable timer overflow interrupt + TIMSK1 |= (1 << TOIE1); + + // Set prescaler to 1024 + TCCR1B |= (1 << CS10) | (1 << CS12); + + // Enable interrupts + sei(); + + Serial.begin(9600); +} + +void loop() +{ + Serial.println(OCR1A); + delay(1000); +} + +ISR(TIMER1_OVF_vect) +{ + digitalWrite(LED, led_flag); + led_flag = !led_flag; +} + +void button_ISR(void) +{ + uint8_t f = ~PINB & 0x0F; + + if (f) + { + OCR1A = 16000000 / (1024 * f) - 1; + } +} diff --git a/Examples/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.png b/Examples/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.png new file mode 100644 index 0000000..31a4373 Binary files /dev/null and b/Examples/Timer_DIP_switch_frequency/Timer_DIP_switch_frequency.png differ diff --git a/Examples/Timer_DIP_switch_frequency/tinkercad_link.txt b/Examples/Timer_DIP_switch_frequency/tinkercad_link.txt new file mode 100644 index 0000000..0095544 --- /dev/null +++ b/Examples/Timer_DIP_switch_frequency/tinkercad_link.txt @@ -0,0 +1 @@ + https://www.tinkercad.com/things/2g5g32R3Fb7