仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
#include "reg52.h"
#include<lcd1602.h>
#include "delay.h"
#include "motor4B.h"
sbit K7 = P3^1;//定时
sbit K1 = P3^2;//加速
sbit K2 = P3^3;//减速
sbit K3 = P3^4;//正传
sbit K4 = P3^5;//反转
sbit K5 = P3^6;//启动
sbit K6 = P3^7;//停止
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void SETP_MOTOR_FFW(uchar n)
{
uchar i,j;
for(i=0;i<5*n;i++)
{
for(j=0;j<8;j++)
{
if(K2 == K3 == 0) break;
DelayMS(25);
}
}
}
void main( )
{
uchar N = 3;
bit fangxiang = 1;//0是逆时针
bit kaiqi = 0;//0是停止
int sudu = 4;//每次步进间隔时间为 1ms * sudu,也就是最快每秒25转
char str_buff[] = { "00.00" };
int speed = 0;
init_lcd();
lcd( 0, 0, "LCD_INIT" );
delay( 100 );
lcd( 0, 0, " " );
Init_Motor();
Set_Motor(kaiqi, fangxiang, sudu);
lcd( 1, 0, " reversal " );
lcd( 0, 0, "Speed: " );
lcd( 0, 12, "r/s" );
speed = 1000 / ( 1.0 * 8.0 * sudu ) * 1000;
str_buff[0] = speed / 10000 % 10 + '0';
str_buff[1] = speed / 1000 % 10 + '0';
str_buff[3] = speed / 100 % 10 + '0';
str_buff[4] = speed % 10 + '0';
lcd( 0, 6, str_buff );
while(1)
{
//////////////////////////////////////////////////////////////////////加速
if( K1 == 0 )
{
delay( 50 );
if( K1 == 0 )
{
sudu--;
if( sudu <= 3 )
{
sudu = 4;
}
speed = 1000 / ( 1.0 * 8.0 * sudu ) * 1000;
str_buff[0] = speed / 10000 % 10 + '0';
str_buff[1] = speed / 1000 % 10 + '0';
str_buff[3] = speed / 100 % 10 + '0';
str_buff[4] = speed % 10 + '0';
lcd( 0, 6, str_buff );
Set_Motor(kaiqi, fangxiang, sudu);
// while( K1 == 0 );
}
}
//////////////////////////////////////////////////////////////////////减速
if( K2 == 0 )
{
delay( 50 );
if( K2 == 0 )
{
sudu++;
if( sudu >= 60000 )
{
sudu = 60000;
}
speed = 1000 / ( 1.0 * 8.0 * sudu ) * 1000;
str_buff[0] = speed / 10000 % 10 + '0';
str_buff[1] = speed / 1000 % 10 + '0';
str_buff[3] = speed / 100 % 10 + '0';
str_buff[4] = speed % 10 + '0';
lcd( 0, 6, str_buff );
Set_Motor(kaiqi, fangxiang, sudu);
// while( K1 == 0 );
}
}
//////////////////////////////////////////////////////////////////////正
if( K3 == 0 )
{
delay( 50 );
if( K3 == 0 )
{
fangxiang = 1;
lcd( 1, 0, "Forward rotation" );
Set_Motor(kaiqi, fangxiang, sudu);
while( K3 == 0 );
}
}
//////////////////////////////////////////////////////////////////////反
if( K4 == 0 )
{
delay( 50 );
if( K4 == 0 )
{
fangxiang = 0;
lcd( 1, 0, " reversal " );
Set_Motor(kaiqi, fangxiang, sudu);
while( K4 == 0 );
}
}
//////////////////////////////////////////////////////////////////////启
if( K5 == 0 )
{
delay( 50 );
if( K5 == 0 )
{
kaiqi = 1;
Set_Motor(kaiqi, fangxiang, sudu);
while( K6 == 0 );
}
}
//////////////////////////////////////////////////////////////////////停
if( K6 == 0 )
{
delay( 50 );
if( K6 == 0 )
{
kaiqi = 0;
Set_Motor(kaiqi, fangxiang, sudu);
while( K6 == 0 );
}
}
///////////////////////////////////////////////////////////////////////定时
if( K7 == 0 )
{
SETP_MOTOR_FFW(N);
if( K6 == 0) break;
}
}
}
不能运行求指导:
基于单片机的微小型按摩器系统设计.zip
(122.49 KB, 下载次数: 210)
|