Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
orginal code via:
* Copyright (c) 2017 pcbreflux. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*
modified by @kosso
*/
hw_timer_t * timer0 = NULL;
hw_timer_t * timer1 = NULL;
portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux1 = portMUX_INITIALIZER_UNLOCKED;
volatile uint8_t led1stat = 0;
volatile uint8_t led2stat = 0;
int steps0 = 200;
int steps1 = 200;
int count = 0;
boolean toggle = false;
void IRAM_ATTR onTimer0(){
// Critical Code here
portENTER_CRITICAL_ISR(&timerMux0);
led1stat=1-led1stat;
digitalWrite(14, led1stat); // turn the LED on or off
portEXIT_CRITICAL_ISR(&timerMux0);
}
void IRAM_ATTR onTimer1(){
// Critical Code here
portENTER_CRITICAL_ISR(&timerMux1);
led2stat=1-led2stat;
digitalWrite(27, led2stat); // turn the LED on or off
portEXIT_CRITICAL_ISR(&timerMux1);
}
void setup() {
Serial.begin(115200);
pinMode(27, OUTPUT);
pinMode(14, OUTPUT);
digitalWrite(27, LOW); // turn the LED off by making the voltage LOW
digitalWrite(14, LOW); // turn the LED off by making the voltage LOW
//delay(2000);
Serial.println("start timer 1");
timer1 = timerBegin(1, 80, true); // timer 1, MWDT clock period = 12.5 ns * TIMGn_Tx_WDT_CLK_PRESCALE -> 12.5 ns * 80 -> 1000 ns = 1 us, countUp
timerAttachInterrupt(timer1, &onTimer1, true); // edge (not level) triggered
timerAlarmWrite(timer1, 25000, true); // 250000 * 1 us = 250 ms, autoreload true
Serial.println("start timer 0");
timer0 = timerBegin(0, 80, true); // timer 0, MWDT clock period = 12.5 ns * TIMGn_Tx_WDT_CLK_PRESCALE -> 12.5 ns * 80 -> 1000 ns = 1 us, countUp
timerAttachInterrupt(timer0, &onTimer0, true); // edge (not level) triggered
timerAlarmWrite(timer0, 200000, true); // 2000000 * 1 us = 2 s, autoreload true
// at least enable the timer alarms
timerAlarmEnable(timer0); // enable
timerAlarmEnable(timer1); // enable
}
void loop() {
// nope nothing here
//vTaskDelay(portMAX_DELAY); // wait as much as posible ...
count++;
Serial.println("hmm " + String(count));
delay(1000);
// Serial.println("erm" + String(count));
// delay(1500);
/*
// test stopping timer1
if(count == 10){
Serial.println("disable timer1 alarm");
timerAlarmDisable(timer1);
Serial.println("stop timer2");
timerStop(timer1);
timerDetachInterrupt(timer1);
timerEnd(timer1);
timer1 = NULL;
digitalWrite(27, LOW);
}
// test stopping timer0
if(count == 20){
Serial.println("disable timer0 alarm");
timerAlarmDisable(timer0);
Serial.println("stop timer0");
timerStop(timer0);
timerDetachInterrupt(timer0);
timerEnd(timer0);
timer0 = NULL;
digitalWrite(14, LOW);
}
*/
if(count == 30){
Serial.println("wait a bit");
//delay(5000);
Serial.println("reset and change speeds");
count = 0;
// toggle speeds every 30 counts
int speed0 = (toggle == true)?200000:25000;
int speed1 = (toggle == true)?25000:200000;
toggle = !toggle;
Serial.println("start timer 0 at " + String(speed0));
timer0 = timerBegin(0, 80, true);
timerAttachInterrupt(timer0, &onTimer0, true);
timerAlarmWrite(timer0, speed0, true);
Serial.println("start timer 1 at " + String(speed1));
timer1 = timerBegin(1, 80, true);
timerAttachInterrupt(timer1, &onTimer1, true);
timerAlarmWrite(timer1, speed1, true);
//timerStart(timer0);
//timerStart(timer1);
timerAlarmEnable(timer0);
timerAlarmEnable(timer1);
Serial.println("go at swapped speeds!!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment