| View previous topic :: View next topic |
| Author |
Message |
ALPL
Joined: 29 Mar 2009 Posts: 30
|
|
Posted: Sat Feb 19, 2011 3:51 am |
|
|
Hi Scott, it is not necessary to use QEI-ISRs if you do nor have to detemine a change in direction immediately or an over/underflow. It is enough to poll the values of the POSCNTL/H-register-pair in the main code. I used a timer-ISR for the intervals.
Best regards
ALPL |
|
 |
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Sat Feb 19, 2011 7:10 pm |
|
|
Hi ALPL. Just to follow up. I stripped the code to just bare bones
using only one parameter setup_qei(QEI_MODE_X2); figuring this
would be enough to see if anything was changing inside the chip.
Firing up the PCW Debugger with my ICD-U64 I see the following
As I turn the encoder clockwise SFR CAP2BUFL will increment by 1
If I turn the encoder anti clockwise it will decrement down to 0. When I
continue turning it anti clockwise SFR CAP2BUFH will change from 00 to FF
and CAP2BUFL will jump to FE and start counting down again. Its like
a rollover.
Here is what I have in main.
| Code: |
set_tris_a(0b0011000);
set_tris_b(0b00001101); //Port B I/O Config
set_tris_c(0b11000000); //Port C I/O Config
setup_adc_ports(no_analogs);
LCD_Start(); //Enable LCD to Start up
Count=100;
setup_qei(QEI_MODE_X2);
|
So with this basic config for the encoder the only Registers that are doing
anything that I can see are CAP2BUFL and CAP2BUFH.
I am not sure that I will be able to exploit these two SFR's to do anything
useful. The fact that CAP2BUFL is rolling over and counting down again
is going to make this difficult to decipher.
Thanks Scott |
|
 |
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Sat Feb 19, 2011 9:43 pm |
|
|
Hi, I am still working with the basic setup and can now display
the value of the POSCNTL (CAP2BUFL) SFR on my lcd display.
It appears that the actual decoder hardware inside the chip is functional
and is decoding the mechanical rotary encoder. On my LCD I can count
up from 0 to 128 and back down again depending what way I turn the encoder.
| Code: |
#BYTE port_a = 0xF80
#BYTE port_b = 0xF81
#BYTE port_c = 0xF82
#byte POSCNTH = 0xF67 // Position register high byte CAP2BUFH
#byte POSCNTL = 0xF66 // Position register low byte CAP2BUFL
#include <LCD4bit.c>
void Write_Display (void)
{
Lcd_Config( LCD_LINE1 );
printf(Write_LCD, "%d.0",POSCNTL);
}
void main(void)
{
set_tris_a(0b0011000);
set_tris_b(0b00001101); //Port B I/O Config
set_tris_c(0b11000000); //Port C I/O Config
setup_adc_ports(no_analogs);
LCD_Start(); //Enable LCD to Start up
setup_qei(QEI_MODE_X2);
while(true)
{
Write_Display();
}
} |
I am curious, perhaps someone can chime in that have experience with the decoder hardware inside this chip.
Since my encoder is a mechanical EL - Cheapo type with 20 positions, A, B
GND. am I way off base in my approach to use the "CAP2BUFL" SFR as the
basis of a routine that will basically take its value and through some code
turn that value into either a 1 or a 0 so I can use that to increment or
decrement a certain int value.
Or do you think that idea is a bad one. The end game for the project is
to just increment or decrement a INT by either turning the encoder clockwise or anticlockwise. Kind of like what happens when you turn up
or down the volume on a stereo that uses a rotary encoder as the volume control.
I don't intend to use any ISR, Index or velocity control, but might try using the in-built digital filter to eliminate bounce if I notice any with the encoder.
So any thoughts would be very welcome.
Thanks Scott |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Sun Feb 20, 2011 4:37 am |
|
|
The question is, 'why'. The hardware is already handling a value for you. To fiddle around decoding this, then handle yet another value, seems to be just making work. Just read the QEI value, and scale this to suit the final range you want.
In your loop:
| Code: |
void Write_Display (void) {
static int last_position;
int current;
current = POSCNTL;
if (current!=last_position) {
Lcd_Config( LCD_LINE1 );
printf(Write_LCD, "%d.0",current);
last_position=current;
}
}
|
Then the display will only update, when the value changes.
Your 'real' code can do exactly the same thing.
Best Wishes |
|
 |
