微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > MSP430模拟串口程序

MSP430模拟串口程序

时间:11-13 来源:互联网 点击:
我用413模拟串口收发数据,只能接收单个字节,用串口助手调试时发现413发送给PC机的字节不正确。
比如串口助手发0xaa给413,413接收后把0xaa发送回给PC机,但是串口助手接收到的是0x00或其他数据,
为什么?怎样才能实现一串数据的收发正确?
程序如下(晶振是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; //位计数

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时钟,连续计

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top