实测程序:
// PIC16F13145 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator Selection bits (Oscillator not enabled)
#pragma config RSTOSC = LFINTOSC// Reset Oscillator Selection bits (LFINTOSC)
#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 PWRTS = PWRT_OFF // Power-up Timer Selection bits (PWRT is disabled)
#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 DEBUG = OFF // Background Debugger (Background Debugger disabled)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT Operating Mode bits (WDT Disabled, SEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT Input Clock Select bits (Software Control)
// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#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
switch(bldc_step){
case 0:
AH_BL(); // EN2 - IN1
//BEMF_C_RISING();
break;
case 1:
AH_CL(); // EN3 - IN1
//BEMF_B_FALLING();
break;
case 2:
BH_CL(); // EN3 - IN2
//BEMF_A_RISING();
break;
case 3:
BH_AL(); // EN1 - IN2
//BEMF_C_FALLING();
break;
case 4:
CH_AL(); // EN1 - IN3
//BEMF_B_RISING();
break;
case 5:
CH_BL(); // EN2 - IN3
//BEMF_A_FALLING();
break;
}
}
void AH_BL(){
// EN1 - EN2 - IN1
LATB4 = 1; // IN1
LATB5 = 1; // EN1
LATB6 = 0;
LATB7 = 1; // EN2
LATC6 = 0;
LATC7 = 0;
}
void AH_CL(){
// EN1 - EN3 - IN1
LATB4 = 1; // IN1
LATB5 = 1; // EN1
LATB6 = 0;
LATB7 = 0;
LATC6 = 0;
LATC7 = 1; // EN3
}
void BH_CL(){
// EN2 - EN3 - IN2
LATB4 = 0;
LATB5 = 0;
LATB6 = 1; // IN2
LATB7 = 1; // EN2
LATC6 = 0;
LATC7 = 1; // EN3
}
void BH_AL(){
// EN2 - EN1 - IN2
LATB4 = 0;
LATB5 = 1; // EN1
LATB6 = 1; // IN2
LATB7 = 1; // EN2
LATC6 = 0;
LATC7 = 0;
}
void CH_AL(){
// EN3 - EN1 - IN3
LATB4 = 0;
LATB5 = 1; // EN1
LATB6 = 0;
LATB7 = 0;
LATC6 = 1; // IN3
LATC7 = 1; // EN3
}
void CH_BL(){
// EN3 - EN2 - IN3
LATB4 = 0;
LATB5 = 0;
LATB6 = 0;
LATB7 = 1; // EN2
LATC6 = 1; // IN3
LATC7 = 1; // EN3
}
|