示例#1
0
void ComSendCmdWatch(u8 cmd,u8 par1,u8 par2,u8 par3) {
    u8 com_t_data[5] = {0,0,0,0,0};//前拨
    u8 com_t_data2[5] = {0,0,0,0,0};//前拨
	com_t_data[0] = cmd; //cmd
	com_t_data[1] = par1;
	com_t_data[2] = par2;
	com_t_data[3] = par3;
    com_t_data[4] = com_t_data[0]+com_t_data[1]+com_t_data[2]
                                    +com_t_data[3];
    INTOFF
    ComSend(com_t_data2);
	ComSend(com_t_data);
    INTEN
}
示例#2
0
文件: Main__.c 项目: prepare/gameswf
void SendCRLFPrompt( void )
{
 	ComStr[0] = 0x0d;
	ComStr[1] = 0x0a;
	ComStr[2] = '>';
	ComStr[3] = 0;
				
	ComSend( ComStr );
}  
示例#3
0
文件: Main__.c 项目: prepare/gameswf
void OutputComData( void )
{
		//Prep Com Data 
		ComStr[ 0 ] = 0;
		if( ComSequence == 0 )
		{
	      		if(( PINB & 0x08 ) != 0 ) // j5 is shorted economy mode
			{                
		        	sprintf( ComStr, "Ec-TH,%d,", cPWMVal );
		 	}
		 	else
		 	{
		        	sprintf( ComStr, "Pf-TH,%d,", cPWMVal );
		 	}
		}
		if( ComSequence == 1 )
		{
		       	sprintf( ComStr, " RPM,%d,", iMotorRPM );
		}
		if( ComSequence == 2 )
		{
			sprintf( ComStr, "BV,%d,", BatteryVoltage );
		}
		if( ComSequence == 3 )
		{
			sprintf( ComStr, " BC,%d,", BatteryCurrent );
		}
		if( ComSequence == 4 )
		{
			sprintf( ComStr, "CT,%d,", HeatSinkTemp );
		}
		if( ComSequence == 5 )
		{
			sprintf( ComStr, " PC,%d,", iPhaseCurrent );
		}
		if( ComSequence == 6 )
		{                         
		        tmpChar = hSensors;
			sprintf( ComStr, " HS,%d", tmpChar );          
			//sprintf( ComStr, " SW:%d,", SavedWattSeconds / 1000 );
		}  
		if( ComSequence == 7 )
		{
			//sprintf( ComStr, " CW:%d\r\n", CurrentWattSeconds / 1000 );
			sprintf( ComStr, "\r\n" );
			// isecTimer
		}  

		// Send Com Data
 		ComSend( ComStr );	
	        
}
示例#4
0
/*
*********************************************************************************************************
*	函 数 名: fputc
*	功能说明: 重定义putc函数,这样可以使用printf函数从串口1打印输出
*	形    参: 无
*	返 回 值: 无
*********************************************************************************************************
*/
int fputc(int ch, FILE *f)
{
	#if 1	/* 将需要printf的字符通过串口中断FIFO发送出去,printf函数会立即返回 */
		ComSend(COM6,(uint8_t *)&ch,1);
		return ch;
	#else	/* 采用阻塞方式发送每个字符,等待数据发送完毕 */
		/* 写一个字节到USART1 */
		USART_SendData(USART1, (uint8_t) ch);
		/* 等待发送结束 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
		{
			
		}
		return ch;
	#endif
}
/*------------------------------------------------------------
 * Function Name  : UartPrintChar
 * Description    : 打印一个字符
 * Input          : None
 * Output         : None
 * Return         : None
 *------------------------------------------------------------*/
static void UartPrintChar( const char ch )
{
	ComSend(COM6,(uint8_t *)&ch,1);
}
示例#6
0
文件: Main__.c 项目: prepare/gameswf
void main( void )
{                             
 
	PhasePtr = 1;
	PhaseErr = 0;    
	iTimerSOC = 0;
    
	// Declare your local variables here

	// Port A
	PORTA=0x00;
	DDRA=0x00;

	// Port B
	PORTB=0x00;
	DDRB=0x00;

	// Port C
	PORTC=0x00;
	DDRC=0xFF;

	// Port D
	PORTD = 0x00;
	DDRD=0xfc;

	// Timer/Counter 0 initialization
	//TCCR0=0x04;  //    CLK / 64
	//TCNT0=0x00;

	// Timer/Counter 1 initialization
	TCCR1A=0x00;
	TCCR1B=0x02; // CLK / 8
	TCNT1H=0xff;
	TCNT1L=0x00;

	// External Interrupt(s) initialization
	GIMSK=0x00;
	MCUCR=0x00;

	// Timer(s)/Counter(s) Interrupt(s) initialization
	TIMSK=0x04;

	// UART initialization
	UCR=0x98;   //0x18;
	UBRR=0x19;  //0x0c for 4 MHZ 0x19 for 8MHZ

	// Analog Comparator initialization
	ACSR=0x80;

	// ADC initialization
	ADMUX=ADC_VREF_TYPE;
	ADCSR=0x81;

	// Prep Commutation array for forward motion
	for( itmp = 0; itmp <= 7; itmp++ )
	{
		ActiveOut[ itmp ] = PhaseOutFWD[ itmp ];
	}
                    

	//TCNT0 = TCOUNT_0;
	TCNT1 = TCOUNT_1;

       
    	PORTC = 0;    
	PORTC = PhaseOutFWD[ 0 ];
	PORTC = PhaseOutFWD[ 0 ];

	PORTC = 0x40 | PhaseOutFWD[ 0 ];
	PORTC = 0x40 | PhaseOutFWD[ 0 ];
	PORTC = 0x00;                  
     	
     	// Global enable interrupts

	#asm("sei")

	LEDbyte = 0;
	OldLEDbyte = 0;
	      
	
	ComSequence = 0;
	DriveEnable = 1;
	
	CurrentLimitByte = 0;
	OvrTemp = 0;             
	
	isecTmr = 0;
	isecTimer = 0;  
	isecTmrWatts = 0;
		
	AutoUpdate = 1;
	iMaxEconoSpeed = 1025; 
     	BatteryPowerLow = 0;
	
	//PORTD = 0x00;

	MaxWattSeconds = __MAX_WATT_SECONDS; //1400000;
	
	sprintf( ComStr, _SOFTWARE_VERSION );
	ComSend( ComStr );
	
	// Turn off all lights...
	for( itmp = 0; itmp <= 5; itmp++ )
	{
		WriteDataToConsoleLEDS( SOC_LEDS[ itmp ] );
		itrDelay = 0;
		while( itrDelay < TIEMR_LED_SCAN ) 
		{
		} 
	 	
		//Read Phase Current
		AdcTempVal = read_adc( 1 );  
		iPhaseZero = AdcTempVal;
		AdcTempVal = read_adc( 1 );  
	         
		iPhaseZero = iPhaseZero + AdcTempVal;
		iPhaseZero = iPhaseZero / 2; 
		iPhaseZeroPoints[ itmp ] = iPhaseZero;
	}
	
       
	// Turn on all lights...
	WriteDataToConsoleLEDS( 0x00 );

	//////////////////////////////////////////////////////////////
	// Startup Flash lights... and Check the Battery Voltage...

	// Read Battery Voltage      
	BatteryVoltage = ReadBatteryVoltage();	      	

	itrDelay = 0;
	while( itrDelay < TIMER_HALF_SECOND ) 
	{
		
	}       
	// Read Battery Voltage      
	BatteryVoltage = BatteryVoltage + ReadBatteryVoltage();
	BatteryVoltage = BatteryVoltage / 2;
	                                
	
	//check low voltage on startup...
	// if voltage is too low, disable the bike...
	if( BatteryVoltage < __MIN_STARTUP_VOLTAGE )
	{
		// if battery is weak, shut it off ...                  
		sprintf( ComStr, "Low Voltage\r\n" );
		ComSend( ComStr );
		
     		ShutdownSystem();
     	}                           

//	sprintf( ComStr, "BV,%d,", BatteryVoltage );
  //	ComSend( ComStr );
 	
 	
	// do New cruise control init here...
	CruiseMode = 0;  // deefault is performance mode...
	if(( PINB & 0x08 ) != 0 ) // j5 is shorted economy mode
	{
	 	CruiseMode = 1;
	}
	
	ReadWattSecondsFromEEPROM();          
	CurrentWattSeconds = SavedWattSeconds;	
	// Reset watt / second counting... 
	// calculate the state of charge on the battery...
	if(( PINB & 0x10 ) == 0 ) // j6 is shorted = SLA mode
	{       
        	if(( BatteryVoltage > __NEW_SLA_VOLTAGE )&&( SavedWattSeconds < __USED_WATT_SECONDS ))  // consider this battery new...
		{      
			//Reset the Watt / Second counter here...
	 		CurrentWattSeconds = MaxWattSeconds;
		}
	}
	else
	{
        	if(( BatteryVoltage > __NEW_NIMH_VOLTAGE )&&( SavedWattSeconds < __USED_WATT_SECONDS ))  // consider this battery new...
		{
		 	//Reset the Watt / Second counter here...
		 	CurrentWattSeconds = MaxWattSeconds;		 	
		}	
	}	
	
	LowBatteryVoltage = __LOW_BATTERY_VOLTAGE;		


	// SHOW THE CURRENT STATE OF CHARGE ON THE DISPLAY
	LEDStatus = CalcStateOfChargeForConsole();
	

	calcLEDbyte = SOC_LEDS[ LEDStatus ]; 
	LEDbyte = calcLEDbyte;
	OldLEDbyte = LEDbyte;
		
	WriteDataToConsoleLEDS( LEDbyte );
	
	// start of main loop
	while( 1 )
      	{         
                // timer routine for "real time" clock, calculated second...
		isecTmr++;
		if( isecTmr > _CALC_SECOND )
 		{
			isecTimer++;
			isecTmrWatts++;
				
   			isecTmr = 0;
     		}                   		
		
      		CurrentLimitByte = 0;  // clear out the current limit control
      		if(( PINB & 0x08 ) != 0 ) // j5 is shorted economy mode
		{                
		 	CurrentLimitByte = 0x80;
		 	PhaseErr = 0;
		}

      		// Read The Throttle Demand 
	      	AdcPWMVal = read_adc( 7 );
      		if( AdcPWMVal > 700 )
	      	{
      			AdcPWMVal = 0;
	      	}                           
	      	if( AdcPWMVal > 511 )
	      	{
	      	 	AdcPWMVal = 511;
	      	}
      		ftmp = AdcPWMVal;
	      	vtmp = (float)(512 - ftmp);
      		vtmp = 1 / vtmp;
	      	ftmp = sqrt( vtmp );
      		ftmp = (ftmp * 360) - 16;
		if( ftmp < 0 )
		{
			ftmp = 0;
		}      	
      	
		itmp = (int)ftmp;     	
      	
	      	//itmp = AdcPWMVal>>2;
      		if( itmp > 63  ) //;;__PWM_RES_TOTAL )
      			itmp = 63; //__PWM_RES_TOTAL;
	        
	        //	oldPWMVal = tmpPWMVal;	
      		tmpPWMVal = ( unsigned char )itmp & 0x3f;
      	 
	 	// do the Speed calculations here.
		vtmp = iMotorSpeed;
		ftmp = TIMER_FREQ / vtmp;
		ftmp = ftmp * 15; // 4 halls / rev 60 revs / sec = RPM
		iMotorRPM = (int)ftmp;  

		if( iMotorRPM < 30 )
		{
			iMotorRPM = 0;
		}  
	
	
		//if(( tmpPWMVal > 6 )&&( iMotorRPM < 75 ))  // 
		if( iMotorRPM < 75 )
		{                     
			CurrentLimitByte = 0xc0; //CurrentLimitByte | 0x40;  // set at lowest limit...
		}                         
      	
	      	// Read Battery Voltage      
 	      	AvgBatteryVoltage = AvgBatteryVoltage + ReadBatteryVoltage();
	      	
	      	if(( ComSequence == 0 )||( ComSequence == 4 ))
	      	{
	      	 	BatteryVoltage = ( AvgBatteryVoltage / 4 ) + 25;	      	
	      	 	AvgBatteryVoltage = 0;
		}
		
		// if batter is too low start timer to shut it off...
	      	if( BatteryVoltage < LowBatteryVoltage )
      		{
      	 		DriveEnable++;
	      	}
      		else
	      	{      
      			if( DriveEnable < _MAX_LOW_VOLTAGE_TIME )
			{
	    	 		DriveEnable = 0;
		 	}
      		}	
      	
	      	//Read Phase Current
      		AdcTempVal = read_adc( 1 );     		
	      	iPhaseCurrent = AdcTempVal - iPhaseZeroPoints[ LEDStatus ];
		if( iPhaseCurrent < 0 )
		{
			iPhaseCurrent = 0;
		}
		
	      	ftmp = iPhaseCurrent;
	      	//PhaseCurrent = iPhaseCurrent;
	      	
                if( ftmp > 36 )
                {
                 	ftmp = ftmp * 0.9333;
                }
                BatteryCurrent = ftmp;
	      	if( BatteryCurrent > 52 )
	      	{
	      		BatteryCurrent = 52;
	      	}	      	
	      	if( BatteryCurrent <= 1 )
	      	{
	      		BatteryCurrent = 0;
	      	}
     

		// Caclulate heat sink temperature 
		AdcTempVal = read_adc( 2 ); 
		HeatSinkTemp = ConvertTempSensor( AdcTempVal );
    
    		if(( HeatSinkTemp > 62 )||( OvrTemp == 1 ))
	    	{        
                        OvrTemp = 1;
			CurrentLimitByte = 0xc0;
			if( tmpPWMVal > 27 )
			{
				tmpPWMVal = 27;
//				CurrentLimitByte = 0xc0;  // set at lowest limit...
			}		
    			// too hot... shut it off completely
    			if( HeatSinkTemp > 75 )
	    		{
    				tmpPWMVal = 0;
    			} 
    		}
    		if( HeatSinkTemp < 60 )
    		{
 	 	 	OvrTemp = 0;
    		}

                
    		
    		// do Throttle Reprofileing here
		if(( iMotorRPM > 300 )&&( iMotorRPM < 650 )) //900 ))
		{
		 	ftmp = iMotorRPM - 300;
		 	ftmp = ftmp / 15; // down to 37
		 	tmpChar = 63 - (char)ftmp;
 			if( tmpChar < tmpPWMVal )
 			{
 			 	tmpPWMVal = tmpChar;
 			}	 	
		}        
		if(( iMotorRPM >= 650 )&&( iMotorRPM < 1700 )) //if(( iMotorRPM >= 900 )&&( iMotorRPM < 1400 ))
		{
		 	ftmp = iMotorRPM - 650;  // 900
		 	ftmp = ftmp / 44; //34;
		 	tmpChar = 39 + (char)ftmp;
 			if( tmpChar < tmpPWMVal )
 			{
 			 	tmpPWMVal = tmpChar;
 			}	 	
		}

	      	// Calculate Battery Temperature
     	        /*
  		AdcTempVal = read_adc( 6 ); 
		BatteryTemp = ConvertTempSensor( AdcTempVal ); 	
      	        */
      	                     
      		if( AutoUpdate == 1 )
			OutputComData();	  
	  
		// perform delay for RS-232 output...
		itrDelay = 0;
		while( itrDelay < COM_SEQ_FREQ ) // )
		{
		}
		
		ComSequence++;
		if( ComSequence > 7 )
		{
			ComSequence = 0;
		}	                                                

		CalcCruiseControl();

	 	// Check low voltage here... 
 		if( DriveEnable >= _MAX_LOW_VOLTAGE_TIME )
	 	{
 		   	tmpPWMVal = 0; 
 		   	DriveEnable = _MAX_LOW_VOLTAGE_TIME;
			
			// shut the drive off
			ShutdownSystem();
	 	}      


		// set the throttle to the new value...		
		cPWMVal = tmpPWMVal;
		
		
		// count up the watts used every second and subtract from the total 
		// to calculate the state of charge...
		iTimerSOC++;		
		if( iTimerSOC > 15 )
		{         
    			// Calculate the Watts / 0.5 seconds / power consumption...
    			ftmp = BatteryVoltage / 100;
    			CurrentWattSeconds = CurrentWattSeconds - ((long)( BatteryCurrent ) * ftmp);
                	if( CurrentWattSeconds < 0 )
	                {
        	        	CurrentWattSeconds = 0;
                	} 
    		
		
		 	iTimerSOC = 0;
		 	
			LEDStatus = CalcStateOfChargeForConsole();

			calcLEDbyte = SOC_LEDS[ LEDStatus ];
			
		}
	       	OldLEDbyte = LEDbyte;	
                LEDbyte = calcLEDbyte;

		// make the lowest voltage blink here...
	     	if( calcLEDbyte == 0 )
	     	{       
		     	if( ComSequence < 2 )
	     		{       
	     			LEDbyte = 0x01;
	   		}
	     		else
	     		{
	     			LEDbyte = 0;
	     		}
       		}
       		else if( OvrTemp == 1 )
	     	{       
		     	if(( ComSequence & 0x03 ) == 1 )
	     		{       
	     			LEDbyte = calcLEDbyte;
	     			
				if( calcLEDbyte == 0 )
				{               
	     				LEDbyte = 0x01;
	   			}
	     		}
	     		else
	     		{
	     			LEDbyte = 0;
	     		}
       		}
       		       		                
		if( isecTmrWatts > 10 )
		{
		 	isecTmrWatts = 0;
		 	OldLEDbyte = 0; // update the LEDS every 10 seconds
		 	SaveWattSecondsToEPPROM();		 	
		}
       		       		
		// do led routines here...
	      	if( LEDbyte != OldLEDbyte )
	     	{
     		        // voltage is really low.... blink the red light...
#ifndef _DEBUG_FREQ_OUTPUT
       			WriteDataToConsoleLEDS( LEDbyte );
#endif

	     	}   	

		// Apply Current limit data here...                          
	     	tmpChar = PORTD;
	     	tmpChar = tmpChar & 0x3f;
	     	tmpChar = tmpChar | CurrentLimitByte;
		PORTD = tmpChar;
		
		
		// auto shut down timer and control
		if(( cPWMVal > 0 )&&( iMotorRPM > 0 ))
		{
			isecTimer = 0;
		}
		
		if( isecTimer > AUTO_SHUTOFF_TIME )
		{
  			ShutdownSystem();	
		}
			
        	// check com port and service any command there...
   		itmp = RecByteRdy;
		RecByteRdy = 0;
		if( itmp > 0 )
		{      
			ProcessCommand( ComRecBuff );
			SendCRLFPrompt();		
		}
      };
}