isr.c

来自「PSOC 电动自行车代码 器件采用CYPRESS新电动自行车器件CY8C245」· C语言 代码 · 共 892 行 · 第 1/2 页

C
892
字号
			}
			else if(st->msg.st2&bitBLOCK_RUN_P2S_OK)
			{
				ppc->msg.speed_profile=ppc->msg.speed_profile>4? ppc->msg.speed_profile-4:0;
				ppc->msg.duty_cycle=ppc->msg.speed_profile;
				PWM_WritePulseWidth(ppc->msg.duty_cycle);	
			}
			else if(st->msg.st0&bitABS_BRAKE_ENA)//st->bits.ABS_BRAKE_ENA)	
			{
				// abs brake process
				if(!(st->msg.st2&bitABS_BRAKE_OK))//st->bits.ABS_BRAKE_OK)
				{
					if(!(st->msg.st3&bitABSentry_set))
					{
//						if(!ppc->msg.speed_profile)
//						{if((ppc->msg.prev_pos_ontime>0x100)||(ppc->msg.cur_pos_ontime>0x100))
//							ppc->msg.duty_cycle=0x38;
//						else
//							ppc->msg.duty_cycle=0x1c;
//						}
//						else
//							ppc->msg.duty_cycle=0xff-ppc->msg.speed_profile;
						ppc->msg.duty_cycle=0x1c;
						ppc->msg.speed_profile=0;
						st->msg.st3|=bitABSentry_set;
						ppc->msg.portena=ppc->msg.ctrlout_set&MASKPWML;
						motor_ctrlout(ppc,st);	
					}
					if(st->msg.st0&bitCTRL_PORT_INACTIVE)//st->bits.CTRL_PORT_INACTIVE)
					{
						if(get_ctrlout_setni(ppc,st))
						{	
							st->msg.st1|=bitinvalid_position;	//st->bits.invalid_position=1;
							st->msg.st0|=bitCTRL_PORT_INACTIVE;	//st->bits.CTRL_PORT_INACTIVE=1;
							ppc->msg.portena=0;
							ppc->msg.pwmena=0;
							ppc->msg.speed_profile=0;
							ppc->msg.duty_cycle=0;
							PWM_WritePulseWidth(ppc->msg.duty_cycle);
							motor_ctrlout(ppc,st);
						}
						else		// modified 2007-10-13 11:32
						{
							st->msg.st0&=~bitCTRL_PORT_INACTIVE;
							set_next_ctrlseq(st,ppc);//???
						}
					}
					if((ppc->msg.prev_pos_ontime>0x4000)||(ppc->msg.cur_pos_ontime>0x4000))
					{
						st->msg.st2|=bitABS_BRAKE_OK;	//st->bits.ABS_BRAKE_OK=1;
						ppc->msg.abs_delaycount=0;
						st->msg.st2&=~bitABS_BRAKE_FINALDELAY;
						st->msg.st2&=~bitABS_DOTBRAKE;
						st->msg.st0|=bitCTRL_PORT_INACTIVE;	//st->bits.CTRL_PORT_INACTIVE=1;
						ppc->msg.portena=0;
						ppc->msg.pwmena=0;
						ppc->msg.speed_profile=0;
						ppc->msg.duty_cycle=0;
						ppc->msg.abs_delaycount=0;
						PWM_WritePulseWidth(ppc->msg.duty_cycle);
						motor_ctrlout(ppc,st);						
					}
					else
					{
						ppc->msg.abs_delaycount++;
						if((ppc->msg.abs_delaycount&0x3f)==0)
						{
							if(adr->msg.bus_current>ppc->msg.LEBMF_CURRENT)
								ppc->msg.duty_cycle=ppc->msg.duty_cycle<0xf8? ppc->msg.duty_cycle+2:0xf8;
							else if((ppc->msg.LEBMF_CURRENT-adr->msg.bus_current)<8)
								ppc->msg.duty_cycle=ppc->msg.duty_cycle<0xf8? ppc->msg.duty_cycle++:0xf8;
							else if(ppc->msg.HEBMF_CURRENT>adr->msg.bus_current)
								ppc->msg.duty_cycle=ppc->msg.duty_cycle>0x10? ppc->msg.duty_cycle--:0x10;	
								
						}
					
					}
					PWM_WritePulseWidth(ppc->msg.duty_cycle);
				}	
				else
				{
					st->msg.st0|=bitCTRL_PORT_INACTIVE;
//					st->msg.st1|=bitinvalid_position;//st->bits.invalid_position=1;
					ppc->msg.portena=0;
					ppc->msg.pwmena=0;
					st->msg.st3&=~bitABSentry_set;
					ppc->msg.speed_profile=0;
//					PWM_WritePulseWidth(0);
					motor_ctrlout(ppc,st);
					ppc->msg.duty_cycle=ppc->msg.speed_profile;
					PWM_WritePulseWidth(ppc->msg.duty_cycle);	
				}
			}	
			else
			{
				if(st->msg.st0&bitBRAKE_VALID)//st->bits.BRAKE_VALID)
				{
					ppc->msg.speed_profile=ppc->msg.speed_profile>4? ppc->msg.speed_profile-4:0;
				}	
				else 
				{
					if(ppc->msg.duty_cycle==0)
					{
						ppc->msg.speed_profile=0;					
						if(get_ctrlout_setni(ppc,st))
						{	st->msg.st1|=bitinvalid_position;//st->bits.invalid_position=1;
							st->msg.st0|=bitCTRL_PORT_INACTIVE;//st->bits.CTRL_PORT_INACTIVE=1;						
							ppc->msg.portena=0;
							ppc->msg.pwmena=0;
							ppc->msg.speed_profile=0;
							motor_ctrlout(ppc,st);
//							return;
						}
						else
						{	
							ppc->msg.portena=0;//
							motor_ctrlout(ppc,st);
							set_next_ctrlseq(st,ppc);
						}
					}
					else if(st->msg.st0&bitCTRL_PORT_INACTIVE)
					{
						if(get_ctrlout_setni(ppc,st))
						{	st->msg.st1|=bitinvalid_position;//st->bits.invalid_position=1;
							ppc->msg.portena=0;
							ppc->msg.pwmena=0;
							ppc->msg.speed_profile=0;
							motor_ctrlout(ppc,st);
						}
						else
						{	st->msg.st0&=~bitCTRL_PORT_INACTIVE;
							ppc->msg.portena=ppc->msg.ctrlout_set;	
							motor_ctrlout(ppc,st);												
							set_next_ctrlseq(st,ppc);						
						}
					}
					if(!(st->msg.st1&bitinvalid_position)){
					if(adr->msg.bus_current<ppc->msg.LSPWM_SWITCH_I)
					{
						ppc->msg.pwmena&=~bitLPWMON;// disable low side pwm enable
						ppc->msg.OV_MINRATEDI_ERR=0;
					}
					else
					{
						ppc->msg.OV_MINRATEDI_ERR=adr->msg.bus_current-ppc->msg.LSPWM_SWITCH_I;
						ppc->msg.pwmena|=bitLPWMON;// enable low side pwm enable
					}	
				
					if(adr->msg.bus_current<ppc->msg.RATED_CURRENT)// not ovi rated current process
					{
						ppc->msg.countgtr=0;
						if(ppc->msg.speed_command>ppc->msg.speed_profile)// increase output ctrl level
						{
							if((ppc->msg.RATED_CURRENT-adr->msg.bus_current)<6)
							{
								ppc->msg.count++;
								if(ppc->msg.count>0x40)
								{
									ppc->msg.count=0;
									ppc->msg.speed_profile++;
								}
							}	
							else{
								ppc->msg.count++;
								if(ppc->msg.count>32)
								{
									ppc->msg.count=0;
									ppc->msg.speed_profile++;
								}
//								ppc->msg.speed_profile++;
								if(ppc->msg.speed_profile<0x8) ppc->msg.speed_profile=0x8;
								ppc->msg.portena=ppc->msg.ctrlout_set;
							}
						}	
						else// decrease output control level
						{
							if(ppc->msg.speed_command<(ppc->msg.speed_profile-1))
							{
								ppc->msg.count++;
								if(ppc->msg.count>32)
								{
									ppc->msg.count=0;
									ppc->msg.speed_profile--;
								}							
//								ppc->msg.speed_profile-=1;
							}	
							else
								ppc->msg.speed_profile=ppc->msg.speed_command;
						}		
								
					}
					else
					{
						ppc->msg.count=0;
						ppc->msg.OV_RATEDT_ERR=adr->msg.bus_current-ppc->msg.RATED_CURRENT;
						adr->msg.bus_current=ppc->msg.RATED_CURRENT;//note modified 2007-102811
						if(ppc->msg.OV_RATEDT_ERR>6)
						{
							ppc->msg.speed_profile=ppc->msg.speed_profile>3? ppc->msg.speed_profile-3:0;
							ppc->msg.countgtr=0;
						}	
						else
						{
							ppc->msg.countgtr++;
//							if(ppc->msg.countgtr>16)
							{	ppc->msg.speed_profile=ppc->msg.speed_profile>0? ppc->msg.speed_profile-1:0;
								ppc->msg.countgtr=0;
							}
						}	
					}
				}		
				// update pwm dutycycle
				if(adr->msg.bus_current<ppc->msg.LSPWM_SWITCH_I)
				{
					ppc->msg.pwmena&=~bitLPWMON;// disable low side pwm enable
				}
				// set pwm duty_cycle, start pwm output with port out enable

				ppc->msg.duty_cycle=ppc->msg.speed_profile;
				ppc->msg.pwmena|=bitPWMON;
				PWM_WritePulseWidth(ppc->msg.duty_cycle);
				if(ppc->msg.duty_cycle==0xff) ppc->msg.pwmena=0;
				motor_ctrlout(ppc,st);			}//invalid_position
			}
		}
		else if(adr->msg.samplechannel==BATTERY)
		{
			while(ADC_CR0&ADC_CR0_ADC_START){};
			adr->msg.battery_voltage=(adr->msg.battery_voltage+ADC_DL)>>1;
		}
		else if(adr->msg.samplechannel==TS)
		{
			while(ADC_CR0&ADC_CR0_ADC_START){};
			adr->msg.ts_voltage=(adr->msg.ts_voltage+ADC_DL)>>1;
		}
		else if(adr->msg.samplechannel==BRAKE)
		{
			while(ADC_CR0&ADC_CR0_ADC_START){};
			
			adr->msg.brake_voltage=(adr->msg.brake_voltage+ADC_DL)>>1;
//			if((!st->bits.SSIDE_DRV_LEAKAGE_OVI&&((st->bits.BRAKE_LEVEL&&(adr->msg.brake_voltage>0x66))||	\
//												(!st->bits.BRAKE_LEVEL&&(adr->msg.brake_voltage<0x70))))||st->bits.SSIDE_DRV_LEAKAGE_OVI)
			if((!(st->msg.st1&bitSSIDE_DRV_LEAKAGE_OVI)&&(adr->msg.brake_voltage<0x70))||(st->msg.st1&bitSSIDE_DRV_LEAKAGE_OVI))
			{
				st->msg.st0&=~bitcruise_on;//st->bits.cruise_on=0;
				st->msg.st0|=bitBRAKE_VALID;//st->bits.BRAKE_VALID=1;
				ppc->msg.speed_command=0;
				if(st->msg.st0&bitabs_ena)//st->bits.abs_ena)
					st->msg.st0|=bitABS_BRAKE_ENA;//st->bits.ABS_BRAKE_ENA=1;		
				
			}
			else
			{
				if(st->msg.st0&bitBRAKE_VALID)
				{
					ppc->msg.portena=0;
					ppc->msg.pwmena=0;
					ppc->msg.duty_cycle=0;
					ppc->msg.speed_profile=0;
					PWM_WritePulseWidth(ppc->msg.duty_cycle);
					motor_ctrlout(ppc,st);
				}
				st->msg.st0&=~bitBRAKE_VALID;//st->bits.BRAKE_VALID=0;
				st->msg.st0&=~bitABS_BRAKE_ENA;//  st->bits.ABS_BRAKE_ENA=0;
  	      		st->msg.st2&=~bitABS_BRAKE_OK;//st->bits.ABS_BRAKE_OK=0;
        		st->msg.st2&=~bitABS_BRAKE_FINALDELAY;//       st->bits.ABS_BRAKE_FINALDELAY=0;
        		st->msg.st2&=~bitABS_DOTBRAKE;
				st->msg.st3&=~bitABSentry_set;
        		ppc->msg.abs_delaycount=0;
			}		
		}	
	}
	// set next analog signal sampling config label_0066;
	//......
		if((ppc->msg.looptimer&0x7f)==0)  // set sample brake voltage
	{
		// set sample channel: brake voltage
		// start sample
		// set sample channel code
		ADC_CR0=(ADC_CR0&7)|brake_in_chan;
		adr->msg.samplechannel=BRAKE;
		}	
	else if((ppc->msg.looptimer&0x3f)==0)
	{
		// set sample channel: ts voltage
		// start sample
		// set sample channel code
		ADC_CR0=(ADC_CR0&7)|ts_chan;
		adr->msg.samplechannel=TS;

	}
	else if((ppc->msg.looptimer&0x1f)==0)
	{
		// set sample channel: battery voltage
		// start sample
		// set sample channel code
		ADC_CR0=(ADC_CR0&7)|battery_v_chan;
		adr->msg.samplechannel=BATTERY;
		
	}
	else
	{
		// set sample channel: bus current
		// start sample
		// set sample channel code
		ADC_CR0=(ADC_CR0&7)|buscurrent_chan;
		adr->msg.samplechannel=BUSCURRENT;
	}
	
}
	
