⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 新能源燃料电池小车系统
💻 C
📖 第 1 页 / 共 3 页
字号:
/*变更说明:2008年7月22日 由于线束可配置线束资源不足
故动力12V供电控制由原系统中的排水阀控制;
程序中将原定时排放程序屏蔽TCH2和主程序中的定时设置程序;
在自动启动和停机中加入了继电器动作;
8月29日110v风机调整输入1-3v Pset和风机调速电压对应发生变化;9月19日屏蔽风机故障信号
风机9.19ncp调整(更换1k电阻和DA芯片)
*/

#include <hidef.h>            /* common defines and macros */
#include <mc9s12dp256.h>      /* derivative information */
#include "DataType.h"					/* data definition information*/
#include "Hd_Mcu_Cfg.h"				/* hardware configuration and macros*/
#include "CAN_Cfg.h"          /* CANBUS configuration and relative function declaration*/

#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
/***************Function declaration*******************/
#pragma CODE_SEG DEFAULT

void RTIInit(void);
void ECTInit(void);
void PORTIOIni(void);
void CHIPADInit(void);
void SPIDAInit(void);
void DAupdate(unsigned int DACA);
void SPIDATrans(unsigned char HighByte, unsigned char LowByte);
void RdCHIPAD(void);
unsigned int CalPPar(void);
void ProcessIni(void);
void CheckforECUReady(void);
void StartupEnable(void);
void StartupWaitDrain(void);
void StartupCheckReady(void);
void StopEnable(void);
void StopWaitDrain(void);
void StopWaitReady(void);
void SysEmgEnable(void);
void SysBeingEmg(void);
void SysRunningCtrl(void);
/***************Varibles declaration*******************/

/**********************************************************/
/**************data of AD and calculation results**********/
/**********************************************************/
unsigned  int   Ist   =0;             //stands for stack output current with resolution of 0.1A
unsigned  int   Icp   =0;             //stands for stack compressor consumed current with resolution of 0.1A
unsigned  int   Vst   =0;             //stands for stack output voltage with resolution of 0.1V
unsigned  int   Vcp   =0;             //stands for stack compressor powersource voltage with resolution of 0.1V
unsigned  int   Pst   =0;             //stands for gross output power of stack  W
unsigned  int   Pcp   =0;             //stands for power consumed by compressor and the unit is W
unsigned  int   PHS   =0;             //stansd for hydrogen supply pressure, base 0 and resoultion is 1mbara
unsigned  int   PHin  =0;             //stansd for anode   inlet pressure, base 0 and resoultion is 1mbara
unsigned  int   PWin  =0;             //stansd for coolant inlet pressure, base 0 and resoultion is 1mbara
unsigned  int   PAin  =0;             //stansd for cathode inlet pressure, base 0 and resoultion is 1mbara
unsigned  int   TAin  =0;             //stansd for cathode inlet tempature, base -40C and resoultion is 0.01
unsigned  int   THin  =0;             //stansd for anode   inlet tempature, base -40C and resoultion is 0.01
unsigned  int   TWin  =0;             //stansd for coolant inlet  tempature, base -40C and resoultion is 0.01
unsigned  int   TWout =0;             //stansd for coolant outlet tempature, base -40C and resoultion is 0.01 
unsigned  int   PPar  =0;					    //stands for power consumed by auxiliaries W
/**********************************************************/
/*************CAN trans and receieve varibles**************/
/**********************************************************/
unsigned  int   Pset  =0;					    //Pset value from VMS, resolution is 1W, which original resolution from CANBUS is 60W
unsigned  int   CVmax =0;					    //stands for maximal cell unit voltage  0.01V
unsigned  int   CVmin =0;					    //stands for minumal cell unit voltage  0.01V
unsigned  int   CMV1  =0;					    //stands for first cell module voltage	0.01V
unsigned  int   CMV2  =0;					    //stands for second cell module voltage	0.01V
unsigned  int   CMV3  =0;					    //stands for third  cell module voltage	0.01V
unsigned  int   CMV4  =0;					    //stands for fourth cell module voltage	0.01V

unsigned  char  CANFCETrigger =0;			//Trigger type for CANFCE sending message controlled by computer
/**********************************************************/
/***********Process and control related varibles***********/
/**********************************************************/
unsigned  char  ErrTsysCd =0;					//Error code for Temperature test result
unsigned  char  ErrPsysCd =0;  				//Error code for Pressure test result
unsigned  char  ErrCVCd   =0;			    //ErrCode for cell voltage
unsigned  char  modeCtrl  =0;         //stands for MCU control mode: 0: FCE manual; 1:FCE auto; 2: VMS auto;           
unsigned  char  stepCtrl  =0;					//step symbol used for detail control
unsigned  int   PLmt      =0;					//stands for net output power limit  W
unsigned  int   PLmtPatch =0;				  //stands for compensate for output power limit  W
unsigned  int   PLmtTarget=0;         //stands for Plmt upper limit value
unsigned  char  Ncp       =0;         //stands for speed of compressor 0-1 resolution is 0.01,eg. Ncp=0-100
unsigned  char  frqR1     =0;					//Voltage set for FAN1 with resloution is 0.001 of 12VDC 
unsigned  char  frqR2     =0;					//Voltage set for FAN2 with resloution is 0.001 of 12VDC 
unsigned  char  perHVTimer=30;		    //period of hydrogen drain action with resolution is s
unsigned  char  dtyHVTimer=1;         //duty of hydrogen drain action with resolution is s
unsigned  char  perWDTimer=15;        //period of water drain action with resolution is s
unsigned  char  dtyWDTimer=1;         //duty of water drain action with resolution is s

unsigned  int   perCANFCETimer=100;		//Period value in millisecond for CANFCE when trigger type is automatically period sending set by computer    
unsigned  int   cntCANFCETimer=0;			//stands for timer of CANFCE trans action
unsigned  char  cntSPIDATimer =0;			//stands for timer counter for SPIDA
unsigned  char  cntCHIPADTimer=0;			//stands for timer counter for inner AD
unsigned  char  cntSysTimer   =0;			//timer used when system auto starting
unsigned  char  cntHVTimer    =0;			//timer counter for hydrogen automatically drain
unsigned  char  cntWVTimer    =0;			//timer counter for water automatically drain

volatile  ByteFlags   TaskFlags;			//stands for trigger flags used for registering a defined task process
volatile  ByteFlags   ErrFlags;				//stands for error flags of respective function module
volatile  ByteFlags   SysFlags;				//stands for now and previous system operation mode
volatile  FCEStateD   StateDevice;		//stands for working station of respective actuator
volatile  StateDef		FCEStatus;			//stands for system status using code defined by VMS CAN
/**********************************************************/
/***********constant varibles used for system cfg**********/
/**********************************************************/
const unsigned  char  perSPIADTimer =200;	//period of SPIAD action initioned to be 200ms
const unsigned  char  perSPIDATimer =200;  //period of SPIDA action initioned to be 50ms
const unsigned  char  perCHIPADTimer=50;	//period of inner AD action initioned to be 50ms		 
const unsigned  int   SafeVstLmt    =64;	//stands for the maxiumal safe stack voltage gap is 6.4V 
const unsigned  int   SafeCVLmt     =45;	//stands for the lowest cell voltage is 0.45V 
const unsigned  char  IniValNcp     =5;		//initial value for compressor speed
const unsigned  int   IniVstLmt     =700;	//threshold stack voltage for ready with resolution 0.1V
const unsigned  char  IniCVLmt      =70;	//threshold cell voltage for ready  with resolution 0.01V
const unsigned  int   IniPLmt				=1000;//Initial power limit unit is W
const unsigned  int   iniPerHVTimer =30;	//Initial hydrogen drain period

//const unsigned  int   PsetToPlmt[38] = { 27  ,  248 ,   470,   691,    912,     //0-800W
  //                                       1133,  1354,  1575,  1796,   2018,    //1000-1800W
    //                                     2239,  2460,  2681,  2902,   3123,    //2000-2800W
      //                                   3344,  3565,  3787,  4008,   4229,    //3000-3800W
        //                                 4450,  4671,  4892,  5113,   5334,    //4000-4800W
          //                               5556,  5777,  5998,  6219,   6440,    //5000-5800W
            //                             6661,  6882,  7104,  7325,   7546,    //6000-6800W
              //                           7767,  7988,  8209 } ;                //7000-7400W

const unsigned  int   PsetToPlmt[38] = { 141,   341 ,   541,   741,    941,     //0-800W            //9月3日修改
                                         1141,  1341,  1553,  1764,   1975,    //1000-1800W
                                         2186,  2397,  2607,  2807,   3007,    //2000-2800W
                                         3207,  3417,  3627,  3837,   4047,    //3000-3800W
                                         4257,  4467,  4667,  4878,   5094,    //4000-4800W
                                         5310,  5536,  5744,  5952,   6160,    //5000-5800W
                                         6372,  6572,  6813,  7031,   7231,    //6000-6800W
                                         7430,  7630,  7830 } ;                //7000-7400W

 	                                  
#pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this module. Placement will be in NON_BANKED area. */
#pragma TRAP_PROC

interrupt void RTI_ISR(void) 
{ /* simple RTI interrupt service routine 1.024ms*/
   CRGFLG = 0x80; /* clear RTIF bit */
  
   /* if SPIDA Time counter is not greater than the setpoint increase the counter */
   /* else trigger the SPIDA task and reset the timer  */
   if((!TaskSPIDA)&&(cntSPIDATimer<=perSPIDATimer))cntSPIDATimer++;  //used for normal operation
   //if((!TaskSPIDA)&&(cntSPIDATimer<=perSPIDATimer))cntSPIDATimer++;    //used only for da test
   else if(cntSPIDATimer>=perSPIDATimer){TaskSPIDA=1;cntSPIDATimer=0;}
   
   /* if CHIPAD Time counter is not greater than the setpoint increase the counter
      else trigger theCHIPAD task and reset the timer  */
   if((!TaskCHIPAD)&&(cntCHIPADTimer<=perCHIPADTimer))cntCHIPADTimer++;
   else if(cntCHIPADTimer>=perCHIPADTimer){TaskCHIPAD=1;cntCHIPADTimer=0;}
   
   /* the following routine is only effective when CANFCE at periodically trigger mode  
      if CANFCE trans Time counter is not greater than the setpoint increase the counter
      else trigger the CANFCE trans task and reset the timer  */
   if(CANFCETrigger==PeriodTrigger) 
    {
      if((!TaskCANFCE)&&(cntCANFCETimer<=perCANFCETimer))cntCANFCETimer++;
      else if(cntCANFCETimer>=perCANFCETimer)           {TaskCANFCE=1;cntCANFCETimer=0;} 
    }
 }
/************************************************************************/  
/*************ICH Timer interrupt*****************/
interrupt void TCH7_ISR(void) 
{  /*14ms timer interval for CANVMS synchronization*/
   /*timer=num/62500s*/
   TC7=TC7+0X036B;
   TFLG1_C7F=1;
   /*used for trigering VMSCAN MSGs*/
   /*when a VMS status message receieved enable the timer enterrupt */
   TaskCANVMS=1;
   DisableCANVMS;
   /*when trigger flag has been set disable the wait timer until a next valid message got*/
 }

interrupt void TCH6_ISR(void) 
{  /*20ms timer interval*/
   /*timer=num/62500s*/
   TC6=TC6+0X04E2;
   TFLG1_C6F=1;
   /*put interval action following*/
   //T6Disable;
 }

interrupt void TCH5_ISR(void) 
{  /*50ms timer interval*/
   /*timer=num/62500s*/
   TC5=TC5+0X0C35;
   TFLG1_C5F=1;
   /*put interval action following*/
  //T5Disable; 
}

