chipKIT® Development Platform

Inspired by Arduino™

prescale on Timer 2

Created Wed, 11 Nov 2015 19:43:53 +0000 by Cnote


Cnote

Wed, 11 Nov 2015 19:43:53 +0000

I'm using an Uno32 board to control a Rugged Circuits Motor Driver. Their provided AccelStepperDemo code has the following:

// Set power on pins D3 and D9, the ENABLE1 and ENABLE2 pins. The parameter // is in the range 0 (no power) to 255 (maximum power). analogWrite(3, 100); analogWrite(9, 100);

// Change from divide-by-64 prescale on Timer 2 to divide by 8 to get // 8-times faster PWM frequency (976 Hz --> 7.8 kHz). This should prevent // overcurrent conditions for steppers with high voltages and low inductance. TCCR2B = _BV(CS21);

Question: What is the equivalent of TCCR2B = _BV(CS21) for Uno32 boards?


majenko

Thu, 12 Nov 2015 00:34:59 +0000

The equivalent on the PIC32 would be:

T2CONbits.TCKPS = 0b011;

However, due to the difference in base frequency that may not be what you want. The available binary values are:

111 = 1:256 prescale value
110 = 1:64 prescale value
101 = 1:32 prescale value
100 = 1:16 prescale value
011 = 1:8 prescale value
010 = 1:4 prescale value
001 = 1:2 prescale value
000 = 1:1 prescale value

By default the prescaler is set to 1:256. The reduction in prescaler that your code is expecting is a factor of 8 (64/8 = 8), so the equivalent reduction for the PIC32 would be 256/8 = 32, so you would want the 1:32 prescaler:

T2CONbits.TCKPS = 0b101;

Cnote

Fri, 13 Nov 2015 02:36:02 +0000

Thank you. That is very helpful.