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


// *********************************************************************** 
// File 	: Hall Test Program for Exercise 5 
// Purpose 	:	  
// *********************************************************************** 
 
#define __dsPIC30F4011__ 
 
#include 	<timer.h> 
#include 	<math.h> 
#include 	<p30F4011.h> 
#include	"APP009V2_LCD.h" 
 
#define	FCY	7372800 * 2 
 
//--------------------------------------------------------------------------- 
// 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 
 
 
#define	LED13		LATDbits.LATD0 
#define	LED14		LATDbits.LATD1 
#define	LED15		LATFbits.LATF0  
#define	LED16		LATFbits.LATF1  
 
#define	DIR_LED13	TRISDbits.TRISD0 
#define	DIR_LED14	TRISDbits.TRISD1 
#define	DIR_LED15	TRISFbits.TRISF0 
#define	DIR_LED16	TRISFbits.TRISF1 
 
#define	HALL_A		LATBbits.LATB3 
#define	HALL_B		LATBbits.LATB4 
#define	HALL_C		LATBbits.LATB5 
 
#define	DIR_HALL_A	TRISBbits.TRISB3 
#define	DIR_HALL_B	TRISBbits.TRISB4 
#define	DIR_HALL_C	TRISBbits.TRISB5 
 
#define	AD_S1		0x2B4 
#define	AD_S2		0x259 
#define	AD_S3		0x1FB 
#define	AD_S4		0x13B 
#define	ADC_MAX		0x3FF 
 
void 	DelayNmSec(unsigned int ) ; 
void 	InitADC10(void); 
void	Initial_Timer1( void ); 
void	S1_Process(void) ; 
void	S2_Process(void) ; 
void	S3_Process(void) ; 
void	S4_Process(void) ; 
void	Set_HALLs(void) ; 
 
unsigned char	TxData[10] = {0,0,0,0,0,0,0,0,0,0} ; 
int				AN0_Result[256] ; 
int				AN1_Result[256] ; 
 
 
int				T1IF_Flag ; 
int				T1IF_Counter ; 
int				ADC_Value ; 
int				HALL_Position ; 
 
int				S1_Counter ; 
int				S2_Counter ; 
int				S3_Counter ; 
int				S4_Counter ; 
int				NoS_Counter ; 
int				KeyPressed ; 
 
void __attribute__((__interrupt__)) _T3Interrupt(void) 
{ 
	ADCON1bits.SAMP = 0 ; 
	IFS0bits.T3IF = 0 ; 
	 
} 
 
void __attribute__((__interrupt__)) _ADCInterrupt(void) 
{ 
 
		IFS0bits.ADIF = 0 ; 
} 
 
void __attribute__((__interrupt__)) _T1Interrupt(void) 
{ 
 
	T1IF_Counter += 1 ; 
 
	if ( T1IF_Counter > 1000) 
		{ 
			T1IF_Counter = 0 ; 
			T1IF_Flag = 1 ; 
		}  
 
		IFS0bits.T1IF = 0 ; 
} 
 
void __attribute__((__interrupt__)) _PWMInterrupt(void) 
{ 
 
		IFS2bits.PWMIF = 0 ; 
} 
 
int	main( void ) 
 
{ 
	// Make FLTA = 0 first ................ 
 
	TRISEbits.TRISE8 = 0 ; 
	LATEbits.LATE8 = 0 ; 
 
	DIR_LED13 = 0 ; 
	DIR_LED14 = 0 ; 
	DIR_LED15 = 0 ; 
	DIR_LED16 = 0 ; 
 
	HALL_A = 0 ; 
	HALL_B = 0 ; 
	HALL_C = 1 ; 
	DIR_HALL_A = 0 ; 
	DIR_HALL_B = 0 ; 
	DIR_HALL_C = 0 ; 
 
	OpenLCD( ) ; 
	InitADC10( ) ;	 
	Initial_Timer1( ) ; 
 
		T1IF_Flag = 0 ; 
		T1IF_Counter = 0 ; 
		HALL_Position = 1 ; 
 
		putrsLCD("dsPIC30F4011 Dem") ; 
		setcurLCD(0,1) ; 
		putrsLCD("HALL POSITION   ") ; 
			setcurLCD(14,1) ; 
			putcLCD(HALL_Position+'0');	 
 
		Set_HALLs( ) ; 
 
 while (1)  
	{ 
		while ( T1IF_Flag == 0 ) ; 
 
			T1IF_Flag = 0 ; 
			IFS0bits.T1IF = 0 ; 
			LED13 = ! LED13 ; 
			LED14 = ! LED14 ; 
 
		// Convert AN8 to get the KeyValue ................... 
 
 				IFS0bits.ADIF = 0 ; 
 				ADCON1bits.SAMP = 0;			//	start Converting 
				while (!IFS0bits.ADIF); 		// conversion done? 
 				ADC_Value = ADCBUF0;			// yes then get ADC value 
		 
		if ( KeyPressed ) 
				{ 
					if ( ADC_Value > ADC_MAX-20) 
						{ 
							NoS_Counter+= 1 ; 
							if ( NoS_Counter >= 3 ) 
							{ 
								KeyPressed = 0 ; 
								NoS_Counter = 0 ; 
							} 
						} 
				} 
		else 
		{	 
			if ( ( ADC_Value < AD_S1+10 ) && ( ADC_Value > AD_S1-10) ) 
					 
					S1_Process( ) ; 
					 
			else if ( ( ADC_Value < AD_S2+10 ) && ( ADC_Value > AD_S2-10) ) 
 
					S2_Process( ) ; 
 
			else if ( ( ADC_Value < AD_S3+10 ) && ( ADC_Value > AD_S3-10) ) 
 
					S3_Process( ) ; 
 
			else if ( ( ADC_Value < AD_S4+10 ) && ( ADC_Value > AD_S4-10) ) 
					 
					S4_Process( ) ;		 
		}	 
	} 
} 
 
void	S1_Process(void) 
{ 
		S1_Counter += 1 ; 
		S2_Counter = 0 ; 
		S3_Counter = 0 ; 
		S4_Counter = 0 ; 
		NoS_Counter = 0 ; 
 
			if ( S1_Counter >= 3 ) 
				{ 
					S1_Counter = 0 ; 
					KeyPressed = 1 ; 
					HALL_Position += 1 ; 
 
						if ( HALL_Position >= 7 )  
								HALL_Position = 1 ; 
						Set_HALLs( ) ; 
						setcurLCD(14,1) ; 
						putcLCD(HALL_Position+'0');							 
				}	 
} 
 
void	S2_Process(void) 
{ 
		S2_Counter += 1 ; 
		S1_Counter = 0 ; 
		S3_Counter = 0 ; 
		S4_Counter = 0 ; 
		NoS_Counter = 0 ; 
 
			if ( S2_Counter >= 3 ) 
				{ 
					S2_Counter = 0 ; 
					KeyPressed = 1 ; 
 
					if ( HALL_Position == 1 ) 
 
							HALL_Position = 6 ; 
					else 
							HALL_Position -= 1 ; 
 
						Set_HALLs( ) ; 
						setcurLCD(14,1) ; 
						putcLCD(HALL_Position+'0');							 
				}	 
} 
 
void	S3_Process(void) 
{ 
		S3_Counter += 1 ; 
		S1_Counter = 0 ; 
		S2_Counter = 0 ; 
		S4_Counter = 0 ; 
		NoS_Counter = 0 ; 
 
			if ( S3_Counter >= 3 ) 
				{ 
					S3_Counter = 0 ; 
					KeyPressed = 1 ;						 
				}	 
} 
 
void	S4_Process(void) 
{ 
		S4_Counter += 1 ; 
		S1_Counter = 0 ; 
		S2_Counter = 0 ; 
		S3_Counter = 0 ; 
		NoS_Counter = 0 ; 
 
			if ( S4_Counter >= 3 ) 
				{ 
					S4_Counter = 0 ; 
					KeyPressed = 1 ;						 
				}	 
} 
 
void DelayNmSec(unsigned int N) 
{ 
		unsigned int j; 
		while(N--) 
 		for(j=0;j < 1000;j++); 
} 
 
 
void InitADC10(void) 
{ 
 
 	ADPCFG = 0xFEFF;				// all PORTB = Digital; RB8 = analog 
 	ADCON1 = 0x2006;				// 0b0010 0000 0000 0110 
									// SAMP bit = 0 ends sampling ..and starts converting 
 	ADCHS =  0x0008;				// Connect RB8/AN8 as CH0 input  .. 
									// in this example RB0/AN0 is the input 
 	ADCSSL = 0; 
 	ADCON3 = 0x040f;				// Manual Sample, Tad = internal 2 Tcy 
 	ADCON2 = 0x0000; 
 
	IFS0bits.ADIF = 0 ; 
	IEC0bits.ADIE = 0 ; 
	IPC2bits.ADIP = 0 ; 
  
 	ADCON1bits.ADON = 1;			// turn ADC ON 
} 
 
void	Initial_Timer1( void ) 
{ 
		ConfigIntTimer1( T1_INT_PRIOR_7 & T1_INT_ON ) ; 
		OpenTimer1( T1_ON & T1_IDLE_STOP & T1_GATE_OFF & T1_PS_1_1 & T1_SYNC_EXT_OFF & T1_SOURCE_INT , 
					683 ) ; 
} 
 
void	Set_HALLs(void) 
{ 
	switch ( HALL_Position ) 
	{ 
		case 1: 
				HALL_A = 1 ; 
				HALL_B = 0 ; 
				HALL_C = 0 ; 
				break ; 
		case 2: 
				HALL_A = 1 ; 
				HALL_B = 1 ; 
				HALL_C = 0 ; 
				break ; 
		case 3: 
				HALL_A = 0 ; 
				HALL_B = 1 ; 
				HALL_C = 0 ; 
				break ; 
		case 4: 
				HALL_A = 0 ; 
				HALL_B = 1 ; 
				HALL_C = 1 ; 
				break ; 
		case 5: 
				HALL_A = 0 ; 
				HALL_B = 0 ; 
				HALL_C = 1 ; 
				break ; 
		case 6: 
				HALL_A = 1 ; 
				HALL_B = 0 ; 
				HALL_C = 1 ; 
				break ; 
		default : 
				break ; 
 
	 
	} 
}