📄 sport1.c
字号:
#include "chip.h"
#include "string.h"
#if CHIP==14 || CHIP==16
unsigned char tx1flg,rx1flg;
unsigned char rxbuf1;
void InitSport1()
{
UCTL1=SWRST; //Ready for configurating serial's parameters
UCTL1 |=CHAR;//+LISTEN; //Data frame format(8-N-1) with listen
UTCTL1=SSEL1+URXSE; //Selection of SMCLK becomes baud ratio clock,edge of starting singal available
URCTL1=URXEIE; //Wrong message interrupt of reception enable
//UBR11=0x00; //115200 @ 760KHz DCO(default)
//UBR01=0x06;
//UMCTL1=0x55; //Baud ratio adjusting
//UBR11=0; //115200 @ 4.786 MHz DCO
//UBR01=41;
//UMCTL1=0x55;
UBR11=0; //115200 @ 8 MHz External Oscillator
UBR01=69;
UMCTL1=0x55;
U1ME=URXE1+UTXE1; //Functions of reception and transmitter enable
UCTL1 &=~SWRST; //Functions of serial port enable
U1IE=URXIE1;//+UTXIE1; //Enable interrupt of reception
P3SEL |=0xc0; //P3.7(URXD1),P3.6(UTXD1)
P3OUT &=0x7F; //Setting P3.7 input
P3OUT |=0x40; //Setting P3.6 ouput
P3DIR |=0x40; //Setting P3.6 direction to output
tx1flg=1;
rx1flg=0;
}
#pragma vector=UART1TX_VECTOR
__interrupt void usart1_tx (void)
{
_DINT(); //Entry routine of TX1 with clearing URXIFG1 automatically
tx1flg=1; //Setting transmission successful flag
_EINT();
}
#pragma vector=UART1RX_VECTOR
__interrupt void usart1_rx (void)
{
_DINT();
// The following is used when setting UXRSE under low-power mode
if(!(U1IFG & URXIFG1))
{
UTCTL1 &=~URXSE; //Clear inner URXS flag without occuring URXIFGO
UTCTL1 |=URXSE; //Re-setting URXSE founction
_BIS_SR(SCG0+SCG1); //Activating DCO if it was disabled by low-power mode
}
//The following is a normal interrupt service
else
{
rx1flg=1; //Setting transmission successful flag;
rxbuf1=RXBUF1; //Reading data from RXBUF1 for clearing URXIFG0 automatically
P2OUT ^=0x08;
}
_EINT();
}
void Tx1Char(unsigned char c)
{
while(!(UTCTL1 & TXEPT));
tx1flg=0;
TXBUF1=c;
while(!(U1IFG & UTXIFG1));
tx1flg=1;
}
void Tx1S(char *p)
{
unsigned i,j;
i=strlen(p);
if(i>=256) i=255;
for(j=0;j<i;j++)
{
if(*(p+j)=='\n')
{
Tx1Char(0x0d);
Tx1Char(0x0a);
}
else Tx1Char(*(p+j));
}
}
void Tx1HexC(unsigned char c)
{
unsigned char temp;
temp=(c & 0xf0) >>4;
if(temp <10) Tx1Char(temp +0x30);
else Tx1Char(temp +'A'-10);
temp=c & 0x0f;
if(temp <10) Tx1Char(temp +0x30);
else Tx1Char(temp +'A'-10);
}
void Rx1Char()
{
while(!rx1flg);
rx1flg=0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -