源自Silicon Lab的F020官方程序~自己把引脚定义直接改了就能用。有一个LED和一个拨码开关~
#include
//-----------------------------------------------------------------------------
// Pin Declarations
//-----------------------------------------------------------------------------
sbit LED1 = P1^6; // LED1 ='1' means ON
sbit SW1 = P3^7; // SW1 ='0' means switch pressed
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
PORT_Init(); // Initialize Port I/O
OSCILLATOR_Init (); // Initialize Oscillator
while (1)
{
if (SW1 == 0) // If switch depressed
{
LED1 = 1; // Turn on LED
}
else
{
LED1 = 0; // Else, turn it off
}
} // end of while(1)
} // end of main()
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to use the internal oscillator
// at its maximum frequency.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency (24.5 Mhz)
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and ports pins.
//
// To configure a pin as a digital input, the pin is configured as digital
// and open-drain and the port latch should be set to a '1'. The weak-pullups
// are used to pull the pins high. Pressing the switch pulls the pins low.
//
// To configure a pin as a digital output, the pin is configured as digital
// and push-pull.
//
// Some ports pins do not have the option to be configured as analog or digital,
// so it not necessary to explicitly configure them as digital.
//
// An output pin can also be configured to be an open-drain output if system
// requires it. For example, if the pin is an output on a multi-device bus,
// it will probably be configured as an open-drain output instead of a
// push-pull output. For the purposes of this example, the pin is configured
// as push-pull output because the pin in only connected to an LED.
//
// P1.6 digital push-pull LED1
// P3.7 digital open-drain Switch 1
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P1MDIN |= 0x40; // P1.6 is digital
P1MDOUT = 0x40; // P1.6 is push-pull
P3MDOUT = 0x00; // P3.7 is open-drain
P3 |= 0x80; // Set P3.7 latch to '1'
XBR2 = 0x40; // Enable crossbar and enable
// weak pull-ups
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
020的我没用过,我用过的是F320的,其实这个的初始化程序就是关闭电子狗,设置时钟频率,电源等等,发个我的F320给你参考吧,你对照着手册做相应的改动就OK了
PCA0MD &= ~0x40; // Disable Watchdog timer
VDM0CN = 0x80; // Enable VDD Monitor
delay(100); // wait for VDD Monitor stable
RSTSRC = 0x06;
/*初始化时钟频率*/
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency
CLKMUL = 0x00; //(Reset the Multiplier | add by lsy) // Select internal oscillator as
// input to clock multiplier
CLKMUL |= 0x80; // Enable clock multiplier
delay(1000);
CLKMUL |= 0xC0; // Initialize the clock multiplier
CLKMUL |= 0x20;
//while(!(CLKMUL & 0x20)); // Wait for multiplier to lock
CLKSEL |= USB_4X_CLOCK; // Select USB clock
CLKSEL |= SYS_4X_DIV_2; // Select SYSCLK as Clock Multiplier/2
IT01CF = 0x65;
XBR0 = 0x02;
XBR1 = 0x40; // Enable Crossbar
TMOD = 0x11; //T0,T1 as 16bits Timer mode
PCON = 0;
CKCON = 0x76; //Clock Control Register
TMR2CN = 0x00; //Timer2 Control Register
TMR3CN = 0xc0; //Timer3 .......
TMR3RLL = 0x00; //Timer3 Reload Register Low Byte
TMR3RLH = 0x00; //.......................High..
TMR2RLL = 0;
TMR2RLH = 0;
/*TR0 = 1; //Enable Mcu Timer
TR1 = 1;
TR2 = 1;
ET2 = 1; //Timer interrupt Enable flag
EX0 = 1;
EX1 = 1;
IT0 = 1; //Extern interrput Enable flag
IT1 = 1;
PX0 = 1; */
#include
#include
#define uint unsigned int
//外部时钟初始化
void SYSCLK_Init(void)
{
uint i;
OSCXCN = 0x67;
for(i=0;i<1000;i++);
while(!(OSCXCN&0x80));
OSCICN = 0x88; }
void PORT_Init(void)
{
XBR0 = 0x04; XBR1 = 0x00;
XBR2 = 0x44;
P0MDOUT |= 0xff; P74OUT |=0xff;
}
void main()
{
uint i ;
PORT_Init();
// SYSCLK_Init();
while(1)
{
P3=0xFF;
for(i=0;i<30000;i++);
P3=0x00;
for(i=0;i<30000;i++);
}
}
试试吧 P3口放灯