chipKIT® Development Platform

Inspired by Arduino™

digitalWrite and LATxSET / LATxCLR Confusion

Created Tue, 03 Jul 2012 22:59:28 +0000 by BoH_Havoc


Tue, 03 Jul 2012 22:59:28 +0000

I was wondering if i'm doing the digitalWrite -> LATxSET conversion correct. For a project of mine i need maximum speed for digital writes.

They way i get it (please correct me if something is wrong here, i'm still trying to figure this out):

Check the manual to get the digital pin's port. Example ("Pinout Table by Connector Pin" in chipKIT max32_rm.pdf, page 17): J08-03 51 11 ECRX/SDA2/SDI2A/U2ARX/PMA4/CN9/RG7 Also JP3,JP4 This is pin 51 on the board, pin 11 on the pic32 itself. RG7 tells me that it's port is G. I also get the info that it's bit is 7. So in order to set pin 51 to high i do this: LATGSET = B00000111; to set it to low i do this: LATGCLR = B00000111;

... however, it seems that this does NOT work.

What am i doing wrong here? :)


Wed, 04 Jul 2012 03:15:40 +0000

LATGSET and similar are images of the port register, and you write masks to it, not bit numbers. Try "LATGSET = 1<<7;" (This should mean that you can set more than one bit at a time. "LATGSET = B111;" should be setting PORTG0, PORTG1, and PORTG2, all at the same time.)


Wed, 04 Jul 2012 04:18:29 +0000

There's a bunch of discussion on fast digital IO here:

Note that an expression like LATGSET = 1; does not end up as a single instruction the way it would on an 8bit PIC or AVR. That's part of the nature of RISC.


Wed, 04 Jul 2012 17:59:43 +0000

Thanks for those two links! I think i understand my mistake now. :)

To turn pin51 high i did use this LATGSET = B00000111; Instead i should be using this LATGSET = B10000000; or this LATGSET = 1<<7;

The reason i was using B00000111 was because there are ports which have bits > 7. I thought i had to use some sort of binary thingy (hence the 00000111 (=7 in binary)). e.g. this one has bit 15 04 18 48 AETXD1/SCK1A/U1BTX/U1ARTS/CN21/RD15. So in order to turn Port D Bit 15 to high i would use LATDSET = B1000000000000000; or LATDSET = 1<<15;

Is this correct now?

I'm now using your digitalWriteFast lib and it's working like a charm, but i still want to understand how this works. :)

[edit] if i want to set bit 14, would i do this LATDSET = B0100000000000000; or this LATDSET = B100000000000000; ?


Wed, 04 Jul 2012 21:47:52 +0000

if i want to set bit 14, would i do this LATDSET = B0100000000000000; or this LATDSET = B100000000000000;

The "B0001" format is not official C at all, and isn't very portable. Which of the above will work is dependent on what things are defined "somewhere" (I really hope that chipkit doesn't have a file somewhere with all 2^16+ possible Bxxxx values), but I'd avoid them all in favor of the "1<<14" format. If you find that syntax particularly annoying (t's quite commonly used it this sort of situation, but not familiar to many, just because this sort of thing doesn't get done in desktop programming), the Arduino environment also defines a macro "_BV(14)" (read as "BitValue")


Thu, 05 Jul 2012 12:00:18 +0000

I was just asking for the B0001 format as this allows me to set multiple pins to high at once instead of setting each pin on its own, hence giving more speed (at least thats what i think). I didn't know though that this was a chipKIT only thing, so i'll just use the 1<<X format in the future for portabilty reasons. Thanks for the hint.

Thanks for all your help on this topic :)

[edit] I was wondering, if i could set two bits (or more) at once by doing this: LATGSET = 1<<2 | 1<<6;


Thu, 05 Jul 2012 14:23:57 +0000

You don'y have to just use a 1. If you want to set multiple bits and not use the binary notation you can use hex values and even shift those over.

Let say you want to access the pins 6, 7 and 8 on your PORTG to high. you could use this:

LATGSET = 0x7 &lt;&lt; 6

This uses the Hexadecimal value 7 (0b111) and shifts it over 6 thus to accomadate the ports you desire (outcome: 0x11100000)


Thu, 05 Jul 2012 16:53:51 +0000

I was wondering, if i could set two bits (or more) at once by doing this: LATGSET = 1<<2 | 1<<6;

Yes, that will work fine. Add parens to make sure!


Thu, 05 Jul 2012 21:21:27 +0000

Thanks, i think i understand the whole pin/port/bit setting thing now :)

My synthesizer code runs a lot faster now thanks to you!


Fri, 06 Jul 2012 06:51:17 +0000

Glad to be of help!