Hello guys,
I am struggling with an apparently naive problem…
In short I need to decode the two-channels PPM signal (basically a 15ms period stram of three pulses) coming from an RC receiver to generate speed (PWM) and direction outputs.
I started writing down some dirty code to see if I was able to reach the target and I successfully obtained what I need on a Nucleo 411RE. So I decide to switch to a F303K8 to squeeze the circuit, that has to be housed in the RC car.
No way to get an interrupt! I tested D0 through D13 without success…
I verified the PWM output is correctly generated.
The LED, that on 411RE blinks when the PPM signal is applied, does nothing on F303K8…
What could be wrong?
#include "mbed.h"
#include "string.h"
#define TMIN 100 // tens of us
#define TGOMIN 140
#define TIDLE 150
#define TGOMAX 160
#define TMAX 200
// Nucleo F303K8 board
//------------------------------------------------------------------------------
DigitalOut LED (D13); // Status LED (signal OK)
PwmOut PWMO (D5); // PWM output (speed)
DigitalOut FWD (D4); // Forward direction (relais)
DigitalOut TM (D6); // Test Mode (output)
InterruptIn PPM (D7); // PPM signal input
Ticker Sampler;
Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection)
//------------------------------------------------------------------------------
void Restart_Count(void);
void Stop_Count(void);
void Count(void);
//------------------------------------------------------------------------------
uint8_t i;
uint16_t Counter, Ton, T1, T2;
bool PSTART=0, PDET=0;
double PWMX;
//------------------------------------------------------------------------------
int main()
{
PC.baud(115200); // set baud-rate of virtual COM port (PC connection)
PPM.mode(PullUp);
PWMO.period_ms(3);
PWMO.write(0.0);
LED = 1; // initial LED flash
wait_ms(100);
LED = 0;
Sampler.attach_us(&Count, 10); // counter ticker
PPM.rise(&Restart_Count); // enable interrupt on rising edge
while(1)
{
if((PDET==1)&&(Ton>500))
{
PDET = 0;
PPM.rise(NULL);
PPM.fall(NULL);
while(PPM==0);
Counter=0;
while(PPM==1);
T1 = Counter;
while(PPM==0);
Counter=0;
TM=1;
while(PPM==1);
T2 = Counter;
TM = 0;
LED = 1;
}
else
{
T1 = 0;
T2 = 0;
LED = 0;
}
if(T2!=0)
{
PWMX = (double) (T2-TMIN)/(TMAX-TMIN);
if(PWMX<0.0)
PWMX = 0.0;
if(PWMX>1)
PWMX= 1.0;
PWMO.write(1.0 - PWMX);
//PC.printf("P2= %d, PWM= %3.2f\n", T2, PWMX);
}
PPM.rise(&Restart_Count);
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void Count(void)
{
Counter++;
return;
}
//------------------------------------------------------------------------------
void Restart_Count(void)
{
PPM.rise(NULL);
PSTART = 1; // pulse start detected
Counter = 0;
TM=1;
PPM.fall(&Stop_Count);
return;
}
//------------------------------------------------------------------------------
void Stop_Count(void)
{
PPM.fall(NULL);
if(PSTART == 1)
{
Ton = Counter;
PSTART = 0;
PDET = 1;
TM=0;
}
PPM.rise(&Restart_Count);
return;
}