Hi everyone
I wanted to receive data from terminal of CCS through UART. I have fixed baudrate as 9600. And initialized an LED to blink when the data from terminal get matched. But when i write data through terminal the LED is not turning on and some garbage is printing on the terminal.So can someone please help me get the proper output. I'm sharing the code here.
///////////////////////////////////////////////////////////////////////////////////////////////// MAIN.C ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <msp430.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <uart.h>
unsigned char addr[30]="";
void pin_setup()
{
P1DIR = 0x02;
P1OUT = ~0x02;
}
void clk_setp()
{
CSCTL0_H = CSKEY_H;
CSCTL1 = DCOFSEL_6; //8MHz |DCOFSEL_0 -1MHz
CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK; // Set SMCLK = MCLK = DCO
CSCTL3 = DIVA_1|DIVS__1|DIVM__1;
CSCTL0_H = 0;
}
void uart_config()
{
P2SEL0 &= ~(BIT0 | BIT1);
P2SEL1 |= (BIT0 | BIT1); // USCI_A0 UART operation
PM5CTL0 &= ~LOCKLPM5;
UCA0CTLW0=UCSWRST;
UCA0CTLW0 |=UCSSEL__SMCLK;
UCA0BRW = 52;
UCA0MCTLW |= UCOS16 | UCBRF_1 | 0x49;
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
}
void UART_RECEIVE()
{
pin_setup();
__bis_SR_register(LPM0_bits|GIE);
sprintf(addr,"%s\r\n",RXData);
if(addr=="y")
{
P1OUT=0x02;
}
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
clk_setp();
uart_config();
while(1)
{
UART_RECEIVE();
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// UART.H ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <msp430.h>
#include <stdint.h>
#include <stdbool.h>
volatile int Rx_index=0;
volatile unsigned int Rx_key=0;
volatile int Tx_index=0;
volatile unsigned int Tx_key=0;
int size=0;
unsigned char TXData[15]="";
unsigned char RXData[5]="";
#pragma vector = EUSCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
switch(__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG))
{
case USCI_NONE: break;
case USCI_UART_UCRXIFG:
Rx_key=0;
RXData[Rx_index]=UCA0RXBUF;
if((RXData[Rx_index] == '\x0d'/*'\r'*/) || (RXData[Rx_index] == '\n')||(RXData[Rx_index] == '\r'))
{
__bic_SR_register_on_exit(CPUOFF|GIE);
UCA0IE &= ~UCRXIE;
Rx_index=0;
Rx_key=0;
break;
}
Rx_index++;
break;
case USCI_UART_UCTXIFG:
if(Tx_key==1)
{
if(Tx_index>size)
{
__bic_SR_register_on_exit(CPUOFF|GIE);
UCA0IE &= ~UCTXIE;
UCA0IE &= ~UCTXCPTIE;
Tx_key=0;
break;
}
UCA0TXBUF = TXData[Tx_index];
Tx_index++;
Tx_key=0;
}
break;
case USCI_UART_UCSTTIFG: break;
case USCI_UART_UCTXCPTIFG:
Tx_key=1;
UCA0IFG|=UCTXIFG;
break;
default: break;
}
}