📄 cryosc.c
字号:
//-----------------------------------------------------------------------------
// Cryosc.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: BW
// DATE: 10 MAR 03
//
// This program shows an example of configuring the external oscillator for
// use with a crystal.
//
// Assumes a high-frequency crystal is attached between XTAL1 and XTAL2 with
// external resistor and loading capacitors.
//
// Target: C8051F31x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f310.h> // SFR declarations
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F31x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbd; // ADC0 data
sfr16 ADC0GT = 0xc3; // ADC0 Greater-Than
sfr16 ADC0LT = 0xc5; // ADC0 Less-Than
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 PCA0CP1 = 0xe9; // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP2 = 0xeb; // PCA0 Module 2 Capture/Compare
sfr16 PCA0CP3 = 0xed; // PCA0 Module 3 Capture/Compare
sfr16 PCA0 = 0xf9; // PCA0 counter
sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 Capture/Compare
sfr16 PCA0CP4 = 0xfd; // PCA0 Module 4 Capture/Compare
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 8000000 // SYSCLK frequency in Hz
sbit LED = P0^7; // LED='1' means ON
sbit SW2 = P3^3; // SW2='0' means switch pressed
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void PORT_Init (void);
void SYSCLK_Init (void);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
// Disable Watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init (); // initialize ports and GPIO
SYSCLK_Init (); // initialize oscillator
while (1);
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.0 -
// P0.1 -
// P0.2 - XTAL1
// P0.3 - XTAL2
// P0.4 -
// P0.5 -
// P0.6 -
// P0.7 -
//
void PORT_Init (void)
{
P0SKIP = 0x0c; // skip crystal pins in crossbar
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0MDIN &= ~0x0c; // configure XTAL1 and XTAL2 as analog
// inputs
P0MDOUT |= 0x10; // enable TX0 as a push-pull output
}
//-----------------------------------------------------------------------------
// SYSCLK_Init -- Fast Crystal with Missing Clock Detector
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use a crystal
// as its clock source. Also enables missing clock detector reset.
//
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 32.768kHz crystal
for (i=0; i < 256; i++) ; // Wait for osc. to start
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
RSTSRC = 0x04; // enable missing clock detector reset
CLKSEL = 0x01; // select external oscillator as SYSCLK
// source
OSCICN = 0x00; // disable internal oscillator
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -