📄 2004810152947.c
字号:
//------------------------------------------------------------------------------------
// Blinky.c
//------------------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: BD
// DATE: 1 JUL 2002
//
// This program flashes the green LED on the C8051F120 target board about five times
// a second using the interrupt handler for Timer3.
// Target: C8051F12x
//
// Tool chain: KEIL Eval 'c'
//////////////////////////////////////////////////////
//在P0.0口测系统时钟频率
////////////////////////////////////////////////////////
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f120.h> // SFR declarations
char i;
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F12x
//-----------------------------------------------------------------------------
sfr16 RCAP3 = 0xCA; // Timer3 reload value
sfr16 TMR3 = 0xCC; // Timer3 counter
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
unsigned int dey;
#define SYSCLK 3062500 // approximate SYSCLK frequency in Hz
sbit LED = P1^6; // green LED: '1' = ON; '0' = OFF
//------------------------------------------------------------------------------------
//
//unsigned int dey;
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void PORT_Init (void);
void Timer3_Init (int counts);
void Timer3_ISR (void);
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void)
{
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
SFRPAGE = CONFIG_PAGE; // Switch to configuration page
PORT_Init ();
SFRPAGE = TMR3_PAGE; // Switch to Timer 3 page
Timer3_Init (SYSCLK / 12 / 10); // Init Timer3 to generate interrupts
// at a 10 Hz rate.
EA = 1; // enable global interrupts
SFRPAGE = LEGACY_PAGE; // Page to sit in for now
while (1)
{ // spin forever
while(dey==100)
{
LED=!LED;
dey=0;
}
}
}
//------------------------------------------------------------------------------------
// PORT_Init
//------------------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
// software timer
int i;
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
OSCXCN |= 0x67;
OSCICN |= 0xc0;
OSCICL |= 0x00;
for(i=0;i++;i<=256)
{}
for(i=0;i++;i<=256) //延时1ms
{}
while(OSCXCN==0x67)
{ }
//OSCICN = 0x83; // set internal oscillator to run
// at its maximum frequency
CLKSEL = 0x00; // Select the internal osc. as
XBR1 |= 0x80;
XBR2 |= 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |=0xff;
P1MDOUT |= 0xff; // the SYSCLK source
//Turn on the PLL and increase the system clock by a factor of M/N = 2
// SFRPAGE = CONFIG_PAGE;
// PLL0CN = 0x00; // Set internal osc. as PLL source
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x10; // Set FLASH read time for 50MHz clk
// or less
SFRPAGE = CONFIG_PAGE;
PLL0CN |= 0x01; // Enable Power to PLL
PLL0DIV = 0x01; // Set Pre-divide value to N (N = 1)
PLL0FLT = 0x01; // Set the PLL filter register for
// a reference clock from 19 - 30 MHz
// and an output clock from 45 - 80 MHz
PLL0MUL = 0x05; // Multiply SYSCLK by M (M = 2)
for (i=0; i < 256; i++) ; // Wait at least 5us
PLL0CN |= 0x07; // Enable the PLL
while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked
CLKSEL = 0x02; // Select PLL as SYSCLK source
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//------------------------------------------------------------------------------------
// Timer3_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate an interrupt at interval
// specified by <counts> using SYSCLK/12 as its time base.
//
//
void Timer3_Init (int counts)
{
TMR3CN = 0x00; // Stop Timer3; Clear TF3;
// use SYSCLK/12 as timebase
RCAP3 = -counts; // Init reload values
TMR3 = 0xffff; // set to reload immediately
EIE2 |= 0x01; // enable Timer3 interrupts
TR3 = 1; // start Timer3
}
//------------------------------------------------------------------------------------
// Interrupt Service Routines
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Timer3_ISR
//------------------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer3 overflows.
//
// NOTE: The SFRPAGE register will automatically be switched to the Timer 3 Page
// When an interrupt occurs. SFRPAGE will return to its previous setting on exit
// from this routine.
//
void Timer3_ISR (void) interrupt 14
{
TF3 = 0; // clear TF3
dey++;
LED=!LED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -