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; } }
// External functions void modem_hal_setup() { // digitalWrites (to turn on/off sensors, mainly) disrupt the timers // (see wiring_digital.c and look for 'digital_pin_to_timer' in // variants/Uno32/Board_Data.c). We need to make sure we don't use // digitalWrite on any pin that is related to timer 2. Note that // other timers are not an option since the only PWM capable timers // are 2 and 3 and 3 is already used for the buzzer. OpenTimer2(T2_ON | T2_PS_1_2, 0xFF); OpenOC1(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // The above could also be accomplished by... ? /* T2CONbits.ON = 0; // Turn timer 2 off before anything else (recommended in ref. guide 14.3.11) T2CON = T2_PS_1_8; // Set prescaler x8 TMR2 = 0; // Clear the counter PR2 = 0xFF; // Set the period IPC2bits.T2IP = 6; // Set T2 interrupt priority to 6 OCxCON = OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE; // Set OCx to use timer2 (OCxCON defined in .h) T2CONbits.ON = 1; // Turn the timer on OCxRS = REST_DUTY; // Set output PWM to rest duty */ // Use pin 43 on the Uno32 as PTT indicator pinMode(43, OUTPUT); }
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); }
// 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"); } }
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); }
int main ( void ) { // Initialize the processor and peripherals. if ( InitializeSystem() != TRUE ) { UART2PrintString( "\r\n\r\nCould not initialize USB Custom Demo App - system. Halting.\r\n\r\n" ); while (1); } if ( USBHostInit(0) == TRUE ) { UART2PrintString( "\r\n\r\n***** USB Custom Demo App Initialized *****\r\n\r\n" ); } else { UART2PrintString( "\r\n\r\nCould not initialize USB Custom Demo App - USB. Halting.\r\n\r\n" ); while (1); } btClientData.State = BT_STATE_IDLE; btClientData.Initialized = FALSE; mPORTAOutputConfig(0x3); mPORTAWrite(0x0); mPORTBOutputConfig(0x10); // OC 1 PPSOutput(PPS_RP4, PPS_OC1); //Enable Interrupt SetPriorityIntOC1(4); EnableIntOC1; OpenTimer2(T2_ON | T2_PS_1_8 ,0xffff); // OpenOC1(OC_IDLE_CON | OC_TIMER2_SRC | OC_PWM_EDGE_ALIGN ,OC_SYNC_TRIG_IN_TMR2,0,0); SetDCOC1PWM(0xc00,0); // Main Processing Loop while (1) { BTClientTasks(); // Maintain USB Host State USBHostTasks(); DelayMs(1); } return 0; } // main
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 }
/* * setupPWM */ int setupPWM(unsigned int fpb) { UINT32 pr2; unsigned int prescalar = 1; pr2 = (UINT32)((fpb/(100*prescalar)) - 1); /*PWM resolution [bits] = log2(peripheral bus frequency / (PWM frequency*prescalar) *e.g. fpb = 40MHz, PWM_FREQUENCY = 100Hz, prescalar = 1 ==> resolution = 18.6 bits *INFO: The lower PWM frequency, the highter the PWM resolution - BUT: The lower the *PWM frequency, the higher the ripple after the low-pass filter . */ OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE | OC_TIMER_MODE32, 0, 0); OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT | T2_32BIT_MODE_ON, pr2); SetDCOC1PWM(pr2/2); //50% duty cycle for starters return 0; }
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); }
void InitApp(void) { // activation de la priorité des interruptions _NSTDIS = 0; //Init des E/S _TRISA0 = 0; led = 0; //Init debug on UART, TX->RP8, RX->RP9 InitDebug(8,9); //Configuration du Output Compare 1 en mode PWM OpenOC1(OC_IDLE_CON & OC_TIMER2_SRC & OC_HIGH_LOW, 20, 20); _RP15R = 18; //OC1(18) sur RP1; //Configuration du Timer 2, période 20ms OpenTimer2(T2_ON & T2_GATE_OFF & T2_PS_1_256 & T2_32BIT_MODE_OFF & T2_SOURCE_INT, 3125); ConfigIntTimer2(T2_INT_ON & T2_INT_PRIOR_4); }
/** * @fn void pwmInit( void ) * @brief Configuration de l'Output Compare 1 */ void pwmInit( void ){ OpenOC1( OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); }
//********************************************************************* //* 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); } } } }
void servo_init(void){ OpenTimer3(T3_ON | T3_PS_1_16, T3_VALUE); OpenOC1(OC_ON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_CONTINUE_PULSE, pulse_stop, pulse_start); }