interrupt void TCH4_ISR(void) 
{  /*100ms timer interval*/
   /*timer=num/62500s*/
   TC4=TC4+0X186A;
   TFLG1_C4F=1;
   /*used for Plmt control***/
   /*the output power increase 100W/0.1s*/
   if(PLmt>=PLmtTarget) 
    {
      DisablePlmtCtrl;
      PLmt=PLmtTarget;
    }
   else           PLmt=PLmt+100;
   
   //if(PLmt>7100)PLmt=7100;风机输出流量以及过流的原因 更改输出限制为4500W
    if(PLmt>5000)PLmt=5000;
}

interrupt void TCH3_ISR(void) 
{   /*1S timer interval*/
   /*timer=num/62500s*/
   //uchar index;
   TC3=TC3+0XF424;
   TFLG1_C3F=1;

}

interrupt void TCH2_ISR(void) 
{  /*500ms timer interval*/
   /*timer=num/62500s*/
   TC2=TC2+0X7A12;
   TFLG1_C2F=1;
   cntWVTimer++;
   /*the following routine used for water drain */
   //if(modeCtrl!=ModeVMSAuto)                          {T2Disable;cntWVTimer=0;} 
   //else if((sysFlgWEV1==0)&&(cntWVTimer>=perWDTimer)) {ACT_DRAIN_WATER;cntWVTimer=0;}
   //else if((sysFlgWEV1==1)&&(cntWVTimer>=dtyWDTimer)) {STP_DRAIN_WATER;cntWVTimer=0;}   
}

interrupt void TCH1_ISR(void) 
{  /*500ms timer interval*/
   /*timer=num/62500s*/
    TC1=TC1+0X7A12;
    TFLG1_C1F=1;
   /*following routine is used for waiting counter in subprocess*/ 
    if(cntSysTimer<250)cntSysTimer++;

 }

interrupt void TCH0_ISR(void) 
{  /*1s timer interval*/
   /*timer=num/62500s*/
    TC0=TC0+0XF424;
    TFLG1_C0F=1;
   /*the following code used to control hydrogen drain*/
    cntHVTimer++;
    /*if system is not in ModeVMSAuto mode disable this Timer
      if hydrogen valve is close and counter is no less than period then open it, and reset the counter
      if hydrogen valve is open then close it*/
    if(modeCtrl!=ModeVMSAuto)                         {T0Disable;cntHVTimer=0;}
    else if((sysFlgHEV3==0)&&(cntHVTimer>=perHVTimer)){ACT_DRAIN_HYDROGEN;cntHVTimer=0;} 
    else if((sysFlgHEV3==1)&&(cntHVTimer>=dtyHVTimer)){STP_DRAIN_HYDROGEN;cntHVTimer=0;} 
}


#pragma CODE_SEG DEFAULT
/************************************************************************/
/************************System Initialization***************************/
/************************************************************************/

 /************************************************************************/ 
 void RTIInit(void) 
 { /*setup of the RTI interrupt frequency adjusted to get 1 millisecond (1.024 ms) with 16 MHz oscillator*/ 
  RTICTL = 0x1F; // set RTI prescaler 
  CRGINT = 0x80; // enable RTI interrupts
 }

void ECTInit(void) 
  {
  TIOS=0XFF;            //Select Channels as output compare
  CFORC=0X00;           //Disable force Output Compare Action for Channel
  
  /*config TSCR1*/
  TSCR1_TEN=0;          //Disable main clock when initialling
  TSCR1_TSWAI=0;        //Allows the timer module to continue running during wait
  TSCR1_TSFRZ=0;        //Allows the timer and modulus counter to continue running while in freeze mode
  TSCR1_TFFCA=1;        //Allows Timer Fast Flag Clear
  TIE=0X00;             //Disable channel interrupt,free it when used
  /*config TSCR1*/
  
  TSCR2_TOI=0;          //Timer Overflow Interrupt inhibited
  TSCR2_TCRE=0;         //Counter reset inhibited and counter free runs
  TSCR2_PR2=1;
  TSCR2_PR1=1;
  TSCR2_PR0=1;          //Set Timer Prescaler Select 128
  
  TC0=0X0000;
  TC1=0X0000;
  TC2=0X0000;
  TC3=0X0000;
  TC4=0X0000;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -