自己用单片机发送一串字符串,但是串口助手会一直接收,无限循环,我很费解,我已经设了 for(k=0;*q!='\0';k++)条件,为什么还是不能发送完一遍就停止发送。还有个问题就是最后那句delay(),我不知道该放在哪里好,要么接收的太快,要么太慢,字符串最后加了/n,可接收到的也没有换行,都乱成一团。搞了好久也搞不出,希望哪位高手指点小弟一下,万分感谢!
---------------------------以下是我的程序
#include <c8051f410.h> // SFR declarations
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 9600 // Baud rate of UART in bps
void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void delay(uint);
void SendData(uchar);
uchar code Table[]="我爱单片机\n"; //发送的字符串
uchar *Send=Table;
void delay(uint t)
{
for (;t>0;t--);
}
void PORT_Init (void)
{
P0MDOUT |= 0x10; // Enable UTX as push-pull output
XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
XBR1 = 0x40; // Enable crossbar and weak pull-ups
}
void SYSCLK_Init (void)
{
OSCICN = 0x87; // configure internal oscillator for
// 24.5MHz
RSTSRC = 0x04; // enable missing clock detector
}
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x08; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else if (SYSCLK/BAUDRATE/2/256 < 48) {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
} else {
while (1); // Error. Unsupported baud rate
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1启动定时器1
TI0 = 0; // Indicate TX0 ready 初始化TX0
//TL0= 0x98;//
// TH0=0x40;//定时器0
// TR0=1;//开启定时器0
// EA=1;//开放中断
// ET0=1;//允许定时器0中断
// ES0=0;//允许 UART0 中断
// RI0=1;//RX0
}
void SendData(uchar *q)
{
uchar k;
for(k=0;*q!='\0';k++){
SBUF0=*q;
while(!TI0);
TI0=0;
q++;
}
q=0;
}
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
PORT_Init(); // Initialize Port I/O
SYSCLK_Init (); // Initialize Oscillator
UART0_Init();
SendData(Send);
delay(1800000);
}
} |