void motorSetup(void){ //Setup output compare CloseOC1(); CloseOC2(); //Configure output compare using Timer 3 ConfigIntOC1(OC_INT_OFF & OC_INT_PRIOR_5); ConfigIntOC2(OC_INT_OFF & OC_INT_PRIOR_5); PR3 = MOTOR_PWM_PERIOD; T3CONbits.TON = 1; T3CONbits.TSIDL = 1; T3CONbits.TGATE = 0; T3CONbits.TCKPS = 0b10; //prescaling 1:64 T3CONbits.TCS = 0; OpenOC1(OC_IDLE_CON & OC_TIMER3_SRC & OC_PWM_FAULT_PIN_DISABLE , 0x00,0x00 ); OpenOC2(OC_IDLE_CON & OC_TIMER3_SRC & OC_PWM_FAULT_PIN_DISABLE , 0x00,0x00 ); //Write decent values to the control values motorSet(0.0,0.0, MOTOR_MODE_COAST); }
void setupPWM(int outputControlX) { switch(outputControlX) { case 1: OpenOC1(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // PWM output on Pin D0, 0 duty cycle break; case 2: OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // PWM output on Pin D0, 0 duty cycle break; case 3: OpenOC3(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // PWM output on Pin D0, 0 duty cycle break; case 4: OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // PWM output on Pin D0, 0 duty cycle break; case 5: OpenOC5(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // PWM output on Pin D0, 0 duty cycle break; default: OpenOC1(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // PWM output on Pin D0, 0 duty cycle break; } }
// setup timers and initialize motors to off void MotorInit(void) { int i; M1_FORWARD_TRIS = OUTPUT_PIN; M1_FORWARD_IO = 0; M1_BACKWARD_TRIS = OUTPUT_PIN; M1_BACKWARD_IO = 0; M2_FORWARD_TRIS = OUTPUT_PIN; M2_FORWARD_IO = 0; M2_BACKWARD_TRIS = OUTPUT_PIN; M2_BACKWARD_IO = 0; M3_FORWARD_TRIS = OUTPUT_PIN; M3_FORWARD_IO = 0; M3_BACKWARD_TRIS = OUTPUT_PIN; M3_BACKWARD_IO = 0; M4_FORWARD_TRIS = OUTPUT_PIN; M4_FORWARD_IO = 0; M4_BACKWARD_TRIS = OUTPUT_PIN; M4_BACKWARD_IO = 0; OpenOC1(OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE,2560,2560); OpenOC2(OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE,2560,2560); OpenOC3(OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE,2560,2560); //OpenOC4(OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE,2560,2560); OpenTimer3(T3_ON | T3_PS_1_1, MOTOR_TIMER_PERIOD); }
/**************************************************************************** Function PWM_init Parameters Channels, used #defined PWM_PORTxxx OR'd together for each PWM Channel Period, An integer representing the frequency in hertz Returns SUCCESS or ERROR Description Initializes the OC channels into PWM mode and sets up the channels at frequncy given Notes None. Author Max Dunne, 2011.11.12 ****************************************************************************/ char PWM_init(unsigned char Channels, unsigned int Period) { if ((Channels < 1) || (Channels > 0x1F) || (usedChannels != 0)) return ERROR; if (Period <= 1000) { OpenTimer2(T2_ON | T2_PS_1_32, F_PB / 32 / Period); dbprintf("Period less than 1KHz, setting prescaler to 32"); } else { OpenTimer2(T2_ON | T2_PS_1_1, F_PB / Period); dbprintf("Period greater than 1KHz, setting prescaler to 1"); } usedChannels = Channels; if (PWM_PORTZ06 & Channels) { OpenOC1(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); dbprintf("Port Z6 Initialized\r\n"); } if (PWM_PORTY12 & Channels) { OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); dbprintf("Port Y12 Initialized\r\n"); } if (PWM_PORTY10 & Channels) { OpenOC3(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); dbprintf("Port Y10 Initialized\r\n"); } if (PWM_PORTY04 & Channels) { OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); dbprintf("Port Y4 Initialized\r\n"); } if (PWM_PORTX11 & Channels) { OpenOC5(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); dbprintf("Port X11 Initialized\r\n"); } }
void PmodHB5_INIT(UART_MODULE uartID) { PORTSetPinsDigitalOut(IOPORT_D, BIT_7); //HB5 Direction PORTSetPinsDigitalOut(IOPORT_D, BIT_1); //HB5 Enable PORTSetPinsDigitalIn(IOPORT_D, BIT_9); //HB5 Sensor A PORTSetPinsDigitalIn(IOPORT_C, BIT_1); //HB5 Sensor B hBridge.sensorAport = IOPORT_D; hBridge.sensorAportBit = BIT_9; hBridge.sensorBport = IOPORT_C; hBridge.sensorBportBit = BIT_1; hBridge.directionPort = IOPORT_D; hBridge.directionPortBit = BIT_7; hBridge.currentDirection = PMOD_HB5_DIR_CW; hBridge.newDirection = PMOD_HB5_DIR_CW; hBridge.ocChannel = 2; OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); OpenTimer2(T2_ON | T2_PS_1_256, SYSTEM_CLOCK/PB_DIV/PRESCALE/(TOGGLES_PER_SEC/2)); OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK); ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); INTEnableSystemMultiVectoredInt(); UARTPutS("\r\nPmodHB5 init complete\r\n",uartID); }
int32_t main(void) { DDPCONbits.JTAGEN = 0; // Disable the JTAG programming port /* SYS_CFG_WAIT_STATES (configures flash wait states from system clock) SYS_CFG_PB_BUS (configures the PB bus from the system clock) SYS_CFG_PCACHE (configures the pCache if used) SYS_CFG_ALL (configures the flash wait states, PB bus, and pCache)*/ /* TODO Add user clock/system configuration code if appropriate. */ SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); /*Configure Multivector Interrupt Mode. Using Single Vector Mode is expensive from a timing perspective, so most applications should probably not use a Single Vector Mode*/ INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); /* TODO <INSERT USER APPLICATION CODE HERE> */ // initialise the IO pins and PWM outputs TRISD = 0; pickerBusTRIS = 0x00; pickerBus = 0x00; OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // vibration motor OpenOC2( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // head led OpenOC3( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // base led OpenTimer2( T2_ON | T2_PS_1_4 | T2_SOURCE_INT, 1024); SetDCOC1PWM(0); SetDCOC2PWM(0); SetDCOC3PWM(0); setVac1off; setVac2off; USBOutHandle = 0; USBInHandle = 0; USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware USBDeviceAttach(); init_component_picker(); /*while(1) { LATBbits.LATB0 = feederXHome; LATBbits.LATB1 = feederZHome; //ProcessIO(); }*/ }
void initPwmForMotor() { OpenOC1( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE , 0,0 ); OpenOC2( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE , 0,0 ); OpenTimer2(T2_ON|T2_PS_1_64 | T2_SOURCE_INT, PWM_TIMER_FOR_MOTOR); OC1RS = (0x0000); // 0 % duty cycle OC2RS = (0x0000); // 0 % duty cycle }
void PWMinit() { // init OC4 & OC5 module OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); OpenOC2( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // init Timer2 mode and period (PR2) (frequency of 1 / 20 kHz = (3999 + 1) / 80MHz * 1 OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 3999); ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2); }
void PWM_Init(void) { #ifdef _TARGET_440H #else // Tmr1 Init // STEP 1. configure the Timer1 OpenTimer3(T3_ON | T3_SOURCE_INT | T3_IDLE_CON | T3_PS_1_1, TMR3_RELOAD); // Enable OC | 16 bit Mode | Timer1 is selected | Continuous O/P | OC Pin High , S Compare value, Compare value OpenOC1( OC_ON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE , 0, 0 ); OpenOC2( OC_ON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE , 0, 0 ); #endif }
void Initialize_OC() { UnlockRP; _RP10R = 0b10010; //Tie OC1 to RP10 (PWM1) _RP11R = 0b10011; //Tie OC2 to RP11 (PWM2) _RP12R = 0b10100; //Tie OC3 to RP12 (PWM3) _RP13R = 0b10101; //Tie OC4 to RP13 (PWM4) LockRP; OpenOC1(OC_IDLE_CON & OC_TIMER3_SRC & OC_OFF, 1000, 0); OpenOC2(OC_IDLE_CON & OC_TIMER3_SRC & OC_OFF, 1000, 0); OpenOC3(OC_IDLE_CON & OC_TIMER3_SRC & OC_OFF, 1000, 0); OpenOC4(OC_IDLE_CON & OC_TIMER3_SRC & OC_OFF, 1000, 0); //dans l'ordre OCxRS et OCxR ConfigIntOC1(OC_INT_OFF & OC_INT_PRIOR_2); ConfigIntOC2(OC_INT_OFF & OC_INT_PRIOR_2); ConfigIntOC3(OC_INT_OFF & OC_INT_PRIOR_2); ConfigIntOC4(OC_INT_OFF & OC_INT_PRIOR_2); }
//********************************************************************* //* PWM output only works on the pins with hardware support. //* These are defined in the appropriate pins_*.c file. //* For the rest of the pins, we default to digital output. //********************************************************************* void analogWrite(uint8_t pin, int val) { // We need to make sure the PWM output is enabled for those pins // that support it, as we turn it off when digitally reading or // writing with them. Also, make sure the pin is in output mode // for consistenty with Wiring, which doesn't require a pinMode // call for the analog output pins. pinMode(pin, OUTPUT); if (val == 0) { digitalWrite(pin, LOW); } else if (val == 255) { digitalWrite(pin, HIGH); } else { switch(digitalPinToTimer(pin)) { #ifdef _OCMP1 case TIMER_OC1: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256 ); //Set duty cycle on fly SetDCOC1PWM((PWM_TIMER_PERIOD*val)/256); break; #endif #ifdef _OCMP2 case TIMER_OC2: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC2( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256 ); //Set duty cycle on fly SetDCOC2PWM((PWM_TIMER_PERIOD*val)/256); break; #endif #ifdef _OCMP3 case TIMER_OC3: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC3( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256 ); //Set duty cycle on fly SetDCOC3PWM((PWM_TIMER_PERIOD*val)/256); break; #endif #ifdef _OCMP4 case TIMER_OC4: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC4( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256 ); //Set duty cycle on fly SetDCOC4PWM((PWM_TIMER_PERIOD*val)/256); break; #endif #ifdef _OCMP5 case TIMER_OC5: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC5( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256 ); //Set duty cycle on fly SetDCOC5PWM((PWM_TIMER_PERIOD*val)/256); break; #endif #if 0 //* this is the original code, I want to keep it around for refernce for a bit longer #ifdef _OCMP1 case TIMER_OC1: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC1( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) ); // SetDCOC1PWM((PWM_TIMER_PERIOD * val) / 256); break; #endif #ifdef _OCMP2 case TIMER_OC2: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC2( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) ); // SetDCOC2PWM((PWM_TIMER_PERIOD * val) / 256); break; #endif #ifdef _OCMP3 case TIMER_OC3: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC3( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) ); // SetDCOC3PWM((PWM_TIMER_PERIOD * val) / 256); break; #endif #ifdef _OCMP4 case TIMER_OC4: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC4( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) ); // SetDCOC4PWM((PWM_TIMER_PERIOD * val) / 256); break; #endif #ifdef _OCMP5 case TIMER_OC5: //* Open Timer2 with Period register value OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD); OpenOC5( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) ); // SetDCOC5PWM((PWM_TIMER_PERIOD * val) / 256); break; #endif #endif case NOT_ON_TIMER: default: if (val < 128) { digitalWrite(pin, LOW); } else { digitalWrite(pin, HIGH); } } } }