金牌会员
- 积分
- 1334
- 威望
- 675
- 贡献
- 385
- 兑换币
- 297
- 注册时间
- 2012-6-9
- 在线时间
- 137 小时
- 毕业学校
- LANGFANG
|
我让单片机连续发送,电脑串口接收总是错误的,但换个字母,也会换错误的内容,波特率上试了很多,外部晶振用的是16M的 ,问问做过的各位 哪有错吗?????
求
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
void SetSysCLK_16M (void)
{
CLKSEL = 0X00; // disengage PLL to system
PLLCTL_PLLON = 1; // turn on PLL
SYNR = 0x00|0x01; // VCOFRQ[7:6];SYNDIV[5:0]
// fVCO= 2*fOSC*(SYNDIV + 1)/(REFDIV + 1)
// fPLL= fVCO/(2 × POSTDIV)
// fBUS= fPLL/2
// VCOCLK Frequency Ranges VCOFRQ[7:6]
// 32MHz <= fVCO <= 48MHz 00
// 48MHz < fVCO <= 80MHz 01
// Reserved 10
// 80MHz < fVCO <= 120MHz 11
REFDV = 0x80|0x01; // REFFRQ[7:6];REFDIV[5:0]
// fREF=fOSC/(REFDIV + 1)
// REFCLK Frequency Ranges REFFRQ[7:6]
// 1MHz <= fREF <= 2MHz 00
// 2MHz < fREF <= 6MHz 01
// 6MHz < fREF <= 12MHz 10
// fREF > 12MHz 11
// pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
POSTDIV = 0x00; // 4:0, fPLL= fVCO/(2xPOSTDIV)
// If POSTDIV = $00 then fPLL is identical to fVCO (divide
by one).
_asm (nop); // BUS CLOCK=16M
_asm (nop);
while (!(CRGFLG_LOCK == 1)); // when pll is steady ,then use it;
CLKSEL_PLLSEL = 1; // engage PLL to system;
}
/**********延时*********/
void Delay(unsigned int i)
{
unsigned int k,j;
for(j=0;j<i;j++)
{
for(k=0;k<1000;k++);
}
}
/********SCI串口初始化********/
void SCIInit(void)
{
SCI0BDH=0x00;
SCI0BDL=0x68; //16M 9600波特率设置 104=16000000/(9600*16)
SCI0CR1=0;
SCI0CR2=0x0C; //没有使用中断
}
/***********发送函数*********/
void SCIWrite(unsigned char data_0)
{
while(!SCI0SR1_TDRE);
SCI0DRH=0;
SCI0DRL=data_0;
}
/******接收函数,返回接收值*******/
unsigned char SCIRend(void)
{
if(SCI0SR1_RDRF==1)
{
SCI0SR1_RDRF=1;
return SCI0DRL;
}
}
void main(void) {
/* put your own code here */
unsigned char data='A';
SetSysCLK_16M ();
SCIInit();
SCIWrite(data);
Delay(100);
SCIWrite(data);
Delay(1);
for(;;)
{
SCIWrite(data);
Delay(1000); //试一下SCIWrite(SCIRend());
}
EnableInterrupts;
for(;;) {
_FEED_COP(); /* feeds the dog */
}
/* loop forever */
/* please make sure that you never leave main */
}
|
|