chipKIT® Development Platform

Inspired by Arduino™

Fast setting of 1 single pin on chipkit (correct way ?)

Created Sun, 02 Sep 2012 19:31:18 +0000 by educa


educa

Sun, 02 Sep 2012 19:31:18 +0000

Hi,

I try to set PORTA.3 on my chipkit max since that pin is connected to the onboard led and is nice for testing stuff out.

I currently use the following code

/*
  Blink
  Turns on an LED for one second, then off for one second, repeatedly.
 
  Using direct port access
 */

void setup() {                
  pinMode(13, OUTPUT);     
}

void loop() {
  
  LATA = 0x08;             // set the LED on on PORTA.3
  delay(1000);              // wait for a second
  LATA = 0x00;             // set the LED off on PORTA.3
  delay(1000);              // wait for a second
}

This works, but I guess that I am now writing 0b0000000000001000 to the whole port A and that was not what I wanted. I wanted to only write a logic 1 to the 4th bit (being the one which controls porta.3)

I read somewhere that pic32 has some kind of set and clear registers, but could somebody please tell me what my code would have to look like to directly set and clear only bit 4 on portA ?

Is this the way to go ?

void setup() {                
  pinMode(13, OUTPUT);     
}

void loop() {
  
  LATASET = 0x08;             // set the LED on on PORTA.3
  delay(1000);              // wait for a second
  LATACLR = 0x08;             // set the LED off on PORTA.3
  delay(1000);              // wait for a second
}

or is the code above a no go and bad coding practice for some reason ?

Kind regards,

Bart


mikes

Sun, 02 Sep 2012 20:29:41 +0000

I have used

PORTBCLR = 0x1000;
PORTBSET = 0x1000;

to set single pins. This type of direct register access would prevent the code from running on an arduino (and the reverse is what prevents us from just copying and using arduino libraries) other than that I don't have a problem with it ;) .


educa

Sun, 02 Sep 2012 20:34:07 +0000

I don't care about compatibility with arduino since I need raw speed.

on arduino I also don't use digitalout but direct port acces, so it will never be compatible.


rasmadrak

Sun, 02 Sep 2012 21:51:39 +0000

From the Wiki: "Fastest way to toggle bits on a PIC32

The fastest way to toggle a pin on PIC32 is going to be something like
      while(1)
        {
          LATGINV = B01000000;
        }

For PIC32, usually the LAT registers are best for output and the PORT registers are best for input. The *INV (invert) registers are the fastest way to toggle an SFR bit. There are also *SET and *CLR registers that allow for quick bit sets and bit clears."

So using the latch-registers is most definitely not a bad idea. The problem would be to keep track on each register, but that's easy if you got the reference manual lying around. :)

I believe there's also a wrapper around this that looks similar to "setPortPin(PORT, PIN)" etc. But it's not really helping much since you can type LATxSET = 1 << PIN_NR and the compiler will optimize it.


educa

Mon, 03 Sep 2012 19:47:22 +0000

1 thing I do wonder is if you can only read the status of a whole port at once ?

Is there not a direct way to read the state of 1 single pin ?

Kind regards,

Bart