isrv1.c

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

C
665
字号
#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=PRT2DR&HALLMASK;
		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(4);
	if(temp_pos!=ppc->msg.position_prev)
	{	i=temp_pos;
		temp_pos=get_motor_position(4);
		if(temp_pos!=i)
		{	if(temp_pos!=ppc->msg.position_prev)
			{
				i=temp_pos;
				temp_pos=get_motor_position(4);
				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 comp_isr(SYSSTAT *st,PROC_PROCESS *ppc)
{
	//disbale global io out
	//....	
	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);
//		PRT1GS = (PRT1GS & 0x33);
//		PRT2GS = (PRT2GS & 0x3f);
//		PRT1DR = (PRT1DR & 0x33)|0x88;
//		PRT2DR = (PRT2DR & 0x3f)|0x80;
	motor_ctrlout(st,ppc);
	// clear interrupt flag
}
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;
	}
	ADC_CR0|=ADC_CR0_ADC_START;
	//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;
	if(ppc->msg.OV_MINRATEDI_ERR >2)
	{
		if(ppc->msg.prev_pos_ontime>0x140)//  heavy load & slow velocity
		{	if(ppc->msg.speed_command>0xa0)
				ppc->msg.speed_command=0x80;
		}		
	}
	else
	{	
		if(ppc->msg.speed_command>0xf0)
				if(ppc->msg.prev_pos_ontime>0xa0)// normal load & slow velocity
					ppc->msg.speed_command=0xe0;
	}
	if(st->msg.st0&bitCTRL_PORT_INACTIVE)
	{
		if(get_ctrlout_setni(&ppc,&st))
		{	inactive_ctrl(st,ppc);
			st->msg.st1|=bitinvalid_position;
			return;
		}
		st->msg.st2&=~bitposition_change;
	}	
	if(st->msg.st1&bitSSIDE_DRV_LEAKAGE_OVI)//st->bits.SSIDE_DRV_LEAKAGE_OVI)
	{
		 	ppc->msg.duty_cycle=0;
		 	inactive_ctrl(st,ppc);
		 	return;
	}
	else
	{
//	if(st->bits.position_change&&!st->bits.SSIDE_DRV_LEAKAGE_OVI)
		if(st->msg.st2&bitposition_change)
		{
			ppc->msg.position_change_cnt++;
			ppc->msg.position_change_cnt_unit++;
			st->msg.st1&=~bitinvalid_position;		//st->bits.invalid_position=0;
			st->msg.st2&=~bitposition_change;		//st->bits.position_change=0;	
			if(ppc->msg.position_prev==ppc->msg.position_next)
			{
//			if(st->bits.ABS_BRAKE_OK||((ppc->msg.duty_cycle==0)&&!st->bits.ABS_BRAKE_ENA))		
				if((st->msg.st2&bitABS_BRAKE_OK)||((ppc->msg.duty_cycle==0)&&!(st->msg.st0&bitABS_BRAKE_ENA)))
				{
					ppc->msg.portena=0;
//					ppc->msg.pwmena&=~bitPWMON;
					st->msg.st0|=bitCTRL_PORT_INACTIVE;//st->bits.CTRL_PORT_INACTIVE=1;
				}	
				else
				{
					st->msg.st0&=~bitCTRL_PORT_INACTIVE;//st->bits.CTRL_PORT_INACTIVE=0;
					
					if(st->msg.st0&bitABS_BRAKE_ENA)//st->bits.ABS_BRAKE_ENA)
					{
						ppc->msg.portena=(ppc->msg.ctrlout_set<<3)&0xe0;
						if(st->msg.st2&bitABS_BRAKE_FINALDELAY)//st->bits.ABS_BRAKE_FINALDELAY)
							ppc->msg.portena=0xe0;
					}
					else
					{
						ppc->msg.portena=ppc->msg.ctrlout_set;
						
					}	
					if(ppc->msg.duty_cycle!=0xff)
					{
						if(ppc->msg.speed_profile>0x1b)
						{
							if(adr->msg.bus_current>(ppc->msg.LSPWM_SWITCH_I+4))
							{
								tt=0;
								ppc->msg.pwmena=3;
//								ppc->msg.pwmena=0;
								ppc->msg.duty_cycle=0xff;
								motor_ctrlout(st,ppc);
								adr->msg.bus_current+=(adr->msg.bus_current-ppc->msg.LSPWM_SWITCH_I);
								while(tt<0x20)
								{	ADC_CR0=buscurrent_chan|ADC_CR0_ADC_EN|ADC_CR0_ADC_START;
									while(ADC_CR0&ADC_CR0_ADC_START);
//									{}
									if(adr->msg.bus_current<ADC_DL) break;
									tt++;
								}
								ppc->msg.duty_cycle=ppc->msg.speed_profile;
//								ppc->msg.pwmena|=bitPWMON;
							}	
						}	
					}
			}
			motor_ctrlout(st,ppc);
		}	
		else
		{
			if(get_ctrlout_setni(&ppc,&st))
			{	inactive_ctrl(st,ppc);
				st->msg.st1|=bitinvalid_position;
				return;
			}
/*			
//			reset_hallposition(st,ppc);
			if(!(st->msg.st0&bithallphase60))//st->bits.hallphase60)
			{	if((ppc->msg.position_prev==0x00)||(ppc->msg.position_prev==0x15))
				{
					st->msg.st1|=bitinvalid_position;//st->bits.invalid_position=1;
					ppc->msg.portena=0;
//					ppc->msg.pwmena=0;
					ppc->msg.speed_profile=0;
					ppc->msg.duty_cycle=0;//speed_profile
					motor_ctrlout(st,ppc);
//					PWM_WritePulseWidth(0);
//					PRT1GS = (PRT1GS & 0x33);
//					PRT2GS = (PRT2GS & 0x3f);
//					ppc->msg.PRT1DRbak = (ppc->msg.PRT1DRbak & 0x33)|0x88;
//					ppc->msg.PRT2DRbak = (ppc->msg.PRT2DRbak & 0x3f)|0x80;
//					PRT1DR=ppc->msg.PRT1DRbak;
//					PRT2DR=ppc->msg.PRT2DRbak;
					return;
				}	
			}
			else
			{
				if((ppc->msg.position_prev==0x04)||(ppc->msg.position_prev==0x11)){
					st->msg.st1|=bitinvalid_position;//st->bits.invalid_position=1;
					ppc->msg.portena=0;
//					ppc->msg.pwmena=0;
					ppc->msg.speed_profile=0;
					ppc->msg.duty_cycle=0;//speed_profile
					motor_ctrlout(st,ppc);

//					PWM_WritePulseWidth(0);
//					PRT1GS = (PRT1GS & 0x33);
//					PRT2GS = (PRT2GS & 0x3f);
//					ppc->msg.PRT1DRbak = (ppc->msg.PRT1DRbak & 0x33)|0x88;
//					ppc->msg.PRT2DRbak = (ppc->msg.PRT2DRbak & 0x3f)|0x80;
//					PRT1DR=ppc->msg.PRT1DRbak;
//					PRT2DR=ppc->msg.PRT2DRbak;
					return;
				}
			}	
*/
			// resetting nextposition & ctrl port

			ppc->msg.position_preset_errcnt++;
			//change hall alternate sequence number{...};
		}
		ppc->msg.prev_pos_ontime=ppc->msg.cur_pos_ontime;
		ppc->msg.cur_pos_ontime=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;
				}
				ppc->msg.position_change_cnt_unit=0;
				ppc->msg.position_preset_errcnt=0;	
		}	
		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.position_next=0x5;
					ppc->msg.ctrlout_set=0x28;
					break;
				}
				case	0x5:{
					ppc->msg.position_next=0x4;
					ppc->msg.ctrlout_set=0x30;
					break;
				}
				case	0x4:{
					ppc->msg.position_next=0x14;
					ppc->msg.ctrlout_set=0x50;
					break;
				}
					
				case  0x14:{
					ppc->msg.position_next=0x10;
					ppc->msg.ctrlout_set=0x44;
					break;
				}
				case	0x10:{
					ppc->msg.position_next=0x11;
					ppc->msg.ctrlout_set=0x84;
					break;
				}
				default:{
					ppc->msg.position_next=0x1;
					ppc->msg.ctrlout_set=0x88;
					break;}
				}
			}
			else
			{
				switch(ppc->msg.position_prev){
				case 0x5:{
					ppc->msg.position_next=0x1;
					ppc->msg.ctrlout_set=0x28;
					break;
				}
				case	0x1:{
					ppc->msg.position_next=0x0;
					ppc->msg.ctrlout_set=0x30;
					break;
				}
				case	0x0:{
					ppc->msg.position_next=0x10;
					ppc->msg.ctrlout_set=0x50;
					break;
				}
				case  0x10:{
					ppc->msg.position_next=0x14;
					ppc->msg.ctrlout_set=0x44;
					break;
				}
				case	0x14:{
					ppc->msg.position_next=0x15;
					ppc->msg.ctrlout_set=0x84;
					break;
				}
				default:{
					ppc->msg.position_next=0x5;
					ppc->msg.ctrlout_set=0x88;
					break;}
				}
			}	
		}
		else
		{
			if(!(st->msg.st0&bithallphase60))//st->bits.hallphase60)//?
			{
				switch(ppc->msg.position_prev){
				case 0x4:{
					ppc->msg.position_next=0x5;
					ppc->msg.ctrlout_set=0x28;
					break;
				}
				case	0x14:{
					ppc->msg.position_next=0x4;

⌨️ 快捷键说明

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