MSP430 simulated serial port program

Publisher:梦想启航Latest update time:2016-08-16 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I use 413 to simulate serial port data transmission and reception, but can only receive single bytes. When debugging with serial port assistant, I find that the bytes sent by 413 to PC are incorrect.
For example, the serial port assistant sends 0xaa to 413, and 413 sends 0xaa back to PC after receiving it, but the serial port assistant receives 0x00 or other data.
Why? How can I achieve the correct transmission and reception of a string of data?
    The program is as follows (the crystal oscillator is 32768, connected to pins 8 and 9 of 413):

#include

#define RXD 0x0002 // RXD on P1.1
#define TXD 0x0002 // TXD on P2.1
#define RS485 0x0001 //enable 485 on P2.0

// Conditions for 2400 Baud SW UART, ACLK = 32768
#define Bitime_5 0x06 // ~ 0.5 bit length + small adjustment
#define Bitime 0x0E // 427us bit length ~ 2341 baud

unsigned int RXTXData;
unsigned char BitCnt;
void Delay(unsigned int i);
void TX_Byte(void);
void RX_Ready(void);

void main(void)
{
    WDTC TL = WDTPW + WDTHOLD; // Stop watchdog timer
    FLL_CTL0 |= 85; P2OUT
    &= ~RS485; _EINT(); // Mainloop for(;;) {     RX_Ready     (     )     ;
    //     UART     ready     to         RX one Byte         _BIS_SR(LPM3_bits + GIE); // Enter LPM3 Until character RXed TX_Byte();     //     TX         Back RXed Byte Received     } } void Delay(unsigned int i) {     unsigned char j;     for     (j = 0;j< 5; j ++)         while(i--); } // Function Transmits Character from RXTXData Buffer void TX_Byte(void) {     P2OUT |= RS485;     Delay(10);     P2OUT &= ~RS485;     BitCnt = 0x0A; // Load Bit counter, 8data + ST/SP     CCR0 = TAR; // Current state of TA counter     CCR0 += Bitime; // Some time till first bit     RXTXData |= 0x0100; // Add mark stop bit to RXTXData     RXTXData = RXTXData << 1; // Add space start bit     CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle     while(CCTL0 & CCIE); // Wait for TX completion } // Function Readies UART to Receive Character into RXTXData Buffer void RX_Ready(void) {     BitCnt = 0x08; // Load Bit counter     CCTL0 = SCS+ CCIS0 + OUTMOD0 + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture } // Timer A0 interrupt service routine interrupt [TIMERA0_VECTOR] void TimerA0_ISR(void) {



















































    CCR0 += Bitime; // Add Offset to CCR0

    // RX------------------------------------ -----------------------------------------------
    if(CCTL0 & CCIS0) // RX on CCI0B?
    {
        if(CCTL0 & CAP) // Capture mode = start bit edge
        {
            CCTL0 &= ~CAP; // Capture to compare mode
            CCR0 += Bitime_5;
        }
        else
        {
            RXTXData = RXTXData >> 1;
            if(CCTL0 & SCCI) / / Get bit waiting in receive latch
                RXTXData |= 0x80;

            BitCnt --; // All bits RXed?
            if(BitCnt == 0)
            //>>>>>>>>>> Decode of Received Byte Here <<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            {
                CCTL0 &= ~CCIE; // All bits RXed, disable interrupt
                _BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR)
            }
            //>>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<
        }
    }
    // TX---------------------- -------------------------------------------------
    else
    {
        if(BitCnt == 0)
            CCTL0 &= ~CCIE; // All bits TXed, disable interrupt
        else
        {
            CCTL0 |= OUTMOD2; // TX Space
            if(RXTXData & 0x0001)
                CCTL0 &= ~OUTMOD2; // TX Mark

            RXTXData = RXTXData >> 1;
            BitCnt--;
        }
    }
}

    Please give me some advice!!!

    lsdfae18
    2006-10-13, 11:49
    Refer to this program

#define RXD 0x02 // RXD on P1.1
#define TXD 0x01 // TXD on P1.0

// Conditions for 2400 Baud SW UART, ACLK = 32768

#define Bitime_5 0x06 // ~ 0.5 bit length + small adjustment(14/2=7)
#define Bitime 0x0E // 427us bit length ~ 2341 baud (14/32768=427.246us,32768/14=2340.571428)

