标题:
单片机28BYJ-48步进电机控制器仿真+代码
[打印本页]
作者:
hongaa
时间:
2018-11-30 23:29
标题:
单片机28BYJ-48步进电机控制器仿真+代码
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
无标题.jpg
(221.63 KB, 下载次数: 55)
下载附件
2018-11-30 23:28 上传
0.png
(11.02 KB, 下载次数: 36)
下载附件
2018-12-1 02:27 上传
单片机源程序如下:
#include "macro.h"
typedef unsigned int u16;
u16 un;
uchar buf_Direction; //?????????
uint set_speed; //?????????
uchar step_motor_loop[8]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09};//28byj-48电机时序
//uchar step_motor_loop[4]={0x09,0x06,0x03,0x0c};//42步进时序
uchar step_index;
#define CST_STEP_MOTOR_Z 10 //??
#define CST_STEP_MOTOR_F 20 //??
#define CST_STEP_MOTOR_ST 0 //?
#define fosc_12MHz 12
#define fosc_24MHz 24
#define fosc_setting fosc_12MHz
//????1
#define CST_TIME_MS 200
#define CST_TH0 (65536-CST_TIME_MS*fosc_setting/fosc_12MHz)/256
#define CST_TL0 (65536-CST_TIME_MS*fosc_setting/fosc_12MHz)%256
uchar buf_SpeedString[]=" ";
//void iniLCD(void);
//void list_write_HZ_Str( int x1,int y1, uchar *point ) ;
void delay(u16 num)
{
u16 x,y;
for(x=num; x>0; x--)
for(y=110; y>0; y--)
{
;//??1ms
}
}
void step_motor_driver(void)
{
static uint speed_delay=0;
speed_delay++;
if( buf_Direction==CST_STEP_MOTOR_Z)
{
P0=step_motor_loop[step_index];
if(speed_delay >=set_speed )
{
speed_delay =0;
step_index--;
if(step_index <=0)
step_index =7;
//=================================================转1圈
un++;
if(un>=3600)
{
un=0;
step_index=3;
buf_Direction=0;
}
//=================================================
}
}
else if( buf_Direction==CST_STEP_MOTOR_F)
{
P0=step_motor_loop[step_index];
if(speed_delay >=set_speed)
{
speed_delay =0;
step_index++; //??7,????
if(step_index >7)
step_index =0;
//=================================================转1圈
un++;
if(un>=4100)
{
un=0;
step_index=3;
buf_Direction=0;
}
//============================================
}
}
else
{
speed_delay =0;
un=0;
step_index=3;
P0=0x00;
}
}
//*********************************************************
//******************??????****************************
//************************************************************
void key_Scan (void)
{
static uchar key_loose=0;
uchar temp=0;
P1=0XFF;
temp=P1&0xFF;
if(key_loose >0)
{
if(temp==0xFF)
key_loose=0;
return;
}
else if(temp !=0xFF)
{
key_loose=10;
temp=P1&0xFF;
switch(temp)
{
case 0xFE:
buf_Direction=CST_STEP_MOTOR_Z;
// list_write_HZ_Str(6,20,"??");
// caluate();
break;
case 0xFD:
buf_Direction=CST_STEP_MOTOR_F;
// list_write_HZ_Str(6,20,"??");
// caluate();
break;
case 0xFB:
buf_Direction=CST_STEP_MOTOR_ST;
// list_write_HZ_Str(6,20,"??");
// list_write_HZ_Str(6,60," ");
break;
case 0xEF:
if(buf_Direction !=CST_STEP_MOTOR_ST)
{
if(set_speed <100)
set_speed +=5;; //??
// caluate();
}
break;
case 0xF7:
if(buf_Direction !=CST_STEP_MOTOR_ST)
{
if(set_speed >2)
set_speed -=5; //??
// caluate();
}
break;
default:
break; //?????
}
}
}
void isr_timer0 (void) interrupt 1 using 1
{
TH0=CST_TH0;
TL0=CST_TL0;
step_motor_driver();
}
void init_timer0(void)
{
TCON=0x00;
TMOD=0x00;
TL0=0x00;
TH0=0x00;
TCON=0x00;
// Timer 0 C/T=0,??????
//Timer 0 M1,M0=0,1 .work mode 1 www.gdzs.si.gov.cn
TMOD=0x01; //GATE=0,??????TR0 ?????0 ?C/T=0,??????
TH0=CST_TH0;
TL0=CST_TL0;
TF0=0;//clear 0. count over flag.
TR0=1;// enable TIMER0 ,start count
ET0=1; //enable accept interrupt
}
//***********************?***?****?********?********************
void main(void)
{
P2=0xff;
P1=0xff;
P0=0Xff;
init_timer0();
//iniLCD(); //??? LCD
buf_Direction=CST_STEP_MOTOR_ST;
set_speed=30;
step_index=3;
EA=1; //???
while(1)
{
key_Scan();
}
}
复制代码
所有资料51hei提供下载:
步进控制.zip
(25.13 KB, 下载次数: 88)
2018-11-30 23:28 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
wgb
时间:
2018-12-10 19:58
尴尬,你的版本太高了
作者:
Chen1108
时间:
2020-12-23 16:03
P1端口不用上拉电阻,好像也是可以的吧?
作者:
dj3365191
时间:
2020-12-23 18:17
P1口接的电阻不是上拉,是稳定工作点的,阻值最好在4.7K到10K之间
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1