cryosc.c

来自「8051试验程序 基础教材」· C语言 代码 · 共 104 行

C
104
字号
//-----------------------------------------------------------------------------
// Cryosc.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: BW
// DATE: 11 DEC 01
//
// This program shows an example of configuring the external oscillator for
// use with a crystal.
//
// Assumes an 22.1184MHz crystal is attached between XTAL1 and XTAL2.
//
// Target: C8051F2xx
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

//TARGET MCU  : C8051F206, 'F220, 'F221, 'F226, 'F230, 'F231 and F236.
// Uncomment to include SFR declarations for desired derivative

#include <ioC8051F206.h>                    // SFR declarations
//#include <ioC8051F220.h>                    // SFR declarations
//#include <ioC8051F221.h>                    // SFR declarations
//#include <ioC8051F226.h>                    // SFR declarations
//#include <ioC8051F230.h>                    // SFR declarations
//#include <ioC8051F231.h>                    // SFR declarations
//#include <ioC8051F236.h>                    // SFR declarations

//------------------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F2xx
//------------------------------------------------------------------------------------

__sfr __no_init volatile unsigned short DP       @ 0x82;                 // data pointer
__sfr __no_init volatile unsigned short ADC0     @ 0xbe;                 // ADC0 data
__sfr __no_init volatile unsigned short ADC0GT   @ 0xc4;                 // ADC0 greater than window
__sfr __no_init volatile unsigned short ADC0LT   @ 0xc6;                 // ADC0 less than window
__sfr __no_init volatile unsigned short RCAP2    @ 0xca;                 // Timer2 capture/reload
__sfr __no_init volatile unsigned short T2       @ 0xcc;                 // Timer2


//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK       22118400          // SYSCLK frequency in Hz

#define LED P2_bit.P24                       // LED='1' means ON
#define SW1 P2_bit.P25                       // SW1='1' means switch pressed

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) {

   WDTCN = 0xde;                       // disable watchdog timer
   WDTCN = 0xad;

   SYSCLK_Init ();                     // initialize oscillator

   while (1);
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use an 22.1184MHz crystal
// as its clock source.
//
void SYSCLK_Init (void)
{
   int i;                              // delay counter

   OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184MHz crystal

   for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?