In the function below I want to first set the pins as OUTPUTs and make them LOW wait 10s and then make all pins INPUTs, then set some pins as OUTPUTs and make those pins HIGH wait 10s and finally set all pins to INPUT mode. However once this line executes
REG_PORT_DIR0 &= ~(PORT_PA05 | PORT_PA06 | PORT_PA07 | PORT_PA08 | PORT_PA09 | PORT_PA14 | PORT_PA15);
the ports get set to INPUTs and they are never swapped to OUTPUT mode. When I'm in debugger mode everything seems to work fine so maybe I'm not initializing some value somewhere? I know some of the registers are atomic like OUTSET and |= operations etc can have weird behaviour, but I followed this tutorial and they seem to use |= &= etc when interacting with the PORTs directly (DIR0).
Complete code
struct pinSettings{ uint8_t number[10] = {0b01111101, 0b00000101, 0b01011011, 0b01001111, 0b00100111, 0b01101110, 0b01111110, 0b01000101, 0b01111111, 0b01101111}; uint8_t segmentPinOrder[7] = {5, 6, 7, 8, 9, 14, 15}; uint8_t clear = 0x00; }pinsettings; bool toggleNumber(uint8_t numberToDisplay){ // PWM is on PIN 16 and it's generated properly myPWM.begin(); numberToDisplay = pinsettings.number[numberToDisplay]; uint32_t pinDirections = 0, pinState = 0; for (uint8_t i = 0; i < 7; ++i) { // make all pins OUTPUTs and set them to LOW pinDirections |= (1 << pinsettings.segmentPinOrder[i]); pinState |= (1 << pinsettings.segmentPinOrder[i]); } // make the pins OUTPUTs and set them to LOW REG_PORT_DIR0 |= pinDirections; REG_PORT_OUT0 &= ~(pinState); myPWM.setOnPercent(45); DelayMs(10000); // make all pins INPUTs REG_PORT_DIR0 &= ~(PORT_PA05 | PORT_PA06 | PORT_PA07 | PORT_PA08 | PORT_PA09 | PORT_PA14 | PORT_PA15); pinDirections = 0x00; pinState = 0x00; for (uint8_t i = 0; i < 7; ++i) { if ((numberToDisplay >> i) & 1) { // set some pins to OUTPUT pinDirections |= (1 << pinsettings.segmentPinOrder[i]); pinState |= (1 << pinsettings.segmentPinOrder[i]); } } // set pins to HIGH REG_PORT_DIR0 |= pinDirections; REG_PORT_OUT0 |= pinState; // PWM = 55 to turn the display ON (45 OFF) myPWM.setOnPercent(55); DelayMs(10000); // set all pins to inputs REG_PORT_DIR0 &= ~(PORT_PA05 | PORT_PA06 | PORT_PA07 | PORT_PA08 | PORT_PA09 | PORT_PA14 | PORT_PA15); //REG_PORT_DIRCLR0 = (PORT_PA05 | PORT_PA06 | PORT_PA07 | PORT_PA08 | PORT_PA09 | PORT_PA14 | PORT_PA15); //DelayMs(60000 * 15); return true; }