www.pudn.com > dsPIC_Advprog_Answer.rar > ex1.c, change:2011-05-11,size:2769b


// *********************************************************************** 
// File : ex1.C 
// Purpose : 	Initial Timer1 & Timer3 by using peripheral library 
//				Timer1 : 200ms  中斷 CPU 一次 
//				Timer3 : 1ms 中斷 CPU 一次 
// *********************************************************************** 
 
#define __dsPIC30F4011__ 
 
#include 	<timer.h> 
#include 	<math.h> 
#include 	<p30F4011.h> 
#include	<uart.h> 
#include	<stdio.h> 
#include	"APP009V2_LCD.h" 
 
#define	FCY			14745600			// Osc. configuration = XT, PLL * 8 and XT = 7.3728 Mhz 
 
#define	LED13		LATDbits.LATD0  
#define	LED14		LATDbits.LATD1  
#define	DIR_LED13	TRISDbits.TRISD0  
#define	DIR_LED14	TRISDbits.TRISD1 
#define	INPUT		1 
#define	OUTPUT		0	 
 
void	Timer3_Initial( void ) ; 
void	Timer1_Initial( void ) ; 
 
struct 
	{ 
		unsigned T3OV : 1 ; 
		unsigned :15 ; 
 
	} Flags ; 
 
int	CounterT3 ; 
 
//--------------------------------------------------------------------------- 
// Configuration bits 
 
//   _FOSC(CSW_FSCM_OFF & EC_PLL4);   //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 
 
 
// --------------------------------------------------------------------- 
// Locate the code for Timer 3 Interrupt  
// Refer to the name of p30f4011.gld 
// --------------------------------------------------------------------- 
void __attribute__((__interrupt__)) _T3Interrupt(void) 
{ 
	 
	CounterT3 += 1 ; 
	IFS0bits.T3IF = 0 ; 
	 
} 
 
// --------------------------------------------------------------------- 
// Locate the code for Timer 3 Interrupt  
// Refer to the name of p30f4011.gld 
// --------------------------------------------------------------------- 
void __attribute__((__interrupt__)) _T1Interrupt(void) 
{ 
 
	LED13 = !LED13 ; 
	IFS0bits.T1IF = 0 ; 
	 
} 
 
 
int	main( void ) 
 
{ 
 
	Timer1_Initial( ) ; 
	Timer3_Initial( ) ; 
	OpenLCD( ) ; 
	 
	CounterT3 = 0 ; 
 
	DIR_LED13 = OUTPUT ; 
	DIR_LED14 = OUTPUT ; 
 
		putrsLCD("Exer1 : Timers  ") ; 
		setcurLCD(0,1) ; 
		putrsLCD("T1:200ms T3:1ms ") ; 
 
 
 while (1)  
	{ 
	 
		if ( CounterT3 >= 400 ) 
		{ 
			LED14 = ! LED14 ;	 
			CounterT3 = 0 ; 
		} 
	} 
 
} 
 
void	Timer1_Initial( void ) 
{ 
		ConfigIntTimer1( T1_INT_PRIOR_7 & T1_INT_ON ) ; 
		OpenTimer1( T1_ON & T1_IDLE_STOP & T1_GATE_OFF & T1_PS_1_64 & T1_SYNC_EXT_OFF & T1_SOURCE_INT , 
					((long)(FCY/ 1000 )* 200 / 64) ) ; 
} 
 
void	Timer3_Initial( void ) 
{ 
		ConfigIntTimer3( T3_INT_PRIOR_7 & T3_INT_ON ) ; 
		OpenTimer3( T3_ON & T3_IDLE_STOP & T3_GATE_OFF & T3_PS_1_64 & T3_SOURCE_INT , 
				    (((long)FCY /1000 )/64 ) ) ; 
 
}