unsigned int code T_H[]={3360,1120,672,480,373,305,258,224,0};
unsigned char code UM_CTL1_N[9]={0x00,0x08,0x22,0x92,0x55,0x6d,0xdb,0x7f,0xff};
/*******************************************************************/
//函数输入参数两个,BRCLK为给定输入频率,BAUD为需要得到的波特率
//UBR01,UBR11,UMCTL1,是430的USART三个分频与调整必须的配置寄存器在程序调用中被设定
//返回值代表成功与否,1为成功,0为失败。失败原因是波特率高于给定频率或过低,无法分频。
//
/*******************************************************************/
unsigned char usart_ubr_init(long unsigned int BRCLK,long unsigned int BAUD)
{
unsigned char i;
unsigned int UBR_temp;
long unsigned int T_UBR_ADD;
if(BAUD>BRCLK){return(0);}
else if((BRCLK/BAUD)>65535){return(0);}
UBR_temp=BRCLK/BAUD;
UBR01=UBR_temp;
UBR_temp>>=8;
UBR11=UBR_temp;
UBR_temp=(BRCLK-(BRCLK/BAUD)*BAUD);
if(UBR_temp==0){UMCTL1=UM_CTL1_N[0];return(1);}
else
{
T_UBR_ADD=BAUD*210/UBR_temp;
if(T_UBR_ADD>65535){UMCTL1=UM_CTL1_N[0];return(1);}
else UBR_temp=T_UBR_ADD;
for(i=0;i<9;i++)
{
if(T_UBR_ADD>T_H){UMCTL1=UM_CTL1_N;return(1);}
}
}
return(1);
}
//我想至于它的应用想用的应该清楚吧
用户48440 2006-9-25 10:44