chipKIT® Development Platform

Inspired by Arduino™

Proper way to read a value in a register/port?

Created Sun, 09 Dec 2012 00:46:52 +0000 by snovich


snovich

Sun, 09 Dec 2012 00:46:52 +0000

Hello,

I have the following code:

The idea is to write a high value to Max32 GPIO 42 (to turn on an LED). Read the state of this register, and apply that state to another pin to turn another LED on (gpio 42).

The code works up to the point that I can turn on+off the first LED (RB12), but apparently I can't read that register's state by going SOMEVAR = PORTB: This just yields zero, so that the 2nd LED always remains off.

It's probably something really silly. Help appreciated!

Thanks!

// goal: write a 1 to max32 gpio 42
// read it's register state
// copy that to max32 gpio

unsigned int xfer;
unsigned int RB12STATE;
unsigned int RB12MASK = 0x1000;

void setup(){
  Serial.begin(9600);
 TRISBCLR = 0x1000; // set RB12 to output (12th bit = gpio p42)
 TRISACLR = 0x40; // set RA6 to output (6th bit = gpio 80)
 PORTBSET = 0x1000; // turn on RB12 (12th bit = gpio p42);
 PORTACLR = 0x40; // ensure 6th bit is cleared (at this point LED should be off)
 xfer = PORTB; // read PORTB's state
 RB12STATE = ((xfer)&(RB12MASK));  // mask PORTB's state to obtain value register RB12 (everything else 0)
 Serial.print("PORT B State: ");
 Serial.println(xfer);
 Serial.print("State of RB12: ");
 Serial.println(RB12STATE);
 PORTASET = (RB12STATE >> 6); // move and set 0bx000000 - gpio 80
 Serial.print("setting to: ");
 Serial.println(PORTASET);
}

void loop(){
  
  
}

snovich

Sun, 09 Dec 2012 01:03:34 +0000

I figured it out. It was somewhat subtle. Reading from the PORT actually reads from the pin: not what's held on the latch (presumably this is always zero if the register/TRIS is set to be an output/0?).

From Section 12 I/O ports of the PIC32MX Family ref:

12.2.2 Registers for Configuring PORT Functions (PORTx) PORTx registers allow I/O pins to be accessed: • A write to a PORTx register writes to the corresponding LATx register (PORTx data latch). Those I/O port pin(s) configured as outputs are updated. • A write to a PORTx register is the effectively the same as a write to a LATx register • A read from a PORTx register reads the synchronized signal applied to the port I/O pins 12.2.3 Registers for Configuring Latch Functions (LATx) LATx registers (PORTx data latch) hold data written to port I/O pin(s): • A write to a LATx register latches data to corresponding port I/O pins. Those I/O port pin(s) configured as outputs are updated. • A read from LATx register reads the data held in the PORTx data latch, not from the port

Working code:

// goal: write a 1 to max32 gpio 42
// read it's register state
// copy that to max32 gpio

unsigned int xfer;
unsigned int RB12STATE;
unsigned int RB12MASK = 0x1000;

void setup(){
  Serial.begin(9600);
 TRISBCLR = 0x1000; // set RB12 to output (12th bit = gpio p42)
 TRISACLR = 0x40; // set RA6 to output (6th bit = gpio 80)
 PORTBSET = 0x1000; // turn on RB12 (12th bit = gpio p42);
 PORTACLR = 0x40; // ensure 6th bit is cleared (at this point LED should be off)
 **xfer = LATB; // read PORTB's state**
 RB12STATE = ((xfer)&(RB12MASK));  // mask PORTB's state to obtain value register RB12 (everything else 0)
 Serial.print("PORT B State: ");
 Serial.println(xfer);
 Serial.print("State of RB12: ");
 Serial.println(RB12STATE);
 PORTASET = (RB12STATE >> 6); // move and set 0bx000000 - gpio 80
 Serial.print("setting to: ");
**Serial.println(LATA);**
 
}

void loop(){
}