PIC32MX: Encoder Motor Control

From Mech
Jump to navigationJump to search

Available Pins

The PIC32MX460F512L has 5 available external timer / counter pins denoted TXCK on the pin list, where x is from 1 to 5. Unfortunately, the PIC32MX440F512H has only 1 available external counter (T1CK), so this cannot be used for quadrature encoder using the external counter pins.

Quadrature Encoder Example

This section details how to set up quadrature encoder with the PIC32MX460F512L and the LS7083.

Sample Code

/*
QuadratureHard.c 

The PIC32MX460F512L has 5 available external timer / counter pins denoted TXCK on the pin list, 
where x is from 1 to 5. Unfortunately, the PIC32MX440F512H has only 1 available external counter (T1CK), 
so this PIC cannot be used for quadrature encoder. The NU32 board uses PIC32MX460F512L. 

This code is based on the QuadratureHard.c for the 8-bit PICS.
Quadrature based on Hardware requires two timers to count up-counts and down-counts. 

PWM uses Timer 2 or Timer 3, therefore, we are going to use Timer 4 and Timer 5 for this code.
Timer 4 is T4CK/RC3 and Timer 5 is SDI1/T5CK/RC4.

LS7083 chip decodes quadrature A&B signals into up&down pulses for these two timers
The software version doesn't use up timers but this hardware version is arbitrarily fast
*/

 #include "HardwareProfile.h"
 
 signed int bigcount = 0; 	// set encoder value initially to zero, it can go + or -
 						 	// 32 bit number
 short count0, count1; 		// 16 bit number
 short last0 = 0, last1 = 0; // 16 bit number
 
 int main(void) {
 	
 	// Configure the system performance
	SYSTEMConfigPerformance(SYS_FREQ);
	
	mInitAllLEDs();

	
	// Must enable glocal interrupts - in this case, we are using multi-vector mode
    INTEnableSystemMultiVectoredInt();
	
     // init Timer4 and Timer5 mode and periods (PR4, PR5)
	OpenTimer4( T4_ON | T4_PS_1_1 | T4_SOURCE_EXT, 0xFFFF);
	OpenTimer5( T5_ON | T5_PS_1_1 | T5_SOURCE_EXT, 0xFFFF);
  	
	while (1) {                
		count0 = ReadTimer4();  // in your routine this must be done at least every 32000
								// encoder counts to avoid rollover ambiguity
		count1 = ReadTimer5(); 	

		bigcount += count0 - last0; // add on the recent up-counts, since the last time
	
		if (count0 < last0)
		{
			bigcount += 65536; 		// count0 only increments, so if it got lower 
	 					   		//it must have rolled over
		}

		last0 = count0;

		bigcount -= count1 - last1; // we're not worrying about rollover of the 32 bit bigcount total
	
		if (count1 < last1)
		{
			bigcount -= 65536;
		}
	
		last1 = count1; 
	      
                // Put other code here to display (RS232 works nice)
		}
}

Associated Circuitry

This Circuit is based off the HEDS5500 encoder, LS7083 quadrature, and PIC32MX460F512L.

Media:Quad_Encoder_PIC32_Circuit.pdf