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

📄 main.c

📁 柴油发动机的电控PID代码。平台freescale的MC9S12DZ60单片机。核心算法
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
*Copyright (c) 2007, 上海云飞自动化设备有限公司
*All Right Reserved.
*
*File Name:main.c
*File ID:
*Discription:This is the main file for ECU designed for Wuxi Fule Ejection
*            Reserach and Institute,
*            including the EGR PID routine and temperature acquision routine.
*
*Verision:6.23
*Author:Andrew Sun
*Date:25 Apr.2007
*
*
*aadred analog_routine()  (8 Jan.2007)
*
*aadred lubricating oil pressure acquisition 
*move the flags' disposals into main routine from RTI routine(11 Jan.2007) 
*
*changed the position differential and acceleration to software realization
*cansered the integral in the rotation cycle.(12,Jan.2007)
*
*added the arithmetic of egr(17 Jan.2007)
* 
*perfected the arithmetix of egr(18 Jan.2007) and optimized the program  
*/

#include <hidef.h>      /* common defines and macros */
#include <mc9s12d32.h>     /* derivative information */
#include <MATH.H>


#pragma LINK_INFO DERIVATIVE "mc9s12d32"

#define uchar unsigned char
#define uint unsigned int

#define PCREAD     0xff
#define PCSAVE     0xfe
#define PCPLUS     0xfd
#define PCMINUS    0xfc
#define PCFPLUS    0xfb
#define PCFMINUS   0xfa

#define MODE1      0x10
#define MODE2      0x20
#define MODE3      0x30
#define MODE4      0x40
#define MODE5      0x50
#define MODE7      0x70
#define MODE8      0x80
#define MODE1N     23
#define MODE2N     20
#define MODE3N     78
#define MODE3NI    26        //mode3 parameter individual
#define MODE5N1    4      //
#define MODE5N2    6      //
#define MODE5N3    3
#define MODE7N     24
#define MODE7NI    12        //mode7 parameter individual
#define MODE8N     120
#define MODE8NI    10
//#define MODE8NII   100
#define M1READ     0x00
#define M2READ     0x00
#define M2INCR     0x01
#define M2DECR     0x02
#define M2FINCR    0x03
#define M2FDECR    0x04
#define M3READ     0x00
#define M3INCR     0x01
#define M3DECR     0x02
#define M3SPEED    0x03
#define M3STORE    0x04
#define M3FINCR    0x05
#define M3FDECR    0x06
#define M4READ     0x00
#define M4SHAKE    0x01
#define M5READ     0x00
#define M5INCR     0x01
#define M5DECR     0x02
#define M7READ     0x00
#define M7INCR     0x01
#define M7DECR     0x02
#define M7SPEED    0x03
#define M7STORE    0x04
#define M7FINCR    0x05
#define M7FDECR    0x06
#define M8READ     0x00
#define M8INCR     0x01
#define M8DECR     0x02
#define M8SPEED    0x03
#define M8STORE    0x04
#define M8FINCR    0x05
#define M8FDECR    0x06

#define ERROR      0xee
#define DONE       0xd0
#define SYN 			 0xf0
#define FACTOR     1
#define FFACTOR    10
#define EEPROMBASE 0x0400

#define DYNAF      1
#define STATF      0
#define LROT       300   //无转速时,预置设定转速
#define SEPSPD     1000  //参数动静态分离起效值
#define SCHMITT    30    //参数高低速分离SCHMITT参数
//#define SPHLSP     300  //参数高低速分离值
#define OVSLCN     200   //the overspeed lamp sparkle interval
#define MAXKI      75
//#define OFFSET   18432 //for calcalute max_sum_rot
#define INTIMES    1     //times of rotation and position cycle
#define V0CNV      450

#define LPKP 7
#define LPKD 7
#define PSEPA 0

#define LKSP 6
#define LKSD 5
#define RPSEPA 300

#define ILEN 12
#define OLEN 120

