Hello everyone,
i'm trying to get a software uart solution with the MSP430F2121 working.
Ive started with the code example from TI (msp430x21x1_ta_uart19200_1MHz.c), but in my application I dont have a 32,768 kHz Osc and also use different Pins for UART RX and TX. Since these Pins are connected to the Capture Units 1A and 2A I have to use the switch (TAIV) instruction.
So far everythings ok if I just implement to send a string via UART with 19200 Baud and just fill the case TAIV_TACCR1 it works perfectly. But for receiving characters I need to use the case TAIV_TACCR2 too, but if I implement anything in there (here just this increment the variable "test"), the transmitting part stops working immediately. The Problem is suddenly I just receive much character errors. I tried to debug this problem and could see that sometimes if I implement more cases in this switch statement, my transmit part won't send 10 bits (8 bit data + start and stoppbit)anymore. instead of it the transmition is ending after about 8 bits.
Does anyone have an idea what could be wrong? I thought about other (or nested) interrupts? could that happen here? im just new to msp 430
Thanks for help
Christian
the source code of course:
//******************************************************************************
// MSP430x21x1 Demo - Timer_A UART 19200 TX, 1MHz DCO
// Modification
// Description: This program demonstrates a half-duplex 19200-baud UART using
// Timer_A3 and a the DCO. The program will wait in LPM0 sending the
// string 'Hello World' with 320ms interval between characters as timed by a
// WDT interrupt. The Set_DCO subroutine will calibrate the DCOCLK
// to ~1Mhz.
// ACLK = n.a., MCLK = SMCLK = target DCO
// //* External watch crystal installed on XIN XOUT is required for ACLK - is absent *//
//
// MSP430F21x1
// -----------------
// /|\| XIN|-
// | | | 32kHz (n.a.)
// --|RST XOUT|-
// | |
// | |
// | CCI2A/RXD/P1.3|<--------
// | | 19200 8N1
// | CCI1A/TXD/P1.2|--------> "Hello World"
//
// Christian Becker, Feb. 2012, University of Applied Sciences Zwickau
//*****************************************************************************
#include <msp430.h>
#define RXD 0x08 // RXD on P1.3
#define TXD 0x04 // TXD on P1.2
#define Bitime_5 27 // ~ 0.5 bit length
#define Bitime 55 // ~ 19200 baud
#define DELTA 256 // Target DCO = DELTA*(4096)
/* Function Prototypes */
void TX_Byte();
void UartSendString (char * data, unsigned int length);
void init_system(void);
/* global variables */
static const char string1[] = { "Hello World\r\n" };
unsigned char i = 0;
unsigned int RXTXData;
unsigned char BitCnt;
unsigned char test;
int main (void)
{
init_system();
// Mainloop
for (;;)
{
_BIS_SR(LPM0_bits + GIE); // Enter LPM3
UartSendString((char *) string1, sizeof string1);
}
}
// Timer A interrupt service routine
#pragma vector=TIMERA0_VECTOR
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A (void)
{
switch (TAIV) // Efficient switch-implementation
{
case TAIV_NONE: // TACCR0 TIMERA0_VECTOR
break;
case TAIV_TACCR1: // TACCR2 TIMERA1_VECTOR
// TX
CCR1 += Bitime; // Add Offset to CCR1
if ( BitCnt == 0)
CCTL1 &= ~ CCIE; // All bits TXed, disable interrupt
else{
CCTL1 |= OUTMOD2; // TX Space
if (RXTXData & 0x01)
CCTL1 &= ~ OUTMOD2; // TX Mark
RXTXData = RXTXData >> 1;
BitCnt --;
}
break;
case TAIV_TACCR2: // TACCR1 TIMERA1_VECTOR
// test++;
break;
default:;
} //switch
}
// Function Transmits Character from RXTXData Buffer
void TX_Byte ()
{
BitCnt = 0xA; // Load Bit counter, 8data + ST/SP
CCR1 = TAR; // Current state of TA counter
CCR1 += Bitime; // Some time till first bit
RXTXData |= 0x100; // Add mark stop bit to RXTXData
RXTXData = RXTXData << 1; // Add space start bit
CCTL1 = OUTMOD0 + CCIE; // TXD = mark = idle
while ( CCTL1 & CCIE ); // Wait for TX completion
}
void UartSendString (char * data, unsigned int length){
unsigned int i = 0;
while (i < length-1){
RXTXData = data[i];
TX_Byte();
i++;
}
}
/*
* Interrupt Service Routine for Watchdog
* Timing: ~ 32ms * 10
*/
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer (void)
{
static char i = 0;
//
if (i<10){
i++;
}
else {
_BIC_SR_IRQ(LPM0_bits); // Clear LPM3 bits from 0(SR)
i = 0;
}
}
void init_system(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set DCO to calibrated 1MHz
DCOCTL = CALDCO_1MHZ;
TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear
WDTCTL = WDT_MDLY_32; // Set Watchdog Timer interval to ~32ms
CCTL1 |= OUT; // TXD Idle as Mark
P1SEL |= TXD; // P1.2/TA1 for TXD function
P1DIR |= TXD; // TXD output on P1
P1SEL |= RXD; // P1.3/TA2 as RXD input
P2SEL &= ~(0xC0); // Disable XTAL1/XTAL2 function
P2DIR |= 0x80; // RF_RESET -> Low
P2OUT &= ~(0x80); //
IE1 |= WDTIE; // Enable WDT interrupt
//__enable_interrupt(); // Debug purpose only, GIE bit set in Mainloop
}