View Single Post
  #7   Spotlight this post!  
Unread 22-03-2004, 16:00
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Exclamation Re: counting digital input

None of that is going to work if you're going at any resonable speed. It's going to miss ticks. Use interupts.

Here's some of my code:
Code:
//Variables
volatile char State = 0;
volatile long Distance = 0;
volatile signed char Dir = 0;
//...

//Call while initializing
void Initialize_Interrupts(void)
{
	// initialize external interrupt 1 (INT2 on user 18F8520)
	TRISBbits.TRISB2 = 1;		// make sure the RB2/INT2 pin is configured as an input [108]
								//
	INTCON3bits.INT2IP = 0;		// 0: interrupt 1 is low priority (leave at 0 for IFI controllers) [91]
								// 1: interrupt 1 is high priority
								//
	INTCON2bits.INTEDG2 = 0;	// 0: trigger on the falling-edge [90]
								// 1: trigger on the rising-edge
								//
	INTCON3bits.INT2IE = 0;		// 0: disable interrupt	1 [91]
								// 1: enable interrupt 1
	
	// initialize external interrupt 2 (INT3 on user 18F8520)
	TRISBbits.TRISB3 = 1;		// make sure the RB3/CCP2/INT3 pin is configured as an input [108]
								//
	INTCON2bits.INT3IP = 0;		// 0: interrupt 2 is low priority (leave at 0 for IFI controllers) [90]
								// 1: interrupt 2 is high priority
								//
	INTCON2bits.INTEDG3 = 0;	// 0: trigger on the falling-edge [90]
								// 1: trigger on the rising-edge
								//
	INTCON3bits.INT3IE = 0;		// 0: disable interrupt	2 [91]
								// 1: enable interrupt 2

	// initialize external interrupts 3-6 (KBI0 - KBI3 on user 18F8520)
	TRISBbits.TRISB4 = 1;		// make sure the RB4/HBI0 pin is configured as an input [108]
	TRISBbits.TRISB5 = 1;		// make sure the RB5/KBI1/PGM pin is configured as an input [108]
	TRISBbits.TRISB6 = 1;		// make sure the RB6/KBI2/PGC pin is configured as an input [108]
	TRISBbits.TRISB7 = 1;		// make sure the RB7/KBI3/PGD pin is configured as an input	[108]
								//
  	INTCON2bits.RBIP = 0;		// 0: interrupts 3 through 6 are low priority (leave at 0 for IFI controllers) [90]
								// 1: interrupts 3 through 6 are high priority
								//
	Old_Port_B = PORTB;			// initialize the Old_Port_B variable (in user_routines_fast.c)
								//
	INTCONbits.RBIE = 1;		// 0: disable interrupts 3 through 6 [89]
								// 1: enable interrupts 3 through 6
}						  
//...

//The ISR Routine
void Int_3_Handler(unsigned char RB4_State)
{
 // wheel encoder Phase A interrupts.
 if (!ShaftA==ShaftB)
 {
  Dir = 1;
 }
 if (ShaftA==ShaftB)
 {
  Dir = -1;
 }
 Distance+=Dir; // Increment

 switch (State) {
  case 0: // A=0, B=0
   State = 1;
   break;
  case 1: // A=1, B=0
   State = 0;
   break;
  case 2: // A=1, B=1
   State = 3;
   break;
  case 3: // A=0, B=1
  State = 2;
   break;
 } // switch (State)
}
It's based on Kevin's interupts stuff and the Quadrature whitepaper. Hope you can figure it out!