|
c8051f410單片機做的步進電機控制系統
0.png (63.61 KB, 下載次數: 54)
下載附件
2017-1-6 02:29 上傳
源碼下載:
c8051f410.zip
(33.63 KB, 下載次數: 14)
2017-1-5 10:55 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
部分源碼預覽:
- /***********************************************
- 功能:定時器控制步進電機延時,定時器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--;
- }
- }
復制代碼
|
|