uchar inbuf[ILEN];
uchar *inlast; //= inbuf;
uchar *getlast; //= inbuf;
uchar inbufsign = 0;   //接收缓冲区非空标志,非空=1
uchar inbufful = 0;  //接收缓冲区满标志,满=1
uchar cmdstr[ILEN];
//uchar outbuf[OLEN];
//uchar *outlast = outbuf;
//uchar *putlast = outbuf;
  
struct toshow
{
  uint arg1;  //feedback rotation   
  uint arg2;  //AD1,for test   
  uint arg3;  //set position,from rotation cycle PID   
  uint arg4;  //feedback position   
  uint arg5;  //油量限制   
  uint arg6;  //set rotation   
  uint arg7;  //输出占空比  
  uint arg8;  //stpkp  
  uint arg9;  //position differential//stpkd  
  uint arg10; //kd_output 
  uint arg11; //stksp   
  uint arg12; //stksd  
  uint arg13;
  uint arg14;
  uint arg15;
  uint arg16;
  uint arg17;
  uint arg18;
  uint arg19;
  uint arg20;
  uint arg21;
  uint arg22;
  uint arg23;
} mnt;
struct limit_value
{
  uint lmt1;
  uint lmt2;
  uint lmt3;
  uint lmt4;
  uint lmt5;
  uint lmt6;
  uint lmt7;
  uint lmt8;
  uint lmt9;
  uint lmt10;
  uint lmt11;
  uint lmt12;
}oil,rot;
struct parameter
{
  uint rksp;   //高速pid
  uint rksi;
  uint rksd;
  uint rdrop;  //调速率
  uint rliml;  //最小油量
  uint rlims;  //启动油量
  uint rdynaw; //rotation cycle 加速度参数
  uint rlimh;  //最大油量
  uint rspdl;  //最小转速
  uint rspds;  //启动转速
  uint rsphlsp;  //高低转分
  uint rspdh;  //最大转速
  uint rbandw; //稳态阈值
  uint rpdynaw; //position cycle 加速度参数
  uint rksp1;  //低速pid
  uint rksi1;
  uint rksd1;
  uint rpkp;   //position pid
  uint rpki;
  uint rpkd;
  uint rt540;
  uint rt541;
  uint rt542;
  uint egrkp;
  uint egrki;
  uint egrkd;
}nrm,max,min;

#pragma DATA_SEG EEPROM_DATA
uint initflag;   //1:eeprom initilized 
struct parameter enrm,emax,emin;
struct limit_value eoil,erot;
char etemperature_value[26];
uint eoil_temperature_data[26];
uint egas_temperature_data[26];
uint erotation[10];
uint eposition[10];
uint eegr[10][10];
#pragma DATA_SEG DEFAULT

uint einitflag = 0; //

uchar sptemp = 0;//PC instruction
uchar summ = 0;  //the summation of the transmitted datas

uchar no_mrot_flag = 0;  //1:no main rotation
uchar no_srot_flag = 0;  //1:no sub. rotation
uchar no_rot_flag = 0; //1:no rotation
uchar first_ovrot_flag = 0; //1:the first over rotation take up
uchar second_ovrot_flag = 0;//1:the second over rotation take up
uchar button1_flag = 0; //1:test mode
uchar button2_flag = 0; //1:stop mode
uchar button3_flag = 0; //1:undefined
uchar button4_flag = 0; //1:undefined
uchar state_flag = STATF;
uchar rp_flag = 1; //1:do position cycle 0:do position cycle

uchar power_on = 0;//AB:power on

//uchar atdctl5 = 0x80; //Sample only one channel //ATD控制寄存器5

uint pre_tc0 = 0;//,current_TC0;

/*position variables*/
uint fb_egr = 0;  //egr阀位置反馈
uint pre_fb_egr = 0;
uint set_egr = 0;
uint pre_set_egr = 0;
uint delta_egr =0;
uint pre_delta_egr = 0;//for soft differential
int pkd_egr = 0;
int pre_pkd_egr = 0;//for soft acceleration
int pka_egr = 0;
long int sum_delta_egr = 0;
uint egr_outdata;

