isr.c

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

C
892
字号
#include <m8c.h>    
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "para.h"
#include "adc.h"
#include "isr.h"
#include "main.h"
#include "i2c.h"

unsigned char get_motor_position(unsigned char count);
void hall_position_check(PROC_PROCESS *ppc,SYSSTAT *st);
//void get_ctrlout_set(PROC_PROCESS *ppc,SYSSTAT *st);

unsigned char get_motor_position(unsigned char count)
{
	unsigned i,j,k;
	k=0;
	i=count;
	while(i)
	{
		j=HALLINPUT;
		if(j==k)
			i--;
		else
		{
			k=j;
			i=count;
		}
	}
	return(k);
}
//####################################################################
void hall_position_check(PROC_PROCESS *ppc,SYSSTAT *st)
{
	unsigned char i,j,k,l;
	unsigned char temp_pos;
	temp_pos=get_motor_position(6);
	if(temp_pos!=ppc->msg.position_prev)
	{	i=temp_pos;
		temp_pos=get_motor_position(6);
		if(temp_pos!=i)
		{	if(temp_pos!=ppc->msg.position_prev)
			{
				i=temp_pos;
				temp_pos=get_motor_position(6);
				if(temp_pos==i)
				{
					ppc->msg.position_prev=temp_pos;
					st->msg.st2|=bitposition_change;//	st->bits.position_change=1;
				}
			}
		}
		else	
		{	ppc->msg.position_prev=temp_pos;
			st->msg.st2|=bitposition_change;//st->bits.position_change=1;
		}
	}
}

void set_next_ctrlseq(SYSSTAT *st,PROC_PROCESS *ppc)
{
		if((st->msg.st2&bitaltout_seqcfg))//st->bits.altout_seqcfg)
		{
			if(!(st->msg.st0&bithallphase60))//st->bits.hallphase60)
			{
				switch(ppc->msg.position_prev)
				{
					case 0x1:{
						ppc->msg.ctrlout_set=PWML2+PWMH0;//0x84;			
						ppc->msg.position_next=0x5;
						break;
					}
					case	0x5:{
						ppc->msg.ctrlout_set=PWML1+PWMH0;//0x44;
						ppc->msg.position_next=0x4;

						break;
					}
					case	0x4:{
						ppc->msg.ctrlout_set=PWML1+PWMH2;//0x50;					
						ppc->msg.position_next=0x6;

						break;
					}
					case  0x6:{
						ppc->msg.ctrlout_set=PWML0+PWMH2;//0x30;
						ppc->msg.position_next=0x2;

						break;
					}
					case	0x2:{
						ppc->msg.ctrlout_set=PWML0+PWMH1;//0x28;
						ppc->msg.position_next=0x3;
						break;
					}
					default:{
						ppc->msg.ctrlout_set=PWML2+PWMH1;//0x88;
						ppc->msg.position_next=0x1;
						break;
					}
				}
			}
			else
			{
				switch(ppc->msg.position_prev)
				{
					case 0x1:{
						ppc->msg.position_next=0x3;
						ppc->msg.ctrlout_set=PWML2+PWMH1;//0x88;
						break;
					}
					case	0x0:{
						ppc->msg.ctrlout_set=PWML0+PWMH1;//0x28;
						ppc->msg.position_next=0x1;

						break;
					}
					case	0x4:{
						ppc->msg.ctrlout_set=PWML0+PWMH2;//0x30;
						ppc->msg.position_next=0x0;

						break;
					}
					case  0x6:{
						ppc->msg.ctrlout_set=PWML1+PWMH2;//0x50;
						ppc->msg.position_next=0x4;

						break;
					}
					case	0x7:{
						ppc->msg.ctrlout_set=PWML1+PWMH0;//0x44;
						ppc->msg.position_next=0x6;

						break;
					}
					default:{
						ppc->msg.ctrlout_set=PWML2+PWMH0;//0x84;
						ppc->msg.position_next=0x7;

						break;
					}
				}
			}	
		}
		else
		{
			if(!(st->msg.st0&bithallphase60))//st->bits.hallphase60)//?
			{
				switch(ppc->msg.position_prev)
				{
					case 0x2:{
						ppc->msg.position_next=0x3;
						ppc->msg.ctrlout_set=PWML0+PWMH2;//0x30;
						break;
					}
					case	0x6:{
						ppc->msg.ctrlout_set=PWML1+PWMH2;//0x50;
						ppc->msg.position_next=0x2;
						break;
					}
					case	0x4:{
						ppc->msg.ctrlout_set=PWML1+PWMH0;//0x44;
						ppc->msg.position_next=0x6;

						break;
					}
					case  0x5:{
						ppc->msg.ctrlout_set=PWML2+PWMH0;//0x84;
						ppc->msg.position_next=0x4;

						break;
					}
					case	0x1:{
						ppc->msg.ctrlout_set=PWML2+PWMH1;//0x88;
						ppc->msg.position_next=0x5;

						break;
					}
					default:{
						ppc->msg.ctrlout_set=PWML0+PWMH1;//0x28;
						ppc->msg.position_next=0x1;
						break;}
				}
			}
			else
			{
				switch(ppc->msg.position_prev){
					case 0x0:{
						ppc->msg.ctrlout_set=PWML0+PWMH2;//0x30;
						ppc->msg.position_next=0x1;

						break;
					}
					case	0x4:{
						ppc->msg.ctrlout_set=PWML1+PWMH2;//0x50;
						ppc->msg.position_next=0x0;

						break;
					}
					case	0x6:{
						ppc->msg.ctrlout_set=PWML1+PWMH0;//0x44;
						ppc->msg.position_next=0x4;

						break;
					}
					case  0x7:{
						ppc->msg.ctrlout_set=PWML2+PWMH0;//0x84;
						ppc->msg.position_next=0x6;

						break;
					}
					case	0x3:{
						ppc->msg.ctrlout_set=PWML2+PWMH1;//0x88;
						ppc->msg.position_next=0x7;

						break;
					}
					default:{
						ppc->msg.position_next=0x3;
						ppc->msg.ctrlout_set=PWML0+PWMH1;//0x28;
						break;
					}
				}
			}	
	}
}
//####################################################################
void comp_isr(SYSSTAT *st,PROC_PROCESS *ppc)
{
	unsigned char i;
	if(CMP_CR0&0x10)
	{
		i=4;
		while(i) i--;
		if(CMP_CR0&0x10)
		{
			ppc->msg.pwmena=bitPWMON;
			ppc->msg.portena=0;
			ppc->msg.duty_cycle=0;
			ppc->msg.speed_profile=0;
			st->msg.st0|=bitCTRL_PORT_INACTIVE;		// st->bits.CTRL_PORT_INACTIVE=1;
			st->msg.st1|=bitovi_faultint;			// st->bits.ovi_faultint=1;
			st->msg.st0&=~bitcruise_on;				// st->bits.cruise_on=0;
			PWM_WritePulseWidth(ppc->msg.duty_cycle);
			motor_ctrlout(ppc,st);
		}
	}
}
//####################################################################
void pwm_isr(SYSSTAT *st,PROC_PROCESS *ppc,ADC *adr)
{
	
	unsigned int temp;
	unsigned char tt,tt1;

	ppc->msg.looptimer++;
	if(ppc->msg.looptimer>511) {
		st->msg.st3|=bitmain_loop_timerce;////st->bits.main_loop_timerce=1;
		ppc->msg.looptimer=0;
	}

	//step 1 judge ovi int fg
	// if T, proc the shutoff event 
	// clear interrupt flag
	// step 2 check hall position, set ctrlport_cfg para
	// calc velocity
	ppc->msg.cur_pos_ontime=ppc->msg.cur_pos_ontime>0x7fff? 0x7fff:ppc->msg.cur_pos_ontime+1;
	if(ppc->msg.cur_pos_ontime>0x200) 
		ppc->msg.prev_pos_ontime|=0x1ff;
//	I2CSBuff.b[0]=ppc->msg.speed_command;		
//	I2CSBuff.b[4]=ppc->msg.OV_MINRATEDI_ERR;
//	I2CSBuff.b[5]=adr->msg.bus_current;
//	I2CSBuff.b[6]=ppc->msg.LSPWM_SWITCH_I;
//	I2CSBuff.b[7]=ppc->msg.prev_pos_ontime;
//	I2CSBuff.b[3]=0xbb;
//	if((ppc->msg.looptimer&3)==0) {	
	if((ppc->msg.OV_MINRATEDI_ERR >1)||(ppc->msg.prev_pos_ontime<=0x100))
	{
		if(ppc->msg.speed_command>0xf0)
				if(ppc->msg.prev_pos_ontime>0x80)// normal load & slow velocity
					ppc->msg.speed_command=0xe0;
	}
//	else if(ppc->msg.prev_pos_ontime>=0x1ff) 
//	{	if(ppc->msg.speed_command>0xc0)
//			ppc->msg.speed_command=0xa0;
//	}
	else if(ppc->msg.speed_command>0xc0)
			ppc->msg.speed_command=0xa0;
	
	ADC_CR0|=ADC_CR0_ADC_START;
////	if(st->bits.position_change&&!st->bits.SSIDE_DRV_LEAKAGE_OVI)
//	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);	
////			ppc->msg.abs_ctrlportset=ppc->msg.ctrlout_set;			
////			if(st->bits.ABS_BRAKE_OK||((ppc->msg.duty_cycle==0)&&!st->bits.ABS_BRAKE_ENA))		
//			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;
////				ppc->msg.pwmena&=~bitPWMON;
//					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
//			{
//				ppc->msg.portena=0;
//				ppc->msg.pwmena=0;				
////				ppc->msg.pwmena&=~bitPWMON;
//				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);
//			}
////			ppc->msg.position_change_cnt++;//noted for block run
//		}
//		else // position alternative abnormal
//		{
////			if(reset_hallposition(st,ppc))
//			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++;
////				st->msg.st0|=bitCTRL_PORT_INACTIVE;
//			}
//		}
//		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;
//			}
//
//	  	}
//	  }
//	}
//	else  // position not change
	{
		if(adr->msg.samplechannel==BUSCURRENT)
		{
			// wait adc finished
			while(ADC_CR0&ADC_CR0_ADC_START){};
			adr->msg.bus_current=(adr->msg.bus_current+ADC_DL)>>1;
			if(st->msg.st1&bitovi_faultint)
			{
				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.st1&bitinvalid_position)//st->bits.invalid_position)
			{
				ppc->msg.speed_profile=0;
				ppc->msg.duty_cycle=ppc->msg.speed_profile;
				PWM_WritePulseWidth(ppc->msg.duty_cycle);				

⌨️ 快捷键说明

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