This program has a generator and blinking LED.
How to modify it to get PWM signal on PB0 when the LED is ON and no signal when the LED is OFF ?
So far I have this error.
C:\Users\OWNER\AppData\Local\Temp\arduino_modified_sketch_892346\Blink.ino: In function ‘void loop()’:
Blink:34: error: lvalue required as left operand of assignment
if (PB1=1)
^
exit status 1
lvalue required as left operand of assignment
HardwareTimer pwmtimer3(3);
void setup() {
// pinMode(PA7, PWM);
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(400); // Timer input clock Prescaler = 1 (= 72MHz input ?)
pwmtimer3.setOverflow(100-1); // PWM Period width for 720kHz ?
pwmtimer3.setCompare(TIMER_CH3, 50); // PWM High Pulse width is 50% duty (1:1)
pwmtimer3.refresh();
pwmtimer3.resume();
// initialize digital pin PB1 as an output.
pinMode(PB1, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(PB1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(PB1, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
if(PB1=HIGH) //error from this line
{
digitalWrite(PB0,PWM);
//LED = 1;
}
else
{
digitalWrite(PB0, LOW);
// LED = 0;
}
}
if (PB1 == HIGH)
No errors, but ON/OFF is not working
void loop() {
digitalWrite(PB1, HIGH); // turn the LED on (HIGH is the voltage level)
pinMode(PB0, PWM);
delay(1000); // wait for a second
digitalWrite(PB1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB0, LOW);
delay(1000); // wait for a second
}
that sentence has tremendous amount of information to help others help you.
digitalWrite(PB0,PWM);
What I looking for is switching function for pin.
There are posts you can find here that talk about it:
https://www.stm32duino.com/viewtopic.php?t=634
There is the maple leaflabs docs:
http://docs.leaflabs.com/static.leaflab … write.html
This is about frequency and duty cycle not about turn on/off the pin output.
// PB0 - using pwm + timer
// PC13 - so i can see it toggling
HardwareTimer pwmtimer3(3);
void setup() {
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(72); // Timer input clock Prescaler = 1MHz 72MHz
pwmtimer3.setOverflow(1000 - 1); // PWM Period width for 1kHz
pwmtimer3.setCompare(TIMER_CH3, 500); // PWM High Pulse width is 50% duty (1:1)
pwmtimer3.refresh();
pwmtimer3.resume();
// initialize led as output
pinMode(PC13, OUTPUT);
}
int state=0;
void loop() {
state ^= 1; // toggle state
digitalWrite(PC13, (state ? HIGH : LOW)); // toggle bluepill led
if ( state ) {
pwmtimer3.resume();
}
else {
pwmtimer3.pause();
}
delay(100); // let the pwm run or stop for 100 ms
}
// PB0 - using pwm + timer
// PC13 - so i can see it toggling
HardwareTimer pwmtimer3(3);
void setup() {
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(1000); // Timer input clock Prescaler = 72kHz
pwmtimer3.setOverflow(2 - 1); // PWM Period width for /2 = 36kHz
pwmtimer3.setCompare(TIMER_CH3,1); // PWM High Pulse width is 50% duty (0->1)
pwmtimer3.refresh();
pwmtimer3.resume();
// initialize led PC13 as an output.
pinMode(PC13, OUTPUT);
}
int state=0;
void loop() {
state ^= 1; // toggle state
digitalWrite(PC13, (state ? HIGH : LOW)); // toggle bluepill led
if ( state ) {
pwmWrite(PB0,1);
}
else {
pwmWrite(PB0,0);
}
delay(100); // let the pwm run or stop for 100 ms
}
It is working, I used blinking LED to demonstrate the problem in simply way, but my application the LED is on when the button attached to another pin (PC13 ) is pressed.
Sorry , I should start from this.
digitalRead(PC13 ); // toggle bluepill
THE PROBLEM SOLVED
// PB0 - using pwm + timer
// PC13 - so i can see it toggling
const int pushBtn=PC13;
HardwareTimer pwmtimer3(3);
void setup() {
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(72); // Timer input clock Prescaler = 1MHz 72MHz
pwmtimer3.setOverflow(1000 - 1); // PWM Period width for 1kHz
pwmtimer3.setCompare(TIMER_CH3, 500); // PWM High Pulse width is 50% duty (1:1)
pwmtimer3.refresh();
pwmtimer3.resume();
// initialize led as output
//pinMode(PC13, OUTPUT);
// pinMode(PC13,INPUT);
pinMode(pushBtn,INPUT);
}
int state=0;
void loop() {
// state ^= 1; // toggle state
//digitalWrite(PC13, (state ? HIGH : LOW)); // toggle bluepill led
//digitalRead(PC13, (state ? HIGH : LOW));
// digitalRead(PC13, state );
digitalRead(PC13);
//if ( state )
//if(PC13==HIGH)
///////////////////////////////
int btnValue=digitalRead(pushBtn); // Read the Button State Pressed or not
if(btnValue==1)
////////////////////////////////
{
pwmtimer3.resume();
}
else {
pwmtimer3.pause();
}
delay(100); // let the pwm run or stop for 100 ms
}