电动车控制器C语言源代码Word文件下载.docx
《电动车控制器C语言源代码Word文件下载.docx》由会员分享,可在线阅读,更多相关《电动车控制器C语言源代码Word文件下载.docx(35页珍藏版)》请在冰豆网上搜索。
0xB00对应限电流最大大约为2.6A
0xB80对应限流值最大大约为3.8A
LowVoltage_Lim(0x9B0);
欠压保护下限值设定
电池电压为47.9V时ADC采样值为0xB6==>
0xB60
推算电池电压为41V时的采样值为0x9B==>
0x9B0
推算电池电压为40V时的采样值为0x98==>
0x980
EABS_Set(1,1);
第一个参数为滑行充电功能使能,1为允许,0为禁止
第二个参数为电刹车功能使能,1为允许,0为禁止
Speed_LimHW(0,0,0,1);
硬件控制最大速度
参数只能有一个为1。
第一个参数对应15km/h
第二个参数对应20km/h
第三个参数对应30km/h
第四个参数对应40km/h
Speed_LimSW(0x01);
软件控制最大速度
参数数值由0x00~0x20,数值越小速度越大,反之则越小
while
(1)
_nop_();
//AutoHelpEN(0,0x1AA,100);
//Keep_SpeedEN(1,0x20,6);
//Current_Lim(0xB50);
//LowVoltage_Lim(0x9B0);
//EABS_Set(0,0);
//Speed_LimHW(0,0,0,1);
}
**********
*I/O端口初始化
voidInit_IO(void)
//------P0端口设置------//
P0M1=0xBE;
P0M2=0x01;
P0M1.YP0M2.Y=00设置I/O端口为普通双向模式
P0M1.YP0M2.Y=01设置I/O端口为推拉模式
P0M1.YP0M2.Y=10设置I/O端口为输入,高阻,模式
P0M1.YP0M2.Y=11设置I/O端口为开漏模式
//P0ID=0x78;
//设置四个AD端口0数字输入禁止
P0=0xFF;
//------P1端口设置------//
P1M1=0x1C;
P1M2=0xC0;
P1=0xFF;
//------P2端口设置------//
P2M1=0x01;
P2M2=0x1E;
P2=0xFF;
*初始化程序
voidInit(void)
unsignedchari;
//------PWM设置------//
//PWMP>
PWMn高电平,反之低电平
PWMPH=0X03;
PWMPL=0X55;
PWM0H=0X00;
PWM0L=0X00;
PWM1H=0X00;
PWM1L=0X00;
PWM2H=0X00;
PWM2L=0X00;
PWMCON1=0XC7;
//打开PWM电路,三个PWM口反相输出
PWMCON3=0xF0;
//------飞车保护--------//
EA=1;
/*do{
ADCCON=1;
ADCCON&
=0xef;
ADCCON|=0x08;
ADC_Ready=0;
while(ADC_Ready);
while(ADCH>
0x60);
//-----相位检测-------//
while(P02==0)
Phase_Detect();
.
//------变量初始化------//
for(i=0;
i<
32;
i++)
Current_Buffer[i]=0;
20;
Speed_Buffer[i]=0;
16;
Voltage_Buffer[i]=0;
Current_P=0;
Speed_P=0;
Voltage_P=0;
;
Speed_SUM=0;
//PWM_MAX=0;
Current_SUM=0;
Voltage_SUM=0;
H_State=0;
Old_State=0;
PWM_Duty=0;
Count_Current=0;
Count_Speed=0;
Count_Voltage=0;
Speed_REQ=0;
Current_REQ=0;
Voltage_REQ=0;
//PWM_Duty=0;
PWM_Duty_min=0;
//*********************
KeepSpeed_Flag=0;
KS_Z1=0;
KS_Z2=0;
Motor_Speed=0x50;
AutoHelp_Flag=0;
Current_Max=0xcffe;
Speed_Low=0x0500;
//Speed_High=0x1c37;
//Speed_MAX=0x1c00;
//-----定时器的设置-----//
TH0=0x50;
TL0=0x50;
//设定定时器的初值
TH1=0xE0;
TL1=0xE0;
TMOD=0x22;
//T0选为定时器,八位,模式2,TL0自动加载TH0中的初值
CKCON&
=0X00;
//定时器选择为1/12系统时钟
ET0=1;
//允许定时器中断
TR0=1;
//启动定时器
ET1=1;
TR1=1;
//-----外部中断设定-----//
EX1=1;
//允许外部中断1
IT1=0;
//电平触収中断1
AUXR1|=0x04;
//打开ADC电路
EADC=1;
//允许ADC中断
ADCCON&
=0xE7;
//SettingsofTimer2capturemode
CKCON=0x60;
CAPCON0=0xA8;
CAPCON1=0x00;
T2MOD=0xF0;
IE1|=0x80;
//enablecapturemodeinterrupt
RCAP2L=0x00;
//自动重装载低位
RCAP2H=0x00;
//自动重装载高位
IE1|=0x40;
//EA=1;
//enableinterrupt
T2CON|=0x04;
//enabletimer2
//***************************//
KS_EN=1;
KS_Time=8;
//***************************//}
*ADC中断处理程序
voidADC_ISR(void)interrupt11
//UB=~UB;
UB=~UB;
//EADC=0;
ADC_Ready=1;
if(Current_REQ)//电流采样
if(Current_SUM>
Current_Buffer[Current_P])
Current_SUM-=Current_Buffer[Current_P];
Current_Buffer[Current_P]=ADCH;
Current_SUM+=ADCH;
Current_P++;
if(Current_P>
31)
if(Speed_REQ)//转把电压采样
if(Speed_SUM>
Speed_Buffer[Speed_P])
Speed_SUM-=Speed_Buffer[Speed_P];
Speed_Buffer[Speed_P]=ADCH;
Speed_SUM+=ADCH;
Speed_P++;
if(Speed_P==14)
if(Voltage_REQ)//电源电压采样
if(Voltage_SUM>
Voltage_Buffer[Voltage_P])
Voltage_SUM-=Voltage_Buffer[Voltage_P];
Voltage_Buffer[Voltage_P]=ADCH;
Voltage_SUM+=ADCH;
Voltage_P++;
if(Voltage_P>
15)
//PWM_ADJ();
*定时器0中断处理函数
//==========InterruptCycle:
100uS===================
voidT0M1_ISR(void)interrupt1
Current_REQ=1;
ADCCON=2;
Count_Speed++;
KS_CNT++;
AH_Count++;
if(Count_Speed>
5)//17
ADCCON=4;
Speed_REQ=1;
Count_Voltage++;
if(Count_Voltage>
5)//50
ADCCON=3;
Voltage_REQ=1;
//********KeepSpeedSetting******//
KS_Finish();
//***FunctionSet***//
if(AH_Count>
=100)
AutoHelp();
//自助力
AH_Count=0;
if(KS_CNT>
=3000)
KS_CNT=0;
Keep_Speed();
//巡航定速
Volt_Low();
//欠压保护
if(P02==0)
Brake_Setting();
//刹车
PWM_ADJ();
*定时器1中断处理函数
voidT1M1_ISR(void)interrupt3{
*定时器2捕获模式中断处理函数
voidTimer2_ISR()interrupt13using2.
//*******MotorSpeed*******//
Motor_Speed=TH2;
TH2=0;
TL2=0;
//霍尔信号采集
*定时器2溢出中断处理函数
voidT2_ISR()interrupt8{
TF2=0;
Block_Detect();
//堵转保护
*外部中断处理函数,过流中断
voidINT1_ISR()interrupt2{
CurrentOver_Count++;
if(CurrentOver_Count>
=5)//防抖处理
PWM_Duty_min=1;
CurrentOver_Count=0;
*定时器3中断处理函数,采叏捕获模式*********************************************************************
voidH_Sample(void){
CAPCON1&
=0xF8;
H1=P12;
H2=P07;
H3=P20;
do
State1=H1<
<
2;
State1+=H2<
1;
State1+=H3;
State2=H1<
State2+=H2<
State2+=H3;
while(State1!
=State2);
//状态去抖
H_State=State1;
.}
*根据电机霍尔换向信号给出相应控制信号*上桥臂:
VT,UT,WT
*下桥臂:
VB,UB,WB
voidPhase_Change(void){
if(EABS_Flag)
if(!
AutoHelp_Flag)
UB=1;
VB=1;
WB=1;
UT=1;
VT=1;
WT=1;
elseif(PWM_Duty_min)
UT=0;
VT=0;
WT=0;
UB=1;
VB=1;
WB=1;
//电机停转
else
switch(H_State)
case6:
//110,V3,V4
WT=1;
UB=0;
break;
case2:
//,V4,V5
case7:
WB=0;
VT=1;
case3:
//011,V5,V6
case1:
//001,V6,V1
UT=1;
case5:
//101,V1,V2
case0:
VB=0;
case4:
//100,V2,V3
case9:
default:
/*if(PWM_Duty_min)
}*/
*相位检测程序
*上桥臂:
voidPhase_Detect(void)
*PWM值转换程序
*在限流允许下,将转把电压ADC值转换为PWMn的值*电流超过限流值时,做限流处理
voidPWM_ADJ(void)
//=====没有超过限流最大值的情冴====//
if(Current_SUM<
Current_Max)
if(Speed_SUM<
Speed_Low)
{//---没有转把电压,由Speed_Low的值决定转把电压最小值---//
KeepSpeed_Flag)