Hi,
I have been trying to use my launchpad board's UART capabilities to echo characters on the serial terminal. The code below works ONLY if I debug the program. That is, if I use the debug option in the code composer studio, let the program load and then press continue without adding any breakpoints, the code works as long as I am still debugging.
As soon as I stop debugging, that is, get out of the debug mode in CCS, I start seeing garbage character '▒' on the serial terminal, whatever I type. Similarly, if I try to directly load the program instead of going into a debug session. I am facing the same problem. I use driverlib and here is my code.
I toggle an LED to see if the code enters the interrupt. It does that everytime I press a key, but the board just returns garbage value from the interrupt.
#include "driverlib.h" void GPIO_init(void); void UART_init(void); uint8_t receivedData; uint8_t transmitData; void main(void) { WDT_A_hold(WDT_A_BASE); GPIO_init(); UART_init(); __delay_cycles(1000); // I read somewhere that this helps. I don't see why. //Enable UART module for operation USCI_A_UART_enable(USCI_A1_BASE); // Receive interrupt USCI_A_UART_enableInterrupt(USCI_A1_BASE, UCRXIE); __bis_SR_register(LPM3_bits+GIE); __no_operation(); } void GPIO_init() { // initialize GPIO pins // We need to setup the two GPIO points to their right functions and toggle the values // Clear GPIO interrupt P3DIR=0xFF; P3OUT&=0x00; P4DIR=0xFF; P4OUT&=0x00; P1DIR=0xFF; P1OUT&=0x00; //Set up LED PIN GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0); GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN7); GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0); GPIO_setOutputLowOnPin(GPIO_PORT_P4,GPIO_PIN7); // Set GPIO for alternate function GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4, GPIO_PIN5); // UART RX as an alternate function for p4.5 GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P4, GPIO_PIN4);//UART TX as an alternate function for p4.4 } void UART_init() { // Here I am not sure if my values for clock prescalar, firstmodreg, secondmodreg are correct. USCI_A_UART_initParam param1 = {0}; param1.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK; param1.clockPrescalar = 6; // Values for 9600 baud rate param1.firstModReg = 13; // Values for 9600 baud rate param1.secondModReg = 0; // Values for 9600 baud rate param1.parity = USCI_A_UART_NO_PARITY; param1.msborLsbFirst = USCI_A_UART_LSB_FIRST; param1.numberofStopBits = USCI_A_UART_ONE_STOP_BIT; param1.uartMode = USCI_A_UART_MODE; param1.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION; if (STATUS_FAIL == USCI_A_UART_init(USCI_A1_BASE, ¶m1)){ return; } } #pragma vector=USCI_A1_VECTOR __interrupt void USCI_A1_ISR(void) { switch(__even_in_range(UCA1IV,4)) { case 2: // Echo back RXed character, confirm TX buffer is ready first // Wait if either transmit or receive is happening while(USCI_A_UART_queryStatusFlags(USCI_A1_BASE, UCBUSY)); // Receive echoed data receivedData=USCI_A_UART_receiveData(USCI_A1_BASE); transmitData=receivedData; while(USCI_A_UART_queryStatusFlags(USCI_A1_BASE, UCBUSY)); while(!USCI_A_UART_getInterruptStatus(USCI_A1_BASE, UCTXIFG)); USCI_A_UART_transmitData(USCI_A1_BASE,transmitData); GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0); // Toggle LED to show it enters interrupt break; default: break; } }
My first thought was it had something to do with the baud rate calculation. However, if I try other values to set the baud rates I don't even read the garbage character at the terminal.