/** Enable a stepper motor. @param stepper Which stepper (0 or 1). \b Example \code // enable stepper 1 stepperEnable(1); \endcode */ void stepperEnable(int stepper) { fasttimerInit(2); Stepper* s = &steppers[stepper]; pwmEnableChannel(stepper * 2); pwmEnableChannel(stepper * 2 + 1); // Fire the PWM's up s->duty = 1023; int pwm = stepper * 2; pwmSetDuty(pwm, s->duty); pwmSetDuty(pwm + 1, s->duty); // Get IO's int i; for (i = 0; i < 4; i++) { s->pins[i] = stepperGetIo(stepper, i); pinSetMode(s->pins[i], OUTPUT); pinOn(s->pins[i]); } s->position = 0; s->destination = 0; s->speed = STEPPER_DEFAULT_SPEED; s->timerRunning = 0; s->halfStep = false; s->bipolar = true; s->fastTimer.handler = stepperIRQCallback; s->fastTimer.id = stepper; fasttimerStart(&s->fastTimer, s->speed * 1000, true); }
/** Set the duty - from 0 to 1023. The default is for 100% power (1023). @param stepper Which stepper (0 or 1). @param duty An integer specifying the stepper duty (0 - 1023). @return status (0 = OK). \b Example \code // set stepper 0 to half power stepperSetDuty(0, 512); \endcode */ int stepperSetDuty(int stepper, int duty) { Stepper* s = &steppers[stepper]; s->duty = duty; // Fire the PWM's up int pwm = stepper * 2; pwmSetDuty(pwm, duty); pwmSetDuty(pwm + 1, duty); return CONTROLLER_OK; }
// Function that sets the brightness of the // backlight LEDs void setBacklight(unsigned int intensity) { if(intensity > 100) intensity = 100; pwmSetDuty(hetRAM1, pwm0, 100-intensity); }
void ColourModeManual ( void ) { ui16 pwmFreq; volatile float freqScale; volatile float tempFloat; pwmFreq = pwmGetFreq() -1; freqScale = pwmFreq; freqScale /= 255; tempFloat = pwmFreq - (ColourSettings.topRed * freqScale); pwmSetDuty((ui16)(tempFloat), COLOUR_RED_PWM_CH); tempFloat = pwmFreq - (ColourSettings.topGreen * freqScale); pwmSetDuty((ui16)(tempFloat), COLOUR_GREEN_PWM_CH); tempFloat = pwmFreq - (ColourSettings.topBlue * freqScale); pwmSetDuty((ui16)(tempFloat), COLOUR_BLUE_PWM_CH); }
// mode - change to supplied mode, see COLOUR_MODES enum // colourOne,colourTwo - bits0-7 = Blue, bits 8-15 = Green, bits 16-23 = Red 0xRRGGBB // mutiple of the 10ms tick rate, used to delay the next adjustment void ColourModeSet( ui8 mode, ui32 colourOne, ui32 colourTwo, ui16 noOfSteps, ui8 ticksPerStep ) { ui16 pwmFreq; pwmFreq = pwmGetFreq() -1; // Bounce - alternate between colourOne and Two // Rotate, ignore colourOne + two, use timing to set the rotation rate // Flash - off to ColourOne 50% of the timing value // off if ( ColourSettings.colourMode != mode ) { // Mode Change! ColourSettings.colourMode = mode; // clear down the current set dutys pwmSetDuty(pwmFreq, 0x07); } ColourSettings.maxSteps = noOfSteps; ColourSettings.ticksPerStep = ticksPerStep; ColourSettings.topRed = 0x000000FF & (colourOne >> 16); ColourSettings.topGreen = 0x000000FF & (colourOne >> 8); ColourSettings.topBlue = 0x000000FF & colourOne; ColourSettings.bottomRed = 0x000000FF & (colourTwo >> 16); ColourSettings.bottomGreen = 0x000000FF & (colourTwo >> 8); ColourSettings.bottomBlue = 0x000000FF & colourTwo; if (ColourSettings.colourMode == COLOUR_MODE_MANUAL) { ColourModeManual(); } }
// ################ Main Program ################ // int main(void) { //* --------------- Init --------------- *// uOpioidInit(); // Init the Clock //outputPIN(REFOCLK); //REFOCONbits.RODIV = 4; //Div by 8 //REFOCONbits.ROSEL = 1; //PBCLK outputed //REFOCONbits.OE = 1; //enbale ouput //REFOCONbits.ON = 1; //enable REFOCLK //* ------------------------------------ *// //* ----------- Testing Space ---------- *// timerInit(TIMER_2,0); ocSetConfig(OC_1,OC_MODE_PWM|OC_TIMER_2); ocSetConfig(OC_2,OC_MODE_PWM|OC_TIMER_2); ocSetConfig(OC_3,OC_MODE_PWM|OC_TIMER_2); pwmSetPeriod(OC_1,1000); pwmSetPeriod(OC_2,1000); pwmSetPeriod(OC_3,1000); ocStart(OC_1); ocStart(OC_2); ocStart(OC_3); //SPI test outputPIN(COM0_IO2); nrfInit(COM0_SPI_ID, &LATA, COM0_IO2); COM0ControlTransaction.pSlave = pCOM0SlaveControl; COM0ControlTransaction.control.all = 0; COM0ControlTransaction.transferNb = 10; COM0ControlTransaction.txNbRemaining = 10; COM0ControlTransaction.rxNbDone = 0; COM0ControlTransaction.pTX = sourceArray; COM0ControlTransaction.pRX = checkArray; spiStartTransaction(&COM0ControlTransaction); //testBufCtlPtr = rBufCreate(10, sizeof(U8)); //if (uartInit(0,UART_TX_INT_TSR_EMPTY|UART_RX_INT_DATA_READY|UART_MODE_8N1,1250000) == STD_EC_SUCCESS) // setPIN(LED_B); //* ------------------------------------ *// //* ----------- Loop forever ----------- *// for (;;) { if ((sysTick - ledSTRed) >= LED_R_SYSTICK_INTERVAL) { // -- Handle boundary -- // if (ledRed == 255) ledRedDir = 0; else if (ledRed == 0) ledRedDir = 1; // --------------------- // // -- Duty mouvement -- // if (ledRedDir) ledRed++; else ledRed--; // -------------------- // pwmSetDuty(OC_1,ledRed,0xFF); ledSTRed = sysTick; } if ((sysTick - ledSTGreen) >= LED_G_SYSTICK_INTERVAL) { // -- Handle boundary -- // if (ledGreen == 255) ledGreenDir = 0; else if (ledGreen == 0) ledGreenDir = 1; // --------------------- // // -- Duty mouvement -- // if (ledGreenDir) ledGreen++; else ledGreen--; // -------------------- // pwmSetDuty(OC_2,ledGreen,0xFF); ledSTGreen = sysTick; } if ((sysTick - ledSTBlue) >= LED_B_SYSTICK_INTERVAL) { // -- Handle boundary -- // if (ledBlue == 255) ledBlueDir = 0; else if (ledBlue == 0) ledBlueDir = 1; // --------------------- // // -- Duty mouvement -- // if (ledBlueDir) ledBlue++; else ledBlue--; // -------------------- // pwmSetDuty(OC_3,ledBlue,0xFF); ledSTBlue = sysTick; } /* switch(ledID) { case 0: setPIN(LED_R); setPIN(LED_G); setPIN(LED_B); break; case 1: clearPIN(LED_R); setPIN(LED_G); setPIN(LED_B); break; case 2: setPIN(LED_R); clearPIN(LED_G); setPIN(LED_B); break; case 3: setPIN(LED_R); setPIN(LED_G); clearPIN(LED_B); break; case 4: clearPIN(LED_R); clearPIN(LED_G); setPIN(LED_B); break; case 5: clearPIN(LED_R); setPIN(LED_G); clearPIN(LED_B); break; case 6: setPIN(LED_R); clearPIN(LED_G); clearPIN(LED_B); break; case 7: clearPIN(LED_R); clearPIN(LED_G); clearPIN(LED_B); break; default: ledID = 0; } */ //uartSendByte(0,0x55); } //* ------------------------------------ *// }
ui8 ColourModeBounce ( void ) { static ui8 goingUp = 1; static ui16 stepCnt; volatile float red = 0; volatile float green = 0; volatile float blue = 0; volatile ui16 temp; volatile float freqScale; volatile ui16 pwmFreq; bool result = 0; if (goingUp) { stepCnt ++; if ( stepCnt >= ColourSettings.maxSteps ) { goingUp = 0; stepCnt = ColourSettings.maxSteps; // let a calling function know we have completed a cycle result = 2; } } else { // if a change occurs to the max steps while counting down, bring the counts back into range if ( stepCnt >= ColourSettings.maxSteps ) { stepCnt = ColourSettings.maxSteps; } stepCnt --; if ( 0 == stepCnt ) { goingUp = 1; // let a calling function know we have completed a cycle result = 1; } } pwmFreq = pwmGetFreq() -1; freqScale = pwmFreq; freqScale /= 255; // RED ------------------------------------------- // Work out the direction of travel if (ColourSettings.topRed > ColourSettings.bottomRed) { // work counts per step in our range, then multiple it by the current step red = (float)((float)(ColourSettings.topRed - ColourSettings.bottomRed) / (float)ColourSettings.maxSteps ) * stepCnt; red += ColourSettings.bottomRed; red *= freqScale; temp = pwmFreq - red; pwmSetDuty(temp, COLOUR_RED_PWM_CH); } else { // work counts per step in our range, then multiple it by the current step red = (float)((float)(ColourSettings.bottomRed - ColourSettings.topRed) / (float)ColourSettings.maxSteps ) * stepCnt; red = ColourSettings.bottomRed - red; red *= freqScale; temp = pwmFreq - red; pwmSetDuty(temp, COLOUR_RED_PWM_CH); } // RED ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // Green ------------------------------------------- // Work out the direction of travel if (ColourSettings.topGreen > ColourSettings.bottomGreen) { // work counts per step in our range, then multiple it by the current step green = (float)((float)(ColourSettings.topGreen - ColourSettings.bottomGreen) / (float)ColourSettings.maxSteps ) * stepCnt; green += ColourSettings.bottomGreen; green *= freqScale; temp = pwmFreq - green; pwmSetDuty(temp, COLOUR_GREEN_PWM_CH); } else { // work counts per step in our range, then multiple it by the current step green = (float)((float)(ColourSettings.bottomGreen - ColourSettings.topGreen) / (float)ColourSettings.maxSteps ) * stepCnt; green = ColourSettings.bottomGreen - green; green *= freqScale; temp = pwmFreq - green; pwmSetDuty(temp, COLOUR_GREEN_PWM_CH); } // Blue ------------------------------------------- // Work out the direction of travel if (ColourSettings.topBlue > ColourSettings.bottomBlue) { // work counts per step in our range, then multiple it by the current step blue = (float)((float)(ColourSettings.topBlue - ColourSettings.bottomBlue) / (float)ColourSettings.maxSteps ) * stepCnt; blue += ColourSettings.bottomBlue; blue *= freqScale; temp = pwmFreq - blue; pwmSetDuty(temp, COLOUR_BLUE_PWM_CH); } else { // work counts per step in our range, then multiple it by the current step blue = (float)((float)(ColourSettings.bottomBlue - ColourSettings.topBlue) / (float)ColourSettings.maxSteps ) * stepCnt; blue = ColourSettings.bottomBlue - blue; blue *= freqScale; temp = pwmFreq - blue; pwmSetDuty(temp, COLOUR_BLUE_PWM_CH); } return( result ); }
void ColourModeCycle ( void ) { static ui16 red = 0; static ui16 green = 0; static ui16 blue = 0; static ui8 mixState = 0; static ui8 redDescending = 0; static ui8 greenDescending = 0; static ui8 blueDescending = 0; ui16 pwmFreq = pwmGetFreq() -1; volatile float freqScale; volatile float tempFloat; freqScale = pwmFreq; freqScale /= ColourSettings.maxSteps; switch(mixState) { case RGMIX: blue = 0; if(green < ColourSettings.maxSteps) { green++; redDescending = false; } else { redDescending = true; } if(redDescending) { if ( red > ColourSettings.maxSteps) red = ColourSettings.maxSteps; if (red) { red--; } if(!red) { redDescending = false; mixState = GBMIX; } } break; case GBMIX: red = 0; if(blue < ColourSettings.maxSteps) { blue++; greenDescending = false; } else { greenDescending = true; } if(greenDescending) { if ( green > ColourSettings.maxSteps) green = ColourSettings.maxSteps; if (green) { green--; } if(!green) { greenDescending = false; mixState = BRMIX; } } break; case BRMIX: green = 0; if(red < ColourSettings.maxSteps) { red++; blueDescending = false; } else { blueDescending = true; } if(blueDescending) { if ( blue > ColourSettings.maxSteps) blue = ColourSettings.maxSteps; if (blue) { blue--; } if(!blue) { blueDescending = false; mixState = RGMIX; } } break; } tempFloat = pwmFreq - (red * freqScale); pwmSetDuty((ui16)(tempFloat), COLOUR_RED_PWM_CH); tempFloat = pwmFreq - (green * freqScale); pwmSetDuty((ui16)(tempFloat), COLOUR_GREEN_PWM_CH); tempFloat = pwmFreq - (blue * freqScale); pwmSetDuty((ui16)(tempFloat), COLOUR_BLUE_PWM_CH); // Update the Colours by first finding the ratio of step counts to 255 tempFloat = ColourSettings.maxSteps / 255; ColourSettings.topRed = red * tempFloat; ColourSettings.topGreen = green * tempFloat; ColourSettings.topBlue = blue * tempFloat; }
/* * Changes the duty cycle for all of the LED PWM pins * period is the current length of the entire PWM period * duty is the length of the new duty cycle and should be 0-100 */ void pwmSetDutyAllLeds(uint16 duty) { pwmSetDuty(RED_LED, duty); pwmSetDuty(GREEN_LED, duty); pwmSetDuty(BLUE_LED, duty); }