本帖最后由 中国白杨 于 2013-9-5 16:43 编辑
用的是MSP430F149这个芯片。利用usart时出现了这样一个现象:usart0在波特率2400,4800,9600,38400下都能实现收发,usart1在波特率2400,4800,9600,19200,下实现收发也没有问题,在38400波特率下发送也没有问题,就是在接收的时候接收不到,我测试了一下。串口发送FF,RXBUF1接收到FE,发送FE,RXBUF1接收到FC,发送FC,RXBUF1接收到F8,发送F8,RXBUF1接收到F0,发送F0,RXBUF1接收到E0,发送E0,接收C0,发送C0,接收80,发送80,接收00。发送EF,接收CE,发送CF,接收8E,发送8F,接收0E。7F及小于7F的接收不到。这个问题是怎么回事?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
能上传一下你的电路图和程序吗
#define MAX_RD_LEN 16
void Init_sys(void);
void Init_Uart(void);
unsigned char Receive[MAX_RD_LEN+3+1]={0}; //receive byte
unsigned int Receive_Cnt=0;
unsigned char R_Time_cnt=0;
unsigned char R_Flag=0;
unsigned char R_End_Flag=0; //If receive end flag equal 1,then receive end and storage message.
unsigned char Tmp_data_Len=0;
unsigned char *R_Data_Ptr=(unsigned char *)0x1000;//Storage message pointer;
//..........................................................................................................
void main(void)
{
unsigned char i;
Init_sys();
Init_Uart();
U0TCTL |=TXEPT;
while(1)
{
if(R_End_Flag==1)
{
if((Receive[0]==0xAA)&&(Receive[1]==0x00)&&(Receive[2]==0x00))
{
if(Receive_Cnt>MAX_RD_LEN+3) //if the length that program receive is longer than max receive data length,initialize the length.
Receive_Cnt=MAX_RD_LEN+3;
Tmp_data_Len=Receive_Cnt-3;
WDTCTL=WDTPW+WDTHOLD; //Disable WDT
_DINT(); //Disable interrupts
while((BUSY&FCTL3)==1); //Test BUSY Loop while busy
FCTL2=FWKEY+FSSEL1+FN0; //SMCLK/2
FCTL3=FWKEY; //Clear LOCK
FCTL1=FWKEY+ERASE; //Enable erase
*R_Data_Ptr=0;
while((BUSY&FCTL3)==1); //Test BUSY Loop while busy
FCTL1=FWKEY+WRT; //Enable write and Disable erase
*R_Data_Ptr++=Tmp_data_Len; //First byte for storaging data length
while((BUSY&FCTL3)==1); //Test BUSY Loop while busy
for(i=3;i<Receive_Cnt;i++)
{
*R_Data_Ptr++=Receive[i];
while((BUSY&FCTL3)==1);
}
FCTL3=FWKEY+LOCK; //Done, set LOCK
_EINT(); //Enable interrupts
R_Data_Ptr=(unsigned char *)0x1000; //initialize pointer
if(*R_Data_Ptr==Receive_Cnt-3)
{
//P5OUT|=RTS10; //485处于发送状态
TXBUF1=0xAA;
while((U1TCTL&0x01)==0);
TXBUF1=0x00;
while((U1TCTL&0x01) ==0);
TXBUF1=0x00;
while((U1TCTL&0x01) ==0);
//P5OUT&=~RTS10; //485处于接收状态
}
}
if((Receive[0]==0xAB)&&(Receive[1]==0x00)&&(Receive[2]==0x00))
{
//P5OUT|=RTS10; //485处于发送状态
TXBUF1=0xAB;
while((U1TCTL&0x01)==0);
TXBUF1=0x00;
while((U1TCTL&0x01)==0);
TXBUF1=0x00;
while((U1TCTL&0x01)==0);
if(*R_Data_Ptr==0xFF)
{
TXBUF1=0x00;
while((U1TCTL&0x01) ==0);
}
else
{
Tmp_data_Len=*R_Data_Ptr++;
for(i=0;i<Tmp_data_Len;i++)
{
TXBUF1=*R_Data_Ptr++;
while((U1TCTL&0x01) ==0);
}
}
//P5OUT&=~RTS10; //485处于接收状态
R_Data_Ptr=(unsigned char *)0x1000; //initialize pointer
}
Receive_Cnt=0;
R_End_Flag=0; //wait for next
}
}
}
void Init_sys(void) //Initalize MSP430 system
{
unsigned char i;
//New:switch 7.3728MHZ and 32768HZ
WDTCTL=WDTPW+WDTHOLD; //Stop WDT
BCSCTL1 &=~(XT2OFF+XTS); //LFXT1 = LF XTAL and Put on XT2!
//BCSCTL1 |=DIVA_3;
do
{
IFG1 &=~OFIFG; //Clear OSC fault flag
for (i=0xFF;i>0;i--); // i = Delay*/
}
while((IFG1 & OFIFG)!=0); //OSC fault flag set?
BCSCTL2 |=(SELM_2+SELS);
BCSCTL2 |=(DIVS0+DIVS1); //;SMCKLK 分频8
//TIMERA 10ms
TACTL=TASSEL_2+MC0+TACLR;
CCTL0=CCIE; //捕获/比较寄存器CCR0
CCR0=9216-1; //CCR0 counts to 10ms;
return;
}
//......................................................................................................
void Init_Uart(void)
{
UCTL1=CHAR+SWRST; //8-bit characters(p216)
UTCTL1=SSEL1; //SMCLK;
switch(3)
{
case 3:
UBR01=0x18; //38400;
UBR11=0x00;
}
UMCTL1=0x00; //7.3728MHz no modulation 115200
UCTL1 &=~SWRST;
ME2 |=(UTXE1+URXE1); //Enable USART0 TXD/RXD
IE2 |=URXIE1; //Enable USART0 RX interrupt
P3SEL |=0xC0; //P3.6,7 = USART0 TXD/RXD
P3DIR |=0x40; //P3.6 = output direction
_EINT();
}
#pragma vector=TIMERA0_VECTOR //在IAR2.0以上中断函数声明。
__interrupt void TIMERA0_rx (void)
{
if(R_Flag==1) //when R_Flag=1,receive time count add.
{
R_Time_cnt++;
if(R_Time_cnt>=2)
{
R_End_Flag=1; //receive end and begin to handle in main
R_Time_cnt=0; //intialize R_Time_cnt 0
R_Flag=0; //count stop
}
}
}
/*******************************************************************************/
//用来控制单片机与上位机之间的通信 232通信,注意:波特率38400行不通
/*******************************************************************************/
#pragma vector=UART1RX_VECTOR
__interrupt void usart1_rx (void)
{
Receive[Receive_Cnt]=RXBUF1;
Receive_Cnt++;
R_Flag=1; //begin to count
R_Time_cnt=0; //restart to count
}
连接光耦,连接max202e,连接电脑。我再2400,4800,9600,19200下测试了,都能实现双向通讯,38400下可实现发送,但不能实现接收,接收出现的现象如我第一版所述。USART0和USART1还是有区别的是吗?在USART0下实现发送和接收知道115200还是很好的。这是怎么个情况,谁给解释一下呢?
一周热门 更多>