/*----------------------------------------------------------------------------*/
/*
File Name : UART.c
Description : Use the UART to communication,
include sending and recviving.
Author : Jones Lee
Copyright_&Date_&Version : Jones_Studio _&<2012.8.27>_&
Connector Fuction :
Notes :
*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*---Head Files-----------*/
#include "UART.h"
/*--Variable Definition---*/
_UINT_8 Flag_Recv;
_UINT_8 Byte_Recv;
_UINT_8 Buffer_Recv[20];
/*---Function Definiton---*/
void Uart_Init();
void UART_SendOneChar(_UINT_8 byte);
void UART_SendChars(_UINT_8 *pBuffer,_UINT_8 cnt);
/*---Macro Definition-----*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
Fuction Name :Uart_Init()
Description :
Input parameter :
Output parameter :void
Creat Date :2012.8.27
Author :Jones Lee
Notes :
*/
/*----------------------------------------------------------------------------*/
void Uart_Init()
{
//Select the IO's function to uart.
P1DIR |= BIT2; //P1.2 UART_TX
P1DIR &=~BIT1; //P1.2 UART_RX
P1SEL |= BIT1+BIT2; //select P1.1 and P1.2 as UART port
P1SEL2 |= BIT1+BIT2;
//Set the bus as uart.
UCA0CTL1 |=UCSWRST; //reset UART module,as well as enable UART module
UCA0CTL1 |=UCSSEL_2; //UART clock is SMCLK
UCA0BR0 |=65; //Baud N=BCLK/rate,rate=9600,BCLK=SMCLK=8M
UCA0BR1 |=3;
UCA0MCTL = UCBRS1; //UCBRSx=2
UCA0CTL1 &=~UCSWRST; //UART reset end
//Open the UART recievc interrupt
IE2 |= UCA0RXIE ;
}
/*----------------------------------------------------------------------------*/
/*
Fuction Name :UART_SendOneChar()
Description :The Sending one char function of UART
Input parameter : _UINT_8 byte
Output parameter :void
Creat Date :2012.8.26-2012.8.27
Author :Jones Lee
Notes :
*/
/*----------------------------------------------------------------------------*/
void UART_SendOneChar(_UINT_8 byte)
{
UCA0TXBUF = byte;
while (!(IFG2&UCA0TXIFG)); //waiting UCA0TXBUF is empty
IFG2 &=~UCA0TXIFG; //clear TX interrupt flag
}
/*----------------------------------------------------------------------------*/
/*
Fuction Name :UART_SendChars()
Description :The Sending chars function of UART
Input parameter : _UINT_8 number
_UINT_8* pBuffer
Output parameter :void
Creat Date :2012.8.26-2012.8.27
Author :Jones Lee
Notes :
*/
/*----------------------------------------------------------------------------*/
void UART_SendChars(_UINT_8 *pBuffer,_UINT_8 cnt)
{
do
{
UART_SendOneChar(*pBuffer);
cnt--;
pBuffer++;
}
while(cnt != 0);
//UART_SendOneChar('\n');
}
/*----------------------------------------------------------------------------*/
/*
Fuction Name :USCI0RX_ISR()
Description :The Recving chars function of UART
Input parameter : _UINT_8* number (global variable)
_UINT_8* pBuffer
Output parameter :void
Creat Date :2012.8.26 -2012.8.27
Author :Jones Lee
Notes :There are only some simple sentences,you can add some flex-
-ible sentences accroding to yourself.
*/
/*----------------------------------------------------------------------------*/
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
Byte_Recv =UCA0RXBUF; //load the recieve byte
IFG2 &=~UCA0RXIFG; //clear RX interrupt flag
Flag_Recv = 1;
}
弄了半天,才弄出来,汗死了,就是感觉比LPC 的还难~~~
和大家分享下