I am currently working on a project using the bluepill (F103C8T6), and I need to use HardwareTimer on TIM3. Not only it is not working, I think that it is blocking execution of what is after it (so it must be failling at runtime). I have tested the other timers with their respective channels and they work alright, also I tested on more than one board, originals and clones. Also curious: The analogWrite functionality for these pins (PA6, PA7, PB0 and PB1) works alright.
Here is the code:
Code: Select all
#include "RGBLed.h"
//#define LED_R PB7
//#define LED_G PB8
//#define LED_B PA9
#define PROBABILITYINPUT PA0
#define STEPSINPUT PA2
#define RANGEINPUT PA1
#define MODESWITCH PA3
#define OUTPUT1 PA6 //They must be from the same timer, in this case timer 3
#define OUTPUT2 PA7
#define OUTPUT3 PB0
#define OUTPUT4 PB1
//
//#define OUTPUT1 PB6 //They must be from the same timer, in this case timer 4
//#define OUTPUT2 PB7
//#define OUTPUT3 PB8
//#define OUTPUT4 PB9
#define TRIGGER1_INPUT PA15
#define TRIGGER2_INPUT PA12 //revisar, el bluepill ya tiene un pullup acá por el USB, esta entrada tiene que ser pulldown
#define TRIGGER3_INPUT PA5
#define TRIGGER4_INPUT PA11
#define TRIGGER2_SWITCH PB3
#define TRIGGER3_SWITCH PB11
#define TRIGGER4_SWITCH PB10
#define GATE2_OUTPUT PB12
#define GATE3_OUTPUT PB13
#define GATE4_OUTPUT PB14
#define OUTPUT_HIGH LOW
#define OUTPUT_LOW HIGH
//#define BUTTON1 PB6
#define BUTTON2 PB5
#define BUTTON3 PB4
void setup() {
// put your setup code here, to run once:
//pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(BUTTON3, INPUT_PULLUP);
pinMode(TRIGGER1_INPUT, INPUT_PULLDOWN);
pinMode(TRIGGER2_INPUT, INPUT_PULLDOWN);
pinMode(TRIGGER3_INPUT, INPUT_PULLDOWN);
pinMode(TRIGGER4_INPUT, INPUT_PULLDOWN);
pinMode(TRIGGER2_SWITCH, INPUT_PULLUP);
pinMode(TRIGGER3_SWITCH, INPUT_PULLUP);
pinMode(TRIGGER4_SWITCH, INPUT_PULLUP);
pinMode(GATE2_OUTPUT, OUTPUT);
digitalWrite(GATE2_OUTPUT, LOW);
pinMode(GATE3_OUTPUT, OUTPUT);
digitalWrite(GATE3_OUTPUT, LOW);
pinMode(GATE4_OUTPUT, OUTPUT);
digitalWrite(GATE4_OUTPUT, LOW);
HardwareTimer *MyTim = new HardwareTimer(TIM3);
MyTim->setMode(1, TIMER_OUTPUT_COMPARE_PWM1, PA6);
MyTim->setMode(2, TIMER_OUTPUT_COMPARE_PWM1, PA7);
MyTim->setMode(3, TIMER_OUTPUT_COMPARE_PWM1, PB0);
MyTim->setMode(4, TIMER_OUTPUT_COMPARE_PWM1, PB1);
MyTim->setPrescaleFactor(1);
MyTim->setOverflow(8000);
MyTim->resume();
MyTim->setCaptureCompare(1, 4000);
MyTim->setCaptureCompare(2, 4000);
MyTim->setCaptureCompare(3, 4000);
MyTim->setCaptureCompare(4, 4000);
}