sseidman
Joined: 14 Mar 2005 Posts: 159
|
|
Posted: Sun Feb 20, 2011 9:43 am |
|
|
| scottc wrote: |
Since my encoder is a mechanical EL - Cheapo type with 20 positions, A, B
GND. am I way off base in my approach to use the "CAP2BUFL" SFR as the
basis of a routine that will basically take its value and through some code
turn that value into either a 1 or a 0 so I can use that to increment or
decrement a certain int value. |
I would think it depends on whether the noise options on the QE inputs can accommodate the noisy mechanical switch you're using. This uprocessor is designed for motor encoders flipping here and there at a relatively high rate, and I'm not sure how well they'll work for your "frob knob". That's not to say they won't work--just that I've never used them for this purpose.
If the noise settings work for you, there's no reason to do what you're suggesting. If they don't work for you, there's no reason to be using the QE inputs on this chip-- put the encoder on port B interrupts, and use the algorithms discussed at length in the last week and a half or so.
You either let the quadrature algorithms Microchip provided work for you, or you don't. Don't shoot for the in-between solution here. |
|
 |
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Sun Feb 20, 2011 3:08 pm |
|
|
Hi Ttelamh, Sseidman
The main reason I was trying to decode this was the hardware is handing
me a value but its a value I dont want. I need to have the ability to count
up from 0 to say 240 and back down again by twisting that rotary encoder
To put it another way from a math perspective the only limitation on counting should be the value I choose weither its a int or int16 etc.
I am looking at the encoder/decoder module and just treating it like
it was a switch connected to a pin on port-b. If I press that switch down
then a value that I choose will increment up up by 1.
Does this make sense ?
The other reason I wanted to use the QEI hardware was because its
in the chip and it does indeed decode the mechanical rotary encoder
very well. Just having the encoder hardware built in does away with
a bunch of static code to figure out direction etc of a rotary encoder
that might be connected to a couple of port pins.
I am going to look and see if there is a way of setting the max count
of the decoder block and try use that as a means to get to where I want to be.
Thanks Scott |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Sun Feb 20, 2011 3:59 pm |
|
|
Not really.
You can just limit the value (simple if test), or scale the value. Anything else seems like doing things the 'hard way'.
Remember you can write to the counter as well as read. Also the number you are getting is really 0 to 255. The only reason you are seeing -ve values, is because you are printing them with %d, rather than %u.
Best Wishes |
|
 |
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Sun Feb 20, 2011 4:45 pm |
|
|
| Quote: |
you are printing them with %d, rather than %u
|
Good catch Ttelmah, so its 0 to 255. Well I think I will take your advice
and just use what the counter is giving me and work any math based on
that.
Thanks Again.
Scott |
|
 |
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Sun Feb 20, 2011 6:13 pm |
|
|
Well I got this to work with my low cost mechanical encoder. Hopefully
the following code can help someone out if the want to use the internal
QEI decoder of the PIC18F2331/PIC18F2431 as part of their project.
The basic idea of this code is to use a cheap mechanical rotary encoder
as the basis of a user interface to control a piece of hardware and give
the user feedback using a LCD display. The little PIC MPU has lots of
power
Thanks to all that assisted.
Scott
| Code: |
//Code !
// PIC MPU = PIC18F2331, PIC18F2431 With hardware qei module
// Mechanical Rotary Encoder connected to pins qei-a & qei-b Port A
// Pins are pulled up with 47k Resistors !
// Encoder is Bourns PEC 11 Incremental Encoder
// http://www.bourns.com/data/global/pdfs/PEC11.pdf
// CCS compiler ver 4.116
// Code compile "Rom = 4% ram = 1%"
#include <MAIN.h>
#USE delay(clock=10Mhz, crystal)
#USE FAST_IO(A)
#USE FAST_IO(B)
#USE FAST_IO(C)
#BYTE port_a = 0xF80
#BYTE port_b = 0xF81
#BYTE port_c = 0xF82
#byte POSCNTL = 0xF66 // Position register low byte CAP2BUFL
#include <LCD4bit.c> // 4BIT Lcd Routines non CCS
INT MYVALUE; // Value Variable to display on LCD
void Write_Display (void) //Display Handler
{
Lcd_Config( LCD_LINE1 );
printf(Write_LCD, "%u.00 QEI Value " ,MYVALUE);
}
VOID QEI(VOID)
{
MYVALUE=POSCNTL;
if (myvalue>200)
{
(myvalue=200) && (POSCNTL=200); //Bound MAX Value & POSCNTL Value
}
Else
If (myvalue<50)
{
(Myvalue=50) && (POSCNTL=50); //Bound MIN Value & POSCNTL Value
}
}
//NOTE the poscntl value will increment/decrement from 0 to 255 as the
//encoder wheel is turned. To keep the poscntl value at a useable state
//bound its value so it does not continue to count up or down !
void main(void)
{
set_tris_a(0b0011000); //Port A I/O Config
set_tris_b(0b00001101); //Port B I/O Config
set_tris_c(0b11000000); //Port C I/O Config
setup_adc_ports(no_analogs);
LCD_Start(); //Enable LCD to Start up
setup_qei(QEI_MODE_X2); //Configure QEI Module
POSCNTL=100; //Starting value of QEI Encoder/Decoder
MYVALUE=100; //Starting Value of "My value"
//Note if you want to increment my value variable by 2 instead of 1 use
// setup_qei(QEI_MODE_X4);
while(true)
{
Write_Display();
qei();
}
}
|
| Code: |
//main.h include file
#include <18F2431.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES LP //Low power osc < 200 khz
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O |
|
|
 |
