📄 uart.c
字号:
/*
** ============================================================================
**
** FILE
** uart.c
**
** DESCRIPTION
** Contains all uart and buffer handle functions
**
** CREATED
** Silicon Laboratories Hungary Ltd
**
** COPYRIGHT
** Copyright 2008 Silicon Laboratories, Inc.
** http://www.silabs.com
**
** ============================================================================
*/
#include "S8051.h"
#include "uart.h"
/*------------------------------------------------------------------------*/
/* GLOBAL variables */
/*------------------------------------------------------------------------*/
idata uint8 CircBuf[80]; //circular buffer length
idata uint8 i_ptr; //circular buffer input pointer
idata uint8 o_ptr; //circular buffer output pointer
idata uint8 BufChar; //character counter
extern xdata uint8 rs232_timer; //timer variable
extern xdata uint8 uart_ch_sent;
/*------------------------------------------------------------------------*/
/* LOCAL function prototypes */
/*------------------------------------------------------------------------*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void UartInit(void)
+
+ DESCRIPTION: initialize the UART peripheral
+
+ INPUT: None
+
+ RETURN: None
+
+ NOTES: None
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void UartInit(void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
#if (SYSCLK/BAUDRATE/2/256 < 1)
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
#elif (SYSCLK/BAUDRATE/2/256 < 4)
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
#elif (SYSCLK/BAUDRATE/2/256 < 12)
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
#else
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
#endif
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
ES0 = 1; // Enable UART0 interrupts
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void putch(char ch)
+
+ DESCRIPTION: print one character to Uart
+
+ INPUT: ch - a character
+
+ RETURN: None
+
+ NOTES: None
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char putchar(char ch)
{
SBUF0 = ch;
while( uart_ch_sent == 0);
uart_ch_sent = 0;
return 0;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void UartRxISR(void)
+
+ DESCRIPTION: UART Receive interrupt handler
+
+ INPUT: None
+
+ RETURN: None
+
+ NOTES: UartInit() has to be call before
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void UartRxISR(void)
{
CircBuf[i_ptr] = SBUF0; // get new character
if(++i_ptr == CIRC_BUF_LEN) i_ptr = 0; // buffer is circular
if(i_ptr == o_ptr) // is the buffer full?
{
if(++o_ptr == CIRC_BUF_LEN) o_ptr = 0; // if yes, inc output pointer
}
else BufChar++;
rs232_timer = 1; // restart rs232 rx timer
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: char ReadBuffer(void)
+
+ DESCRIPTION: Read one byte from the circulat buffer
+
+ INPUT: None
+
+ RETURN: the next character from the circular buffer
+
+ NOTES: None
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char ReadBuffer(void)
{
idata uint8 nextChar;
if (BufChar)
{
BufChar--; // decrement the buffer
nextChar = CircBuf[o_ptr]; // get the last character
if(++o_ptr == CIRC_BUF_LEN) o_ptr = 0; // step to the next one
return nextChar;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -