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

📄 main.c

📁 Keil 下编译的基于mcs51单片机的PCA82C250CAN收发器控制的程序源代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************
// @Module        Main
// @Filename     MAIN.C
// @Project       lac_can.dav
//----------------------------------------------------------------------------
// @Controller    Infineon C505C-2R
//
// @Compiler      Keil
//
// @Codegenerator DAvE 2-0-0
//
// @Description   This file contains the project initialization function
//                
//
//----------------------------------------------------------------------------
// @Date          01-2-24 12:51:12
//
//****************************************************************************
//****************************************************************************
// @Project Includes
//****************************************************************************
#include "MAIN.H"
#include "PER.H"
#include "CAN.H"
#include "INT.H"
#include "MSGTABLE.H"
//****************************************************************************
// @variable definition
//****************************************************************************
unsigned char xdata MsgTrans[20][8];  //message translated buffer
unsigned char xdata diagnostic[8];
bit ReceivedFlag;                  //receive ok flag
bit TurnLeft,TurnRight;
unsigned char xdata  * p;

unsigned char xdata CANSendCTimer=3;
////RS232 communication //////////////
bit NewReceive;//if a character is received,set this flag
unsigned int  xdata ReceiveTimer=0;//receive delay timer between two character
unsigned char xdata ReceiveBuffer[RXD_MAX_NUM];//
unsigned char xdata SendBuffer[TXD_MAX_NUM];
unsigned char xdata ReceiveCounter=0;
unsigned char xdata SendCounter=0;
bit NewCommand=0;//if a command have been received ,set this flag to indicator
				//the command is processing
unsigned char xdata SendNumber=0;//need sending byte number
unsigned char xdata SendLoop=1;
unsigned char xdata SendTimer=0;
unsigned int xdata HaveSignalTimer[2]={0,0};
bit ReceiveRight=0;
bit NoSignal1;
bit NoSignal2;
/////////////////////////////////////////////////////////////////////////////////
unsigned int xdata EngineSpeed=0;
unsigned int xdata VehicleSpeedAngle=0;//stepping motor rotating angle
unsigned int xdata VehicleSpeed=0;//run speed
unsigned int xdata EngineSpeedArray[5]={0,0,0,0,0};
unsigned int xdata VehicleSpeedAngleArray[5]={0,0,0,0,0};
unsigned int xdata EngineSpeedAverge=0;
unsigned int xdata EnginefreAverge=0;



unsigned int xdata WaterT=0;
unsigned int xdata WaterT1=0;
bit WaterTAlarm;//if water temperature is high than 100 degree,set the bit
unsigned char xdata FuelQ=0;
bit FuelAlarm;//fuel quantiy alarm flag
unsigned char xdata EnvironmentT[2]={0,0};
//unsigned char xdata SingleMileage[5];
//unsigned char xdata TotalMileage[6];
unsigned char xdata RunTime=0;
unsigned char xdata FuelUse=0;
unsigned char xdata RunErrorTimes=0;
//unsigned char xdata RunMileage[4];
unsigned char xdata WaterTTemp=0;//temp calculate result for filter
unsigned char xdata Water1TTemp=0;//temp calculate result for filter
unsigned char xdata FuelQTemp=0;//temp calculate result for filter
/////////////////////////////////////////////////////////////////////////////////
unsigned char xdata * ExamCodePointer;
unsigned char xdata  ErrorDataLength=0;
unsigned char xdata lamp[2]={0,0};//indication lamp
unsigned char xdata lamp0[5]={0,0,0,0,0};//20040922
unsigned char xdata lamp1[5]={0,0,0,0,0};//indication lamp

unsigned char xdata SwitchBuffer[2]={0,0};//
unsigned int xdata fre1=0;
unsigned int xdata fre2=0;
unsigned int xdata fre2cal=0;
unsigned char KeyBuffer=0;
unsigned char xdata bcd[6]={0,0,0,0,0,0};
unsigned char xdata GateTimer=0;
unsigned int xdata fre[4]={0,0,0,0};

float temp=0;
unsigned char Send_count=0;
unsigned char Send_count1=0;
unsigned char Send_count2=0;
unsigned char Send_obj=0;
unsigned char Send_data=0;
//////////////////////////////////////////////////////////////////////////////////
unsigned int code WaterTSensorTable[81]={ 23,  24,  25,  25,  26,  27,  28,  29,  30,  31,  31,  32,  33,  34,  35,  35,  36,  37,  38,  39,  39,  41,  43,  44,  45,  46,  48,  49,  51,  53,  54,  56,  58,  60,  61,  62,  63,  65,  67,  68,  69,  71,  73,  75,  77,  79,  81,  83,  85,  88,  91,  94,  97,  100, 103, 107, 110, 113, 115, 118, 120, 126, 132, 137, 142, 148, 153, 158, 163, 168, 173, 181, 190, 198, 206, 215, 229, 244, 258, 273, 287};
unsigned char code WaterTTable[81]= {     120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99,  98,  97,  96,  95,  94,  93,  92,  91,  90,  89,  88,  87,  86,  85,  84,  83,  82,  81,  80,  79,  78,  77,  76,  75,  74,  73,  72,  71,  70,  69,  68,  67,  66,  65,  64,  63,  62,  61,  60,  59,  58,  57,  56,  55,  54,  53,  52,  51,  50,  49,  48,  47,  46,  45,  44,  43,  42,  41,  40};
unsigned int code Water1TSensorTable[81]={ 57,  61,  65,  69,  73,  77,  81,  85,  89,  93,  97,  103,  107,  111,  115,  119,  123,  127,  131,  135,  139,  143,  147,  160,  175,  185,  195,  205,  215,  225,  235,  250,  265,  280,  295,  310,  325,  340,  355,  370,  385,  400,  415,  430,  445,  460,  475,  490,  505,  520,  535,  550,  565,  580, 595, 610, 625, 640, 655, 670, 685, 700, 715, 730, 760, 800, 840, 900, 960, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
unsigned char code Water1TTable[81]= {     150, 148, 146, 144, 142, 140, 138, 136, 134, 132, 130, 128, 126, 124, 122, 120, 118, 116, 114, 112, 110, 109,  108,  107,  106,  104,  102,  100,  98,  96,  94,  92,  90,  87,  86,  85,  84,  83,  82,  81,  80,  79,  78,  77,  76,  75,  74,  73,  72,  71,  70,  69,  68,  67,  66,  65,  64,  63,  62,  61,  60,  59,  58,  57,  56,  55,  54,  53,  52,  51,  50,  49,  48,  47,  46,  44,  40,  40,  40,  40,  40};
unsigned int xdata ADValue[8][8]={{0x3ff,0x3ff,0x3ff,0x3ff,0x3ff,0x3ff,0x3ff,0x3ff},{0,0,0,0,0,0,0,0}};
unsigned int xdata ADResult;
unsigned char xdata ADTimer;
float SensorValue;

unsigned char xdata SingleM;
float xdata TotalM;
unsigned char PowerOnTimer=0;
bit PowerOn=0;

float xdata StartVelocity;//start velocity per 10s
float xdata EndVelocity;//end velocity per 10s
float xdata MileageUnit;//mileague of 10s 
float xdata RunMileage;//mileage from engine starting to engine stopping
float xdata SingleMileage;//mileage from last clearing
float xdata TotalMileage;//total mileage
unsigned char xdata MileageTimer=0;//1s timer for calculating mileage
bit EngineStart;//engine start flag
unsigned int xdata SaveTimer;//10min auto save timer
bit SaveFlag;//
unsigned int xdata SlaveTimer1;//if slave have sent right message clear the timer
unsigned int xdata SlaveTimer2;//else if the counter value is more enought ,reset system

//unsigned char xdata ErrorCodeTimes[30];//error code times,if no error ErrorCodeTimes[]=0
								//else ErrorCodeTimes[]++,if ErrorCodeTimes[]>ERROR_TIMES
								//set the error bit
unsigned int xdata ClearDelayTimer1=0;//clear error code delay timer
unsigned int xdata ClearDelayTimer2=0;//clear single mileage delay timer
unsigned char xdata ClearOnTimer1=0;//clear switch on times
unsigned char xdata ClearOnTimer2=0;
bit ClearErrorFlag;
bit ClearMileageFlag;
unsigned char T0IntCounter;
unsigned char TH0Buffer;
unsigned char TL0Buffer;
unsigned char T0IntBuffer;
unsigned char EX1OffTimer;
bit EX1IntFlag;
unsigned char Fre2CalTimer;
bit Fre2CalFlag;
unsigned int xdata FuelQTimer;
unsigned int xdata WaterTimer;
bit  cantransflag=0;
////////////////////////////////数据滤波处理地址定义////////////////////////////
unsigned char  xdata  rainfilter0;
unsigned char  xdata  rainfilter1;
unsigned char  xdata  rainfilter2;
unsigned char  xdata  rainfilter3;
bit      rainfil0=0;
bit      rainfil1=0;
//****************************************************************************
// @funtion definition
//****************************************************************************

//****************************************************************************
//@function:    void delay(unsigned int time);
//@description: run this funtion code delay time(ms).
//@parameter:   "time" is the delay time ,unit ms.
//@return:none.
//**************************************************************************** 
void delay(unsigned int time)
{
   unsigned int delay1,delay2;
   for(delay1=0;delay1<=time;delay1++)     
   {
      for(delay2=0;delay2<=150;delay2++)     //1ms delay
      {
         cs5045=0;
		 _nop_();
	  	 cs5045=1;
	  	 wdt_refresh();
      }
   }
}

//***************************************************************************
//@Function:      void Hex_to_ASC(unsigned char * HexP,unsigned char * ASCP);
//@description:   This function change one byte hex to 2 byte ASCII code
//                first byte is high location
//@parameter:     Hexp---pointer for hex data; ASCP------pointer for ASC data
//@return:        none
//***************************************************************************
void Hex_to_ASC(unsigned char * HexP,unsigned char * ASCP)
{
	(*ASCP)=(unsigned char) ( (*HexP)/16 );
	if( (*ASCP)<=0x09 )
	{
		(*ASCP)+=0x30;
	}
	else
	{
		if( (*ASCP)<=0x0F )
		{
			(*ASCP)+=0x37;		
		}
	}
	
	ASCP++;
	
	(*ASCP)=(unsigned char)( (*HexP)%16 );
	if( (*ASCP)<=0x09 )
	{
		(*ASCP)+=0x30;
	}
	else
	{
		if( (*ASCP)<=0x0F )
		{
			(*ASCP)+=0x37;		
		}
	}
}
//***************************************************************************
//@Function:      void timer0_init(void);
//@description:   This function initialize and start up the time0.
//@parameter:     none  
//@return:        none
//***************************************************************************
//void timer0_init(void)
//{
//    TH0=T0_TH;
//    TL0=T0_TL;
//    TR0=1;
//}
//***************************************************************************
//@Function:      void init_timer2(void);
//@description:   This function initialize and start up the time1.
//@parameter:     none  
//@return:        none
//***************************************************************************
void init_timer2(void)
{
    TH2=T2_TH;
    TL2=T2_TL;
    T2CON=0x81; //Timer2 work at timer mode,is clocked with 1/12 oscillator frequency.
}

//***************************************************************************************
//function:     void init_comm(void)
//discription:  this function initialize communication
//parameter:    none
//return:       none
//***************************************************************************************
void init_comm(void)
{
	SCON=0x50;//communication mode 1
	PCON=0x00;
	TH1=0xfd;//20.83k
	TL1=0xfd;
	TR1=1;
}
//***************************************************************************
//@Function:      void Project_init(void);
//@description:   This function initializes the microcontroller.
//@parameter:     none  
//@return:        none
//***************************************************************************
void Project_Init(void)
{
	SYSCON = 0x20; //XMAP1=1, during access to XRAM or CAN controller WR and RD 
               		//become active.
                   //and P0 and P2 used as data bus and address bus.
                   //XMAP0=0, enable access to CAN controller.
                   // EALE=0, if EA pin is forced to ground  ALE is always generation 
                   // is enabled.
                   // else ALE is disable when access to code memory on chip.
  // ---------------- used peripherals -------------
 	CAN_vInit();
  /// initializes the CAN module
  	TMOD=0x21;           //Timer0 counter mode 1;Timer1 time mode 2 for RS232;
	TH0=0;
	TL0=0;
	TR0=0;
    init_timer2();
	init_comm();
	/// initializes A/D convertor 
  	ad_init(0x00); //P1.0~P1.7 used as analog input port
	INT_vInit();
	 // ---------------- SYSCON Register --------------   
}
//****************************************************************************************************
//function:		void int_to_bcd(unsigned char *pBCD,unsigned int Char);
//description:	convert int variable to 3 bits BCD code ,stored into buffer pointed by pBCD
//parameter:	pBCD---pointer of buffer to store BCD code 
//				Char---char  to be converted
//return:		none
//****************************************************************************************************
void int_to_bcd(unsigned char xdata *pBCD,unsigned int Char)
{
	*(pBCD+2)=Char/100;
	*(pBCD+1)=(Char%100)/10;
	*pBCD=Char%10;
}
//****************************************************************************************************
//function:		void float_to_bcd(unsigned char *pBCD,float Float);
//description:	convert float variable to 6 bits BCD code ,stored into buffer pointed by pBCD
//parameter:	pBCD---pointer of buffer to store BCD code 
//				Char---char  to be converted
//return:		none

⌨️ 快捷键说明

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