//uint hard_pkd = 0;
//uint hard_pka = 0;
uint fb_exe = 0;  //执行器位置反馈
uint pre_fb_exe = 0;
uint set_exe = 0;     //set position
uint pre_set_exe = 0;
int delta_exe = 0;
int pre_delta_exe = 0; //for soft differential
long int sum_delta_exe = 0;
long int max_sum_pos = 0x0000b000;
long int min_sum_pos = 0xffff5000;
int pkd_exe = 0;
int pre_pkd_exe = 0;//for soft acceleration
int pka_exe = 0;
uint exe_outdata;

uint limt1;  //油量限制

/*rotation variables*/
uint stpkp = LPKP;
uint stpkd = LPKD;

uint fb_rot = 0;    //反馈转速
uint pre_fb_rot = 0; //平滑滤波用
uint set_rot = LROT;	 //设定转速
uint pre_set_rot = 0;  //平滑滤波用
int delta_rot = 0; //
int pre_delta_rot = 0;  //for kd
uint drop_rot = 0;      //调速率
long int sum_delta_rot = 0;
long int max_sum_rot = 0;
long int min_sum_rot = 0;  //0xfffb0000;//-327680
int kd_output = 0;
int pre_kd_output = 0;   //for ka
int ka_output = 0;

//uint cak_fb_rot[8] = {0,0,0,0,0,0,0,0};
//process variables
uchar average_times;
int temp1,temp2,temp3,temp4,temp;
//int rtemp1,rtemp2,rtemp3,rtemp4,rtempx,rtemp;//,rtempki;
long int ltemp;
//long int rltemp;
//use in rti routine
uint *p;  
uint *q;                                                                            
uchar i; 
uchar j;
uint data1;//use in write_eeprom() and get_egr_position()
uint data2;
//use in main routine
//long int ltempp;
uint *pp; 
char *qq; //for analog_routine
uint *qqq; //for get_egr_position
uchar ii; 
uchar jj;
//uint data1;
//uint data2;
//uchar errtimes;
uint stksp;
uint stksi;
uint stksd;
uint caksp = LKSP; //get it through insert arithmetic 
uint caksd = LKSD;
uchar ovslc = OVSLCN; //超保指示灯闪烁间隔
uchar coun3 = 0;          //位置环执行次数
int x1,x2,y1,y2;
uint x;
int x11,x22,y11,y22;  //use in main routine
uint x12;

//analog variables
int temp_analog;
uint oil_temperature_atd = 0; // the atd data of lubricating oil's temperature
char oil_temperature = 0;     //lubricating oil's temperature 
char pre_oil_temperature = 0;

uint gas_temperature_atd = 0; //
char gas_temperature = 0;   //input gas temperature 
char pre_gas_temperature = 0;

uint water_temperature_atd = 0;
char water_temperature = 0; //cooling water temperature
char pre_water_temperature = 0;

//char temperature_value[26];
        // = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//uint oil_temperature_data[26];
        // = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//uint gas_temperature_data[26];
        // = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

uint gas_pressure_atd = 0;
uint gas_pressure = 0;  //空气压力
uint pre_gas_pressure = 0;

uint oil_pressure_atd = 0;
uint oil_pressure = 0;  //润滑油压力
uint pre_oil_pressure = 0;

uint rotation[10];// = {0,0,0,0,0,0,0,0,0,0};
uint position[10];// = {0,0,0,0,0,0,0,0,0,0};
uint egr[10][10];

//const uint OFFSET = 0x4800;
//const uint mode1_init[12]  = { 1,2,3,4,5,6,7,8,9,10,11,12 };
const uint MODE3_NRM[26] = { 20,    80,   25, 255,//rksp,rksi,rksd,rdrop,
                              5,     6,    2,   8,//rliml,rlims,rdynaw,rlimh,
                              9,    10,   11,  12,//rspdl,rspds,rspdm,rspdh,
                            100,     0,           //rbandw,rpdynaw,
                             20,    40,   30, //rksp1,rksi1,rksd1,high speed
                             15,    30,    8,   //rpkp,rpki,rpkd,
                           1000,  2000, 3000,//rt540,rt541,rt542,
                             15,    30,    8};   
                             
