#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
//Function Prototypes
void PWMStep (void);
void PWMRef (void);
void PWMVpfd (void);
//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 = 1; //The input on terminals MS1 and MS2 selects the microstepping format.
ms1 = 1; //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.
PWMRef ();
PWMVpfd();
// PWMStep ();
while (1)
{
ena = 0;
step = 1;
__delay_us(500);
step = 0;
ena = 0;
__delay_us(500);
}
}
//void PWMStep (void)
//{
// RPOR2bits.RP4R = 18; //OC1 Output to Pin RB4 PWM
// OC1CON1 = 0; // Clear registers
// OC1CON2 = 0;
// OC1RS = 199; //20KHz PWM period
// OC1CON2bits.SYNCSEL = 0x1F; //synchronized by itself
// OC1CON1bits.OCTSEL = 0x07; //Peripheral clock is the source for output Compare
// OC1CON1bits.OCM = 0b110; // Edge-aligned PWM mode on OC
//} //endPWMStep
void PWMRef (void)
{
RPOR7bits.RP14R = 19; //OC2 Output to Pin RB14 PWM
OC2CON1 = 0; // Clear registers
OC2CON2 = 0;
OC2RS = 199; //20KHz PWM period
OC2R = 180; //25% initial duty cycle, (180/199) * 3.3 = ~2.98Vout, Motor Current = (2.98V/(8*0.33OHM) = ~1.13A
OC2CON2bits.SYNCSEL = 0x1F; //Synchronized by itself
OC2CON1bits.OCTSEL = 0x07; //Peripheral clock is the source for output Compare
OC2CON1bits.OCM = 0b110; // Edge-aligned PWM mode on OC
} //endPWMRef
void PWMVpfd (void)
{
RPOR6bits.RP13R = 20; //OC3 Output to Pin RB13 PWM
OC3CON1 = 0; // Clear registers
OC3CON2 = 0;
OC3RS = 199; //20KHz PWM period
OC3R = 82; //40% initial duty cycle, (80/199) * 3.3V = ~1.32Vout
OC3CON2bits.SYNCSEL = 0x1F; //Synchronized by itself
OC3CON1bits.OCTSEL = 0x07; //Peripheral clock is the source for output Compare
OC3CON1bits.OCM = 0b110; // Edge-aligned PWM mode on OC
} //endPWMVpfd