【Curiosity Nano测评报告】L6234 + BLDC
本帖最后由 MianQi 于 2024-9-21 18:09 编辑KiCad 上没有 Curiosity Nano Kit PIC16F13145,自己画了一个。
能不能分享出来 正在做。。。 最新进展:
这次要做的是将之间在 ATmega328P 上测试过的方案在 PIC16F13145 上复现一下,上次的帖子在这里 - https://bbs.21ic.com/icview-3376522-1-1.html 很好,借鉴一下 LEDyyds 发表于 2024-9-23 16:01
能不能分享出来
L6234 运行的核心功能来自 PIC MCU 的 GPIO,具体来说,就是每一步有一个管脚输出高,相对应的另一个管脚输出低,我先将基础的 GPIO 程序发到回复中,重要的是要注意的几个细节。 // 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.
#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) */
LATB4 = 0;
LATB5 = 1;
// Insert some delay
i = 500;
while(i--);
}
return;
}
要注意的细节是:
1、PIC MCU 要能正常运行,首要的是“ Configuration Bits”的设置,这个设置最重要的有两项:时钟源的选择(本例中选用内部低速振荡器)、关闭看门狗定时器(默认为开)。
2、每一个管脚在重启后默认的都是模拟模式为开,用户要自己给相应的寄存器清零,确保关闭这项功能。 MCC没有备注是什么意思 grfqq325 发表于 2024-9-26 11:00
MCC没有备注是什么意思
? 没明白你的意思。 MianQi 发表于 2024-9-26 15:12
? 没明白你的意思。
我用的工具组合是:
MPLAB X IDE v6.15
XC8 v2.50
PIC16F1xxxx_DFP(1.25.389)
IDE提示 MCC 不可用,我就手动设置了一下。 完整的电路图:
实物照:
程序(待测试):
// 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();
/* 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(){
// EN2 - IN1
LATB4 = 1;// IN1
LATB5 = 0;
LATB6 = 0;
LATB7 = 1;// EN2
LATC6 = 0;
LATC7 = 0;
}
void AH_CL(){
// EN3 - IN1
LATB4 = 1;// IN1
LATB5 = 0;
LATB6 = 0;
LATB7 = 0;
LATC6 = 0;
LATC7 = 1;// EN3
}
void BH_CL(){
// EN3 - IN2
LATB4 = 0;
LATB5 = 0;
LATB6 = 1;// IN2
LATB7 = 0;
LATC6 = 0;
LATC7 = 1;// EN3
}
void BH_AL(){
// EN1 - IN2
LATB4 = 0;
LATB5 = 1;// EN1
LATB6 = 1;// IN2
LATB7 = 0;
LATC6 = 0;
LATC7 = 0;
}
void CH_AL(){
// EN1 - IN3
LATB4 = 0;
LATB5 = 1;// EN1
LATB6 = 0;
LATB7 = 0;
LATC6 = 1;// IN3
LATC7 = 0;
}
void CH_BL(){
// EN2 - IN3
LATB4 = 0;
LATB5 = 0;
LATB6 = 0;
LATB7 = 1;// EN2
LATC6 = 1;// IN3
LATC7 = 0;
}
台钳不错。 实测程序:
// 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
} 实际运行效果演示:
https://www.bilibili.com/video/BV1VmxheYEgR/
详细说明:
https://www.bilibili.com/video/BV1a74ueqEYm/
BLDC不是用PWM驱动吗