Difference between revisions of "PIC32MX: Encoder Motor Control"

From Mech
Jump to navigationJump to search
(New page: ==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 av...)
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Available Pins==
==Available Pins==
The PIC32MX460F512L has 5 available external timer / counter pins denoted TXCK on the pin list, where x is from 1 to 5.
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.
Unfortunately, the PIC32MX440F512H has only 1 available external counter (T1CK), so this cannot be used for quadrature encoder using the external counter pins.
<br><br>
<br><br>


Line 9: Line 9:
===Sample Code===
===Sample Code===


/*
First include header files with definitions for generic type definitions, compiler, and for specific PIC. Also include the plib header file.
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==
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "HardwareProfile.h"
#include <plib.h>


This Circuit is based off the HEDS5500 encoder, LS7083 quadrature, and PIC32MX460F512L.
NOTE THAT BECAUSE WE USE THE BOOTLOADER, NO CONFIGURATION IS NECESSARY. THE BOOTLOADER PROJECT ACTUALLY CONTROLS ALL OF OUR CONFIG BITS.


[[Media:Quad_Encoder_PIC32_Circuit.pdf]]
Define the system frequency
#define SYS_FREQ (80000000L)

Define global variables for counting
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

Begin main function
int main(void)
{

Configure the proper PB frequency and the number of wait states
SYSTEMConfigPerformance(SYS_FREQ);

Initialize Timer1 and Timer2 as external clocks and periods (PR1, PR2)
OpenTimer1( T1_ON | T1_PS_1_1 | T1_SOURCE_EXT, 0xFFFF);
OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_EXT, 0xFFFF);

main loop
while (1)
{
count0 = ReadTimer1(); // in your routine this must be done at least every 32000
// encoder counts to avoid rollover ambiguity
count1 = ReadTimer2();

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 Code to Display Here
I put the above code in the Communicate Via USB Code and added the following lines:

sprintf (USB_Out_Buffer, "Encoder Count = %d!\r\n", bigcount);
if(mUSBUSARTIsTxTrfReady())
{
putrsUSBUSART(USB_Out_Buffer);
}

end the main function
return 0;
}

Latest revision as of 23:44, 15 April 2010

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