|
c8051f410单片机做的步进电机控制系统
源码下载:
c8051f410.zip
(33.63 KB, 下载次数: 14)
部分源码预览:
- /***********************************************
- 功能:定时器控制步进电机延时,定时器0控速
- 芯片C8051F410
- 晶振24.5MHz
- P0推挽控制步进电机
- L298N驱动
- 作者:JZ
- ***********************************************/
- #include"C8051F410.h"
- #include"stepping_motor410.h"
- // Peripheral specific initialization functions,
- // Called from the Init_Device() function
- #define uint unsigned int
- #define uchar unsigned char
- uchar code single_pos[4]={0xee,0xdd,0xbb,0x77};//单四拍驱动方式正转表A-B-C-D
- uchar code single_rev[4]={0x77,0xbb,0xdd,0xee};//单四拍驱动方式反转表D-C-B-A
-
- uchar code double_pos[4]={0x99,0x33,0x66,0xcc};//双四拍驱动方式正转表AB-BC-CD-DA
- uchar code double_rev[4]={0xcc,0x66,0x33,0x99};//双四拍驱动方式反转表AD-DC-CB-BA
- uchar code eight_pos[8]={0xee,0xcc,0xdd,0x99,0xbb,0x33,0x77,0x66}; //八拍驱动方式正转表A-AB-B-BC-C-CD-D-DA
- uchar code eight_rev[8]={0x66,0x77,0x33,0xbb,0x99,0xdd,0xcc,0xee}; //八拍驱动方式反转表AD-D-DC-C-CB-B-BA-A
- uchar code eight_right_pos[8]={0x0e,0x0c,0x0d,0x09,0x0b,0x03,0x07,0x06}; //八拍驱动方式右轮正转表A-AB-B-BC-C-CD-D-DA
- uchar code eight_right_rev[8]={0x06,0x07,0x03,0x0b,0x09,0x0d,0x0c,0x0e}; //八拍驱动方式右反轮转表AD-D-DC-C-CB-B-BA-A
- uchar code eight_left_pos[8]={0xe0,0xc0,0xd0,0x90,0xb0,0x30,0x70,0x60}; //八拍驱动方式左轮正转表A-AB-B-BC-C-CD-D-DA
- uchar code eight_left_rev[8]={0x60,0x70,0x30,0xb0,0x90,0xd0,0xc0,0xe0}; //八拍驱动方式右轮反转表AD-D-DC-C-CB-B-BA-A
- uint times;//时间次数记录
- unsigned int step; //记录脉冲数,即要走的步数
- uint beat; //步进电机每种驱动方式下的拍数
- char *p1,*p2;//存储运行方式表
- void PCA_Init()
- {
- PCA0MD &= ~0x40;
- PCA0MD = 0x00;
- }
- void Timer_Init()
- {
- TCON = 0x10;
- TMOD = 0x02;
- TL0 = 0x38;
- TH0 = 0x38;
- }
- void Port_IO_Init()
- {
- P0MDOUT = 0xFF;
- XBR1 = 0x40;
- }
- void Oscillator_Init()
- {
- OSCICN = 0x87;
- }
- void Interrupts_Init()
- {
- IE = 0x82;
- }
- // Initialization function for device,
- // Call Init_Device() from your main program
- void Init_Device(void)
- {
- PCA_Init();
- Timer_Init();
- Port_IO_Init();
- Oscillator_Init();
- Interrupts_Init();
- }
- //单四拍驱动正转(N*360/200)度
- void m_single_pos(unsigned int N)
- {
- beat=4; //拍数
- p1=single_pos;
- p2=single_pos+beat;
- step=N;
- TR0=1;//开定时器0,电机运行
- while(1)
- {
- P0=*p1;
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //单四拍驱动反转(N*360/200)度
- void m_single_rev(unsigned int N)
- {
- beat=4;
- p1=single_rev;
- p2=single_rev+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //双四拍驱动正转(N*360/200)度
- void m_double_pos(unsigned int N)
- {
- beat=4;
- p1=double_pos;
- p2=double_pos+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //双四拍驱动反转(N*360/200)度
- void m_double_rev(unsigned int N)
- {
- beat=4;
- p1=double_rev;
- p2=double_rev+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //八拍驱动正转(N*360/400)度
- void m_eight_pos(unsigned int N)
- {
- beat=8;
- p1=eight_pos;
- p2=eight_pos+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //八拍驱动反转(N*360/400)度
- void m_eight_rev(unsigned int N)
- {
- beat=8;
- p1=eight_rev;
- p2=eight_rev+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //八拍驱动右轮正转(N*360/400)度
- void m_eight_right_pos(unsigned int N)
- {
- beat=8;
- p1=eight_right_pos;
- p2=eight_right_pos+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //八拍驱动右轮反转(N*360/400)度
- void m_eight_right_rev(unsigned int N)
- {
- beat=8;
- p1=eight_right_rev;
- p2=eight_right_rev+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //八拍驱动左轮正转(N*360/400)度
- void m_eight_left_pos(unsigned int N)
- {
- beat=8;
- p1=eight_left_pos;
- p2=eight_left_pos+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- //八拍驱动左轮反转(N*360/400)度
- void m_eight_left_rev(unsigned int N)
- {
- beat=8;
- p1=eight_left_rev;
- p2=eight_left_rev+beat;
- step=N;
- TR0=1;// 开定时器0,电机运行
- while(1)
- {
- P0=*p1;
-
- if(step==0)
- {
- P0=0x00;
- TR0=0;//关定时器0
- break;
- }
- }
- }
- void motor_timer0() interrupt 1 //定时器0中断,产生电机驱动脉冲的延时
- {
- times++;
- if(times==102) //TH0=0x38时,times最小为5 tinmes=102延时10ms
- {
- times=0;
- p1++;
- if(p1==p2)
- p1=p1-beat;
- step--;
- }
- }
复制代码
|
|