初始化,硬件和连线是没有问题的,因为用其他串口范例程序能够发送和接受。感觉是do...while语句有问题。
//串口试验程序
/*电脑向
单片机发送数据,单片机返回该数据给电脑*/
#include<c8051f310.h>
#define SYSCLK 24500000
#define BUADRATE 9600
void SYSCLK_Init(void);
void UART0_Init(void);
void PORT_Init(void);
void main(void)
{
unsigned char temp;
PCA0MD &= ~0x40;
PORT_Init();
SYSCLK_Init ();
UART0_Init();
do{
while(RI0==0);
temp=SBUF0;
RI0=0;
temp=SBUF0;
while(ti0==0);
TI0=0;
}while(1);
}
void PORT_Init(void)
{
P0MDOUT = 0x10;//P0.4 digital push-pull uart TX
XBR0 = 0x01; //Configure the crossbar and GPIO ports
XBR1 = 0x40;//P0.5 digital open-drain uart RX
}
//-------------------------------------------------------------------------------------
void SYSCLK_Init(void)
{
OSCICN = 0x83;//internal oscillater at it maximum frequncy
RSTSRC = 0x04;//enable missing clock detecter
}
//--------------------------------------------------------------------------------------
void UART0_Init(void)
{
SCON0 = 0x00; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
CKCON = 0x00;
TH1=0x96;
TL1 = TH1; // Init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
}
while(RI0==0);
temp=SBUF0;
RI0=0;
SBUF= temp0;
while(TI0==0);
TI0=0;
}while(1);
}
还有就是f310单片机Uart端口初始化时要准许接收。
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
接收使能没打开
的确。。。。后来发现了。。。还是谢谢了
一周热门 更多>