// Mainloop
for(;;)
{
RX_Ready(); // UART ready to RX one Byte
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 Until character RXed
TX_Byte(); // TX Back RXed Byte Received
}
}
void Delay(unsigned int i)
{
unsigned char j;
for(j = 0; j < 5; j ++)
while(i--);
}
// Function Transmits Character from RXTXData Buffer
void TX_Byte(void)
{
P2OUT |= RS485;
Delay(10);
P2OUT &= ~RS485;
BitCnt = 0x0A; // Load Bit counter, 8data + ST/SP
CCR0 = TAR; // Current state of TA counter
CCR0 += Bitime; // Some time till first bit
RXTXData |= 0x0100; // Add mark stop bit to RXTXData
RXTXData = RXTXData << 1; // Add space start bit
CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle
while(CCTL0 & CCIE); // Wait for TX completion
}
// Function Readies UART to Receive Character into RXTXData Buffer
void RX_Ready(void)
{
BitCnt = 0x08; // Load Bit counter
CCTL0 = SCS + CCIS0 + OUTMOD0 + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture
}
// Timer A0 interrupt service routine
interrupt [TIMERA0_VECTOR]
void TimerA0_ISR(void)
{
CCR0 += Bitime; // Add Offset to CCR0
// RX--------------------------------------------------------------------------
if(CCTL0 & CCIS0) // RX on CCI0B?
{
if(CCTL0 & CAP) // Capture mode = start bit edge
{
CCTL0 &= ~CAP; // Capture to compare mode
CCR0 += Bitime_5;
}
else
{
RXTXData = RXTXData >> 1;
if(CCTL0 & SCCI) // Get bit waiting in receive latch
RXTXData |= 0x80;
BitCnt --; // All bits RXed?
if(BitCnt == 0)
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
CCTL0 &= ~CCIE; // All bits RXed, disable interrupt
_BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR)
}
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
// TX--------------------------------------------------------------------------
else
{
if(BitCnt == 0)
CCTL0 &= ~CCIE; // All bits TXed, disable interrupt
else
{
CCTL0 |= OUTMOD2; // TX Space
if(RXTXData & 0x0001)
CCTL0 &= ~OUTMOD2; // TX Mark