Hi Patrick. I have all family reference manuals (also the one you linked) but none of them has any sample code. If it makes any sense I use MPLABX 2.0 and XC16 1.20/1.21. Another thing, the speed of micro is aproximately 67MIPS: > > #define SYS_FREQ 138188000ULL > > #define FCY SYS_FREQ/2 > > /* CONFIGURATION REGISTER MAP */ > > _FOSCSEL(FNOSC_FRCPLL & IESO_OFF) > > _FOSC(OSCIOFNC_ON & IOL1WAY_OFF & FCKSM_CSECME) > > _FWDT(FWDTEN_OFF) > > _FPOR(BOREN_ON & FPWRT_PWR128 & ALTI2C1_OFF) > > _FICD(ICS_PGD2 & JTAGEN_OFF) // PGD1 per a entrenadora, PGD2 per a rob= ot > > // Configura PLL prescaler, PLL postscaler, PLL divisor: CLKDIVbits.PLLPRE =3D 0; /* Pot agafar valors de 0 a 31 (N1=3D2 a = 33) N1 =3D PLLPRE + 2 (0,8MHz - 8MHz) F =3D 7,37 / 2 =3D 3,685 MHz */ PLLFBD=3D73; /* Pot agafar valors de 0 a 511 (M=3D2 a = 513) M =3D PLLDIV + 2 (120MHz - 340MHz) F =3D 3,685 * 76 =3D 269,005 MHz */ CLKDIVbits.PLLPOST =3D 0; /* Pot agafar valors 0,1,3 (N2=3D2,4,8) N2 =3D 2 * (PLLPOST + 1) (<140MHz @85=BAC) Fosc =3D 269,005 / 2 =3D 134,502 MHz */ > // Fcy =3D 134,502 / 2 =3D 67,251 MIPS CLKDIVbits.DOZEN =3D 0; CLKDIVbits.FRCDIV =3D 0; __builtin_write_OSCCONH(0b00000001); /* New Oscillator FRC w/ PLL */ __builtin_write_OSCCONL(0b00000001); /* Enable Switch */ while(OSCCONbits.COSC !=3D 0b001); /* Wait for new Oscillator to become > FRC w/ PLL */ while(OSCCONbits.LOCK !=3D 1); /* Wait for Pll to Lock */ Another thing I do, previous to configure QEI, is to deactivate all analog inputs, paralel port and I2C: > ANSELB =3D 0; > > ANSELC =3D 0; > > ANSELD =3D 0; > > ANSELE =3D 0; > > ANSELG =3D 0; > > I2C1CON =3D 0; > > I2C2CON =3D 0; > > AD1CON1 =3D 0; > > AD2CON1 =3D 0; > > PMCON =3D 0; > > PMAEN =3D 0; > > In last mail, I forgot to copy the function I use to configure QEI and, yes, QEIEN is set. I copy that function here: > > void inicia_QEI_dre(void) > > { > > QEI1CONbits.PIMOD =3D 0b000; // Index no afecta > > QEI1CONbits.IMV =3D 0b00; // Index no afecta > > QEI1CONbits.INTDIV =3D 0b000; // pre-scaler 1:1 > > QEI1CONbits.CNTPOL =3D 0b0; // Avan=E7a en positiu > > QEI1CONbits.GATEN =3D 0b0; // External Gate no afecta > > QEI1CONbits.CCM =3D 0b00; // Mode Quadratura > > QEI1IOCbits.QCAPEN =3D 0b0; // Home no afecta > > QEI1IOCbits.FLTREN =3D 0b0; // Desactiva el filtre digital > > QEI1IOCbits.QFDIV =3D 0b000; // Divisor filtre entrada 1:1 > > QEI1IOCbits.OUTFNC =3D 0b00; // Desactiva sortida > > QEI1IOCbits.SWPAB =3D 0b0; // Canals A i B no es canvien > > QEI1IOCbits.HOMPOL =3D 0b0; // Home no est=E0 invertit > > QEI1IOCbits.IDXPOL =3D 0b0; // Index no est=E0 invertit > > QEI1IOCbits.QEBPOL =3D 0b0; // QEB no est=E0 invertit > > QEI1IOCbits.QEAPOL =3D 0b0; // QEA no est=E0 invertit > > QEI1CONbits.QEIEN =3D 1; // Activa el QEI > > POS1CNTH =3D 0; // Borra comptador > > POS1CNTL =3D 0; // Borra comptador > > VEL1CNT =3D 0; // Borra el comptador. S'ha de lleg= ir >> a 1-4 kHz > > } > > I do exactly the same with QEI2. (I know actually most of bits are zero, but I always include all of them for future changes). Ariel, Patrick, do you see any difference with your codes and my code? I have a doubt with this registers: QEIxICH, QEIxICL. Are them needed to run QEI? What are them for exactly? 2014-02-11 9:43 GMT+01:00 Patrick Moody : > On 11 February 2014 06:55, Vicent Colomar Prats >wrote: > > > Hi Ariel. Do you refer to this? > > > > > http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=3Den557367#doc= umentation > > > > In code examples there is no PWM nor QEI section, most of them are for > ADC > > or communications. > > > > I do it in this way: > > > > > /* QEI */ > > > > > > TRISDbits.TRISD5 =3D 1; > > > > > > TRISDbits.TRISD4 =3D 1; > > > > > > TRISDbits.TRISD9 =3D 1; > > > > > > TRISDbits.TRISD8 =3D 1; > > > > > > > > >> /* PWM */ > > > > > > TRISEbits.TRISE0 =3D 0; // RE0 output > > > > > > TRISEbits.TRISE1 =3D 0; // RE1 output > > > > > > TRISEbits.TRISE2 =3D 0; // RE2 output > > > > > > TRISEbits.TRISE3 =3D 0; // RE3 output > > > > > > // Pins remapables: > > > > > > __builtin_write_OSCCONL(OSCCON & ~(1<<6)); // Unlock PPS IOLOCK > > >> registers > > > > > > RPINR18bits.U1RXR =3D 97; // U1RX -> RP97 (RF1) > > > > > > RPOR7bits.RP96R =3D 1; // U1TX -> RP96 (RF0) > > > > > > RPINR14bits.QEA1R =3D 69; // RP69 > > > > > > RPINR14bits.QEB1R =3D 68; // RP68 > > > > > > RPINR16bits.QEA2R =3D 73; // RPI73 > > > > > > RPINR16bits.QEB2R =3D 72; // RPI72 > > > > > > __builtin_write_OSCCONL(OSCCON | (1<<6)); // Lock PPS IOLOC= K > > >> registers > > > > > > > > And then, I read the registers and send them to the serial port: > > > > > signed long ence,encd; > > > > signed long tence, tencd; > > > > ence =3D POS1CNTL; > > > > // ence +=3D (POS1HLD << 16); > > > > encd =3D POS2CNTL; > > > > // encd +=3D (POS2HLD << 16); > > > > tence =3D INT1HLDL; > > > > // tence +=3D (INT1HLDH << 16); > > > > tencd =3D INT2HLDL; > > > > // tencd +=3D (INT2HLDH << 16); > > > > printf("QEesq=3D%ld, Tesq=3D%ld. QEdre=3D%ld, Tdre=3D%ld\n",enc= e,tence, > > > encd, tencd); > > > > > > > > But they four are always 0, never changes. > > > > Can you show me your piece of code about QEI? > > > > > The data-sheet you're probably looking for is in the "reference manual" > sub-section of the "Documentation and Software" section, near the bottom = of > the page, named "dsPIC33E/PIC24E FRM, Quadrature Encoder Interface" > Not 100% sure about the link, but this might work: > http://ww1.microchip.com/downloads/en/DeviceDoc/S15.pdf > > > I didn't see any sign of you setting up the QEI module before attempting = to > read from it in the code you posted. > In my code (by no means perfect) among others, you would expect to see a > line like this one: > (Assuming you have the appropriate #include line for the PIC at the top o= f > your file) > > QEI1CONbits.QEIEN =3D 1; //Enable the QEI counter > > If you haven't already, you should look at the data-sheet I linked to (or > find it if the link didn't work), particularly from page 6 onwards, where > the registers you should be setting up before you use the QEI modules are > described. > > Hope that helps, > > Patrick > -- > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .