Thanks Henrik,
I understand now.
Thanks Henrik,
I understand now.
Dave
Always wear safety glasses while programming.
Now I am probe on many ways to do next but I cant to image how.
1. I was setup count for QEI with friends from these forum to count from 0 - 12500 - becous my motor have gear 6.25:1 and 6.25*2000 = 12500(reding of one turn of motor shaft with 500 lines encoder) give me full 360' of output shaft range.That work extra.
Next : If I want to move for example 1mm on my mechanic I need one turn of output shaft that give me easi to control position of count from CW 0-12500 or CCW 12500 to 0.That is all in 16bit counter.
But what if I need to move more then 16bit counter ?
OK I can use PBPL and define 32bit variable - I was probe and it work 100%.
I can easy set to se if X VAR LONG number 25000000 that give me 25000000/12500 = 2000mm - inpresive!
But how I can my software variable defined as LONG from 0 - 25000000 to increment or decrement from hardware COUNT buffer wich count in circle from 0-12500 and vice verse ?
Is any idea becouse all what I was probe give me garbage of result.
Regards Robert![]()
Last edited by phoenix_1; - 8th September 2009 at 17:09.
OK now next step are steped.
I write calculation to increment in CW my variable defined as LONG to be incremented from position buffer
which go from 0-12500 and again and again..
Now I can count a long trip of mechanic of machine which is present in LONG variable in CW direction.Code:' Pic 18F4431 @ 20MHz true XTAL define osc 20 DEFINE HSER_RCSTA 90h DEFINE HSER_TXSTA 24h DEFINE HSER_SPBRG 64 DEFINE HSER_CLROERR 1 ADCON0=0 ANSEL0=0 PortA = 0 PortB = 0 PortC = 0 TRISA = %011100 TRISB = %00000000 TRISC = %10000000 QEICON=%10011000 DFLTCON = %00111000 MAXCNTL = %11010100 MAXCNTH = %00110000 PosLow VAR BYTE PosHigh VAR BYTE PosTemp VAR BYTE Position VAR WORD y var LONG x var BYTE x = 0 y = 0 POSCNTL = 0 POSCNTH = 0 PIR3.2 = 0 CLEAR Start: Gosub GetPosition y = (x * 12500) + position HSEROUT [dec y] Pause 10 Goto Start GetPosition: PosHigh = POSCNTH PosLow = POSCNTL PosTemp = POSCNTL If PosLow - PosTemp = 0 then Goto Done PosHigh = POSCNTH PosLow = POSCNTL Done: Position = POSHIGH * 256 + PosLow if PIR3.2 = 1 then x = x+ 1 PIR3.2 = 0 RETURN END
But I have new problem in my head all these days and it is next.
I have BIT which indicate direction of motor and it is in QEICON.5.
If QEICON.5 is 0 then we go CCW and if it is 1 then we go CW direction.
Now if we go CW and stop motor it wil be 1 all the time to next move
and if we go CCW it will be 0 and after we stop from CCW it will be 0 all time to next move..
Confuse how to make matematic to implement new formula for decrement LONG variable in CCW :
y = y - (12500-position) or similar becous in CCW position go from 12500-0 and again 12500-0....
If any idea or someone want to help THANKS.
Regards Robert![]()
Last edited by phoenix_1; - 10th September 2009 at 20:21.
Maybe I am missing something but if you stop the motor and then restart it you know the direction?? Set the direction bit when you start the motor.
Dave
Always wear safety glasses while programming.
Here is code which can measure and put to Y variable defined as LONG position of mechanic on machine to 32bit and count up and down.
I am not good programmer but code work good.
Code:' Pic 18F4431 @ 20MHz true XTAL define osc 20 DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1 DEFINE HSER_SPBRG 64 ' 19200 Baud @ 20MHz, 0.16% DEFINE HSER_CLROERR 1 ' Clear overflow automatically ADCON0=0 'Turn of ADC ANSEL0=0 'Make Port A inputs Digital PortA = 0 PortB = 0 PortC = 0 TRISA = %011100 'QEI pins as inputs. TRISB = %00000000 TRISC = %10000000 'USART RX pin as input. QEICON=%10011000 DFLTCON = %00111000 ' enable error filter for all capture inputs MAXCNTL = %11010100 'low set for maxcnt of 12500 becouse have gear 6.25:1 MAXCNTH = %00110000 'hig set for maxcnt of 12500 becouse have gear 6.25:1 'MAXCNTL = %00000001 'lowbyte = 207 'MAXCNTH = %00000000 'highbyte = 7 but after math: highbyte * 256 = 1792 + 207 = 1999 'QEICON = %00010100 'Setup QEI, 4X mode, reset on index. PosLow VAR BYTE 'Storage for the low byte of encoder count PosHigh VAR BYTE 'Storage for the high byte of encoder count PosTemp VAR BYTE 'Temporary storage for low byte of encder count Position VAR WORD 'Actual encoder position, 16bit word. y var long 'Total pulse count storage variable 32bit x var byte 'Number of full shaft rotation of 360' z var BIT 'Actual bit if any move of shaft to any direction h var BIT 'direction bit 0 for left , 1 for right turn of encoder z = 0 x = 0 y = 0 h = 0 POSCNTL = 0 ' clear lowbyte of counter for start POSCNTH = 0 ' clear highbyte of counter for start PIR3.2 = 0 CLEAR Start: Gosub GetPosition y = (12500 * x) + position ' increment total counter if z = 1 and h = 0 then y = y-(12500-position) ' dectement total counter HSEROUT [DEC y] ' debug via rs232 @ 19200 Pause 100 'Wait.... Goto Start '...and do it again. GetPosition: z = 0 PosHigh = POSCNTH 'Get high byte of counter PosLow = POSCNTL 'Get low byte of counter PosTemp = POSCNTL 'Get low byte again If PosLow - PosTemp = 0 then Goto Done 'Compare, if equal we're done. PosHigh = POSCNTH 'If not, get values again. PosLow = POSCNTL z = 1 Done: Position = POSHIGH * 256 + PosLow 'Put high and lowbyte together. h = QEICON.5 'QEICON.5 for right = 1 for left = 0 if PIR3.2 = 1 and h =1 then x = x+ 1 if x <> 0 and PIR3.2 = 1 and h =0 then x = x- 1 PIR3.2 = 0 'PIR3.2 maxcount transition down or up was detected h = 0 RETURN END
Last edited by phoenix_1; - 12th September 2009 at 13:02.
Hi, Dave
For HSPLL Osc ...
I just run a 12 Mhz XTAL on a 18F4420, PLL Engaged ... everything fine.
So, May be the XTAL ... or may be your board layout ( 40 Mhz is HF !!! ) or ... poor quality capacitors.
Alain
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
Bookmarks