新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > MSP430模擬串口程序

MSP430模擬串口程序

作者: 時間:2016-11-13 來源:網(wǎng)絡 收藏
我用413模擬串口收發(fā)數(shù)據(jù),只能接收單個字節(jié),用串口助手調試時發(fā)現(xiàn)413發(fā)送給PC機的字節(jié)不正確。
比如串口助手發(fā)0xaa給413,413接收后把0xaa發(fā)送回給PC機,但是串口助手接收到的是0x00或其他數(shù)據(jù),
為什么?怎樣才能實現(xiàn)一串數(shù)據(jù)的收發(fā)正確?
程序如下(晶振是32768,接413的8和9腳):

#include <msp430x41x.h>

#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)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FLL_CTL0 |= XCAP14PF; // Configure load caps
CCTL0 |= OUT; // TXD Idle as Mark
TACTL = TASSEL_1 + MC_1; // ACLK, continous mode
P1SEL |= RXD;
P1DIR &= ~RXD;
P2SEL |= TXD;
P2DIR |= TXD;
P2DIR |= RS485;
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--;
}
}
}

請各位大蝦不吝指教?。?!

lsdfae18
2006-10-13, 11:49
參考這個程序吧

#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; //位計數(shù)

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(關閉看門狗模塊)
FLL_CTL0 |= XCAP14PF; // Configure load caps(配置振蕩器電容)
CCTL0 = OUT; // TXD Idle as Mark(捕獲/比較寄存器0:輸出高電平)
TACTL = TASSEL0+MC1; // ACLK, continous mode(定時器A:ACLK時鐘,連續(xù)計數(shù)模式)
P1SEL = TXD + RXD; // PP1.0/1 TA0 for TXD/RXD function(TA0引腳功能)
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(回傳收到的字節(jié))
}
}


// Function Transmits Character from RXTXData Buffer
void TX_Byte (void)
{
BitCnt = 0xA; // Load Bit counter, 8data + ST/SP(發(fā)送10位數(shù)據(jù))
CCR0 = TAR; // Current state of TA counter(定時器計數(shù)器當前值放比較寄存器中)
CCR0 += Bitime; // Some time till first bit(后推一位的時間)
RXTXData |= 0x100; // 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(等待發(fā)送完成)
}


// Function Readies UART to Receive Character into RXTXData Buffer
void RX_Ready (void)
{
BitCnt = 0x8; // Load Bit counter(接收8位)
CCTL0 = SCS+CCIS0+OUTMOD0+CM1+CAP+CCIE; // Sync, Neg Edge, Capture
//(CCIxB引腳同步下降沿捕獲,置位輸出模式,允許中斷)
}


// Timer A0 interrupt service routine
interrupt[TIMERA0_VECTOR] void Timer_A (void)
{
CCR0 += Bitime; // Add Offset to CCR0(下一定時時間為1位時間)

// RX(接收字節(jié))
if (CCTL0 & CCIS0) // RX on CCI0B?(如果CCI0B是捕獲輸入引腳)
{
if( CCTL0 & CAP ) // Capture mode = start bit edge(如果是在捕獲模式,則是在等待起始位)
{
CCTL0 &= ~ CAP; // Switch from capture to compare mode(得到起始位后改為比較模式)
CCR0 += Bitime_5; //定時位置改到位的中間位置(加半個位的時間)
}
else
{
RXTXData = RXTXData >> 1; //先收到的是低位
if (CCTL0 & SCCI) // Get bit waiting in receive latch
RXTXData |= 0x80; //SCCI中鎖存的是比較相等時的輸入引腳上的信號

BitCnt --; // All bits RXed? //位計數(shù)減1
if ( BitCnt == 0) //接收完一個字節(jié)?
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
CCTL0 &= ~ CCIE; // All bits RXed, disable interrupt(接收完一個字節(jié)后禁止中斷)
_BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR)(準備退出低功耗模式)
}
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
// TX(發(fā)送字節(jié),在進入發(fā)送中斷前已經(jīng)置位OUTMOD0)
else
{
if ( BitCnt == 0) //所有位發(fā)送完成
CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt(禁止中斷)
else
{
CCTL0 |= OUTMOD2; // TX Space(輸出模式OUTMOD2+OUTMOD0:復位)
if (RXTXData & 0x01)
CCTL0 &= ~ OUTMOD2; // TX Mark(輸出模式OUTMOD0:置位)

RXTXData = RXTXData >> 1; //低位先發(fā)
BitCnt --; //位計數(shù)
}
}
}


關鍵詞: MSP430模擬串

評論


技術專區(qū)

關閉