Here is success probe with count encoder from 0 to 2000 for one turn of shaft of motor.
Will continue...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=%11001100 DFLTCON = %00111000 ' enable error filter for all capture inputs MAXCNTL = %11001111 'lowbyte = 207 MAXCNTH = %00000111 '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. POSCNTL = 0 ' clear lowbyte of counter for start POSCNTH = 0 ' clear highbyte of counter for start CLEAR Main: Gosub GetPosition HSEROUT [dec position] Pause 100 Goto Main GetPosition: 'Since the position counter isn't double buffered we can get to situations 'where the lowbyte overflows between readings of the high and the low byte. This 'is prevented by reading the low byte two times and compare the two readings. 'If they are the same then we're fine, if not re-read the registers. 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 Done: Position = POSHIGH * 256 + PosLow 'Put high and lowbyte together. RETURN END
Robert![]()




Bookmarks