STM32F746G and Interrupt Pin?

Discussions about the STM32generic core
Post Reply
nathanS
Posts: 5
Joined: Fri Jul 14, 2017 2:53 pm

STM32F746G and Interrupt Pin?

Post by nathanS » Fri Jul 14, 2017 3:02 pm

Hi I am trying to get the following code below, which works fine on an Uno R3 to work correct on a STM32F746G I had lying around. The speed of the motor can be change, but the rpm measurement doesn't work at all. I made sure to wire the rpm sensor to the 3.3v on the board.

/**
simple program for controlling the SCK-200 and reading the rpms
code example taken from
http://www.instructables.com/id/How-to- ... h-Arduino/
*/
#include <Servo.h>

// initialize the Servo object
Servo ESC1;

int RPM_RESOLUTION = 20;

int rpmPin = 2;
int rpmValue = 0;
int rpm = 0; // the desired rpm value
int rmc = 0; // keep track of how many times we measured the rpm
int pwm = -1;

int pole_revolutions = 0; // couints the number of resolutions
unsigned long timeold = 0;

void setSpeed(int ms) {
ESC1.writeMicroseconds(ms);
}

void setup() {
pinMode(rpmPin, INPUT_PULLUP);
//digitalWrite(rpmPin, HIGH); // inable the pullup resistor

ESC1.attach(10); // attach the ESC to pin 10

setSpeed(1000); // arm ESC

// used for rpm measurement
attachInterrupt(0, rpmFun, FALLING);

Serial.begin(9600);

// set the speed range
//setSpeed(2000);
Serial.println("Turn On ESC");
delay(5000);
Serial.println("ESC ARMED");
}

void loop() {
// first get the speed in rpms
if (Serial.available() > 0) {
int input = Serial.parseInt();
rmc = 0; // reset this to zero

if (input > 0) {
rpm = input;
Serial.print("Speed set to: ");
Serial.println(rpm);

pwm = getSpeedPWM(rpm); // get a rouch estimate of speed

Serial.print("PWM: ");
Serial.println(pwm);

if (pwm > 1000 && pwm < 2000) {
setSpeed(pwm);
}
} else if (input == -1) {
Serial.println("\nMotor Stopped\n");
setSpeed(1000);
}
}

// get the desired rpm and set speed
if (pole_revolutions >= 70) {
rmc++;
detachInterrupt(0);

rpmValue = 8.6 * 1000 / (millis() - timeold) * pole_revolutions;
timeold = millis();
pole_revolutions = 0;

// print out the rpms
Serial.print("Measured RPM: ");
Serial.println(rpmValue);

if (rmc > 2) {
int npwm = computeNewPWM(pwm, rpm, rpmValue);

if (npwm != 0) {
Serial.print("Corrected PWM: ");
Serial.print(pwm);
Serial.print(" -> ");
Serial.println(npwm);

pwm = npwm;
setSpeed(pwm);
}
}

attachInterrupt(0, rpmFun, FALLING);
}
}

int computeNewPWM(int pwm, int rpm, int rpmValue) {
int diff = rpmValue - rpm;
int adiff = fabs(diff);

if (diff > 0 && adiff > RPM_RESOLUTION) {
return pwm - 2;
} else if (diff < 0 && adiff > RPM_RESOLUTION) {
return pwm + 2;
} else {
return 0;
}
}

// get the speed pwm given a certain rpm
int getSpeedPWM(int rpm) {
long crpm = 0;
int p = 0;

for (p = 1000; p <= 2000; p++) {
//int crpm = -21457 + 28 * pww + -0.007 * square(pwm);
crpm = -29334L + 39L * p + -0.01 * pow(p, 2);
//Serial.println(crpm);
if (fabs(rpm - crpm) < RPM_RESOLUTION * 2) {
break;
}

}

return p + 5; // set to lower speed
}

//Each rotation, this interrupt function runs 7 times for the 7 poles of the motor
void rpmFun() {
pole_revolutions++;
}

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: STM32F746G and Interrupt Pin?

Post by danieleff » Fri Jul 14, 2017 4:16 pm

Just guessing but maybe you wrote `attachInterrupt(0, `, and used pin 2, that is interrupt 0 on UNO.
STM32 has interrupts on every pin, so `attachInterrupt(0`= pin 0.
(If still it does not work, I can check on weekend)

zmemw16
Posts: 1486
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: STM32F746G and Interrupt Pin?

Post by zmemw16 » Fri Jul 14, 2017 4:25 pm

please enclose code with the </> button

Code: Select all

this is code and is so much more readable
stephen

nathanS
Posts: 5
Joined: Fri Jul 14, 2017 2:53 pm

Re: STM32F746G and Interrupt Pin?

Post by nathanS » Fri Jul 14, 2017 4:31 pm

Thanks Daniel. That was the problem. It works fine now after attaching interrupt to the same pin.

Post Reply