📄 uart.c
字号:
/*********************************************************************
* Description:
* This pgm demostrate the UART block control and setting.
* Enable FIFO and use int method.
* Communication with any COM tools in PC end.
* History:
* 1) Rev. 1.0: primarily version; by felix; Oct.10/2002
*
* Status:
* Function test only, not overall. Please contat us if bug detected.
* All rights reserved by Fudan JHB Tech. Co. Ltd.
*
* Any quesiton or comments, please feel free to contact us.
*********************************************************************/
#include "global_function.h"
#include "uart.h"
/* defined for uart block */
// set baud rate: baud value = MCLK/(BaudRate*16) - 0.5
#define BAUD_VALUE 390 //baud rate = 9600 bps: 60000000/(19200*16) - 0.5 = 390
#define BUFLEN 15
B8 _Buf[BUFLEN];
volatile int _BufRdPt = 0;
volatile int _BufWrPt = 0;
volatile char * _uart0TxStr;
B8 _EscFlag;
void Uart_Init(void)
{
B8 key;
Config_PortE_as_Uart();
Uart0_Init();
Uart0_TxEmpty();
//* UART config
rUCON0 = 0x2c5; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
Uart0_Clear_RxFifo();
key = rUERSTAT0; //To clear the error state
pISR_URXD0 = (int)Uart0_RxFifoInt;
pISR_UTXD0 = (int)Uart0_TxFifoInt;
rINTMSK = ~(BIT_GLOBAL | BIT_URXD0 | BIT_UERR01);
}
void Config_PortE_as_Uart(void) // port init
{
rPCONE = 0x28; // select UART Rx0/Tx0
rPUPE = 0xff;
}
void Uart0_TxEmpty()
{
while(!(rUTRSTAT0 & 0x4)); //wait until tx shifter is empty.
}
void Uart0_Init(void)
{
rULCON0 = 0x03; //Normal,No parity,1 stop,8 bit
rUFCON0 = 0x07; //enable FIFO
rUBRDIV0 = BAUD_VALUE;
}
void Uart0_SendByte(int data)
{
if(data == '\r')
{
while((rUFSTAT0 & 0x200)); //Wait if transfer fifo is full
Delay(50); //because the slow response of hyper_terminal
rUTXH0 = '\n';
Delay(1);
rUTXH0 = '\r';
return;
}
while((rUFSTAT0 & 0x200)); //Wait if transfer fifo is full
Delay(50);
rUTXH0 = data;
}
B8 Uart0_IntGetkey(void) //get input anytime
{
if(_BufRdPt == BUFLEN)
_BufRdPt= 1;
while(_BufRdPt == _BufWrPt); //until FIFO is triggered
return(_Buf[_BufRdPt++]);
}
B8 Uart0_GetInput(void) //get input in the given time
{
B8 key;
Uart0_Clear_RxFifo();
key = rUERSTAT0;
//rINTMSK &= (~BIT_URXD0);
_BufRdPt = 0;
_BufWrPt = 0;
key = Uart0_IntGetkey();
//rINTMSK |= BIT_URXD0;
return(key);
}
void Uart0_SendString(char * pt)
{
while(*pt)
Uart0_SendByte(*pt++);
}
void Uart0_Clear_RxFifo(void)
{
B8 key;
while( (rUFSTAT0 & 0xf) >0 )
key = RdURXH0(); //To clear the Rx FIFO
}
void __irq Uart0_RxFifoInt(void)
{
B8 key;
while( (rUFSTAT0 & 0xf) >0 ) //until FIFO is empty
{
key = rURXH0;
_Buf[_BufWrPt++] = key; //rx buffer->Buf[]
if(key == 0x1b)
_EscFlag = TRUE;
else
_EscFlag = FALSE;
if(_BufWrPt == BUFLEN)
_BufWrPt = 0;
}
rI_ISPC = BIT_URXD0;
}
void __irq Uart0_TxFifoInt(void)
{
while( !(rUFSTAT0 & 0x200) && (*_uart0TxStr != '\0') ) //until tx fifo full or end of string
{
rUTXH0 = *_uart0TxStr++; // write out data
Delay(1);
}
if(*_uart0TxStr == '\0') // end of string
{
rINTMSK |= BIT_UTXD0; // end of string send, disable Tx int enable
}
rI_ISPC=BIT_UTXD0;
}
void Uart0_FifoInt_SendString(char * pt)
{
rUCON0 = 0x244; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
_uart0TxStr = pt;
rINTMSK &= ~BIT_UTXD0; //start Tx int
rUCON0 = 0x2c5;
Uart0_TxEmpty();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -