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

📄 main_ctl_test1.c

📁 MEGA32L 单片机的按键,LED,LCD等测试例程,ICCAVR编译器
💻 C
📖 第 1 页 / 共 5 页
字号:
		st_work_mode.flag_finding_level_mode &= ~(uchar)CONST_FLAG_IS_Z_HAS_SEARCH_END_LEVEL;		
		
		//准备进入初始找平预处理,但是不能更改当前的操作模式
		prepare_for_goto_finding_level();
	}	
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].curr_filter_dat >st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].mid_pos +st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].static_up_delta)
	{	//>中心 +static
		//z(x)细步left up输出,数据要变小
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_X_MOVE_ONE_SMALL_STEP);
	}
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].curr_filter_dat <st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].mid_pos -st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].static_down_delta)
	{	//<中心 -static
		//z(x)细步right down输出,数据要变大
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_X_MOVE_ONE_SMALL_STEP);
	}
	else
	{	//>=中心 -static && <=中心 +static
		//20070907(1-3)
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_STOP,0);
	}
}



//----------------------------------------------------------------------------
//水平位置下的tilt模式时的x,y的控制, 界限是nor以下认为平衡,tilt以上重新找平!
//输入:
//	[x].curr_filter_dat
//	[y].curr_filter_dat
//输出:
//	control_motor_x_move
//	control_motor_y_move
//X:key_left=0x0317, key_right=0x0017	
//Y:key_up=0x0020, key_down=0x038A
//----------------------------------------------------------------------------
void	control_on_tilt_mode_at_horizontal(void)
{
	if( (st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].curr_filter_dat >st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].mid_pos +st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].tilt_up_delta) ||
	    (st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].curr_filter_dat <st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].mid_pos -st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].tilt_down_delta) )
	{	//超出范围, <=中心 -tilt || >=中心 +tilt
		//标识 &=X,进入初始找平状态;
		st_work_mode.flag_finding_level_mode &= ~(uchar)CONST_FLAG_IS_X_HAS_SEARCH_END_LEVEL;		

		//准备进入初始找平预处理,但是不能更改当前的操作模式
		prepare_for_goto_finding_level();
	}	
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].curr_filter_dat >st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].mid_pos +st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].nor_up_delta)
	{	//>中心 +nor
		//x细步right down输出,数据要变小
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_X_MOVE_ONE_SMALL_STEP);
	}
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].curr_filter_dat <st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].mid_pos -st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_X].nor_down_delta)
	{	//<中心 -nor
		//x细步left up输出,数据要变大
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_X_MOVE_ONE_SMALL_STEP);
	}
	else
	{	//>=中心 -nor && <=中心 +nor
		//20070907(1-4)
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_STOP,0);
	}
	
	if( (st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].curr_filter_dat >st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].mid_pos +st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].tilt_up_delta) ||
	    (st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].curr_filter_dat <st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].mid_pos -st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].tilt_down_delta) )
	{	//超出范围, <=中心 -tilt || >=中心 +tilt
		//标识 &=Y,进入初始找平状态;
		st_work_mode.flag_finding_level_mode &= ~(uchar)CONST_FLAG_IS_Y_HAS_SEARCH_END_LEVEL;		
		
		//准备进入初始找平预处理,但是不能更改当前的操作模式
		prepare_for_goto_finding_level();
	}	
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].curr_filter_dat >st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].mid_pos +st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].nor_up_delta)
	{	//>中心 +nor
		//y细步left up输出,数据要变小
		control_motor_y_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_Y_MOVE_ONE_SMALL_STEP);
	}
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].curr_filter_dat <st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].mid_pos -st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Y].nor_down_delta)
	{	//<中心 -nor
		//y细步right down输出,数据要变大
		control_motor_y_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_Y_MOVE_ONE_SMALL_STEP);
	}
	else
	{	//>=中心 -nor && <=中心 +nor
		//20070907(1-5)
		control_motor_y_move(CONST_MOTOR_X_Y_MOVING_STOP,0);
	}
}



//----------------------------------------------------------------------------
//垂直位置下的tilt模式时的z(x)的控制
//输入:
//	[z].curr_filter_dat
//输出:
//	control_motor_x_move
//	flag_finding_level_mode
//Z(x): key_Z(x)_left=0x0017, key_Z(x)_right=0x0360;	
//----------------------------------------------------------------------------
void	control_on_tilt_mode_at_vertical(void)
{
	if( (st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].curr_filter_dat >st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].mid_pos +st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].tilt_up_delta) ||
	    (st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].curr_filter_dat <st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].mid_pos -st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].tilt_down_delta) )
	{	//超出范围, <=中心 -tilt || >=中心 +tilt
		//标识 &=Z,进入初始找平状态;
		st_work_mode.flag_finding_level_mode &= ~(uchar)CONST_FLAG_IS_Z_HAS_SEARCH_END_LEVEL;		
		
		//准备进入初始找平预处理,但是不能更改当前的操作模式
		prepare_for_goto_finding_level();
	}	
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].curr_filter_dat >st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].mid_pos +st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].nor_up_delta)
	{	//>中心 +nor
		//z(x)细步left up输出,数据要变小
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_X_MOVE_ONE_SMALL_STEP);
	}
	else if(st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].curr_filter_dat <st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].mid_pos -st_gdc_adc_sets_paras[CONST_ADC_CHANNEL_Z].nor_down_delta)
	{	//<中心 -nor
		//z(x)细步right down输出,数据要变大
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_X_MOVE_ONE_SMALL_STEP);
	}
	else
	{	//>=中心 -nor && <=中心 +nor,关闭找平z方向的找平电机
		//20070907(1-6)
		control_motor_x_move(CONST_MOTOR_X_Y_MOVING_STOP,0);
	}
}

//----------------------------------------------------------------------------
//程序执行过程中的异常处理,最后停机处理!
//----------------------------------------------------------------------------
void	dowith_exception_by_power_off(uchar exception_no)
{
	switch(exception_no)
	{
	case CONST_EXCEPTION_DETECT_HV_POSITION_ERROR:		//位置开关检测异常出错
		st_warning_op.err_number =exception_no;
		memcpy((uchar *)&st_warning_op.mark_str[0], CONST_EXCEPTION_STRING_DETECT_HV_POSITION_ERROR, strlen(CONST_EXCEPTION_STRING_DETECT_HV_POSITION_ERROR));
		break;
	
	case CONST_EXCEPTION_DETECT_X_MID_POS_ERROR:
		st_warning_op.err_number =exception_no;
		memcpy((uchar *)&st_warning_op.mark_str[0], CONST_EXCEPTION_STRING_DETECT_X_MID_POS_ERROR, strlen(CONST_EXCEPTION_STRING_DETECT_X_MID_POS_ERROR));
		break;
		
	case CONST_EXCEPTION_DETECT_Y_MID_POS_ERROR:
		st_warning_op.err_number =exception_no;
		memcpy((uchar *)&st_warning_op.mark_str[0], CONST_EXCEPTION_STRING_DETECT_Y_MID_POS_ERROR, strlen(CONST_EXCEPTION_STRING_DETECT_Y_MID_POS_ERROR));
		break;
		
	case CONST_EXCEPTION_DETECT_Z_MID_POS_ERROR:
		st_warning_op.err_number =exception_no;
		memcpy((uchar *)&st_warning_op.mark_str[0], CONST_EXCEPTION_STRING_DETECT_Z_MID_POS_ERROR, strlen(CONST_EXCEPTION_STRING_DETECT_Z_MID_POS_ERROR));
		break;
		
	default:
		break;
	}
		
	_SEI();
	strcpy((char *)&st_serial_buff.send, (uchar *)&st_warning_op.mark_str);
	st_serial_buff.tx_pos_start =1;
	st_serial_buff.slen_needed =strlen((uchar *)&st_warning_op.mark_str);
	UDR =st_serial_buff.send[0];	//启动发送
	UCSRB |=BIT(UDRIE);		//因为UDRIE中断会自我关断,以减少CPU负荷!
				
	while(st_serial_buff.slen_needed !=0);
	_CLI();

	excute_power_off();			//异常处理后,自动关电处理	
}			
				
				
//----------------------------------------------------------------------------
//硬件调试流程函数,最后关闭电源的方式退出!
//这里已经启动'I',Timer0、Timer1,INT0/1/2
//注意:
//	这里使用 st_z_circu.curr_work_mode =CONST_WORK_MODE_IS_MAN_CIRCU_SPEED_0 模式来实行功能
//	关于PWM的控制数据都是备份的,用于手动微调的!
//	这里的ADC转换采用查询模式!
//x,y电机:	上/下/左/右
//PWM:	   	X_PAN加速, Y_TILE减速, Z_SPEED变速
//LED点亮:	MAN_AUTO
//关电:		POWER
//模拟扇扫:	X_PAN + Y_TILT
//激光开关:	X_PAN + Z_SPEED
//码盘检测:	Y_TILT + Z_SPEED
//----------------------------------------------------------------------------
void	Dowith_Hardware_Debug(void)
{
uchar	sreg_bak;
uchar	key;
uchar	ji_guang_is_on;
uchar	pwm_output_ctl_num;
uchar	led_output_ctl_num;
uint	bak_pwm_ctl_data;
uint	control_serial_send_count;

uchar	pan_is_on;
uchar	pan_type;
uint	count;
uchar	mp_state;
uchar	mp_state_buff[300];
uchar	i,j;

	//所有的灯和蜂鸣器都熄灭	
	led_output_ctl_num =0;
	led_and_beep_all_off();
	
	//初始状态是z为速度60rmp的PWM输出驱动,目前向左输出;
	CODE_MOTOR_Z_MOVE_STOP;
	pwm_output_ctl_num =CONST_NUM_OF_CIRCU_SPEED_60;
	bak_pwm_ctl_data =PWM_Output_Control_Data_By_Ctl_Num[pwm_output_ctl_num];
	
	sreg_bak =SREG;
	_CLI();
	OCR1B	=bak_pwm_ctl_data;
	SREG	=sreg_bak;

	CODE_MOTOR_Z_MOVE_RIGHT;
	control_serial_send_count =0;
	pan_type	=0;	
	
	do
	{
		key =judge_read_local_and_ir_keys();
	}while(key ==CONST_HAS_READ_KEY);
	
	//使用Timer2和INT1中断来接收红外按键;
	while(1)
	{	
//!soft!
		//进行HC165的输入[按键,限位开关,水银开关]以及电源按键的查询;
		key =judge_read_local_and_ir_keys();		//耗时 130ms 左右
		if(key ==CONST_HAS_READ_KEY)
		{	//提取当前的按键并清除按键标识,防止重复!
			if(st_curr_key.flag_now_is_local_key ==CONST_KEY_TYPE_IS_LOCAL)
			{
				key =st_curr_key.local_key_label;
				st_curr_key.local_key_label =CONST_KEY_LAB_IDEL;
			}
			else
			{
				key =st_curr_key.ir_key_label;
				st_curr_key.ir_key_label =CONST_KEY_LAB_IDEL;
			}
			
			switch(key)
			{
			//为了手动方式下驱动更快,这里的手动驱动时间*4倍!
			case CONST_KEY_LAB_UP:
			     	//20070823(8-3)
				if(st_work_mode.bak_flag_is_horizontal ==CONST_FLAG_IS_HORIZONTAL_MODE)
				{	
					if(st_curr_key.flag_key_is_long_type ==CONST_KEY_PRESS_IS_SHORT)
					{
						//y电机向上驱动1小步
						control_motor_y_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_XYZ_MANUAL_MOVE_SMALL_STEP);
					}
					else
					{
						//y电机向上驱动1大步
						control_motor_y_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_XYZ_MANUAL_MOVE_BIG_STEP);						
					}
				}
				break;	

			case CONST_KEY_LAB_DOWN:
			     	//20070823(8-4)
				if(st_work_mode.bak_flag_is_horizontal ==CONST_FLAG_IS_HORIZONTAL_MODE)
				{	
					if(st_curr_key.flag_key_is_long_type ==CONST_KEY_PRESS_IS_SHORT)
					{
						//y电机向下驱动1小步
						control_motor_y_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_XYZ_MANUAL_MOVE_SMALL_STEP);
					}
					else
					{
						//y电机向下驱动1大步
						control_motor_y_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_XYZ_MANUAL_MOVE_BIG_STEP);						
					}
				}
				break;	
		
			case CONST_KEY_LAB_LEFT:
			     	//20070823(8-5)
				if(st_work_mode.bak_flag_is_horizontal ==CONST_FLAG_IS_HORIZONTAL_MODE)
				{	
					if(st_curr_key.flag_key_is_long_type ==CONST_KEY_PRESS_IS_SHORT)
					{
						//x电机向左驱动1小步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_XYZ_MANUAL_MOVE_SMALL_STEP);
					}
					else
					{
						//x电机向左驱动1大步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_XYZ_MANUAL_MOVE_BIG_STEP);
					}
				}
				else	//垂直方向
				{
					if(st_curr_key.flag_key_is_long_type ==CONST_KEY_PRESS_IS_SHORT)
					{
						//Z(x)电机向上驱动1小步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_XYZ_MANUAL_MOVE_SMALL_STEP);
					}
					else
					{
						//Z(x)电机向上驱动1大步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_LEFT_UP, CONST_MOTOR_XYZ_MANUAL_MOVE_BIG_STEP);
					}						
				}
				break;		

			case CONST_KEY_LAB_RIGHT:
			     	//20070823(8-6)
				if(st_work_mode.bak_flag_is_horizontal ==CONST_FLAG_IS_HORIZONTAL_MODE)
				{	
					if(st_curr_key.flag_key_is_long_type ==CONST_KEY_PRESS_IS_SHORT)
					{
						//x电机向右驱动1小步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_XYZ_MANUAL_MOVE_SMALL_STEP);
					}
					else
					{
						//x电机向右驱动1大步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_XYZ_MANUAL_MOVE_BIG_STEP);
					}
				}
				else	//垂直方向
				{
					if(st_curr_key.flag_key_is_long_type ==CONST_KEY_PRESS_IS_SHORT)
					{
						//Z(x)电机向下驱动1小步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_XYZ_MANUAL_MOVE_SMALL_STEP);
					}
					else
					{
						//Z(x)电机向下驱动1大步
						control_motor_x_move(CONST_MOTOR_X_Y_MOVING_RIGHT_DOWN, CONST_MOTOR_XYZ_MANUAL_MOVE_BIG_STEP);
					}
				}
				break;		

#if	(DEBUG_ENABLE_NEGLET_LEFT_UP_OP ==0)
			case CONST_KEY_LAB_LEFT_UP:
			     	//20070823(8-7)

⌨️ 快捷键说明

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