这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » w79e825程序

共1条 1/1 1 跳转至

w79e825程序

助工
2014-11-13 19:18:57     打赏
#include <W79E825.H>
#define CT P07
#define BT P10
#define AT P11
// set to PWM register
unsigned char PWM_Duty_H,PWM_Duty_L;//,Status_Tmp;//Add_DEC;
// Time base for 电流、转把、电压
unsigned char TB_ACH1,TB_ACH2,TB_ACH3;
// Buffer for 电流、电压
unsigned char ADC_Buffer[16],ADC_Point;
unsigned char ADC_Point2,ADC_Buffer2[14];
// 电流处理数据
unsigned int ADC_SUM1,INT_tmp1,INT_tmp2,ADC_SUM2,ADC_SUM3;
unsigned char ADC_CH,ADC_H,ADC_L;
// ADC request Flag
bit ADC_REQ;
// ADC_ready request Flag
unsigned char ADC_RD;
unsigned char PWM_TMR,PWM_MAX_H,PWM_MAX_L;
unsigned int PWM_tmp;
bit PWM_REQ,PWM_0_Duty;
bit BRK_F;
unsigned char DS_CNT,KS_TM;//,KS_CNT
bit DS_Start,DS_Flag,ZB_Z1,ZB_Z2;
unsigned int KS_ADCSUM2;
unsigned char KS_PWM_H,KS_PWM_L;
//故障指示部分数据
unsigned char Error_Type;
//智能助力功能部分数据
bit SenBak,AH_F,AH_Prt,AH_Start;
unsigned char AH_TM,   AH_ST,  AH_CT; 
unsigned char AH_CNTL, AH_CNTH;// AH_Cy;
   //信号H时间   信号周期
// 过流保护部分数据
unsigned int Volt_L,ADC_SUM_tmp;
unsigned char Vstep,Vstep2,Vstep1;
bit PowerOff;//,PowerOff_bak;

//堵转处理部分数据
bit DZ_Flag;
unsigned char DZ_CNT;
unsigned char Detect_Tmp_BAK;

//************* 飞车保护 ***************************
bit FeiChe_F;
//********* 超静音 ******************************
unsigned char CJY_CNT,CYJ_TM,DutyH_B,DutyL_B;
bit CJY_F,PG_start;
//***********************************************
//* 上电初始化程序   
void Reset_Initial(void)
{
 //unsigned int i;
 
 P1M1=0x3c;//3f;
 P1M2=0xc3;//c0;
 P1=0xc0;
 
 P0M1=0x7c;
 P0M2=0x83;
 P0ID=0x78;
 P0=3;
 
 P2M1=0x01;
 P2M2=0x02;
 P2=00;
 TH0=0Xba;TL0=0Xba;
 TH1=0x00;TL1=0x00;
 TMOD=0x22;// timer 0 work in mode1
 CKCON&=0X00; // time source as clk/12
 ET0=1;// enable timer0 interrupt
 TR0=1;
 TR1=1;
 ET1=1;
 PWMCON1=0Xc0;
 PWMPH=0X01;// 设置PWM的状态
 PWMPL=0X77;
 PWM0H=0X00;
 PWM0L=0Xcc;
 PWM1H=2;
 PWM1L=0X00; 
 PWM2H=2;
 PWM2L=0X00; 
 PWM3H=2;
 PWM3L=0X00;
 PWMCON1=0Xc0;
 
 AUXR1|=0x04;  // 打开ADC电路
 EADC=1;   // 允许ADC中断
 
 Error_Type=0;
 PWM_Duty_H=0;
 PWM_Duty_L=0;
 
 ADC_Point=0;
 ADC_SUM1=0;
 ADC_REQ=0;
 ADC_RD=0X55;
 ADC_SUM2=0;
 PWM_REQ=0;
 PWM_TMR=0;
 PWM_MAX_H=0;
 PWM_MAX_L=0;
 
 PWM_0_Duty=1;
 PowerOff=0;
 ADC_SUM_tmp=0x630;
 DS_Flag=0;
 DZ_Flag=0;
 CYJ_TM=0;
 EA=1;
 
 //设置程序开始运行标志,超静音用
 PG_start=1;
 ADC_H=0XFF;
 while(ADC_H>0x50)
 {
  ADCCON=1;
  ADCCON&=0xef;
  ADCCON|=0x08;
  while(ADC_RD==0X55);
 }
 EA=0;
 while(P02==0)
 {
  PWM1H=0;PWM2H=0;PWM3H=0;
  PWM1L=0;PWM2L=0;PWM3L=0;
  PWMCON1=0Xe0;
 
  Vstep=P1;
  Vstep=Vstep>>2;
  Vstep&=0x07;
  switch(Vstep)
  {
   case 3://6 
     BT=0;AT=0;CT=1;
     break;
   case 1: //4
     BT=0;AT=0;CT=1;
     break;
   case 5: //5 
   case 0: //0
     BT=0;CT=0;AT=1;
     break;
   case 4: //1
     BT=0;CT=0;AT=1;
     break;
   case 6: //3
     AT=0;CT=0;BT=1;
     break;
   case 2: //2
   case 7: //7
     CT=0;AT=0;BT=1;
   default: break;
  }
 
 }
 
}
//***********************************************
// ADC 转换设置程序
void ADC_Convert(void)
{ if(ADC_CH==3)
  {ADCCON=2;}
 if(ADC_CH==2)
  {ADCCON=1;}
 if(ADC_CH==1)
  {ADCCON=0;}
 ADCCON&=0xef;ADCCON|=0x08;
}
//***********************************************
// ADC中断服务程序
void ADC_ISR() interrupt 11
{
 ADC_RD=0XAA;
 ADCCON&=0xe7;
 ADC_H=ADCH;
 ADC_L=ADCCON&0xc0;
}
//***********************************************
// 欠压检测设定程序
void ADC_Volt()
{
 TB_ACH3--;
 if(TB_ACH3<2)
 {
  TB_ACH3=20;
  ADC_CH=3;  
 }
}
//***********************************************
// 定时器 0 中断服务程序
void T0M1_ISR(void) interrupt 1
{
 if(CJY_F==1)
 {
  CJY_CNT++;
  if(CJY_CNT>CYJ_TM)
  {
   CJY_CNT=0;
   CJY_F=0;
  }
 }

 PWM_TMR++;
 if(PWM_TMR==10)
 {
  PWM_TMR=0;
  PWM_REQ=1;
 }
 
 TB_ACH1--;
 if(TB_ACH1<2)
 {
  ADC_REQ=1;
  ADC_CH=1;
  TB_ACH1=10;
  TB_ACH2--;
  if(TB_ACH2<2)
  {
   ADC_CH=2;
   TB_ACH2=10;
  }
  ADC_Volt();
 } 
}
//***********************************************
// 自动程序助力
void Autohelp()
{
 if(PWM_0_Duty==1) 
  {if (P20==0)
   { 
   if(P20==SenBak)
    {
    AH_CNTL++;
    if(AH_CNTL>180)
     {
     AH_CNTH=0;
     AH_CNTL=0;
     AH_CT=0;
     }
    }
   else
    {
    if(AH_CNTH>AH_CNTL)
     {
     AH_CT++;
     }
    else
     {
     AH_CT=0; 
     }
    AH_CNTL=0;
    SenBak=P20;
    if(AH_CT>4)
     {
      AH_F=1;
      AH_CT=0;
      AH_CNTL=0;
      AH_CNTH=0;
      AH_ST=200;
     }
    }
   }
  else
   {
   if(P20==SenBak)
    {
     AH_CNTH++;
     if(AH_CNTH>240)
     {
      AH_CNTH=0;
      AH_CNTH=0;
      AH_CT=0;
     }
    }   
   else
    {
     SenBak=P20;
     AH_CNTH=0;
    }
  
    
   }
  } 
 else
  {
  AH_CNTL=0;
  AH_CNTH=0;
  AH_CT=0;
  AH_F=0;
  }
 if(AH_F==1)
  {
   AH_ST--;
   if(AH_ST<2)
   AH_F=0;
  }
 if(DS_Flag==1)
  {
   AH_CT=0;
   AH_F=0;
  }
} 
// **********************************************
// 定速设定程序
void KeepSpeed()
{
 if(DS_Flag==0)
  {
  DS_Start=0;
  if (ADC_SUM2>0x508)
  {
   PWM_tmp=ADC_SUM2+0x4a;
   if(PWM_tmp<KS_ADCSUM2)
   {
    DS_CNT=0;
    KS_ADCSUM2=ADC_SUM2;
    DS_Start=1; 
   }
   PWM_tmp=ADC_SUM2-0x4a;
   if(PWM_tmp>KS_ADCSUM2)
    {
    DS_CNT=0;
    KS_ADCSUM2=ADC_SUM2;
    DS_Start=1;
    }
   if (DS_Start==0)
   {
    DS_CNT++;
    if(DS_CNT>70)
    {
     DS_CNT=70;
     DS_Flag=1;
     KS_PWM_H=PWM_MAX_H;
     KS_PWM_L=PWM_MAX_L;
    }
   }
  }
 }
 else
  {
   if (ADC_SUM2<0x380)
   {
    ZB_Z1=1;
    if(ZB_Z2==1)
    {
     DS_CNT=0;
     DS_Flag=0;
     ZB_Z1=0;
     ZB_Z2=0; 
    }
   }
   else
   {
    if(ZB_Z1==1)
    {
    ZB_Z2=1;
    }
   }
  }
}
//***********************************************
// 堵转程序 
void DZ_Process()
{
 unsigned int DZ_T1;
 DZ_T1=ADC_SUM_tmp-0x50;
  if(ADC_SUM1>DZ_T1)
  {
   DZ_CNT++;
   if(DZ_CNT>70)
   {
    DZ_CNT=0;
    DZ_Flag=1;
   }
  }
 DZ_T1=ADC_SUM_tmp-0x100;
  if(ADC_SUM1<DZ_T1)
  {
   DZ_CNT=0;
  }
  
}
//***********************************************
// 定时器1 中断服务程序
void T1M1_ISR(void) interrupt 3
{
 AH_TM++;
 if (AH_TM>30)
 {
  AH_TM=0;
  Autohelp();
  KS_TM++;
  if(KS_TM>3)
  {
   KS_TM=0;
   if(P15==0)
   {
    KeepSpeed();
   }
   DZ_Process();
  }
 }
 
}
//***********************************************
// 加速程序
void speed(void)
{
 unsigned char Detect_Tmp;
 
 Detect_Tmp = P1;
 Detect_Tmp = Detect_Tmp>>2;
 Detect_Tmp &= 0X07;
 
 if(Detect_Tmp != Detect_Tmp_BAK)
 {
  Detect_Tmp=P1;
  Detect_Tmp=Detect_Tmp>>2;
  Detect_Tmp&=0X07;
  
  if(Detect_Tmp != Detect_Tmp_BAK)
  {
   Detect_Tmp_BAK = Detect_Tmp;
   DZ_CNT=0;
   CJY_F=0;
   CYJ_TM=0;
   if(ADC_SUM1>0x730)
   {
    CJY_F=1;
    CYJ_TM=1;
   }
   if(ADC_SUM1>0x7a0)
    CYJ_TM=2;
   if(ADC_SUM1>0x810)
    CYJ_TM=3;
  }
 } 
 
 if(DZ_Flag==1)
  Detect_Tmp=9;
    
 if(PowerOff==1)
 {
  Detect_Tmp=9;
  DS_CNT=0;
  DS_Flag=0;
 }
 
 if(DS_Flag==0) 
 {
  if(AH_F==0)
  {
   if(PWM_0_Duty==1)
   Detect_Tmp=9;
  }
 }
  
 if(FeiChe_F==1)
  Detect_Tmp=9;
 DutyH_B=PWM_Duty_H;
 DutyL_B=PWM_Duty_L;
 
 if(PG_start==0)
 {
  if(CJY_F==1)
  {
   DutyH_B=0x01;
   DutyL_B=0x77;
  }
 }
 else
 {
  CJY_F=0; 
 }
 PG_start=0;
 
 if(P02==0)//刹车处理
 {
  Detect_Tmp=9;
  DS_CNT=0;
  DS_Flag=0;
  ZB_Z1=0;
  ZB_Z2=0;
  AH_F=0;
  AH_CT=0;
  PWM_Duty_H=0;
  PWM_Duty_L=0;
  PWM_MAX_H=0;
  PWM_MAX_L=0;
 }
 switch(Detect_Tmp)
 {
  case 3://6 
    PWM3H=0X00;PWM3L=0X00; //Phase 1
    PWM2H=0X00;PWM2L=0X00;
    BT=0;AT=0;
    PWMCON1=0XE0;
    PWM1H=DutyH_B;PWM1L=DutyL_B;
    CT=1;
    break;
  case 1: //4
    PWM3H=0X00;PWM3L=0X00; //Phase 1
    PWM1H=0X00;PWM1L=0X00;
    BT=0;AT=0;
    PWMCON1=0XE0;
    PWM2H=DutyH_B;PWM2L=DutyL_B;
    CT=1;
    break;
  case 5: //5 
  case 0: //0
    PWM3H=0X00;PWM3L=0X00; //Phase 1
    PWM1H=0X00;PWM1L=0X00;
    BT=0;CT=0;
    PWMCON1=0XE0;
    PWM2H=DutyH_B;PWM2L=DutyL_B;
    AT=1;
    break;
  case 4: //1
    PWM1H=0X00;PWM1L=0X00; //Phase 1
    PWM2H=0X00;PWM2L=0X00;
    BT=0;CT=0;
    PWMCON1=0XE0;
    PWM3H=DutyH_B;PWM3L=DutyL_B;
    AT=1;
    break;
  case 6: //3
    PWM1H=0X00;PWM1L=0X00; //Phase 1
    PWM2H=0X00;PWM2L=0X00;
    AT=0;CT=0;
    PWMCON1=0XE0;
    PWM3H=DutyH_B;PWM3L=DutyL_B;
    BT=1;
    break;
  case 2: //2
  case 7: //7
    PWM3H=0X00;PWM3L=0X00; //Phase 1
    PWM2H=0X00;PWM2L=0X00;
    CT=0;AT=0;
    PWMCON1=0XE0;
    PWM1H=DutyH_B;PWM1L=DutyL_B;
    BT=1;
    break;
  case 9:
    PWM1H=0X00;PWM1L=0X00;  //Phase 7
    PWM3H=0X00;PWM3L=0X00;
    PWM2H=0;PWM2L=0;
    AT=0;
    CT=0;
    BT=0;
    break;
  default: break;
 }
 PWMCON1|=0X40;
}

void ADC_VoltSum()
{
 unsigned int ttmp;
 ttmp=ADC_SUM3>>4;
 ADC_SUM3-=ttmp;
 ADC_SUM3+=ADCH;
 if(ADC_SUM3<0xabb)//A20
 {
  Volt_L=ADC_SUM3-0x9c0;
 }
 else
 {
  Volt_L=0;
 }
 
 if(ADC_SUM3<0x9c0)
 {
  PowerOff=1;
  //PowerOff_bak=1;
  Volt_L=0;
 }
 if(ADC_SUM3>0xa35)
 {
  PowerOff=0;
 }
 
 Vstep=0;
 Vstep1=1;
 while(Volt_L>0x25)
 {
  Vstep++;
  if(Vstep>8)
   Vstep=8;
  Volt_L-=0x20;
 }
 if(ADC_SUM3<0xabb)
 {
  Vstep2=8-Vstep;
  if(Vstep2>8)
   Vstep2=0; 
 }
 else
 {
  Vstep2=0; 
 }
}
void Monitor_ADC(void)
{
 unsigned int MA_T;
 if(ADC_RD==0XAA)
 {
  ADC_RD=0X55;
  if(ADC_CH==1)
  { 
   ADC_SUM1-=ADC_Buffer[ADC_Point];
   ADC_Buffer[ADC_Point]=ADC_H;
   ADC_SUM1+=ADC_H;
   ADC_Point++;
   if(ADC_Point==16)
    ADC_Point=0;
  }
  if(ADC_CH==2)
  { 
  if(ADC_SUM2>ADC_Buffer2[ADC_Point2])   
   {
    ADC_SUM2-=ADC_Buffer2[ADC_Point2];
   }
   ADC_Buffer2[ADC_Point2]=ADC_H;
   ADC_SUM2+=ADC_H;
   ADC_Point2++;
   if(ADC_Point2==13)
     ADC_Point2=0;
   PWM_0_Duty=0;
   if(ADC_SUM2<0x03d0)
   {
    PWM_MAX_H=0;
    PWM_MAX_L=0;
    PWM_0_Duty=1;
    DZ_CNT=0;
    DZ_Flag=0;
   }
   else
   {
    
    if(ADC_SUM2>0x0c00)
    {
     FeiChe_F=1;
    }
    MA_T=ADC_SUM2;
    if(MA_T>0x0a75)
    {
     MA_T=0x0a75;
    }
    if(MA_T>0x3a0)
     {PWM_tmp=MA_T-0x3a0;}
    else
     {PWM_tmp=0;}
    //欠压处理***************
    if(PowerOff==1)
    {
     PWM_tmp=0;
    }
     PWM_MAX_H=PWM_tmp>>10;
     PWM_MAX_L=PWM_tmp>>2; 
   }
  }
 if(ADC_CH==3)
  {
   ADC_VoltSum();
  }
 }
 EA=1;
 if(AH_F==1)
 {
  PWM_MAX_H=1;
  PWM_MAX_L=0x55;
 }
 else
 {
 if(DS_Flag==1)
  {
   PWM_MAX_H=KS_PWM_H;
   PWM_MAX_L=KS_PWM_L;
  } 
 }

 EA=0;
 if(ADC_REQ==1)
 {
  EA=0;
  ADC_Convert(); 
  ADC_REQ=0;
 }

}

