📄 uart0_init.c
字号:
#include "common.h"
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1. In order to
// increase the clocking flexibility of Timer0, Timer1 is configured to count
// SYSCLKs.
//
// To use this routine SYSCLK/BAUDRATE/16 must be less than 256. For example,
// if SYSCLK = 50 MHz, the lowest standard baud rate supported by this
// routine is 19,200 bps.
void UART0_Init (void)
{
char old_SFRPAGE = SFRPAGE; // Store current SFRPAGE
SFRPAGE = UART0_PAGE; // Switch to UART0 Page
SCON0 = 0x50; // SCON0: mode 0, 8-bit UART, enable RX
SSTA0 = 0x10; // Timer 1 generates UART0 baud rate and
// UART0 baud rate divide by two disabled
SFRPAGE = TIMER01_PAGE;
TMOD &= ~0xF0;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = -(SYSCLK/BAUDRATE/16); // Set the Timer1 reload value
// When using a low baud rate, this
// equation should be checked to ensure
// that the reload value will fit in
// 8-bits.
CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
TL1 = TH1; // initialize Timer1
TR1 = 1; // start Timer1
SFRPAGE = UART0_PAGE;
TI0 = 1; // Indicate TX0 ready
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -