chipKIT® Development Platform

Inspired by Arduino™

setting multiple bits simultaneously

Created Sun, 08 Jan 2012 00:25:31 +0000 by raypm


raypm

Sun, 08 Jan 2012 00:25:31 +0000

Hi,

I've been looking around trying to find a way to set multiple digital outputs on a Max32 board using port registers like the Arduino. I did find the port mappings, but my code won't quite work.

PORTB is is pins 25...20, 26, 27, 32..35, 41...44 so...

PORTB = input;

should set whichever pins I want, right? In setup() I have...

Serial.begin(115200);
  
  pinMode (25, OUTPUT);
  pinMode (24, OUTPUT);
  pinMode (23, OUTPUT);
  pinMode (22, OUTPUT);

  pinMode (21, OUTPUT);
  pinMode (20, OUTPUT);
  pinMode (26, OUTPUT);
  pinMode (27, OUTPUT);

  pinMode (32, OUTPUT);
  pinMode (33, OUTPUT);
  pinMode (34, OUTPUT);
  pinMode (35, OUTPUT);

  pinMode (41, OUTPUT);
  pinMode (42, OUTPUT);
  pinMode (43, OUTPUT);
  pinMode (44, OUTPUT);
  
  pinMode (52, OUTPUT);
  pinMode (53, OUTPUT);

Where have I gone wrong?


majenko

Sun, 08 Jan 2012 00:29:35 +0000

You should be using "LATB" not "PORTB". PORTB is for reading, LATB is for writing to the output latches.


raypm

Sun, 08 Jan 2012 00:41:36 +0000

Alright, so I'm trying

long int loopCounter = 0;
void loop ()
{
  loopCounter++;
  
  int i = loopCounter % 16;
  
  LATB = (2 << i);
  Serial.println (i);
  
  delay (250);
}

to toggle through the pins at 4Hz so I can see which pins go where but, it doesn't seem to work lighting up the LED makeshift probe. Some pins are always on and some are always off, no scrolling through them.


majenko

Sun, 08 Jan 2012 12:56:01 +0000

Page 14 of the UNO32â„¢ user guide PDF ([url]http://digilentinc.com/Data/Products/CHIPKIT-UNO32/chipKIT%20Uno32_rm.pdf[/url]) lists all the pin / port assignments.

Of port B only bits 0 and 1 go to the digital pins (pins 42 and 41 respectively).

The other members of port B that are used all go to the analogue pins.

You will need to read through that table to find the pins you want to work with and match those up to the different ports and the corresponding bits.


WestfW

Mon, 09 Jan 2012 04:36:09 +0000

The other members of port B that are used all go to the analogue pins.

For Arduino compatibility, the pinMode() statements in the original post should have converted those pins to digital mode. (Actually, I'm not sure whether that bit of magic happens in pinMode() or digitalRead/Write. But the analog pins are supposed to be usable for digital IO.)


raypm

Tue, 10 Jan 2012 04:46:48 +0000

To remind, I'm using a Max32 board. Also according to:

http://www.designspark.com/files/ds/supporting_materials/chipKIT%20Max32_bysa_sch.pdf

I have my pins for PORTB correct. I'm still at a loss for a solution...


WestfW

Tue, 10 Jan 2012 06:30:20 +0000

pinMode (25, OUTPUT); pinMode (24, OUTPUT); pinMode (23, OUTPUT); pinMode (22, OUTPUT);

I have my pins for PORTB correct.

No! (?? how can...)

Ah hah! I see what you are doing. The "pin numbers" used in the pinMode command are NOT the CHIP pin numbers; they're the BOARD pin numbers (as show up on the PCB silkscreen.) So board pin numbers 22-29 end up being split across port C and G and who knows what else.

This "abstraction" of ports and bits to "pins" is (IMO) one of the things that helps make Arduino/etc accessible to beginners, but it also means that PCBs tend to be laid out "opportunistically", rather than logically based on ports/bits, and it can a real pain to find an appropriate group of pins to do efficient byte (or wider) IO. It looks like your best bet for experiments would be pins 30 to 37, which map to portE7..0 (yeah, bit-reversed)...


raypm

Thu, 12 Jan 2012 19:03:42 +0000

Sweet, now I understand it. Thanks a lot guys. Now that I have a faster microcontroller I can finally get to programming these guitar effects. When I'm done I'll post pictures! You guys are awesome! =]