/**************************************************************************** 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 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); }
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(); }*/ }
/** * Backlight is open drain on pin D2, controlled by OC4, using Timer 3 * Contrast uses normal CMOS with OC3, also driven by Timer 3 */ void lcd_init(void) { //Set backlight to open drain mPORTDOpenDrainOpen( BIT_2 ); //Start the timer for contrast/brightness PWM OpenTimer3(T3_ON, LCD_PWMPERIOD); //Enable contrast control OpenOC3( OC_ON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x500 ); //Enable brightness control OpenOC4( OC_ON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x500 ); }
void setupPWMForIR() { // init OC3 module OpenOC3(OC_OFF | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // init Timer3 mode and period (PR2) 40 kHz freq: 1 / 40 kHz = (X + 1) / 80MHz * 1 //80 000 / 20 = X + 1 = 3999 //80 000 / 38 = X + 1 = 2104 //80 000 / 36 = X + 1 = 2221 //80 000 / 40 = X + 1 = 2001 OpenTimer3(T3_ON | T3_PS_1_1 | T3_SOURCE_INT, MAX_DUTY); SetDCOC3PWM(PR3 / 2); }
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); } } } }
static void lcd_setup(void) { //mPORTDSetPinsDigitalOut(BIT_3); //mPORTDClearBits(BIT_3); mPORTDSetPinsDigitalOut( BIT_2 ); //Set backlight to open drain mPORTDOpenDrainOpen( BIT_2 ); //Enable contrast control OpenTimer2(T2_ON, LCD_PWMPERIOD); OpenOC4( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x0458 ); //Start the timer for contrast/brightness PWM //OpenTimer3(T3_ON | T3_PS_1_1 | T3_SOURCE_INT, LCD_PWMPERIOD); //PR3 = LCD_PWMPERIOD - 1; //OC3R = 0; //mT3SetIntPriority(4); //mT3ClearIntFlag(); //mT3IntEnable(1); //SetDCOC3PWM(LCD_PWMPERIOD >> 2); //SetDCOC3PWM(1); //Enable brightness control OpenOC3( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x500 ); // Open ENABLE line as output LCD_EN_CLR(); PORT_DIR_OUT(LCD_EN_P, LCD_EN); LCD_EN_CLR(); // Open RW and RS lines as output LCD_RS_CLR(); PORT_CLR(LCD_RW_P, LCD_RW); PORT_DIR_OUT(LCD_RS_P, LCD_RS); PORT_DIR_OUT(LCD_RW_P, LCD_RW); LCD_RS_CLR(); PORT_CLR(LCD_RW_P, LCD_RW); // Set data lines as output PORT_CLR(LCD_DATA_P, LCD_DATA_MASK); PORT_DIR_OUT(LCD_DATA_P, LCD_DATA_MASK); PORT_CLR(LCD_DATA_P, LCD_DATA_MASK); // Wait for proper power up task_delay(LCD_LONG_DELAY); task_delay(LCD_LONG_DELAY); task_delay(LCD_LONG_DELAY); task_delay(LCD_LONG_DELAY); // Wait for the LCD to power up correctly lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS); task_delay(LCD_SHORT_DELAY); lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS); task_delay(LCD_VERY_SHORT_DELAY); lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS); task_delay(LCD_VERY_SHORT_DELAY); // Set up the LCD function lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES); task_delay(LCD_VERY_SHORT_DELAY); // Turn the display on lcd_command(LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_CURSOR_ON | LCD_DISPLAY_CTRL_BLINK_ON); task_delay(LCD_VERY_SHORT_DELAY); // Clear the display lcd_command(LCD_CLEAR_DISPLAY_CMD); task_delay(LCD_SHORT_DELAY); // Increase the cursor lcd_command(LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE); task_delay(LCD_SHORT_DELAY); lcd_command(LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON); task_delay(LCD_SHORT_DELAY); }