📄 uart.c
字号:
/*****************************************************************************
** [File]
** uart1.c
** [Purpose]
** UART routines
** [Changes]
**
*/
#include "hpa449lib.h"
#include <signal.h>
#ifdef __BUFFERED_SERIAL
#define TX_BUFSIZE 100
static volatile unsigned char TXBuffer[TX_BUFSIZE];
static volatile int ReadPointer;
static volatile int WritePointer;
unsigned volatile char nTXItems;
unsigned volatile char fTXBufFree;
#endif
void UART1_Init(unsigned int BaudRateDivider, unsigned char Modulator)
{
UCTL1 = SWRST;
#ifdef __BUFFERED_SERIAL
ReadPointer = WritePointer = 0;
nTXItems = 0;
fTXBufFree = 1;
#endif
UTCTL1 = SSEL1; // UCLK = SMCLK
UBR01 = BaudRateDivider & 0xff; // baud rate
UBR11 = (BaudRateDivider >> 8) & 0xff;
UMCTL1 = Modulator; // modulation
UCTL1 = CHAR | SWRST; // 8-bit character *SWRST*
ME2 |= (UTXE1) + (URXE1); // Enable USART0 TXD/RXD
P4SEL |= 0x03; // P4.1,0 = USART0 TXD/RXD
P4DIR |= 0x01; // P4.0 output direction
UCTL1 &= ~SWRST;
#ifdef __BUFFERED_SERIAL
IE2 |= UTXIE1; // Enable USART0 TX interrupt
#endif
}
void UART1_EnableReceiveInterrupt(int fEnable)
{
if (fEnable)
IE2 |= URXIE1; // Enable USART1 RX interrupt
else
IE2 &= ~URXIE1;
}
void UART1_EnableTransmitInterrupt(int fEnable)
{
if (fEnable)
IE2 |= UTXIE1; // Enable USART1 TX interrupt
else
IE2 &= ~UTXIE1;
}
#ifdef __BUFFERED_SERIAL
int SendString(char * pData)
{
int fSuccess = 1;
while (*pData)
{
fSuccess &= SendByte(*pData++);
}
return fSuccess;
}
int SendBytes(unsigned char * pData, unsigned char nBytes)
{
int fSuccess = 1;
while (nBytes--)
{
fSuccess &= SendByte(*pData++);
}
return fSuccess;
}
int SendByte(unsigned char Byte2Send)
{
int fSuccess = 1;
// don't mess with the buffer with interrupts enabled
// ???? GH possible problem if TXInt was not enabled before
IE2 &= ~UTXIE1;
// if TXBUF is empty, send direct, else buffer
if ((nTXItems == 0) && fTXBufFree)
{
TXBUF1 = Byte2Send;
fTXBufFree = 0;
}
else
{
// wait until another byte is available
while (nTXItems == TX_BUFSIZE)
{
IE2 |= UTXIE1;
IE2 &= ~UTXIE1;
}
nTXItems++;
TXBuffer[WritePointer++] = Byte2Send;
if (WritePointer == TX_BUFSIZE)
WritePointer = 0;
}
IE2 |= UTXIE1;
return fSuccess;
}
interrupt (USART1TX_VECTOR) UART1TXIntRoutine(void)
{
// if we have more items to send, do so
if (nTXItems > 0)
{
nTXItems--;
TXBUF1 = TXBuffer[ReadPointer++];
if (ReadPointer == TX_BUFSIZE)
{
ReadPointer = 0;
}
}
else
{
// mark the transmitter free
fTXBufFree = 1;
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -