您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 综合/其它 > 单片机控制步进电机转动程序
xxxxxxxxxxxxxxxxxxx#includereg52.h#includeINTRINS.H#includeabsacc.h#includemath.h#defineuintunsignedint#defineucharunsignedchar#defineACC_LEVEL42#defineDEC_LEVEL42#defineSTOP_LEVEL42#defineUP_LEVEL(ACC_LEVEL-2)#defineDOWN_LEVEL1ucharcodeslave_addr[4]={00,01,02,255};ucharcodec1[10]={0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xA0,0xA2};ucharcodec2[10]={0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,0xB0,0xB2};/*加速过程各档速度定时器初值,用16位的T2实现*/uintcodeacc_pulse_T2_const[ACC_LEVEL]={64170,64170,64841,65065,65176,65243,65288,65320,65344,65362,65377,65389,65399,65407,65415,65421,65426,65431,65435,65439,65443,65446,65448,65451,65453,65455,65457,65459,65461,65462,65464,65465,65466,65467,65468,65469,65470,65471,65472,65473,65474,65474};/*加速过程各档速度加速持续时间(用脉冲数表示),用16位的T0计数,计数器初值*/uintcodeacc_time_T0_const[ACC_LEVEL]={65463,65463,65460,65458,65455,65452,65449,65446,65443,65440,65436,65433,65429,65425,65421,65417,65413,65409,65404,65399,65395,65389,65384,65379,65373,65368,65362,65355,65349,65342,65335,65328,65321,65313,65305,65297,65289,65280,65271,65261,65251,65251};/*减速过程各档速度定时器初值,用16位的T2实现*/uintcodedec_pulse_T2_const[DEC_LEVEL]={64170,64170,64841,65065,65176,65243,65288,65320,65344,65362,65377,65389,65399,65407,65415,65421,65426,65431,65435,65439,65443,65446,65448,65451,65453,65455,65457,65459,65461,65462,65464,65465,65466,65467,65468,65469,65470,65471,65472,65473,65474,65474};/*减速过程各档速度减速持续时间(用脉冲数表示),用16位的T0计数,计数器初值*/uintcodedec_time_T0_const[DEC_LEVEL]={65463,65463,65460,65458,65455,65452,65449,65446,65443,65440,65436,65433,65429,65425,65421,65417,65413,65409,65404,65399,65395,65389,65384,65379,65373,65368,65362,65355,65349,65342,65335,65328,65321,65313,65305,65297,65289,65280,65271,65261,65251,65251};/*停机过程各档速度定时器初值,用16位的T2实现*/uintcodestop_pulse_T2_const[STOP_LEVEL]={64170,64170,64841,65065,65176,65243,65288,65320,65344,65362,65377,65389,65399,65407,65415,65421,65426,65431,65435,65439,65443,65446,65448,65451,65453,65455,65457,65459,65461,65462,65464,65465,65466,65467,65468,65469,65470,65471,65472,65473,65474,65474};/*停机过程各档速度减速持续时间(用脉冲数表示),用16位的T0计数,计数器初值*/uintcodestop_time_T0_const[STOP_LEVEL]={65463,65463,65460,65458,65455,65452,65449,65446,65443,65440,65436,65433,65429,65425,65421,65417,65413,65409,65404,65399,65395,65389,65384,65379,65373,65368,65362,65355,65349,65342,65335,65328,65321,65313,65305,65297,65289,65280,65271,65261,65251,65251};ucharidataacc_count,dec_count,input_count,temp_count,done_no,input_order;ucharidataT0low,T0high,T2low,T2high,key_in;uintidatainterval,pulse_counter,common_count;/*common_count记录当前T2定时器初值数组序号,即当前转速,不用到处寻找*//*关键在那里获得common_count的值,应该在输出速度脉冲定时器的启动程序里找到*//*当前转速,在哪里获得?在本程序,应该为T2定时器当前初值,在T0中断服务程序里获得*//*加速\减速\停机函数经过处理可以合并为一个函数,但分为独立函数效率高一点*/ucharbdatasign0,sign1,test_s0,test_s1;sbitnew_speed_in=sign0^0;/*新速度数据从串口输入标志位*/sbitacc_run_permit=sign0^1;/*允许电机加速标志位*/sbitdec_run_permit=sign0^2;/*允许电机减速标志位*/sbitcarry_acc=sign0^3;/*执行加速函数标志位*/sbitcarry_dec=sign0^4;/*执行减速函数标志位*/sbitcarry_stop=sign0^5;/*执行停机函数标志位*/sbitnormal_stop=sign0^6;/*调用停机函数标志位*/sbittest_p=sign0^7;/*串口输入停机指令标志位*//*------------------------------------------*/sbitcheck_input_spd=sign1^0;/*当前输入转速级别处理函数完毕标志位*/sbitfirst_startup=sign1^1;/*电机启动过的标志位*/sbitaddress_true=sign1^2;/*从机地址正确标志位*/sbitprint_permit=sign1^3;/*允许输出标志位*/sbitcmd_in_permit=sign1^4;/*允许主机命令输入标志位*//*------------------------------------------*/sbitP2_0=P2^0;/*作输入步进电机的脉冲信号发送口*/sbitP2_2=P2^2;/*作输入步进电机的旋转方向信号发送口*/sbitP1_0=P1^0;/*开机时需要关断串口发送功能,需要时再接通*//*作串口输出信号的使能口,P1_0=0时接通串口,输出信号*/sbitP1_2=P1^2;/*现在作检测位,用仿真机仿真时用该接口*/sbitP1_5=P1^5;/*作脉冲信号输入T0的发送口*/sbitP1_6=P1^6;/*作脉冲信号输入T0的使能口*/sbitWD=P1^7;/*看门狗*/voidini_down(void);/*初始化硬件函数*/uchardecide_AccDecStop(void);/*确定加速或减速处理函数*/voidacc_run(void);/*步进电机加速处理函数*/voiddec_run(void);/*步进电机减速处理函数*/voidstop_stepper(void);/*步进电机停机处理函数*/voidcheck_addr(void);/*地址核对*/voidsend_data(void);/*发送数据函数*//*======================================================*//*定时器任务分配:T2负责输出所需频率脉冲(因T2具有16位自动重装功能),T1负责产生波特率,T0负责输出脉冲数计数*//*在硬件上p1_5经过与非门后与P3_4连接,然后把与非门输出脉冲输入T0计数*//*这样P3_4的脉冲输入是可控的,需要用MCS52芯片*//*或者用软件定时,但不准确,且占用CPU时间*//*定时器2中断的产生取决与TF2和EXF2的逻辑或。当定时器2溢出或是遇到捕捉/重装事件时这些标志位会置位。当系统执行定时器2中断服务程序时,这些标志位不会被硬件清除。软件应当解析定时器2中断的类型并清除相应的标志位。*//*======================================================*/main(){test_s0=0;test_s1=0;test_s1=0x80;test_s0=0x0f;/*=====================================================*/interval=0;done_no=0;pulse_counter=0;common_count=0;acc_count=0;dec_count=0;temp_count=0;T0low=0;T0high=0;T2low=0;T2high=0;new_speed_in=0;acc_run_permit=0;dec_run_permit=0;carry_acc=0;carry_dec=0;carry_stop=0;normal_stop=0;test_p=0;check_input_spd=0;first_startup=0;print_permit=0;cmd_in_permit=0;input_order=0;P2_0=0;P2_2=0;/*步进电机的旋转方向待试验后确定*/P1_0=1;/*开机时需要关断串口发送功能,需要时再接通,作串口输出信号的使能口,P1_0=0时接通串口,输出信号*/P1_2=0;/*现在作检测位,用仿真机仿真时用该接口*/P1_5=0;P1_6=0;/*开机时需要关闭脉冲信号输入T0,需要时再接通,P1_6=1时脉冲信号可以输入T0*/WD=1;/*看门狗*/ini_down();TF2=0;/*T2溢出中断需软件清0*//*------------------------------------------------------*/do{if(address_true==1){address_true=0;check_addr();}if(print_permit==1){print_permit=0;
本文标题:单片机控制步进电机转动程序
链接地址:https://www.777doc.com/doc-3355404 .html