![]() |
dsPid33
|
00001 /* //////////////////////////////////////////////////////////////////////////// 00002 ** It contains dsPIC settings and initializations 00003 ** 00004 ** Detailed description are on file "descrEng.txt" 00005 ** numbers between brackets, eg.: [1] , are the references to the specific 00006 ** decription into the file 00008 00009 // standard includes 00010 #include "dsPID33_common.h" 00011 00012 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00013 /* ports and peripherals registers setting an initialization */ 00014 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00015 00016 // DMA buffers 00017 // ADC buffer, 2 channels (AN0, AN1), 32 bytes each, 2 x 32 = 64 bytes 00018 int DmaAdc[2][64] __attribute__((space(dma),aligned(256))); 00019 00020 void Settings(void) 00021 { 00022 #ifdef CLOCK_FREQ_10 //{ 00023 // Configure Oscillator to operate the device at 40 Mhz 00024 // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 00025 // Fosc= 10M*32(2*2)=80Mhz for 10 MHz input clock 00026 PLLFBD=30; // M=32 00027 #warning ********************************************************************** 00028 #warning -- compiling for 10MHz oscillator, set MPLAB SIM to 80.0000 MHz ****** 00029 #else 00030 00031 // Configure Oscillator to operate the device at 39,6288 Mhz 00032 // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 00033 // Fosc= 7.3728M*43(2*2)=79,2576Mhz for 7,3728 MHz input clock 00034 PLLFBD=41; // M=43 00035 #warning ********************************************************************** 00036 #warning -- compiling for 7.3728MHz oscillator, set MPLAB SIM to 79.2576 MHz ** 00037 #endif //} 00038 00039 CLKDIVbits.PLLPOST=0; // N1=2 00040 CLKDIVbits.PLLPRE=0; // N2=2 00041 00042 // Disable Watch Dog Timer 00043 RCONbits.SWDTEN=0; 00044 00045 // Clock switching to incorporate PLL 00046 __builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary 00047 // Oscillator with PLL (NOSC=0b011) 00048 __builtin_write_OSCCONL(0x01); // Start clock switching 00049 00050 #ifndef SIM // [21] 00051 while (OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur 00052 00053 while(OSCCONbits.LOCK!=1) {}; // Wait for PLL to lock 00054 #endif 00055 00056 #if defined(__dsPIC33FJ64MC802__) || defined(__dsPIC33FJ128MC802__) //{ 00057 #warning -- compiling for a 28 pin DSC **************************************** 00058 00059 #ifdef PROTOTYPE //{ 00060 #warning -- compiling for prototype board ************************************* 00061 00062 // Peripheral PIN selection *********************************** 00063 // Unlock Registers 00064 //************************************************************* 00065 asm volatile ( "mov #OSCCONL, w1 \n" 00066 "mov #0x45, w2 \n" 00067 "mov #0x57, w3 \n" 00068 "mov.b w2, [w1] \n" 00069 "mov.b w3, [w1] \n" 00070 "bclr OSCCON, #6 "); 00071 //************************************************************ 00072 // Configure Input Functions 00073 //************************************************************ 00074 //*************************** 00075 // Assign IC1 To Pin RP6 00076 //*************************** 00077 RPINR7bits.IC1R = 6; 00078 00079 //*************************** 00080 // Assign IC2 To Pin RP10 00081 //*************************** 00082 RPINR7bits.IC2R = 10; 00083 00084 //*************************** 00085 // Assign QEA1 To Pin RP6 00086 //*************************** 00087 RPINR14bits.QEA1R = 6; 00088 00089 //*************************** 00090 // Assign QEB1 To Pin RP5 00091 //*************************** 00092 RPINR14bits.QEB1R = 5; 00093 00094 //*************************** 00095 // Assign QEA2 To Pin RP10 00096 //*************************** 00097 RPINR16bits.QEA2R = 10; 00098 00099 //*************************** 00100 // Assign QEB2 To Pin RP11 00101 //*************************** 00102 RPINR16bits.QEB2R = 11; 00103 00104 //*************************** 00105 // Assign U1RX To Pin RP8 00106 //*************************** 00107 RPINR18bits.U1RXR = 8; 00108 00109 //*************************** 00110 // Assign U2RX To Pin RP7 00111 //*************************** 00112 RPINR19bits.U2RXR = 7; 00113 00114 //************************************************************ 00115 // Configure Output Functions 00116 //************************************************************ 00117 00118 //*************************** 00119 // Assign U1Tx To Pin RP9 00120 //*************************** 00121 RPOR4bits.RP9R = 3; 00122 00123 //*************************** 00124 // Assign U2Tx To Pin RP4 00125 //*************************** 00126 RPOR2bits.RP4R = 5; 00127 00128 //************************************************************ 00129 // Lock Registers 00130 //************************************************************ 00131 asm volatile ( "mov #OSCCONL, w1 \n" 00132 "mov #0x45, w2 \n" 00133 "mov #0x57, w3 \n" 00134 "mov.b w2, [w1] \n" 00135 "mov.b w3, [w1] \n" 00136 "bset OSCCON, #6"); 00137 // *********************************** Peripheral PIN selection 00138 00139 /*-----------------------------------------------------------------------*/ 00140 /* Port A */ 00141 /*-----------------------------------------------------------------------*/ 00142 _TRISA4 = 0; 00143 /* 00144 RA4 12 LED1 00145 */ 00146 00147 /*-----------------------------------------------------------------------*/ 00148 /* Port B */ 00149 /*-----------------------------------------------------------------------*/ 00150 _TRISB2 = 0; 00151 _TRISB3 = 0; 00152 00153 /* 00154 RB2 6 H-bridge1 enable 00155 RB3 7 H-bridge2 enable 00156 */ 00157 #endif //} 00158 00159 #ifdef DROIDS //{ 00160 #warning -- compiling for 990.011 board ************************************** 00161 00162 // Peripheral PIN selection *********************************** 00163 // Unlock Registers 00164 //************************************************************* 00165 asm volatile ( "mov #OSCCONL, w1 \n" 00166 "mov #0x45, w2 \n" 00167 "mov #0x57, w3 \n" 00168 "mov.b w2, [w1] \n" 00169 "mov.b w3, [w1] \n" 00170 "bclr OSCCON, #6 "); 00171 //************************************************************ 00172 // Configure Input Functions 00173 //************************************************************ 00174 //*************************** 00175 // Assign IC1 To Pin RP10 00176 //*************************** 00177 RPINR7bits.IC1R = 10; 00178 00179 //*************************** 00180 // Assign IC2 To Pin RP6 00181 //*************************** 00182 RPINR7bits.IC2R = 6; 00183 00184 //*************************** 00185 // Assign QEA1 To Pin RP10 00186 //*************************** 00187 RPINR14bits.QEA1R = 10; 00188 00189 //*************************** 00190 // Assign QEB1 To Pin RP11 00191 //*************************** 00192 RPINR14bits.QEB1R = 11; 00193 00194 //*************************** 00195 // Assign QEA2 To Pin RP6 00196 //*************************** 00197 RPINR16bits.QEA2R = 6; 00198 00199 //*************************** 00200 // Assign QEB2 To Pin RP5 00201 //*************************** 00202 RPINR16bits.QEB2R = 5; 00203 00204 //*************************** 00205 // Assign U1RX To Pin RP3 00206 //*************************** 00207 RPINR18bits.U1RXR = 3; 00208 00209 //*************************** 00210 // Assign U2RX To Pin RP7 00211 //*************************** 00212 RPINR19bits.U2RXR = 7; 00213 00214 //************************************************************ 00215 // Configure Output Functions 00216 //************************************************************ 00217 00218 //*************************** 00219 // Assign U1Tx To Pin RP2 00220 //*************************** 00221 RPOR1bits.RP2R = 3; 00222 //*************************** 00223 // Assign U2Tx To Pin RP8 00224 //*************************** 00225 RPOR4bits.RP8R = 5; 00226 00227 //************************************************************ 00228 // Lock Registers 00229 //************************************************************ 00230 asm volatile ( "mov #OSCCONL, w1 \n" 00231 "mov #0x45, w2 \n" 00232 "mov #0x57, w3 \n" 00233 "mov.b w2, [w1] \n" 00234 "mov.b w3, [w1] \n" 00235 "bset OSCCON, #6"); 00236 // *********************************** Peripheral PIN selection 00237 00238 /*-----------------------------------------------------------------------*/ 00239 /* Port A */ 00240 /*-----------------------------------------------------------------------*/ 00241 _TRISA4 = 0; 00242 /* 00243 RA4 12 LED1 00244 */ 00245 00246 /*-----------------------------------------------------------------------*/ 00247 /* Port B */ 00248 /*-----------------------------------------------------------------------*/ 00249 _TRISB0 = 0; 00250 _TRISB1 = 0; 00251 _TRISB4 = 0; 00252 _TRISB9 = 0; 00253 00254 /* 00255 RB0 4 H-bridge2 enable 00256 RB1 5 H-bridge1 enable 00257 RB4 11 LED2 00258 RB9 18 DIR 485 00259 */ 00260 #endif //} 00261 //} 00262 00263 #elif defined(__dsPIC33FJ64MC804__) || defined(__dsPIC33FJ128MC804__) //{ 00264 #warning -- compiling for a 44 pin DSC *************************************** 00265 00266 00267 #ifdef DSNAVCON33 //{ 00268 #warning -- compiling for DSNAVCON33 ***************************************** 00269 // Peripheral PIN selection *********************************** 00270 // Unlock Registers 00271 //************************************************************* 00272 asm volatile ( "mov #OSCCONL, w1 \n" 00273 "mov #0x45, w2 \n" 00274 "mov #0x57, w3 \n" 00275 "mov.b w2, [w1] \n" 00276 "mov.b w3, [w1] \n" 00277 "bclr OSCCON, #6 "); 00278 //************************************************************ 00279 // Configure Input Functions 00280 //************************************************************ 00281 //*************************** 00282 // Assign IC1 To Pin RP23 00283 //*************************** 00284 RPINR7bits.IC1R = 23; 00285 00286 //*************************** 00287 // Assign IC2 To Pin RP25 00288 //*************************** 00289 RPINR7bits.IC2R = 25; 00290 00291 //*************************** 00292 // Assign QEA1 To Pin RP23 00293 //*************************** 00294 RPINR14bits.QEA1R = 23; 00295 00296 //*************************** 00297 // Assign QEB1 To Pin RP22 00298 //*************************** 00299 RPINR14bits.QEB1R = 22; 00300 00301 //*************************** 00302 // Assign QEA2 To Pin RP25 00303 //*************************** 00304 RPINR16bits.QEA2R = 25; 00305 00306 //*************************** 00307 // Assign QEB2 To Pin RP24 00308 //*************************** 00309 RPINR16bits.QEB2R = 24; 00310 00311 //*************************** 00312 // Assign U1RX To Pin RP5 00313 //*************************** 00314 RPINR18bits.U1RXR = 5; 00315 00316 //*************************** 00317 // Assign U2RX To Pin RP20 00318 //*************************** 00319 RPINR19bits.U2RXR = 20; 00320 00321 //************************************************************ 00322 // Configure Output Functions 00323 //************************************************************ 00324 00325 //*************************** 00326 // Assign U1Tx To Pin RP6 00327 //*************************** 00328 RPOR3bits.RP6R = 3; 00329 00330 //*************************** 00331 // Assign U2Tx To Pin RP21 00332 //*************************** 00333 RPOR10bits.RP21R = 5; 00334 00335 //************************************************************ 00336 // Lock Registers 00337 //************************************************************ 00338 asm volatile ( "mov #OSCCONL, w1 \n" 00339 "mov #0x45, w2 \n" 00340 "mov #0x57, w3 \n" 00341 "mov.b w2, [w1] \n" 00342 "mov.b w3, [w1] \n" 00343 "bset OSCCON, #6"); 00344 // *********************************** Peripheral PIN selection 00345 00346 /*-----------------------------------------------------------------------*/ 00347 /* Port A */ 00348 /*-----------------------------------------------------------------------*/ 00349 _TRISA8 = 0; 00350 /* 00351 RA8 32 LED1 00352 */ 00353 00354 /*-----------------------------------------------------------------------*/ 00355 /* Port B */ 00356 /*-----------------------------------------------------------------------*/ 00357 _TRISA7 = 0; 00358 _TRISA10 = 0; 00359 00360 /* 00361 RA7 13 H-bridge1 enable 00362 RA10 12 H-bridge2 enable 00363 */ 00364 #endif //} 00365 00366 #ifdef ROBOCONTROLLER //{ 00367 #warning -- compiling for ROBOCONTROLLER ************************************* 00368 // Peripheral PIN selection *********************************** 00369 // Unlock Registers 00370 //************************************************************* 00371 asm volatile ( "mov #OSCCONL, w1 \n" 00372 "mov #0x45, w2 \n" 00373 "mov #0x57, w3 \n" 00374 "mov.b w2, [w1] \n" 00375 "mov.b w3, [w1] \n" 00376 "bclr OSCCON, #6 "); 00377 //************************************************************ 00378 // Configure Input Functions 00379 //************************************************************ 00380 //*************************** 00381 // Assign IC1 To Pin RP22 00382 //*************************** 00383 RPINR7bits.IC1R = 22; // (Input Capture 1) QEA_1 su RoboController 00384 00385 //*************************** 00386 // Assign IC2 To Pin RP24 00387 //*************************** 00388 RPINR7bits.IC2R = 24; // (Input Capture 2) QEA_2 su RoboController 00389 00390 //*************************** 00391 // Assign QEA1 To Pin RP22 00392 //*************************** 00393 RPINR14bits.QEA1R = 22; // QEA_1 su RoboController 00394 00395 //*************************** 00396 // Assign QEB1 To Pin RP23 00397 //*************************** 00398 RPINR14bits.QEB1R = 23; // QEB_1 su RoboController 00399 00400 //*************************** 00401 // Assign QEA2 To Pin RP24 00402 //*************************** 00403 RPINR16bits.QEA2R = 24; // QEA_2 su RoboController 00404 00405 //*************************** 00406 // Assign QEB2 To Pin RP25 00407 //*************************** 00408 RPINR16bits.QEB2R = 25; // QEB_2 su RoboController 00409 00410 //*************************** 00411 // Assign U1RX To Pin RP8 00412 //*************************** 00413 RPINR18bits.U1RXR = 20; // (UART1 Receive) su RoboController 00414 00415 //*************************** 00416 // Assign U2RX To Pin RP20 00417 //*************************** 00418 RPINR19bits.U2RXR = 8; // (UART2 Receive) su RoboController 00419 00420 //************************************************************ 00421 // Configure Output Functions 00422 //************************************************************ 00423 00424 //*************************** 00425 // Assign U1Tx 00426 //*************************** 00427 RPOR10bits.RP21R = 3; 00428 00429 //*************************** 00430 // Assign U2Tx 00431 //*************************** 00432 RPOR4bits.RP8R = 5; 00433 00434 //************************************************************ 00435 // Lock Registers 00436 //************************************************************ 00437 asm volatile ( "mov #OSCCONL, w1 \n" 00438 "mov #0x45, w2 \n" 00439 "mov #0x57, w3 \n" 00440 "mov.b w2, [w1] \n" 00441 "mov.b w3, [w1] \n" 00442 "bset OSCCON, #6"); 00443 // *********************************** Peripheral PIN selection 00444 00445 /*---------------------------------------------------------------------------*/ 00446 /* Port A */ 00447 /*---------------------------------------------------------------------------*/ 00448 _TRISA0 = 0; // MOTOR_EN1 00449 _TRISA1 = 0; // MOTOR_EN2 00450 _TRISA8 = 0; // LED1 00451 _TRISA9 = 0; // LED2 00452 _TRISA7 = 0; // AUX1 00453 _TRISA10 = 0; // AUX2 00454 00455 /*---------------------------------------------------------------------------*/ 00456 /* Port B */ 00457 /*---------------------------------------------------------------------------*/ 00458 //_TRISB2 = 0; 00459 //_TRISB3 = 0; 00460 00461 00462 /*---------------------------------------------------------------------------*/ 00463 /* Port C */ 00464 /*---------------------------------------------------------------------------*/ 00465 _TRISC3 = 0; // DIR RS485 00466 _TRISC5 = 0; // TX UART1 00467 00468 #endif //} 00469 00470 #else 00471 00472 #error -- dsPIC33FJ not recognized. Accepted only 64/128MC802 or 64/128MC804 00473 00474 #endif //} 00475 #warning ********************************************************************* 00476 00477 /*---------------------------------------------------------------------------*/ 00478 /* A/D converter [2] */ 00479 /*---------------------------------------------------------------------------*/ 00480 AD1CON1bits.ADSIDL = 1; // stop in idle 00481 AD1CON1bits.ADDMABM = 0;// scatter/gather mode 00482 AD1CON1bits.AD12B = 0; // 10 bits ADC 00483 AD1CON1bits.FORM = 0; // integer format 00484 AD1CON1bits.SSRC = 0b111;// auto-convert 00485 AD1CON1bits.SIMSAM = 1; // CH0 CH1 sampled simultaneously 00486 AD1CON1bits.ASAM = 1; // auto sampling 00487 00488 AD1CON2bits.CSCNA = 0; // do not scan inputs 00489 AD1CON2bits.CHPS = 0b01;// convert CH0 and CH1 00490 AD1CON2bits.SMPI = 0b0001;// number of DMA buffers -1 00491 AD1CON2bits.BUFM = 0; // filling buffer from start address 00492 AD1CON2bits.ALTS = 0; // sample A 00493 00494 AD1CON3bits.ADRC = 0; // system clock 00495 AD1CON3bits.SAMC=0b11111;// 31 Tad auto sample time 00496 AD1CON3bits.ADCS = 63; // Tad = 64 * Tcy 00497 00498 AD1CON4bits.DMABL=0b110;// 64 word DMA buffer for each analog input 00499 00500 AD1CHS123bits.CH123NB=0;// don't care -> sample B 00501 AD1CHS123bits.CH123SB=0;// don't care -> sample B 00502 AD1CHS123bits.CH123NA=0;// CH1,2,3 negative input = Vrefl 00503 AD1CHS123bits.CH123SA=0;// CH1 = AN0, CH2=AN1, CH3=AN2 00504 00505 AD1CHS0bits.CH0NB = 0; // don't care -> sample B 00506 AD1CHS0bits.CH0SB = 0; // don't care -> sample B 00507 AD1CHS0bits.CH0NA = 0; // CH0 neg -> Vrefl 00508 AD1CHS0bits.CH0SA = 1; // CH0 pos -> AN1 00509 00510 AD1PCFGL = 0xFFFF; // set all Analog ports as digital 00511 AD1PCFGLbits.PCFG0 = 0; // AN0 00512 AD1PCFGLbits.PCFG1 = 0; // AN1 00513 00514 IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit 00515 IEC0bits.AD1IE = 0; // Do Not Enable A/D interrupt 00516 AD1CON1bits.ADON = 1; // module on 00517 00518 /*.........................................................DMA A/D converter */ 00519 00520 // ADC connected to DMA 7 00521 DMA7CONbits.AMODE = 2; // Peripheral Indirect Addressing mode 00522 DMA7CONbits.MODE = 0; // Continuous, 00523 DMA7PAD = (volatile unsigned int)&ADC1BUF0;// Point DMA to ADC1BUF0 00524 DMA7CNT = 127; // 64 DMA request 00525 DMA7REQ = 13; // Select ADC1 as DMA Request source 00526 DMA7STA = __builtin_dmaoffset(DmaAdc); 00527 _DMA7IF = 0; //Clear the DMA interrupt flag bit 00528 _DMA7IE = 1; //Set the DMA interrupt enable bit 00529 DMA7CONbits.CHEN=1; // Enable DMA 00530 00531 /*.............................................................A/D converter */ 00532 00533 /*---------------------------------------------------------------------------*/ 00534 /* PWM [11] */ 00535 /*---------------------------------------------------------------------------*/ 00536 // Holds the value to be loaded into dutycycle register 00537 unsigned int period; 00538 // Holds the value to be loaded into special event compare register 00539 unsigned int sptime; 00540 // Holds PWM configuration value 00541 unsigned int config1; 00542 // Holds the value be loaded into PWMCON1 register 00543 unsigned int config2; 00544 // Holds the value to config the special event trigger postscale and dutycycle 00545 unsigned int config3; 00546 00547 // Config PWM 00548 period = 2048; 00549 // PWM F=19,340Hz counting UP 12bit resolution @ Fcy=39.628 MHz (osc 7.3728MHz) 00550 // PWM F=19,522Hz counting UP 12bit resolution @ Fcy=39.628 MHz (osc 10MHz) 00551 sptime = 0x0; 00552 // 1:1 postscaler, 1:1 prescale, free running mode 00553 // PWM time base ON, count up 00554 config1 = PWM1_EN & PWM1_IDLE_CON & PWM1_OP_SCALE1 & PWM1_IPCLK_SCALE1 & 00555 PWM1_MOD_FREE; 00556 00557 // PWM1H e PWM1L enabled in complementar mode 00558 // dsPICs with 3 pairs of PWM pins have one timer only (A) 00559 config2 = PWM1_MOD1_COMP & PWM1_PEN1L & PWM1_PEN1H & 00560 PWM1_MOD2_COMP & PWM1_PEN2L & PWM1_PEN2H & 00561 PWM1_PDIS3H & PWM1_PDIS3L; 00562 00563 config3 = PWM1_SEVOPS1 & PWM1_OSYNC_PWM & PWM1_UEN; 00564 OpenMCPWM1(period, sptime, config1, config2, config3); 00565 00566 // Dead Time Unit A assigned to both 1 & 2 PWM pairs 00567 /* SetMCPWM1DeadTimeAssignment(PWM1_DTS1A_UA & PWM1_DTS1I_UA & 00568 PWM1_DTS2A_UA & PWM1_DTS2I_UA); 00569 */ 00570 P1DTCON2bits.DTS1A = 0; 00571 P1DTCON2bits.DTS1I = 0; 00572 P1DTCON2bits.DTS2A = 0; 00573 P1DTCON2bits.DTS2I = 0; 00574 00575 // Dead time 100ns = 0.2% of PWM period 00576 SetMCPWM1DeadTimeGeneration(PWM1_DTA4 & PWM1_DTAPS1); 00577 00578 // dutycyclereg=1, dutycycle=50% (motore fermo in LAP mode , updatedisable=0 00579 SetDCMCPWM1(1, 2048, 0); 00580 SetDCMCPWM1(2, 2048, 0); 00581 00582 // configure PWM2 pins as a generic I/O 00583 PWM2CON1bits.PEN1L = 0; 00584 PWM2CON1bits.PEN1H = 0; 00585 /*.......................................................................PWM */ 00586 00587 00588 /*---------------------------------------------------------------------------*/ 00589 /* QEI1 [4] */ 00590 /*---------------------------------------------------------------------------*/ 00591 /* 00592 OpenQEI(QEI_MODE_x4_MATCH & QEI_INPUTS_NOSWAP & QEI_IDLE_STOP 00593 & QEI_NORMAL_IO & QEI_INDEX_RESET_DISABLE, 00594 QEI_QE_CLK_DIVIDE_1_128 & QEI_QE_OUT_ENABLE & POS_CNT_ERR_INT_DISABLE); 00595 */ 00596 00597 QEI1CONbits.QEIM = 7; // QEI_MODE_x4_MATCH 00598 QEI1CONbits.SWPAB = 0; // QEI_INPUTS_SWAP 00599 QEI1CONbits.QEISIDL = 1; // QEI_IDLE_STOP 00600 QEI1CONbits.POSRES = 0; // QEI_INDEX_RESET_DISABLE 00601 QEI1CONbits.PCDOUT = 0; // QEI_NORMAL_IO 00602 QEI1CONbits.POSRES = 0; // POS_CNT_ERR_INT_DISABLE 00603 00604 DFLT1CONbits.QECK = 6; // QEI_QE_CLK_DIVIDE_1_128 00605 DFLT1CONbits.QEOUT = 1; // QEI_QE_OUT_ENABLE 00606 00607 MAX1CNT = 0xFFFF; 00608 POS1CNT = 0; 00609 /*.......................................................................QEI */ 00610 00611 /*---------------------------------------------------------------------------*/ 00612 /* QEI2 [4] */ 00613 /*---------------------------------------------------------------------------*/ 00614 /* 00615 OpenQEI(QEI_MODE_x4_MATCH & QEI_INPUTS_NOSWAP & QEI_IDLE_STOP 00616 & QEI_NORMAL_IO & QEI_INDEX_RESET_DISABLE, 00617 QEI_QE_CLK_DIVIDE_1_128 & QEI_QE_OUT_ENABLE & POS_CNT_ERR_INT_DISABLE); 00618 */ 00619 00620 QEI2CONbits.QEIM = 7; // QEI_MODE_x4_MATCH 00621 QEI2CONbits.SWPAB = 0; // QEI_INPUTS_SWAP 00622 QEI2CONbits.QEISIDL = 1; // QEI_IDLE_STOP 00623 QEI2CONbits.POSRES = 0; // QEI_INDEX_RESET_DISABLE 00624 QEI2CONbits.PCDOUT = 0; // QEI_NORMAL_IO 00625 QEI2CONbits.POSRES = 0; // POS_CNT_ERR_INT_DISABLE 00626 00627 DFLT2CONbits.QECK = 6; // QEI_QE_CLK_DIVIDE_1_128 00628 DFLT2CONbits.QEOUT = 1; // QEI_QE_OUT_ENABLE 00629 00630 MAX2CNT = 0xFFFF; 00631 POS2CNT = 0; 00632 /*.......................................................................QEI */ 00633 00634 /*---------------------------------------------------------------------------*/ 00635 /* Input Capture 1 [7] */ 00636 /*---------------------------------------------------------------------------*/ 00637 IC1CONbits.ICSIDL = 1; // Stop in idle 00638 IC1CONbits.ICTMR = 1; // Timer 2 00639 IC1CONbits.ICI = 0; // Interrupt on every capture event 00640 IC1CONbits.ICM = 3; // Capture mode every rising edge 00641 /*.............................................................Input Capture */ 00642 00643 /*---------------------------------------------------------------------------*/ 00644 /* Input Capture 2 [7] */ 00645 /*---------------------------------------------------------------------------*/ 00646 // IC_EVERY_EDGE & IC_INT_1CAPTURE & IC_IDLE_STOP & IC_TIMER2_SRC) 00647 IC2CONbits.ICSIDL = 1; // Stop in idle 00648 IC2CONbits.ICTMR = 1; // Timer 2 00649 IC2CONbits.ICI = 0; // Interrupt on every capture event 00650 IC2CONbits.ICM = 3; // Capture mode every rising edge 00651 /*.............................................................Input Capture */ 00652 00653 00654 00655 #ifndef TIMER_OFF 00656 /*---------------------------------------------------------------------------*/ 00657 /* Timer 2 [12] */ 00658 /*---------------------------------------------------------------------------*/ 00659 #define TMR2_VALUE 0xFFFF 00660 /*OpenTimer2( T2_ON & 00661 T2_GATE_OFF & 00662 T2_PS_1_1 & 00663 T1_SYNC_EXT_OFF & 00664 T2_SOURCE_INT, 00665 TMR2_VALUE); 00666 */ 00667 T2CONbits.TON = 0; // Disable Timer 00668 T2CONbits.TCS = 0; // Select internal instruction cycle clock 00669 T2CONbits.TGATE = 0; // Disable Gated Timer mode 00670 T2CONbits.TCKPS = 0b00; // Select 1:1 Prescaler 00671 TMR2 = 0x00; // Clear timer register 00672 PR2 = TMR2_VALUE; // Load the period value 00673 /*...................................................................Timer 2 */ 00674 00675 00676 /*---------------------------------------------------------------------------*/ 00677 /* Timer 1 1ms [13] */ 00678 /*---------------------------------------------------------------------------*/ 00679 #ifdef CLOCK_FREQ_10 00680 #define TMR1_VALUE 40000 00681 #else 00682 #define TMR1_VALUE 39628 00683 #endif 00684 00685 OpenTimer1( T1_ON & 00686 T1_GATE_OFF & 00687 T1_PS_1_1 & 00688 T1_SYNC_EXT_OFF & 00689 T1_SOURCE_INT, 00690 TMR1_VALUE); 00691 #endif 00692 /*................................................................ ..Timer 1 */ 00693 } 00694 00695 00696 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00697 /* Interrupts setting */ 00698 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 00699 void ISR_Settings(void) 00700 { 00701 //-------PWM [11] 00702 ConfigIntMCPWM1(PWM1_INT_DIS); 00703 00704 //-------Input Capture 1 [7] 00705 // ConfigIntCapture1(IC_INT_ON & IC_INT_PRIOR_4); 00706 IEC0bits.IC1IE=1; 00707 IPC0bits.IC1IP=4; 00708 00709 //-------Input Capture 2 [7] 00710 // ConfigIntCapture2(IC_INT_ON & IC_INT_PRIOR_5); 00711 IEC0bits.IC2IE=1; 00712 IPC1bits.IC2IP=4; 00713 00714 00715 #ifndef TIMER_OFF 00716 //-------Timer 2 [12] 00717 // ConfigIntTimer2(T2_INT_PRIOR_4 & T2_INT_ON); 00718 IPC1bits.T2IP = 0x04; // Set Timer 2 Interrupt Priority Level 00719 _T2IF = 0; // interrupt flag reset 00720 IEC0bits.T2IE = 1; // Enable Timer2 interrupt 00721 T2CONbits.TON = 1; // Start Timer 00722 00723 //-------Timer 1 [13] 00724 ConfigIntTimer1(T1_INT_PRIOR_4 & T1_INT_ON); 00725 PR1 = TMR1_VALUE; 00726 #endif 00727 } 00728