// 欠压处理****************
void PWM_ADJ(void)
{
 unsigned int SUB_tmp;  //ADC_SUM_tmp
 if(Vstep1==1)
 {
  Vstep1=0;
  SUB_tmp=0x50;
  ADC_SUM_tmp=0x880;
  while(Vstep2>0)
  {
   Vstep2--;
   ADC_SUM_tmp-=SUB_tmp;
   SUB_tmp-=7;   //6 指数曲线斜率
  } 
 }
 if(ADC_SUM1 < ADC_SUM_tmp-5)
 {
  INT_tmp1 = PWM_Duty_H*256 + PWM_Duty_L;
  INT_tmp2 = PWM_MAX_H*256 + PWM_MAX_L;
  
  if(INT_tmp1 >= INT_tmp2)
  {
   PWM_Duty_H = PWM_MAX_H;
   PWM_Duty_L = PWM_MAX_L;
  }
  else
  {
   PWM_Duty_L++;
   if(PWM_Duty_L>0xfe)
   {
    PWM_Duty_L=0;
    PWM_Duty_H++;
   }
  }
 }

 if(ADC_SUM1>ADC_SUM_tmp+5)
 {
  INT_tmp1=PWM_Duty_H*256+PWM_Duty_L;
  if(INT_tmp1<0x30)
  {
   PWM_Duty_H=0;
   PWM_Duty_L=0;
  }
  else
  {
   if(PWM_Duty_L<2)
   {
    if(PWM_Duty_H==0)
    {
    PWM_Duty_L=0;
    PWM_Duty_H=0; 
    }
    else
    {
    PWM_Duty_L=254;
    PWM_Duty_H=0;
    }
   }
   else
   {
   PWM_Duty_L--;
   }
  }
 }
}
/////////////////////////////////////////////////////////////
void main(void)
{ 
 Reset_Initial();
 while(1)
 {
  Monitor_ADC();
  if(PWM_REQ==1)
  {
   PWM_REQ=0;
   PWM_ADJ();
  }
  speed(); 
 }
}

 电路在此下载,电路.rar


共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]