void hall_position_isr(PROC_PROCESS *ppc,SYSSTAT *st,ADC *adr)
{
	hall_position_check(ppc,st);
	if(st->msg.st2&bitposition_change)
	{
	  st->msg.st2&=~bitposition_change;		//st->bits.position_change=0;	
	  if(st->msg.st1&bitSSIDE_DRV_LEAKAGE_OVI)
	  {
		ppc->msg.portena=0;
		ppc->msg.pwmena=0;
		st->msg.st0|=bitCTRL_PORT_INACTIVE;//st->bits.CTRL_PORT_INACTIVE=1;
		motor_ctrlout(ppc,st);
	  }
	  else
	  {
		if(ppc->msg.position_prev==ppc->msg.position_next)
		{
			set_next_ctrlseq(st,ppc);	
			if(!(st->msg.st2&bitABS_BRAKE_OK))
			{	if((ppc->msg.duty_cycle!=0)||(st->msg.st0&bitABS_BRAKE_ENA))
				{
					st->msg.st0&=~bitCTRL_PORT_INACTIVE;
					if(st->msg.st0&bitABS_BRAKE_ENA)//st->bits.ABS_BRAKE_ENA)
					{
						ppc->msg.pos_chk_cnt1=0;
						ppc->msg.portena=ppc->msg.ctrlout_set&MASKPWML;
						motor_ctrlout(ppc,st);	
					}
					else
					{
						ppc->msg.pos_chk_cnt1=0;
						ppc->msg.portena=ppc->msg.ctrlout_set;
						motor_ctrlout(ppc,st);
						if(ppc->msg.duty_cycle!=0xff)
						{
							ppc->msg.pos_chk_cnt2=0;	
							if(ppc->msg.speed_profile>0x38)//>note modified 2007-10-13 11:04
							{
								if(adr->msg.bus_current>(ppc->msg.LSPWM_SWITCH_I+4))
								{
									if(adr->msg.bus_current>ppc->msg.RATED_CURRENT)
										adr->msg.bus_current=ppc->msg.RATED_CURRENT;
									ppc->msg.pwmena=0;
									motor_ctrlout(ppc,st);
									adr->msg.bus_current+=(((adr->msg.bus_current-ppc->msg.LSPWM_SWITCH_I)>>4)&7);
									adr->msg.samplechannel=BUSCURRENT;
									while(1)
									{	ADC_CR0=buscurrent_chan|ADC_CR0_ADC_EN|ADC_CR0_ADC_START;
										if(ppc->msg.pos_chk_cnt2<=0x10)
										{
											while(ADC_CR0&ADC_CR0_ADC_START) {}
											if(adr->msg.bus_current<ADC_DL) break;
											ppc->msg.pos_chk_cnt2++;
										}
										else
										{
											if(ppc->msg.pos_chk_cnt1<=4) break;
											else if(ppc->msg.pos_chk_cnt2<=0x18)
											{
												while(ADC_CR0&ADC_CR0_ADC_START) {}
												if(adr->msg.bus_current<ADC_DL) break;
												ppc->msg.pos_chk_cnt2++;
											}
											else if(ppc->msg.pos_chk_cnt2<=0x20) break;
											
										}		
									}
									ppc->msg.pwmena|=bitPWMON;
									motor_ctrlout(ppc,st);
								}	
							}	
						}
					}
				}
				else
				{
					ppc->msg.portena=0;
					ppc->msg.pwmena=0;
					st->msg.st0|=bitCTRL_PORT_INACTIVE;//st->bits.CTRL_PORT_INACTIVE=1;
					motor_ctrlout(ppc,st);
				}
			}
			else
			{
				ppc->msg.portena=0;
				ppc->msg.pwmena=0;				
				st->msg.st0|=bitCTRL_PORT_INACTIVE;//st->bits.CTRL_PORT_INACTIVE=1;
				ppc->msg.duty_cycle=0;
				PWM_WritePulseWidth(ppc->msg.duty_cycle);
				motor_ctrlout(ppc,st);
			}
		}
		else // position alternative abnormal
		{
			if(get_ctrlout_setni(ppc,st))
			{	
				st->msg.st1|=bitinvalid_position;//st->bits.invalid_position=1;
				st->msg.st0|=bitCTRL_PORT_INACTIVE;//st->bits.CTRL_PORT_INACTIVE=1;			
				ppc->msg.portena=0;
				ppc->msg.pwmena=0;
				ppc->msg.speed_profile=0;
				ppc->msg.duty_cycle=0;
				inactive_ctrl(st,ppc);
			}
			else{
				set_next_ctrlseq(st,ppc);
				ppc->msg.position_preset_errcnt++;
			}
		}
		if(!(st->msg.st1&bitinvalid_position))
		{	
		// preset next position & ctrlport label_00A7
			ppc->msg.position_change_cnt++;
			if(ppc->msg.position_change_cnt>0x200) ppc->msg.position_change_cnt=0x200;
			ppc->msg.position_change_cnt_unit++;
			ppc->msg.prev_pos_ontime=ppc->msg.cur_pos_ontime;
			ppc->msg.cur_pos_ontime=0;
			st->msg.st1&=~bitinvalid_position;		//st->bits.invalid_position=0;
			if(ppc->msg.position_change_cnt_unit>12) 
			{	
				if(ppc->msg.position_preset_errcnt>8)
				{
//					st->msg.st2^=bitaltout_seqcfg;//st->bits.altout_seqcfg^=1;
//			   		I2CSBuff.b[7]=0xdd;//adc.msg.bus_current;		
				}
//				else
				ppc->msg.position_change_cnt_unit=0;
				ppc->msg.position_preset_errcnt=0;
			}

	  	}
	  }
	}
}	

⌨️ 快捷键说明

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