|
楼主 |
发表于 2014-11-23 09:40:57
|
显示全部楼层
24F32KA301 Example LED Flash using Microchip C
Example program for the Microchip PIC 24F32KA301 using Microchip C compiler
Using Timer1 and interrupts, flashes LED on A0 once per second
/*
* Microchip C Compiler Demo Program for PIC24F32KA301
* PIC24F32KA301 flash LED on A0 using Timer1 and interrupts
* Copyright (C)2011 HobbyTronics.co.uk
* Freely distributable.
*/
//include basic header definition
#include <p24f32ka301.h>
//config
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF); // FRC Oscillator with PLL (32MHz)
_FOSC (FCKSM_CSDCMD & SOSCSEL1_SOSCLP & POSCFREQ_MS
& OSCIOFNC_OFF & POSCMOD_NONE);
/*Clock switching and Fail-Safe Clock monitor DISABLED
Secondary Oscillator Select
Primary Oscillator Frequency Range
OSCO Pin Configuration OFF
Oscillator Selection
*/
//prototypes
void timer1_init(void);
//main loop
int main(void)
{
//Set up Clock
OSCCON = 0x11C0; // select INTERNAL FRC (32MHz), Post Scale & PLL
CLKDIV = 0x0000; // No clock Divide
//Set Ports to Digital
ANSELA=0x0000;
ANSELB=0x0000;
ANSELC=0x0000;
TRISA=0x0000;
LATAbits.LATA0 = 0;
timer1_init();
SRbits.IPL = 0 ; // turn on all interrupt priorities
//Loop forever
while(1)
{
}
}
//Set up Timer, target 2Hz interrupts
void timer1_init(void)
{
//Setup Timer1
PR1 = 0x7A12; // set for 2Hz (Clock is 32KHz/2 = 16MHz)
IPC0bits.T1IP = 5; //set interrupt priority
T1CON = 0b1000000000110000; //turn on the timer
IFS0bits.T1IF = 0; //reset interrupt flag
IEC0bits.T1IE = 1; //turn on the timer1 interrupt
}
//_T1Interrupt() is the T1 interrupt service routine (ISR).
void __attribute__((__interrupt__, auto_psv)) _T1Interrupt(void)
{
LATAbits.LATA0 = ~LATAbits.LATA0; //Toggle output to LED
IFS0bits.T1IF = 0;
}
这个是某网站找到的例程,用的是FNOSC_FRCPLL,8M内部,PLL后32m,Fcy取得是16m。LZ位例程用的也是FNOSC_FRCPLL ,按道理Fcy也是16m,这样算下来500ms才对。 |
|