我手上有30F2010的範例:如下!
//---------------------------------------------------------------------------
// Code to flash LED and turn on LEDs when switches are pressed
//Timer1 counter 0.5s interrupt
//Switch1 exter interrupt
//---------------------------------------------------------------------------
// Header files
#include <p30f2010.h> //Standard header file
//--------------------------內部設定-----------------------------------------------
// Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL8); //XT with 4xPLL oscillator, Failsafe clock off
_FWDT(WDT_OFF); //Watchdog timer disabled
_FBORPOR(PBOR_OFF & MCLR_EN); //Brown-out reset disabled, MCLR reset enabled
_FGS(CODE_PROT_OFF); //Code protect disabled
//---------------------------------------------------------------------------
// Definitions
#define Fcy 20000000 //7.37MHz oscillator with 4xPLL -> 7.37MIPs
#define Vi PORTBbits.RB0
#define Vo PORTBbits.RB1
#define LED1 PORTEbits.RE8 // 0.5 sec flash
#define LED2 PORTDbits.RD0 // when sw0=1 ,LED2 is on and LED3 is off
#define LED3 PORTDbits.RD1 // when sw1=1 ,LED3 is on and LED2 is off
#define SW1 PORTEbits.RE5
#define Speaker PORTEbits.RE4
//---------------------------------------------------------------------------
// Main routine
//---------------------------------------------------------------------------
int main(void)
{
//------------------------------------------------------------------------------
//Initialize LED outputs on PORTD bits 0-3 and switch inputs on PORTA bits 12 - 15
PORTD = 0xFFFF; //Turn off all LEDS
PORTB = 0xFFFF; //Turn off all LEDS
PORTE = 0x0000;
TRISD = 0xFFF0; //Set LED pins as outputs
TRISB = 0xFFFF; //Set pushbutton switch pins as inputs
TRISE = 0X0000;
//------------------------------------------------------------------------------
//Initialize Timer1 for 1/2 second period
T1CON = 0; //Turn off Timer1 by clearing control register
TMR1 = 0; //Start Timer1 at zero
PR1 = (Fcy/256)/2; //Set period register value for 1/2 second
T1CON = 0x8030; //Configure Timer1 (timer on, continue in IDLE, not gated, 1:256 prescaler, internal clock)
//------------------------------------------------------------------------------
//Loop while flashing LED RD4 and testing switch inputs
while (1)
{
if(IFS0bits.T1IF == 1) //Check if Timer1 interrupt flag is set
{
IFS0bits.T1IF = 0; //Clear Timer1 interrupt flag
LED1 = !LED1; //Toggle LED1 RD0
}
//------------------------------------------------------------------
if(SW1 == 0) //Check if SW1 button is pressed
{
LED2 = 0; //Turn LED3 RD3 on
LED3 = 1; //Turn LED2 RD2 off
}
else
{
LED2 = 1; //Turn LED3 RD3 off
LED3 = 0; //Turn LED2 RD2 on
}
}
}
//---------------------------------------------------------------------------
改成用30F4012去跑範例!
該注意哪些??除啦腳位要改!還有哪些??