/*
* main.c
*/
#include "msp430g2231.h"
#define Ts 1E6/100
#define START_BIT 0
#define STOP_BIT 9
unsigned int tx_buff=0, bit_index;
void FaultRoutine(void)
{
P1DIR |= 0x01; //set P1.0 as osc. fault indicator
P1OUT = 0x01; // red LED on
while(1); // TRAP
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
//setup clocks
if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF) // If calibration data is erased
FaultRoutine(); // run FaultRoutine()
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO
IFG1 &= ~OFIFG; // Clear OSCFault flag
BCSCTL2 |= SELM_0 + DIVM_0 + DIVS_0; // MCLK=DCOCLK, SMCLK=DCOCLK, MCLK/1, SMCLK/1
//setup tranmission and indicator
P1DIR |= (BIT1+BIT0); //P1.1 as TXD, P1.0 to red LED to indicate transmission line state
P1DIR &= ~BIT2; //P1.2 as RXD
P1OUT |= BIT1 + BIT0; //idle state high
//setup timerA as synchronous clock
TACCR0 = Ts-1; //interrupt every Ts
TACTL = TASSEL_2 + MC_1; //TA CLK = SMCLK, UP mode
TACCR0 = Ts-1; //interrupt every Ts
tx_buff=0xAA;
bit_index = START_BIT;
TACCTL0 = CCIE;
_BIS_SR(GIE); // enable interrupt;
while(1)
{while(1);};
}
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A(void)
{
switch (bit_index)
{
case START_BIT:
P1OUT &= ~(BIT1+BIT0);
bit_index+=1;
break;
case STOP_BIT:
P1OUT |= (BIT1+BIT0);
// bit_index = START_BIT; //prepare to re-send
TACCTL0 &= ~CCIE;
// TACTL &= 0xFFCF;
break;
default:
if(tx_buff & (1 << (bit_index-1))) //mask out the bit in buffer to set on the trans. line
P1OUT |= (BIT1+BIT0);
else P1OUT &= ~(BIT1+BIT0);
bit_index++;
break;
}
}