unsigned int RXTXData;
unsigned char BitCnt; //bit count

void TX_Byte (void);
void RX_Ready (void);

// M.Buccini
// Texas Instruments, Inc
// March 2002
//************************************************ **********************************

#include

void main (void)
{
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer (turn off the watchdog module)
    FLL_CTL0 |= XCAP14PF; // Configure load caps (configure oscillator capacitance)
    CCTL0 = OUT; // TXD Idle as Mark (capture/compare register 0: output high level)
    TACTL = TASSEL0+MC1; // ACLK, continous mode
    P1SEL = TXD + RXD; // PP1.0/1 TA0 for TXD/RXD function
    P1DIR = TXD; // TXD output on P1

    // Mainloop
    for (;;)
    {
        RX_Ready(); // UART ready to RX one Byte
        _BIS_SR(LPM3_bits+GIE); // Enter LPM3 Until character RXed
        TX_Byte(); // TX Back RXed Byte Received
    }
}


// Function Transmits Character from RXTXData Buffer
void TX_Byte (void)
{
    BitCnt = 0xA; // Load Bit counter, 8data + ST/SP
    CCR0 = TAR; // Current state of TA counter(Timer counter current value is placed in compare register)
    CCR0 += Bitime; // Some time till first bit(Push back one bit time)
    RXTXData |= 0x100; // Add mark stop bit to RXTXData(Fill stop bit)
    RXTXData = RXTXData << 1; // Add space start bit(Fill start bit)
    CCTL0 = OUTMOD0+CCIE; // TXD = mark = idle(Set output mode, enable interrupt)
    while ( CCTL0 & CCIE ); // Wait for TX completion(Wait for TX completion)
}


// Function Readies UART to Receive Character into RXTXData Buffer
void RX_Ready (void)
{
    BitCnt = 0x8; // Load Bit counter(Receive 8 bits)
    CCTL0 = SCS+CCIS0+OUTMOD0+CM1+CAP+CCIE; // Sync, Neg Edge, Capture
    //(CCIxB pin synchronous falling edge capture, set output mode, enable interrupt)
}


// Timer A0 interrupt service routine
interrupt[TIMERA0_VECTOR] void Timer_A (void)
{
    CCR0 += Bitime; // Add Offset to CCR0 (next timing time is 1 bit time)

    // RX (receive byte)
    if (CCTL0 & CCIS0) // RX on CCI0B? (If CCI0B is the capture input pin)
    {
        if( CCTL0 & CAP ) // Capture mode = start bit edge (If it is in capture mode, it is waiting for the start bit)
        {
            CCTL0 &= ~ CAP; // Switch from capture to compare mode (Change to compare mode after getting the start bit)
            CCR0 += Bitime_5; //Change the timing position to the middle of the bit (add half a bit time)
        }
        else
        {
            RXTXData = RXTXData >> 1; //The low bit is received firstif
            (CCTL0 & SCCI) // Get bit waiting in receive latch
                RXTXData |= 0x80; //What is latched in SCCI is the signal on the input pin when the comparison is equal

            BitCnt --; // All bits RXed? //Decrease the bit count by 1
            if ( BitCnt == 0) //Has one byte been received?
            //>>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            {
                CCTL0 &= ~ CCIE; // All bits RXed,disable interrupt (disable interrupt after receiving a byte)
                _BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR) (prepare to exit low power mode)
            }
            //>>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        }
    }
    // TX (send byte, OUTMOD0 is set before entering the transmit interrupt)
    else
    {
        if ( BitCnt == 0) //All bits TXed
            , disable interrupt
        else
        {
            CCTL0 |= OUTMOD2; // TX Space (output mode OUTMOD2+OUTMOD0: reset)
            if (RXTXData & 0x01)
                CCTL0 &= ~ OUTMOD2; // TX Mark (output mode OUTMOD0: set)

            RXTXData = RXTXData >> 1; //Low bit first
            BitCnt --; //bit count
        }
    }
}
Keywords:MSP430 Reference address:MSP430 simulated serial port program

Previous article:MSP430F149-Using IO interrupt mode to implement key detection program
Next article:MSP430 P1.1 P1.2 P1.3 Second function

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号