cc2530串口操作
串口初始化
void UART0_Init(BaudSel baud)
{CLKCONCMD &= ~0X40; //晶振 0100 0000while(!(SLEEPSTA & 0X40)); //等待晶振稳定CLKCONCMD &= ~0X47; //TICHSPD128分频,CLKSPD不分频 0100 0111SLEEPCMD |= 0X04; //关闭不用的RC振荡器PERCFG = 0X00; //位置1 P0口P0SEL |= 0X0C; //P0用作串口 1100U0CSR |= 0X80; //UART方式switch(baud){case BAUD_2400: U0GCR |= 6; U0BAUD |= 59; break;case BAUD_4800: U0GCR |= 7; U0BAUD |= 59; break;case BAUD_9600: U0GCR |= 8; U0BAUD |= 59; break;case BAUD_14400: U0GCR |= 8; U0BAUD |= 216; break;case BAUD_19200: U0GCR |= 9; U0BAUD |= 59; break;case BAUD_28800: U0GCR |= 9; U0BAUD |= 216; break;case BAUD_38400: U0GCR |= 10; U0BAUD |= 59; break;case BAUD_57600: U0GCR |= 10; U0BAUD |= 216; break;case BAUD_76800: U0GCR |= 11; U0BAUD |= 59; break;case BAUD_115200: U0GCR |= 11; U0BAUD |= 216; break;case BAUD_230400: U0GCR |= 12; U0BAUD |= 216; break;default : U0GCR |= 11; U0BAUD |= 216; break; //BAUD_115200;}UTX0IF = 0;U0CSR |= 0X40; //允许接收IEN0 |= 0X84; //开总中断,接收中断}
寄存器
1、CLKCONCMD寄存器
CLKCONCMD &= ~0X40; //晶振 0100 0000while(!(SLEEPSTA & 0X40)); //等待晶振稳定
CLKCONCMD &= ~0X47; //TICHSPD128分频,CLKSPD不分频 0100 0111
2、SLEEPCMD寄存器
SLEEPCMD |= 0X04; //关闭不用的RC振荡器 0000 0100
3、PERCFG寄存器
PERCFG = 0X00; //位置1 P0口
4、PxSEL寄存器
P0SEL |= 0X0C; //P0用作串口 1100
5、UxCSR寄存器
U0CSR |= 0X80; //UART方式 1000 0000
U0CSR |= 0X40; //允许接收 0100 0000
6、RCON2寄存器
UTX0IF = 0;
7、IEN0寄存器
IEN0 |= 0X84; //开总中断,接收中断 1000 0100
8、UxUCR寄存器
接收中断
#pragma vector = URX0_VECTOR //不清楚作用__interrupt void UART0_ISR(void){static char temp[1];temp[0] = U0DBUF;UART0_Send(temp, 1);URX0IF = 0; //清中断标志}
发送
void UART0_Send(char *Data,int len)
{int i;for(i=0;i<len;i++){U0DBUF = *Data++;while(UTX0IF == 0);UTX0IF = 0;}
}