11 years, 1 month ago.

KL25Z: PTA1 "reserved" PwmOut port?

In the attached code, if I use PTA1 as my PWM control out, I get some weird behavior: the built-in red LED acts like a "complementary" indicator of the "on/off" percentage, even though it should be completely off. For example, if I slide green on all the way, the red LED fades out. If I slide green off, red turns on. At half-way, it's half red, half green. There's also no change on the PTA1 pin output: it stays at just under 3v. This is unexpected.

If I use PTA2, instead, I get the expected behavior: the blue or green LEDs are on in proportion to the TSI slider value, and red is completely off. There's also a change in the output on the PTA2 pin. So, the question is: Is PTA1 "special" or reserved, in some manner, that we can't actually use it as a PwmOut pin, even though it's listed as PwmOut capable on the pinout sheet?

I'm aware I could use the tsi.readPercentage() function.

Thanks!

Aaron

#include "mbed.h"
#include "TSISensor.h"

// Motor control: uses three pushbuttons to control forward, reverse, stop
// and the touch pad to control speed.

#define LEDON 0
#define LEDOFF 1

TSISensor tsi;
InterruptIn sForward(PTD0);
InterruptIn sStop(PTD6);
InterruptIn sReverse(PTD7);

PwmOut speed(PTA1);  // program works as expected for PTA2, but not PTA1
DigitalOut mode2(PTC7);
DigitalOut mode1(PTC0);
DigitalOut stdby(PTC3);

PwmOut rLed(LED_RED);
PwmOut gLed(LED_GREEN);
PwmOut bLed(LED_BLUE);
PwmOut *activeLed;

Serial pc(USBTX, USBRX); // tx, rx

int stopped = 0;

void triggerForward( void )
{
  stopped = 0;
  mode2 = 0;
  mode1 = 0;
  stdby = 1;
  activeLed = &gLed;
  rLed = LEDOFF; bLed = LEDOFF;
}

void triggerStop( void )
{
  stopped = 1;
  stdby = 0;
  activeLed = &rLed;
  gLed = LEDOFF; rLed = LEDON; bLed = LEDOFF;
}

void triggerReverse( void )
{
  stopped = 0;
  mode2 = 0;
  mode1 = 1;
  stdby = 1;
  activeLed = &bLed;
  gLed = LEDOFF; rLed = LEDOFF;
}

void flashLed( DigitalOut *led )
{
  int i;
  for ( i = 0; i < 3; i++ ) {
    *led = LEDON;
    wait( 0.2 );
    *led = LEDOFF;
    wait( 0.2 );
  }
}

int main() {
  rLed = LEDOFF;
  gLed = LEDOFF;
  bLed = LEDOFF;
  sForward.mode(PullUp);
  sStop.mode(PullUp);
  sReverse.mode(PullUp);
  wait( 0.01 );
  sForward.fall(&triggerForward);
  sStop.fall(&triggerStop);
  sReverse.fall(&triggerReverse);

  triggerStop();  // default mode

  int dist = tsi.readDistance();
  float fDist = 0.0;
  while( 1 ) {
    if ( stopped ) {
      wait( 0.1 );
      continue;
    }
    if ( dist > 1 ) {
      fDist = dist * 2.5 / 100;
      speed = fDist;
      *activeLed = 1.0 - speed;
      pc.printf( "fDist: %f\n\r", fDist );
    }
    wait( 0.1 );
    dist = tsi.readDistance();
    //pc.printf( "dist: %d\n\r", dist );
  }
}

Update: if I shift to PTA2, I get similar interference, now, with green and blue. When it's supposed to be only blue, green acts as a complementary indicator. However, this is only after I touch the TSI sensor. If I hit a button, the lights behave as expected. Once I adjust the TSI value, the green interferes. The voltage on the PTA2 pin is also not what I'd expect: it fluctuates, but returns to 2.9v.

Hmm... For a quick test, I changed to the next PWM slot - PTD4 - and it works as written: no other colors interfere with the others. On the PTD4 pin, the voltage is more like what I'd expect: it goes as low as .15v up to nearly 3v.

So, let me change the question: Are PTA1 and PTA2 somehow "special" on the KL25Z?

posted by Aaron Minner 15 Mar 2013

2 Answers

11 years, 1 month ago.

Actually, we are using PTA1 and PTA2 for the "stdio" UART, we have now updated the pinout diagram accordingly.

Accepted Answer
11 years, 1 month ago.

Hello Aaron Minner,

as I reviewed your code, problem seem to be in TSI_Sensor.h and it's related to the TSI module. PTA1 and PTA2 are pins with various functionality and one of those is analog = TSI. In my opinion, your TSI sensor reading switches those 2 pins to the analog reading therefore it interferes with any other functionality your application assigns to those pins. This is my assumption based on the fact you did not provide TSI sensor header file.

Regards, Kojto

My TSI_Sensor.h file is the standard file included in the TSI library. Thanks for checking.

posted by Aaron Minner 15 Mar 2013