www.pudn.com > C8051F30x.rar > Cryosc.c


//----------------------------------------------------------------------------- 
// Cryosc.c 
//----------------------------------------------------------------------------- 
// Copyright (C) 2004 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 
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51 
// 
 
//----------------------------------------------------------------------------- 
// Includes 
//----------------------------------------------------------------------------- 
 
#include                  // SFR declarations 
 
//----------------------------------------------------------------------------- 
// 16-bit SFR Definitions for 'F30x 
//----------------------------------------------------------------------------- 
 
sfr16 DP       = 0x82;                 // data pointer 
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 PCA0     = 0xf9;                 // PCA0 counter 
sfr16 PCA0CP0  = 0xfb;                 // PCA0 Module 0 Capture/Compare 
 
//----------------------------------------------------------------------------- 
// Global CONSTANTS 
//----------------------------------------------------------------------------- 
 
#define SYSCLK       32768             // SYSCLK frequency in Hz 
 
sbit LED = P0^2;                       // LED='1' means ON 
sbit SW2 = P0^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 - 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 
}