void L6470::run(byte dir, float spd) { // RUN sets the motor spinning in a direction (defined by the constants // FWD and REV). Maximum speed and minimum speed are defined // by the MAX_SPEED and MIN_SPEED registers; exceeding the FS_SPD value // will switch the device into full-step mode. // The SpdCalc() function is provided to convert steps/s values into // appropriate integer values for this function. unsigned long speedVal = SpdCalc(spd); Xfer(RUN | dir); if (speedVal > 0xFFFFF) speedVal = 0xFFFFF; Xfer((byte)(speedVal >> 16)); Xfer((byte)(speedVal >> 8)); Xfer((byte)(speedVal)); }
void run( int speed, int dir){ // First things first: let's check communications. The CONFIG register should // power up to 0x2E88, so we can use that to check the communications. // On the test jig, this causes an LED to light up. if (dSPIN_GetParam(dSPIN_CONFIG) == 0x2E88) printf("Configuration successful!"); // The following function calls are for this demo application- you will // need to adjust them for your particular application, and you may need // to configure additional registers. // First, let's set the step mode register: // - dSPIN_SYNC_EN controls whether the BUSY/SYNC pin reflects the step // frequency or the BUSY status of the chip. We want it to be the BUSY // status. // - dSPIN_STEP_SEL_x is the microstepping rate- we'll go full step. // - dSPIN_SYNC_SEL_x is the ratio of (micro)steps to toggles on the // BUSY/SYNC pin (when that pin is used for SYNC). Make it 1:1, despite // not using that pin. dSPIN_SetParam(dSPIN_STEP_MODE, !dSPIN_SYNC_EN | dSPIN_STEP_SEL_1 | dSPIN_SYNC_SEL_1); // Configure the MAX_SPEED register- this is the maximum number of (micro)steps per // second allowed. You'll want to mess around with your desired application to see // how far you can push it before the motor starts to slip. The ACTUAL parameter // passed to this function is in steps/tick; MaxSpdCalc() will convert a number of // steps/s into an appropriate value for this function. Note that for any move or // goto type function where no speed is specified, this value will be used. dSPIN_SetParam(dSPIN_MAX_SPEED, MaxSpdCalc(290)); // Configure the FS_SPD register- this is the speed at which the driver ceases // microstepping and goes to full stepping. FSCalc() converts a value in steps/s // to a value suitable for this register; to disable full-step switching, you // can pass 0x3FF to this register. dSPIN_SetParam(dSPIN_FS_SPD, FSCalc(150)); // Configure the acceleration rate, in steps/tick/tick. There is also a DEC register; // both of them have a function (AccCalc() and DecCalc() respectively) that convert // from steps/s/s into the appropriate value for the register. Writing ACC to 0xfff // sets the acceleration and deceleration to 'infinite' (or as near as the driver can // manage). If ACC is set to 0xfff, DEC is ignored. To get infinite deceleration // without infinite acceleration, only hard stop will work. dSPIN_SetParam(dSPIN_ACC, 0x040); // Configure the overcurrent detection threshold. The constants for this are defined // in the dSPIN.h file. // 3000mA is somewhere a bit above the rated capacity w/o heatsinking. dSPIN_SetParam(dSPIN_OCD_TH, dSPIN_OCD_TH_1875mA); // Set up the CONFIG register as follows: // PWM frequency divisor = 1 // PWM frequency multiplier = 2 (62.5kHz PWM frequency) // Slew rate is 290V/us // Do NOT shut down bridges on overcurrent // Disable motor voltage compensation // Hard stop on switch low // 16MHz internal oscillator, nothing on output dSPIN_SetParam(dSPIN_CONFIG, dSPIN_CONFIG_PWM_DIV_1 | dSPIN_CONFIG_PWM_MUL_2 | dSPIN_CONFIG_SR_290V_us | dSPIN_CONFIG_OC_SD_ENABLE | dSPIN_CONFIG_VS_COMP_DISABLE | dSPIN_CONFIG_SW_USER | dSPIN_CONFIG_INT_16MHZ); // Configure the RUN KVAL. This defines the duty cycle of the PWM of the bridges // during running. 0xFF means that they are essentially NOT PWMed during run; this // MAY result in more power being dissipated than you actually need for the task. // Setting this value too low may result in failure to turn. // There are ACC, DEC, and HOLD KVAL registers as well; you may need to play with // those values to get acceptable performance for a given application. dSPIN_SetParam(dSPIN_KVAL_RUN, 0xaF); // Calling GetStatus() clears the UVLO bit in the status register, which is set by // default on power-up. The driver may not run without that bit cleared by this // read operation. printf("Status code is: %x\n" "It shouldn't be 0, but this is normal at the moment.\n", dSPIN_GetStatus()); while (digitalRead(dSPIN_BUSYN) == LOW); delay(500); dSPIN_Run(dir, SpdCalc(speed)); }