QEI and PWM Timers

Questions on other types of hardware and getting it talking to the ARM CPU
Post Reply
GKDarin
Posts: 9
Joined: Mon Nov 19, 2012 1:44 pm

QEI and PWM Timers

Post by GKDarin »

I am having an issue with quadrature encoder and PWM Channel 1 interfering with each other.

I have a motor being controlled by PWM and it's speed monitored by a 720 pulse per revolution Quadrature encoder.

Turning the motor really slowly, 4 rpm, and when it goes around past 720 and wraps around to 0, the PWM channel gets a voltage spike.
It's like clockwork watching it happen.

Do the two items share a timer, is there a way to select a timer for either of them?

Thanks,
Darin



basicchip
Posts: 1029
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: QEI and PWM Timers

Post by basicchip »

The Quadrature decoder is separate hardware from the Timers, so I'm not sure how they would interact

I need some more details on how you have them connected up, and what your program is doing.

GKDarin
Posts: 9
Joined: Mon Nov 19, 2012 1:44 pm

Re: QEI and PWM Timers

Post by GKDarin »

I have a 720 pulse per revolution encoder hooked up to the dedicated pins for A and B inputs for an encoder.

Then I have a MOSFET transistor pair driven from HPWM 1, driving a 12V Gear motor with the encoder attached to the output shaft of the gear motor.

With just a fixed PWM value of say 10% duty cycle, as the encoder passes it's QEIMAXPOS value, the PWM sends a voltage spike, and the motor speed jumps.

With a slowly rotating motor running at 5 rpm, when the encoder recycles from 720 to 0 the motor will jerk to about 20 rpm then settle back down again until the next revolution.

This was at a cycle time like HWPWM(1,8192,1024). In working with it today, ramping up to an ultra high PWM frequency like HWPWM(1,512,128) makes it so that the speed doesn't jump as much, but you can still hear it in the signal whine from the motor.

I have a scope on order to try to see out what it going on.

Changing QEIMAXPOS to 0 will get rid of the pulse, put then the encoder position report goes away, but I do still get the RPM from the interrupt routine.

Darin

basicchip
Posts: 1029
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: QEI and PWM Timers

Post by basicchip »

QEI and timers are separate and should not interact, maybe what you are seeing is not the cause but an effect.

