cryosc.c

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

C
122
字号
//-----------------------------------------------------------------------------
// Cryosc.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: BW
// DATE: 24 JUN 02
//
// This program shows an example of configuring the external oscillator for
// use with a crystal.
//
// Assumes an 32.768kHz crystal is attached between XTAL1 and XTAL2.
//
// Target: C8051F30x
//

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

#include <ioC8051f300.h>                 // SFR declarations

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

__sfr __no_init volatile unsigned short DP       @ 0x82;                 // data pointer
__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 PCA0     @ 0xf9;                 // PCA0 counter
__sfr __no_init volatile unsigned short PCA0CP0  @ 0xfb;                 // PCA0 Module 0 Capture/Compare

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

#define SYSCLK       32768             // SYSCLK frequency in Hz

#define LED P0_bit.P02                       // LED='1' means ON
#define SW2 P0_bit.P03                       // 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 - UART TX (push-pull)
// P0.5 - UART RX
// P0.6 -
// P0.7 - C2D
//
void PORT_Init (void)
{
   XBR0    =  0x0c;                    // skip crystal pins in crossbar
                                       // assignments
   XBR1    =  0x03;                    // UART0 TX and RX pins enabled
   XBR2    =  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
//-----------------------------------------------------------------------------
//
// 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 = 0x61;                      // 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
   OSCICN = 0x08;                      // select external oscillator as SYSCLK
                                       // source
}

⌨️ 快捷键说明

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