#define FCY 8000000UL
#include <P24FJ64GB002.h>
#include <libpic30.h>
_CONFIG1( FWDTEN_OFF & ICS_PGx1 & GWRP_OFF & GCP_OFF & GWRP_OFF & JTAGEN_OFF)
_CONFIG2( POSCMOD_NONE & I2C1SEL_PRI & IOL1WAY_ON & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_FRC & PLL96MHZ_OFF & IESO_OFF)
_CONFIG3(SOSCSEL_IO)
//Fast Internal RC (FRC) Oscillator 8MHz Clock Source
//Defines
#define sleep LATAbits.LATA0
#define ena LATAbits.LATA1
//#define pirsens PORTAbits.RA2
#define rst LATAbits.LATA3
#define sr LATAbits.LATA4
//#define sense1 PORTBbits.RB2
//#define sense2 PORTBbits.RB3
#define step LATBbits.LATB4
#define ms1 LATBbits.LATB8
#define ms2 LATBbits.LATB7
//#define home PORTBbits.RB9
#define dir PORTBbits.RB15
main ()
{
AD1PCFG = 0xFFFF; //All pins are digital
//Output Ports
TRISAbits.TRISA0 = 0; //Set sleep port as output
TRISAbits.TRISA1 = 0; //Set ena port as output
TRISAbits.TRISA3 = 0; //Set rst port as output
TRISAbits.TRISA4 = 0; //Set sr port as output
TRISBbits.TRISB4 = 0; //Set step port as output
TRISBbits.TRISB7 = 0; //Set ms1 port as output
TRISBbits.TRISB8 = 0; //Set ms2 port as output
TRISBbits.TRISB15 = 0; //Set dir port as output
//Input Ports
//TRISBbits.TRISB9 = 1; //Set home port as input
//TRISAbits.TRISA2 = 1; //Set pirsens port as input
//TRISBbits.TRISB2 = 1; //Set sense1 port as input. Analog
//TRISBbits.TRISB3 = 1; //Set sense2 port as input. Analog
//Output ports initial states
sleep = 1; //Logic high is normal operation. When logic low minimize power consumption when the motor is not in use.
ena = 1; //Logic high, the outputs are disabled. Logic low enables all of the DMOS outputs.
rst = 1; //Logic high is normal operation. Pulse low resets the device.
sr = 0; //When the SR input is logic low, Active mode is enabled and synchronous rectification can occur. When the SR input is logic high, synchronous rectification is disabled
step = 0; //A low-to-high transition on the STEP input sequences the translator and advances the motor one increment.
ms2 = 0; //The input on terminals MS1 and MS2 selects the microstepping format.
ms1 = 0; //H H Sixteenth Step 4W1-2 Phase; L H Quarter Step W1-2 Phase; H L Half Step 1-2 Phase; L L Full Step 2 Phase
dir = 1; //The state of the DIR input determines the direction of rotation of the motor.
while (1)
{
__delay_ms(1);
ena = 0;
__delay_ms(1);
LATBbits.LATB4 = 0;
__delay_ms(1);
LATBbits.LATB4 = 1;
__delay_ms(1);
LATBbits.LATB4 = 0;
}
}