首先先把六路PWM关掉,然后,可以使三个下管导通当作电子刹车。如果还对降速要求高,可以适当加些反向控制。但是需要考虑反向带来的危害。
电动车无刷电机控制器需要编程电压的这个东西都是需要设置好的,不然的话电机它的输出功率就是有大有小嘛,如果输大了的话,你的续航里程还有就是行驶速度都会影响,所以厂家在出厂之前都会先给电动车的电机进行程序设计,比如说它的输出功率,进行设置#include<c8051f120.h>#define uint unsigned int
#define uchar unsigned char
uchar num1 = 0
uint v_set = 40
xdata uint pcacap2
xdata uchar dutycycle
uint A=10//float D=0.04float C=5
//int xdata e0=0,e1=0,e2=0
float xdata PWM=0
bit isnewdata
bit DIRECTON
#define POSITIVE DIRECTON = 1
#define NEGATIVE DIRECTON = 0
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE
OSCICN= 0x83
}
void Port_IO_Init()
{
SFRPAGE = 0x0F
XBR0 = 0xF7
XBR2 = 0x40
}
void PCA_Init()
{
SFRPAGE=0x00
PCA0CPM0=0x02
PCA0CPM1=0x02
PCA0CPM2=0x21
PCA0CPL2=0x00
PCA0CPH2=0x00
PCA0MD=0x00
PCA0CN=0x40
EIE1|=0x08
}
void PWM_set(uchar low)
{//占空比设置,高电平占空比为(256-low)/256
SFRPAGE = PCA0_PAGE
if (DIRECTON == 1) {PCA0CPH0 = lowPCA0CPM1 = 0x02PCA0CPM0 = 0x42}
else {PCA0CPH1 = lowPCA0CPM0 = 0x02PCA0CPM1 = 0x42}
}
void PCA_ISR(void) interrupt 9 using 1 {
static xdata uint tmpcnt=0
static xdata uint PCA0CP2=0
xdata uint tmpcnt2
if(CCF2){
tmpcnt2 = PCA0CPH2
tmpcnt2 = tmpcnt2<<8
PCA0CP2 = tmpcnt2 + PCA0CPL2
pcacap2 = PCA0CP2-tmpcnt
tmpcnt = PCA0CP2
CCF2=0
}
}
PID_SC(uint v)
{
int Uk_zint e
e = v - v_set
//e1 = v1 - v_set
//e2 = v2 - v_set
//v2 = v1
//v1 = v
Uk_z = A*e/*+ D*e0 + C*(e0 - 2*e1 + e2)*/
//Uk0 = Uk1 + Uk_z
//Uk1 = Uk0
//PWM = PWM - 256
dutycycle = 0.0256 * Uk_z
}
void main()
{
//Oscillator_Init()
WDTCN = 0x07
WDTCN = 0xDE
WDTCN = 0xAD
Port_IO_Init()
EA=1
num1=0
isnewdata=0
PCA_Init()
POSITIVE
while(1){PID_SC(pcacap2)PWM_set(dutycycle)}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)