sseidman
Joined: 14 Mar 2005 Posts: 159
|
|
Posted: Mon Feb 21, 2011 10:39 am |
|
|
Good Job! See how much simpler your code got!!
You'd save yourself many clock ticks, and probably have a better looking display, if you only wrote to the display when the value you were writing changed. |
|
 |
bkamen
Joined: 07 Jan 2004 Posts: 1619 Location: Central Illinois, USA
|
|
Posted: Mon Feb 21, 2011 12:06 pm |
|
|
Now,
Keeping in mind, you're constantly sending data to the LCD.
(and in this application it doesn't matter) -- but it's good practice to understand that unless the encoder position doesn't change, why bother doing anything else except spinning in a loop *checking for a change*.
When you have to share CPU cycles, this kind of thing (with polling) becomes important.
Don't do the work of updating the LCD if there's nothing new to report.
Just a mention.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 21, 2011 12:51 pm |
|
|
| Quote: | VOID QEI(VOID)
{
MYVALUE=POSCNTL;
if (myvalue>200)
{
(myvalue=200) && (POSCNTL=200); //Bound MAX Value & POSCNTL Value
}
Else
If (myvalue<50)
{
(Myvalue=50) && (POSCNTL=50); //Bound MIN Value & POSCNTL Value
}
} |
What's the purpose of doing the logical AND operations ?
| Quote: |
//main.h include file
#include <18F2431.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES LP //Low power osc < 200 khz
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#USE delay(clock=10Mhz, crystal)
|
Why is the LP fuse used for a 10 MHz crystal ? A 10 MHz crystal requires
the HS fuse. The LP fuse is normally used only for a 32.768 KHz watch
crystal. Was this code tested in real hardware ? |
|
 |
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Mon Feb 21, 2011 2:29 pm |
|
|
PCM the && sets the poscntl value to either the min or max value I
want to display. If I dont stuff the min and max value into poscntl then
it will not match the bounds of what is getting displayed.
The ccs compiler puts the "#FUSES LP //Low power osc < 200 khz"
in there by default. The code was tested on real hardware so the fuse
does not appear to stop anything from working.
Sseidman/Bkaman, your 100% right on only updating the display when something changes. Normally I use a changed flag to handle this.
Keep in mind that this is just some concept code to illustrate that the
QEI decoder can be made to work with a chepo rotary encoder. It seems
to work fairly well.
Thanks Scott |
|
 |
bkamen
Joined: 07 Jan 2004 Posts: 1619 Location: Central Illinois, USA
|
|
Posted: Mon Feb 21, 2011 2:31 pm |
|
|
| scottc wrote: | PCM the && sets the poscntl value to either the min or max value I
want to display. If I dont stuff the min and max value into poscntl then
it will not match the bounds of what is getting displayed.
The ccs compiler puts the "#FUSES LP //Low power osc < 200 khz"
in there by default. The code was tested on real hardware so the fuse
does not appear to stop anything from working.
Sseidman/Bkaman, your 100% right on only updating the display when something changes. Normally I use a changed flag to handle this.
Keep in mind that this is just some concept code to illustrate that the
QEI decoder can be made to work with a chepo rotary encoder. It seems
to work fairly well.
Thanks Scott |
You do realize that && is used in conditionals while & is used for logic. Right? _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
 |
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Mon Feb 21, 2011 6:06 pm |
|
|
& and && can be used anywhere one really wants the ones and zeros
to fall inside the chip
in this line of code the effect is
(myvalue=200) && (POSCNTL=200);
int myvalue will be 200 and the poscntl value will also be 200 or C8 if you
look at the cap2bufl SFR with a debugger.
To arrive at 200 the encoder is turned clockwise. If the cap2bufl value is not bound to 200 then the internal QEI module will continue to count up.
The effect is that while the LCD may display 200 the actual Encoder value
will not be 200 but more depending on how many clicks past 200 you
went. The cap2bufl SFR will go to 255 before it rolls over back to 0.
Without bounding the poscntl value if we went 10 clicks above 200
with the encoder and then we turned the encoder anti-clockwise you
would experience 10 clicks that do nothing until the value of 200 is
reached. From that point on continuing to turn the encoder anti-clockwise
you would see the value on the LCD display start to decrement by 1
for each click of the encoder wheel.
Make sense ?
Thanks Scott |
|
 |
|