const uint MODE3_MAX[26] = {100,   100,  100, 300,    
                            100,   100,  100, 100,
                            100,   100,  100, 100,
                            100,   100,  
                            100,   100,  100,
                            100,   100,  100,
                           4000,  4000, 4000,
                            100,   100,  100};
                                 
const uint MODE3_MIN[26] = {  0,     0,    0,   0,
                              0,     0,    0,   0,
                              0,     0,    0,   0,
                              0,     0,
                              0,     0,    0,   
                              0,     0,    0,   
                              0,     0,    0,
                              0,     0,    0};

const uint MODE7_ROTLM[12] = {   0, 600, 800,1200,1500,1800,
                              2100,2400,2700,3000,3300,3520};   

const uint MODE7_OILLM[12] = {1023,1023,1023,1023,1023,1023,
                              1023,1023,1023,1023,1023,   0};

const char TEMPERATURE[26] =     {-25,-20,-15,-10, -5,  0,  5, 
                                   10, 15, 20, 25, 30, 35, 40, 
                                   45, 50, 55, 60, 65, 70, 
                                   75, 80, 85, 90, 95,100};

const uint WO_TEMPERATURE[26] =  {124,136,153,173,197,226,260,
                                  298,341,387,435,485,534,583,
                                  629,673,713,750,783,812,
                                  838,861,883,898,912,925};

const uint GAS_TEMPERATURE[26] = {264,267,271,276,283,291,301,
                                  313,327,343,362,383,407,434,
                                  462,493,524,556,589,621,
                                  653,684,713,741,766,790};

const uint ROTATION[10] = {300,600,900,1200,1500,1800,2100,2400,2700,3000};
const uint POSITION[10] = {100,200,300, 400, 500, 600, 700, 800, 900,1000};
const uint EGR[10][10] =  { 
                           0,0,300, 300, 300, 300, 300, 0, 0, 0,
                           0,0,300, 300, 300, 300, 300, 0, 0, 0,
                           0,0,300, 300, 300, 300, 300, 0, 0, 0, 
                           0,0,300, 300, 300, 300, 300, 0, 0, 0,
                           0,0,300, 300, 300, 300, 300, 0, 0, 0,
                           0,0,300, 300, 300, 300, 300, 0, 0, 0,
                           0,0,300, 300, 300, 300, 300, 0, 0, 0, 
                           0,0,300, 300, 300, 300, 300, 0, 0, 0, 
                           0,0,300, 300, 300, 300, 300, 0, 0, 0,
                           0,0,300, 300, 300, 300, 300, 0, 0, 0
                          };
const uchar ATDCTL5 = 0x80; //Sample only one channel //ATD控制寄存器5
                             

/******************************************************************************
* 函数名称:delay()																												
* 函数功能:延迟5ms																												
* 入口参数:无																																
* 出口参数:无																																
******************************************************************************/
void delay(void)  //delay about 5ms
{
  int i,j=1;
  for(i=1; i<2800; i++)
  {
    j++;
    j = j * i;
  }
}

/****************************************************************************
* 函数名称:init_para()																												
* 函数功能:parameter初始化:显示、模式3参数、油量转速限制																												
* 入口参数:无																																
* 出口参数:无																																
*****************************************************************************/
void init_para(void)
{
  //uint i;
  //uint *p;
  //uint *q;
  p = &nrm.rksp;
  q = &enrm.rksp;
  for(i=0; i<MODE3NI; i++)
  {
    *p = *q++;
    p++;
  }
  //
  p = &max.rksp;
  q = &emax.rksp;
  for(i=0; i<MODE3NI; i++)
  {
    *p = *q++;
    p++;
  }  
  //
  p = &min.rksp;
  q = &emin.rksp;
  for(i=0; i<MODE3NI; i++)
  {
    *p = *q++;
    p++;
  }   
  //
  p = &oil.lmt1;
  q = &eoil.lmt1;
  for(i=0; i<MODE7NI; i++)
  {
    *p = *q++;
    p++;
  }
  //
  p = &rot.lmt1;
  q = &erot.lmt1;
  for(i=0; i<MODE7NI; i++)
  {

⌨️ 快捷键说明

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