Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

FR5969 UART Bluetooth

$
0
0

Hi guys, new around here and I am also a beginner (my only experience so far is withat needs your help to implement a UART interface for the FR5969. This is modified from an example from http://longhornengineer.com/projects/code/msp-430-launch-pad/uart-and-fifos/ that was implemented for the G2553. I tried to get it to work but can't seem to receive anything on my phone(Using BlueTerm). I am not sure if I missed anything out, or made a mistake and would like you guys to help me.

#include "msp430.h"
#include "uart.h"

#define LED BIT0
#define RXD BIT6
#define TXD BIT5


volatile unsigned int tx_flag;			//Mailbox Flag for the tx_char.
volatile unsigned char tx_char;			//This char is the most current char to go into the UART

volatile unsigned int rx_flag;			//Mailbox Flag for the rx_char.
volatile unsigned char rx_char;			//This char is the most current char to come out of the UART

/*uart_init
* Sets up the UART interface via USCI
* INPUT: None
* RETURN: None
*/
void uart_init(void)
{
	P2SEL0 |= RXD + TXD;					//Setup the I/O
	//P2SEL1 |= RXD + TXD;

    P1DIR |= LED; 						//P1.0 red LED. Toggle when char received.
    P1OUT |= LED; 						//LED off

	UCA1CTLW0 |= UCSSEL_2; 				//SMCLK
										//8,000,000Hz, 9600Baud, UCBRx=52, UCBRSx=0, UCBRFx=1
	UCA1BR0 = 52;                  		//8MHz, OSC16, 9600
	UCA1BR1 = 0;                   	 	//((8MHz/9600)/16) = 52.08333
	UCA1MCTLW = 0x10|UCOS16; 			//CBRFx=1,UCBRSx=0, UCOS16=1
	UCA1CTLW0 &= ~UCSWRST; 				//USCI state machine
	UCA1IE |= UCRXIE ; 					//Enable USCI_A1 RX interrupt

	rx_flag = 0;						//Set rx_flag to 0
	tx_flag = 0;						//Set tx_flag to 0

	return;
}

/*uart_getc
* Get a char from the UART. Waits till it gets one
* INPUT: None
* RETURN: Char from UART
*/
unsigned char uart_getc()				//Waits for a valid char from the UART
{
	while (rx_flag == 0);		 		//Wait for rx_flag to be set
	rx_flag = 0;						//ACK rx_flag
    return rx_char;
}

/*uart_gets
* Get a string of known length from the UART. Strings terminate when enter is pressed or string buffer fills
* Will return when all the chars are received or a carriage return (\r) is received. Waits for the data.
* INPUT: Array pointer and length
* RETURN: None
*/
void uart_gets(char* Array, int length)
{
	unsigned int i = 0;

	while((i < length))					//Grab data till the array fills
	{
		Array[i] = uart_getc();
		if(Array[i] == '\r')				//If we receive a \r the master wants to end
		{
			for( ; i < length ; i++)		//fill the rest of the string with \0 nul. Overwrites the \r with \0
			{
				Array[i] = '\0';
			}
			break;
		}
		i++;
	}

    return;
}

/*uart_putc
* Sends a char to the UART. Will wait if the UART is busy
* INPUT: Char to send
* RETURN: None
*/
void uart_putc(unsigned char c)
{
	tx_char = c;						//Put the char into the tx_char
	UCA1IE |= UCRXIE ; 					//Enable USCI_A1 TX interrupt
	while(tx_flag == 1);				//Have to wait for the TX buffer
	tx_flag = 1;						//Reset the tx_flag
	return;
}

/*uart_puts
* Sends a string to the UART. Will wait if the UART is busy
* INPUT: Pointer to String to send
* RETURN: None
*/
void uart_puts(char *str)				//Sends a String to the UART.
{
     while(*str) uart_putc(*str++);		//Advance though string till end
     return;
}

#pragma vector = USCI_TX_VECTOR_		//UART TX USCI Interrupt
__interrupt void USCI0TX_ISR(void)
{
	UCA1TXBUF = tx_char;				//Copy char to the TX Buffer
	tx_flag = 0;						//ACK the tx_flag
	UCA1IE = 0; 						//Turn off the interrupt to save CPU
}

#pragma vector = USCI_RX_VECTOR			//UART RX USCI Interrupt. This triggers when the USCI receives a char.
__interrupt void USCI0RX_ISR(void)
{
	rx_char = UCA1RXBUF;				//Copy from RX buffer, in doing so we ACK the interrupt as well
	rx_flag = 1;						//Set the rx_flag to 1

	P1OUT ^= LED;						//Notify that we received a char by toggling LED
}
 

For more information I have this in my main program

WDTCTL = WDTPW + WDTHOLD; 			//Stop WDT
    PM5CTL0 &= ~LOCKLPM5;			// Disable the GPIO power-on default high-impedance mode to activate
    				                // previously configured port settings
    CSCTL1 = DCOFSEL0 + DCOFSEL1;               // 8 MHz DCO (8 MHz is FRAM speed limit);Set DCO to 8Mhz

    uart_init();				//Initialize the UART connection

    __enable_interrupt();		        //Interrupts Enabled

I am also using a HC-05 Bluetooth module and have crossed the RXD and TXD.

Thank you very much in advance.


Viewing all articles
Browse latest Browse all 2077

Trending Articles