📄 port_init.c
字号:
#include <c8051f120.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// Initialization Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
char old_SFRPAGE = SFRPAGE; // Store current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to configuration page
XBR0 = 0x04; // Enable UART0 on crossbar
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x01; // Set TX0 pin to push-pull
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use an external 22.1184 MHz
// crystal oscillator multiplied by a factor of 9/4 using the PLL as its
// clock source. The resulting frequency is 22.1184 MHz * 9/4 = 49.7664 MHz
//
void SYSCLK_Init (void)
{
int i; // delay counter
char old_SFRPAGE = SFRPAGE; // Store current SFRPAGE
SFRPAGE = CONFIG_PAGE; // set SFR page
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // Wait for osc. to start up
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
CLKSEL = 0x01; // Select the external osc. as
// the SYSCLK source
OSCICN = 0x00; // Disable the internal osc.
//Turn on the PLL and increase the system clock by a factor of M/N = 9/4
SFRPAGE = PLL0_PAGE;
PLL0CN = 0x04; // Set PLL source as external osc.
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x10; // Set FLASH read time for 50MHz clk
// or less
SFRPAGE = PLL0_PAGE;
PLL0CN |= 0x01; // Enable Power to PLL
PLL0DIV = 0x04; // Set Pre-divide value to N (N = 4)
PLL0FLT = 0x01; // Set the PLL filter register for
// a reference clock from 19 - 30 MHz
// and an output clock from 45 - 80 MHz
PLL0MUL = 0x09; // Multiply SYSCLK by M (M = 9)
for (i=0; i < 256; i++) ; // Wait at least 5us
PLL0CN |= 0x02; // Enable the PLL
while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked
CLKSEL = 0x02; // Select PLL as SYSCLK source
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -