Question
I need help with my code for MSP430 8.3 Sending an ASCII String over UART In this part, write a function that transmits a string
I need help with my code for MSP430
8.3 Sending an ASCII String over UART In this part, write a function that transmits a string over the UART connection. This is the header of the function. void uart write string(char * str); This function should work for a string of any size and should call the function we wrote earlier, uart write char(), to send ASCII characters. 65 For example, if we declare the string below and call our function, it should transmit this string to the terminal application. Remember that strings in the C language are null terminated (they end with the NULL character). Therefore, the function should transmit all the characters until it reaches the NULL character. char mystring[] = "UART Transmission Begins..."; Perform the following: Complete the code and demo it to the TA. Test the function for a few strings of different sizes, and containing spaces Submit the code in your report.
//part 8.3 #include
#define FLAGS UCA1IFG // Contains the transmit & receive flags #define RXFLAG UCRXIFG // Receive flag #define TXFLAG UCTXIFG // Transmit flag #define TXBUFFER UCA1TXBUF // Transmit buffer #define RXBUFFER UCA1RXBUF // Receive buffer
#define redLED BIT0 #define greenLED BIT7
// Configure UART to the popular configuration // 9600 baud, 8-bit data, LSB first, no parity bits, 1 stop bit // no flow control // Initial clock: SMCLK @ 1.048 MHz with oversampling void Initialize_UART(void) { // Divert pins to UART functionality P3SEL1 &= ~(BIT4|BIT5); P3SEL0 |= (BIT4|BIT5); // Use SMCLK clock; leave other settings default UCA1CTLW0 |= UCSSEL_2; // Configure the clock dividers and modulators // UCBR=6, UCBRF=13, UCBRS=0x22, UCOS16=1 (oversampling) UCA1BRW = 6; UCA1MCTLW = UCBRS5|UCBRS1|UCBRF3|UCBRF2|UCBRF0|UCOS16; // Exit the reset state (so transmission/reception can begin) UCA1CTLW0 &= ~UCSWRST; }
void uart_write_char(unsigned char ch) { // Wait for any ongoing transmission to complete while ( (FLAGS & TXFLAG)==0 ) {} // Write the byte to the transmit buffer TXBUFFER = ch; }
const unsigned char ASCII_Num[11] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x0 };
void uart_write_uint16(unsigned int n) {
n = n%65536; unsigned int tclock = n; unsigned int p1 = tclock % 10; tclock = tclock/10; unsigned int p2 = tclock % 10; tclock = tclock/10; unsigned int p3 = tclock % 10; tclock = tclock/10; unsigned int p4 = tclock %10; tclock = tclock/10; unsigned int p5 = tclock %10;
if(p5 != 0) { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[p5]; } else { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[10]; }
if((p4 == 0)&&(p5 == 0)) { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[10]; } else { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[p4]; }
if((p3 == 0)&&(p4 == 0)&&(p5 == 0)) { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[10]; } else { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[p3]; }
if((p2 == 0)&&(p3 == 0)&&(p4 == 0)&&(p5 == 0)) { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[10]; } else { while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[p2]; }
while ( (FLAGS & TXFLAG)==0 ) {} TXBUFFER = ASCII_Num[p1];
P9OUT ^= greenLED; }
// The function returns the byte; if none received, returns NULL unsigned char uart_read_char(void) { unsigned char temp; // Return NULL if no byte received if( (FLAGS & RXFLAG) == 0) return 'NULL'; // Otherwise, copy the received byte (clears the flag) and return it temp = RXBUFFER; return temp; }
unsigned int clock = 65530; unsigned int i;
int main(void) { unsigned char ch; char recieve;
Initialize_UART(); config_ACLK_to_32KHz_crystal(); //up mode with interrupt I am placing interrupts ahead of time in anticipation of needing controls TA0CCR0 = 32767; // Fill to get 1 second @ 32 KHz TA0CCTL0 = CCIE; // Enable Channel 0 CCIE bit TA0CCTL0 &= ~CCIFG; // Clear Channel 0 CCIFG bit // Timer_A: ACLK, div by 1, up mode, clear TAR (leaves TAIE=0) TA0CTL = TASSEL_1 | ID_0 | MC_1 | TACLR;
WDTCTL = WDTPW | WDTHOLD; //stop wdt PM5CTL0 &= ~LOCKLPM5; //enable gpio pins __enable_interrupt();
//enable leds P1DIR |= redLED; // Direct pin as output P9DIR |= greenLED; P1OUT &= ~redLED; // Turn LED Off P9OUT &= ~greenLED;
for(;;){} }
#pragma vector = TIMER0_A0_VECTOR __interrupt void T0A0_ISR() { uart_write_uint16(clock); uart_write_char(' '); uart_write_char(' '); for(i=0; i<60000;i++){} clock = clock + 1; }
void config_ACLK_to_32KHz_crystal()
{ // By default, ACLK runs on LFMODCLK at 5MHz/128 = 39 KHz // Reroute pins to LFXIN/LFXOUT functionality PJSEL1 &= ~BIT4; PJSEL0 |= BIT4; // Wait until the oscillator fault flags remain cleared CSCTL0 = CSKEY; // Unlock CS registers do { CSCTL5 &= ~LFXTOFFG; // Local fault flag SFRIFG1 &= ~OFIFG; // Global fault flag } while((CSCTL5 & LFXTOFFG) != 0); CSCTL0_H = 0; // Lock CS registers return;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started