cryosc.c
来自「8051试验程序 基础教材」· C语言 代码 · 共 129 行
C
129 行
//-----------------------------------------------------------------------------
// 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
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <ioC8051f310.h> // SFR declarations
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F31x
//-----------------------------------------------------------------------------
__sfr __no_init volatile unsigned short DP @ 0x82; // data pointer
__sfr __no_init volatile unsigned short TMR3RL @ 0x92; // Timer3 reload value
__sfr __no_init volatile unsigned short TMR3 @ 0x94; // Timer3 counter
__sfr __no_init volatile unsigned short ADC0 @ 0xbd; // ADC0 data
__sfr __no_init volatile unsigned short ADC0GT @ 0xc3; // ADC0 Greater-Than
__sfr __no_init volatile unsigned short ADC0LT @ 0xc5; // ADC0 Less-Than
__sfr __no_init volatile unsigned short TMR2RL @ 0xca; // Timer2 reload value
__sfr __no_init volatile unsigned short TMR2 @ 0xcc; // Timer2 counter
__sfr __no_init volatile unsigned short PCA0CP1 @ 0xe9; // PCA0 Module 1 Capture/Compare
__sfr __no_init volatile unsigned short PCA0CP2 @ 0xeb; // PCA0 Module 2 Capture/Compare
__sfr __no_init volatile unsigned short PCA0CP3 @ 0xed; // PCA0 Module 3 Capture/Compare
__sfr __no_init volatile unsigned short PCA0 @ 0xf9; // PCA0 counter
__sfr __no_init volatile unsigned short PCA0CP0 @ 0xfb; // PCA0 Module 0 Capture/Compare
__sfr __no_init volatile unsigned short PCA0CP4 @ 0xfd; // PCA0 Module 4 Capture/Compare
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 8000000 // SYSCLK frequency in Hz
#define LED P0_bit.P07 // LED='1' means ON
#define SW2 P3_bit.P33 // 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 + =
减小字号Ctrl + -
显示快捷键?