Figured this made more sense as a new answer. I made some changes to the code and included the relayOn()
,relayOff()
, and relayLatch()
functions.
The main changes are using the for loop as many of you suggested (duh!), and a new feature, checking to see if the relay is already in the state requested.
Set state of all relays:
uint8_t relayStatus; // Global holder for relay status
void
relaySet(uint8_t relayCmd)
{
char printStatus[7]; // For updating status on LCD screen
// Set the relays on/off as required. Update display.
for ( int r=0; r<4; r++ )
{
if ( relayCmd & (1 << r) != relayStatus & (1 << r) )
{
if ( relayCmd & (1 << r) )
{
relayOn(r);
snprintf (printStatus,7,"R%u ON ", r+1 );
LCDstring(r,0,printStatus,NORMAL);
}
else
{
relayOff(r);
snprintf (printStatus,7,"R%u OFF", r+1 );
LCDstring(r,0,printStatus,INVERSE);
}
}
}
// Wait 10ms
ROM_SysCtlDelay(ROM_SysCtlClockGet()/100);
// Set appropriate pins back to ground
for ( int r=0; r<4; r++ )
{
if ( relayCmd & (1 << r) != relayStatus & (1 << r) )
{
relayLatch(r);
}
}
// Update global relay status
relayStatus = relayCmd;
}
Turn single relay on:
void
relayOn(int relayNum)
{
if ( relayNum == 0 )
{
// Rel1
if (hardwareRev == 1) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); }
else if (hardwareRev == 2) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0); }
}
else if ( relayNum == 1 )
{
// Rel2
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, GPIO_PIN_3);
}
else if ( relayNum == 2 )
{
// Rel3
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, GPIO_PIN_2);
}
else if ( relayNum == 3 )
{
// Rel4
if (hardwareRev == 1) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_PIN_4); }
else if (hardwareRev == 2) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_PIN_4); }
}
}
Turn single relay off:
void
relayOff(int relayNum)
{
if ( relayNum == 0 )
{
// Rel1N
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_4, GPIO_PIN_4);
}
else if ( relayNum == 1 )
{
// Rel2N
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, GPIO_PIN_5);
}
else if ( relayNum == 2 )
{
// Rel3N
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, GPIO_PIN_5);
}
else if ( relayNum == 3 )
{
// Rel4N
if (hardwareRev == 1) { GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_1, GPIO_PIN_1); }
else if (hardwareRev == 2) { GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0, GPIO_PIN_0); }
}
}
Latch single relay:
void
relayLatch(int relayNum)
{
if ( relayNum == 0 )
{
// Rel1
if (hardwareRev == 1) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0); }
else if (hardwareRev == 2) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0); }
// Rel1N
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_4, 0);
}
else if ( relayNum == 1 )
{
// Rel2
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0);
// Rel2N
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, 0);
}
else if ( relayNum == 2 )
{
// Rel3
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, 0);
// Rel3N
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0);
}
else if ( relayNum == 3 )
{
// Rel4
if (hardwareRev == 1) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0); }
else if (hardwareRev == 2) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0); }
// Rel4N
if (hardwareRev == 1) { GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_1, 0); }
else if (hardwareRev == 2) { GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0, 0); }
}
}
0b1 & relayBitmap
is either 0 or 1. So you only every get one of the first two options. \$\endgroup\$else if
? What should happen ifrelayBitmap
is0b1011
, and why? \$\endgroup\$relayOn()
andrelayOff()
functions. \$\endgroup\$0b
is not part of ISO C. Consider using hex instead (0x01, 0x02, 0x04, 0x08
respectively in your code) \$\endgroup\$==
part \$\endgroup\$