How is the motor grounded (how are the MOSFET signal connections made)? The MOSFET GND (where all that motor current it going should NOT go through the same GND as the controller.

If the MOSFET is mounted onboard with the controller, a wire should come in from the motor and one go back out to the motor power supply GND.

Motor power supply GND and GND for the controller should only be made at a single point, you do not want loops in GND.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: QEI and PWM Timers

Post by YahooArchive »

>from help line
>> Do you have any sample code which uses the QEI I/F to count
> the number
> of encoder pulse.

While the QEI and MOTOPWM are capable of some very complex operations, it can
also do the easy ones.

From the NXP user manual--

The QEI is configured using the following registers:
1. Power: In the PCONP register (Table 46), set bit PCQEI.
Remark: On reset, the QEI is disabled (PCQEI = 0).

SCB_PCONP = SCB_PCONP or (1<<18)

2. Peripheral clock: In the PCLKSEL0 register (Table 40), select PCLK_QEI.

The default value (divide by 4) is OK so nothing needed here

3. Pins: Select QEI pins through the PINSEL registers. Select pin modes for port
pins
with QEI functions through the PINMODE registers (Section 8.5).

The index pin is MCI2 (yes their nomenclature could be better) its on the last
page of the QEI section

PCB_PINSEL3 = PCB_PINSEL3 or (1<<16)

would turn that on assuming it was set as a GPIO (default state)

Then just read the index counter

index_count = INXCNT

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: QEI and PWM Timers

Post by YahooArchive »

>from the help line
>I have to use the Index compare register to generate an interrupt when the
index count register reaches a certain number of counts.

In general the steps are the same for any interrupt

Create an INTERRUPT SUB in BASIC or __attribute__ ((interrupt)) for ARM7 in C
(nothing special for Cortex parts, ie. attribute not needed in C)

Then set the vector to point to the ISR in the NVIC/VIC

In the Cortex part in BASIC ...ISR = ADDRESSOF theISR + 1
For the ARM7 set VICVectAddrX and VICVectCntlX

To be on the safe side, its good to clear any pending interrupts before you do
the final enable, so for the QEI

QEICLR = &H1FFF

Then enable the proper enables in the device. Often devices have an enable
register you can read/write directly. Or some have set/clear bit registers to
enable disable interrupts. For the QEI, it has a set/clear register with index
compare, that's bit 9

QEIIES = 1<<9

You should also set the compare value in INXCMP register.

Final step is to enable the interrupt in the NVIC

VICIntEnable0 = 1<<31 ' bit for QEI interrupt

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: QEI and PWM Timers

Post by YahooArchive »

Can anyone help me figure out how to use the LPC1756 Quadrature Encoder
Interface peripheral?

I know that there are many registers that can be set up to do lots of cool
stuff, BUT all I want to do is get a position count. I don't understand if this
peripheral (once enabled) runs all the time in the background, and the program
just reads the count like a variable whenever, OR do I need to set and clear
interrupts to be able to do anything at all with it?

Here is what I have so far...

' Quadrature Encoder Interface Testing Program 7/19/12 DG

#include "LPC17xx.bas"

DIM POSITION As Integer

SUB INIT

SCB_PCONP = SCB_PCONP or (1<<18) 'enable QEI bit in power/clock register
(PCONP)
PCB_PINSEL3 = PCB_PINSEL3 or (1<<8) 'Set pin P1.20 mode for PH A
PCB_PINSEL3 = PCB_PINSEL3 or (1<<14) 'Set pin P1.23 mode for PH B
QEICON = QEICON or 1 'clear position

ENDSUB

MAIN:
INIT

MAINLOOP:

POSITION = QEIPOS
Print POSITION
WAIT (1000)
Goto MAINLOOP


END

This code runs but POSITION is always zero. Thoughts?

Dan

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: QEI and PWM Timers

Post by YahooArchive »

> I know that there are many registers that can be set up to do lots of cool
stuff, BUT all I want to do is get a position count. I don't understand if this
peripheral (once enabled) runs all the time in the background, and the program
just reads the count like a variable whenever, OR do I need to set and clear
interrupts to be able to do anything at all with it?

Once enabled it does run all the time.

Looks like you need to set QEIMAXPOS

from the user manual--

QEI Maximum Position register (QEIMAXPOS - 0x400B C010)

This register contains the maximum value of the encoder position. In forward
rotation the position register resets to zero when the position register exceeds
this value. In reverse rotation the position register resets to this value when
the position register decrements from zero

Most encoders do X pulses per rotation, so this is one way to run without an
index pulse and keep angular accuracy.

If you're using something like linear actuator, you may want to set that max to
the number of pulses times the rotations for max travel.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: QEI and PWM Timers

Post by YahooArchive »

That was the problem. I thought MAXPOS was only for triggering interrupts, and
not necessary. It works now...THANK YOU!!!

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: QEI and PWM Timers

Post by YahooArchive »

Had some issues getting a quadrature encoder working with a ProPlus after Bruce
kindly pointed me to some NXP documentation on the subject, so I thought I'd
post a note here to save the next person a few minutes (or hours in my case!!).

In my case, I was just trying to read the encoder position counter (QEIPOS) to
calculate distance and velocity of my encoder. The basic configuration
documented on pg 543 the NXP manual for the LPC17xx (UM10360.pdf) leaves out one
critical setup requirement if you're reading QEIPOS: It gets reset to 0 if it
exceeds QEIMAXPOS. If you neglect to initialize QEIMAXPOS, its default value is
0 and QEIPOS resets continuously to 0, which caused me to look all over my
hardware and program before I finally read section 26.4.2 (pg 546) carefully!!

Hope this helps someone...

Post Reply