Hi everyone,
I'm trying to use the UART with a MSP430F2132 but the output is constantly giving be weird results. I want a standard 9600 - 8N1 output and send string of characters.
Here is the code :
#include <msp430.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define GREEN_LED BIT7 // Greend led on P1OUT BIT 7 #define GREEN_LED_ON (P1OUT |= GREEN_LED) // Turn on green led void Init_board( void ); void uart_init( void ); static inline void Wait_1sec( void ); // Wait 1 sec static void UART_start_sending( void ); // Start sending throught UART static unsigned char UART_get_rx( void ); // Get UART RX byte volatile char UART_rx_string[32] = {0} ; // Rx array volatile char UART_tx_string[] = { '@', '1', '\r', '\0' } ; // Tx array volatile unsigned char i = 0 ; // Counter for array volatile unsigned char UART_sending = 0 ; // UART sending flag void main(void) { WDTCTL = WDTPW | WDTHOLD; // stop watch dog timer _EINT(); // Enable interrupts Init_board(); // Init inputs/outputs Wait_1sec(); // Wait Power-up // UART test { uart_init(); // Init UART in 9600 - 8N1 unsigned int rxd = 0 ; // Read byte UCA0TXBUF = 'A' ; // Sample, test only Wait_1sec(); UART_start_sending(); // Start sending string while(rxd == 0) { rxd = UART_get_rx(); // Get RX byte Wait_1sec(); } GREEN_LED_ON ; } } static void UART_start_sending(void) { UART_sending = 1 ; // UART is sending IE2 |= UCA0TXIE; // Enable interrupt UCA0TXBUF = UART_tx_string[i++] ; // Fill buffer } static unsigned char UART_get_rx( void ) { return UART_rx_string[0]; } static inline void Wait_1sec(void) { // Enable WDT for 16ms waiting BCSCTL1 |= DIVA_0; // ACLK /1 (diminue l'interval) WDTCTL = WDT_ADLY_250; // WDT interval timer in ms (divided by BSCTL1) IE1 |= WDTIE; // Enable WDT interrupt volatile unsigned char i = 0; // Wait system to power-up ( 4 x 250ms ) for(i = 4 ; i > 0 ; i--) __bis_SR_register(LPM0_bits + GIE); // Enter LPM0 WDTCTL = WDTPW | WDTHOLD; // stop watch dog timer } void Init_board(void) { /* * P1.7 OUTPUT GREEND LED 1 */ P1DIR = 0x80; P2DIR = 0x00; /* * P3.4 OUTPUT UART_TXD 1 * P3.5 INPUT UART_RXD 0 * P3.6 INPUT UART_CLEAR_TO_SEND 0 // CTS and RTS connected * P3.7 OUTPUT UART_REQUEST_TO_SEND 1 // on Pmod RS232 */ P3DIR = 0x90; P1OUT = 0x00; // No outputs on P1DIR P2OUT = 0x00; // No outputs on P2DIR P3OUT = 0x00; // No outputs on P3DIR } void uart_init() { WDTCTL = WDTPW + WDTHOLD; // Stop WDT if (CALBC1_1MHZ==0xFF) // If calibration constant erased while(1); // do not load, trap CPU!! DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; //------------ Configuring the UART(USCI_A0) ----------------// P3SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 104; // 1MHz 9600 UCA0BR1 = 0; // 1MHz 9600 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt //---------------- Enabling the interrupts ------------------// IE2 &= ~UCA0TXIE; // Disable the Transmit interrupt IE2 |= UCA0RXIE; // Enable the Receive interrupt } #pragma vector=WDT_VECTOR __interrupt void watchdog_timer (void) { __bic_SR_register_on_exit(LPM0_bits); // Clear LPM0 bits from 0(SR) } #pragma vector = USCIAB0RX_VECTOR __interrupt void USCIAB0RX_ISR(void) { // Check for UART RX if (UC0IFG & UCA0RXIFG) { IFG2 &= ~UCA0RXIFG; // Clear RX flag UART_rx_string[0] = UCA0RXBUF; } } #pragma vector = USCIAB0TX_VECTOR __interrupt void USCIAB0TX_ISR(void) { // Check for UART TX if (UART_sending) { char current_str = UART_tx_string[i++] ; // TX next character if( '\0' == current_str ) // TX over? { UART_sending = 0 ; // UART stop sending i = 0; // Counter to zero IE2 &= ~UCA0TXIE; // Disable interrupt } else UCA0TXBUF = current_str; } }
The 'A' character, alone, is giving me this :
and the complete string "@1\r" :
The character 'A' is 0x41, but the TXD output starts at zero volts and I should see only two bits at one (0100 0001).
For the string, only one byte is sent (I think) and the ' @' character is 0x40, so for sure not what I'm looking at.
Any help is welcome
Thanks in advance