#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config VDDAR = HI // VDD Range Analog Calibration Selection bit (Internal analog systems are calibrated for operation between VDD = 2.3 - 5.5V)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
// CONFIG2
#pragma config MCLRE = EXTMCLR // Master Clear Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RA3 pin function is MCLR)
#pragma config LPBOREN = OFF // Low-Power BOR Enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit is ignored)
#pragma config DACAUTOEN = OFF // DAC Buffer Automatic Range Select Enable bit (DAC Buffer reference range is determined by the REFRNG bit)
#pragma config BORV = LO // Brown-out Reset Voltage Selection bit (Brown-out Reset Voltage (VBOR) set to 1.9V)
#pragma config PPS1WAY = ON // PPSLOCKED One-Way Set Enable bit (The PPSLOCKED bit can be cleared and set only once after an unlocking sequence is executed; once PPSLOCKED is set, all future changes to PPS registers are prevented)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // Storage Area Flash (SAF) Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block is NOT write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block is NOT write-protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register is NOT write-protected)
#pragma config WRTSAF = OFF // Storage Area Flash (SAF) Write Protection bit (SAF is NOT write-protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR. MCLRE Configuration bit is ignored)
// CONFIG5
#pragma config CP = OFF // Program Flash Memory Code Protection bit (Program Flash Memory code protection is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
void main(void) {
TRISB4 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISB5 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
ANSELB4 = 0;
ANSELB5 = 0;// Digital I/O. Pin is assigned to port or digital special function.
// The ANSELx bits default to the Analog mode after Reset.
// To use any pins as digital general purpose or peripheral inputs,
// the corresponding ANSEL bits must be changed to ‘0’ by the user.
// for delay
unsigned int i = 0;
while(1) {
/* turn off the LED (RB5 ---> 1Kohm ---> LED ---> RB4) */
LATB4 = 1;
LATB5 = 0;
// Insert some delay
i = 500;
while(i--);
/* turn on the LED (RB5 ---> 1Kohm ---> LED ---> RB4) */
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config VDDAR = HI // VDD Range Analog Calibration Selection bit (Internal analog systems are calibrated for operation between VDD = 2.3 - 5.5V)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
// CONFIG2
#pragma config MCLRE = EXTMCLR // Master Clear Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RA3 pin function is MCLR)
#pragma config LPBOREN = OFF // Low-Power BOR Enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit is ignored)
#pragma config DACAUTOEN = OFF // DAC Buffer Automatic Range Select Enable bit (DAC Buffer reference range is determined by the REFRNG bit)
#pragma config BORV = LO // Brown-out Reset Voltage Selection bit (Brown-out Reset Voltage (VBOR) set to 1.9V)
#pragma config PPS1WAY = ON // PPSLOCKED One-Way Set Enable bit (The PPSLOCKED bit can be cleared and set only once after an unlocking sequence is executed; once PPSLOCKED is set, all future changes to PPS registers are prevented)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // Storage Area Flash (SAF) Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block is NOT write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block is NOT write-protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register is NOT write-protected)
#pragma config WRTSAF = OFF // Storage Area Flash (SAF) Write Protection bit (SAF is NOT write-protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR. MCLRE Configuration bit is ignored)
// CONFIG5
#pragma config CP = OFF // Program Flash Memory Code Protection bit (Program Flash Memory code protection is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// Wiring
// PIC16F13145 L6234
// RB4 IN1
// RB5 EN1
// RB6 IN2
// RB7 EN2
// RC6 IN3
// RC7 EN3
#include <xc.h>
unsigned int bldc_step = 0;
void AH_BL(void);
void AH_CL(void);
void BH_CL(void);
void BH_AL(void);
void CH_AL(void);
void CH_BL(void);
void bldc_move(void);
void main(void) {
TRISB4 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISB5 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISB6 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISB7 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISC6 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISC7 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
ANSELB4 = 0;
ANSELB5 = 0;// Digital I/O. Pin is assigned to port or digital special function.
// The ANSELx bits default to the Analog mode after Reset.
// To use any pins as digital general purpose or peripheral inputs,
// the corresponding ANSEL bits must be changed to ?0? by the user.
ANSELB6 = 0;
ANSELB7 = 0;
ANSELC6 = 0;
ANSELC7 = 0;
// for delay
unsigned int i = 0;
while(1) {
bldc_move();
/* turn off the LED (RB5 ---> 1Kohm ---> LED ---> RB4) */
LATB4 = 1;
LATB5 = 0;
// Insert some delay
//i = 500;
//while(i--);
bldc_step++;
bldc_step %= 6;
/* turn on the LED (RB5 ---> 1Kohm ---> LED ---> RB4) */
LATB4 = 0;
LATB5 = 1;
// Insert some delay
//i = 500;
//while(i--);
}
return;
}
void bldc_move(){ // BLDC motor commutation function
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config VDDAR = HI // VDD Range Analog Calibration Selection bit (Internal analog systems are calibrated for operation between VDD = 2.3 - 5.5V)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
// CONFIG2
#pragma config MCLRE = EXTMCLR // Master Clear Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RA3 pin function is MCLR)
#pragma config LPBOREN = OFF // Low-Power BOR Enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit is ignored)
#pragma config DACAUTOEN = OFF // DAC Buffer Automatic Range Select Enable bit (DAC Buffer reference range is determined by the REFRNG bit)
#pragma config BORV = LO // Brown-out Reset Voltage Selection bit (Brown-out Reset Voltage (VBOR) set to 1.9V)
#pragma config PPS1WAY = ON // PPSLOCKED One-Way Set Enable bit (The PPSLOCKED bit can be cleared and set only once after an unlocking sequence is executed; once PPSLOCKED is set, all future changes to PPS registers are prevented)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // Storage Area Flash (SAF) Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block is NOT write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block is NOT write-protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register is NOT write-protected)
#pragma config WRTSAF = OFF // Storage Area Flash (SAF) Write Protection bit (SAF is NOT write-protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR. MCLRE Configuration bit is ignored)
// CONFIG5
#pragma config CP = OFF // Program Flash Memory Code Protection bit (Program Flash Memory code protection is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// Wiring
// PIC16F13145 L6234
// RB4 IN1
// RB5 EN1
// RB6 IN2
// RB7 EN2
// RC6 IN3
// RC7 EN3
#include <xc.h>
unsigned int bldc_step = 0;
void AH_BL(void);
void AH_CL(void);
void BH_CL(void);
void BH_AL(void);
void CH_AL(void);
void CH_BL(void);
void bldc_move(void);
void main(void) {
TRISB4 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISB5 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISB6 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISB7 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISC6 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
TRISC7 = 0; // PORTx output driver is enabled. PORTx pin configured as an output.
ANSELB4 = 0;
ANSELB5 = 0;// Digital I/O. Pin is assigned to port or digital special function.
// The ANSELx bits default to the Analog mode after Reset.
// To use any pins as digital general purpose or peripheral inputs,
// the corresponding ANSEL bits must be changed to ?0? by the user.
ANSELB6 = 0;
ANSELB7 = 0;
ANSELC6 = 0;
ANSELC7 = 0;
// for delay
unsigned int i = 0;
while(1) {
bldc_move();
// Insert some delay
//i = 10;
//while(i--);
/* turn off the LED (RB5 ---> 1Kohm ---> LED ---> RB4) */
//LATB4 = 1;
//LATB5 = 0;
// Insert some delay
//i = 500;
//while(i--);
bldc_step++;
bldc_step %= 6;
/* turn on the LED (RB5 ---> 1Kohm ---> LED ---> RB4) */
//LATB4 = 0;
//LATB5 = 1;
// Insert some delay
//i = 500;
//while(i--);
}
return;
}
void bldc_move(){ // BLDC motor commutation function