This may have a completely obvious answer, so apologies if that's the case, but this has been giving me some real trouble.
I've been playing with some UART code on an MSP430 launchpad board, trying to move away from the arduino environment I'm so familiar with.
The code I wrote hasn't been doing what I wanted, so I've now narrowed it down to this.
#include "msp430g2553.h"
#include <stdio.h>
unsigned int InterruptFlags;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog
//------------------- Configure the Clocks -------------------//
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
//---------------- Configuring the LED's ----------------------//
P1DIR |= BIT0 + BIT6; // P1.0 and P1.6 output
P1OUT &= ~BIT0 + BIT6; // P1.0 and P1.6 = 0
//--------- Setting the UART function for P1.1 & P1.2 --------//
P1SEL |= BIT1 + BIT2; // P1.1 UCA0RXD input
P1SEL2 |= BIT1 + BIT2; // P1.2 UCA0TXD output
//------------ Configuring the UART(USCI_A0) ----------------//
UCA0CTL1 |= UCSSEL_2 + UCSWRST; // USCI Clock = SMCLK,USCI_A0 disabled
UCA0BR0 = 104; // 104 From datasheet table-
UCA0BR1 = 0; // -selects baudrate =9600,clk = SMCLK
UCA0MCTL = UCBRS_1; // Modulation value = 1 from datasheet
UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0
IE2 |= UCA0TXIE;
IE2 |= UCA0RXIE;
InterruptFlags = IFG2;
_BIS_SR(LPM0_bits + GIE); // Going to LPM0
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void TransmitInterrupt(void)
{
IFG2 &= ~UCA0TXIFG;
InterruptFlags = IFG2;
}
#pragma vector = USCIAB0RX_VECTOR
__interrupt void ReceiveInterrupt(void)
{
IFG2 &= ~UCA0RXIFG;
}
And I've finally figured out by looking at the "InterruptFlag" variable in the debugger (CCS), that the transmit interrupt flag "UCA0TXIFG" appears to be raised when I enable the interrupts...
This means that as soon as my code runs, without any input, the TX interrupt is being called, which doesn't seem right.
Now I can just reset this bit straight after I enable the interrupt, but why does it give me the chance to do this first, instead of calling the interrupt straight away?
I'm sure I'm missing something obvious, but I think I need some help figuring it out.