当前位置:网站首页>Stepper motor
Stepper motor
2022-06-30 07:28:00 【weixin_ forty-six million one thousand two hundred and twenty-o】
system.c
uchar flag_1ms=0;
void Timer0_Init(void)
{
TH0 = T1MS>>8; // Assign initial value to
TL0 = T1MS; // Assign initial value to
TMOD = 0x01; // Set the timer 0 Using a 16 bit timer
EA = 1; // Open total interrupt
ET0 = 1; // Turn on timer 0 interrupt
TR0 = 1; // Start timer 0
}
void Timer0() interrupt 1 // Interrupt number 1
{
TH0 = T1MS>>8; // Assign initial value to
TL0 = T1MS; // Assign initial value to
flag_1ms=1;
}
/*void Delay_us(uint xus)
{
for(;xus>0;xus--);
}
void Delay_ms(uint xms)
{
for(;xms>0;xms--)
{
Delay_us(1000);
}
}
*/
void delay(uint i)
{
while(i--);
}
system.h
#include "reg52.h"
#define uchar unsigned char // Define unsigned characters
#define uint unsigned int // Defines an unsigned integer
#define FOSC 12000000L // Define the crystal oscillator frequency
#define T1MS (65536-FOSC/12/1000) // timing 10 In milliseconds , The value to be written by the counter
//void Delay_us(uint xus); //us Stage delay
//void Delay_ms(uint xms); //ms Stage delay
void Timer0_Init(void); // Timer initialization
void delay(uint i);
system.h
#include "system.h"
sbit MOTOR_PS = P1^6;
sbit MOTOR_PHASE1 = P1^5;
sbit MOTOR_PHASE2 = P1^4;
sbit MOTOR_I01 = P1^3;
sbit MOTOR_I11 = P1^2;
sbit MOTOR_I02 = P1^1;
sbit MOTOR_I12 = P1^0;
#define Motor_Active() (MOTOR_PS=1)
#define Motor_Sleep() (MOTOR_PS=0)
#define MOTOR_STEP_1() do{MOTOR_PHASE1=1;MOTOR_PHASE2=1;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP_2() do{MOTOR_PHASE1=0;MOTOR_PHASE2=1;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP_3() do{MOTOR_PHASE1=0;MOTOR_PHASE2=0;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP_4() do{MOTOR_PHASE1=1;MOTOR_PHASE2=0;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_1() do{MOTOR_PHASE1=1;MOTOR_PHASE2=1;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=1;MOTOR_I12=1;}while(0)
#define MOTOR_STEP1_4_2() do{MOTOR_PHASE1=1;MOTOR_PHASE2=1;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=1;}while(0)
#define MOTOR_STEP1_4_3() do{MOTOR_PHASE1=1;MOTOR_PHASE2=1;MOTOR_I01=1;MOTOR_I11=0;MOTOR_I02=1;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_4() do{MOTOR_PHASE1=1;MOTOR_PHASE2=1;MOTOR_I01=0;MOTOR_I11=1;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_5() do{MOTOR_PHASE1=0;MOTOR_PHASE2=1;MOTOR_I01=1;MOTOR_I11=1;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_6() do{MOTOR_PHASE1=0;MOTOR_PHASE2=1;MOTOR_I01=0;MOTOR_I11=1;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_7() do{MOTOR_PHASE1=0;MOTOR_PHASE2=1;MOTOR_I01=1;MOTOR_I11=0;MOTOR_I02=1;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_8() do{MOTOR_PHASE1=0;MOTOR_PHASE2=1;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=1;}while(0)
#define MOTOR_STEP1_4_9() do{MOTOR_PHASE1=0;MOTOR_PHASE2=0;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=1;MOTOR_I12=1;}while(0)
#define MOTOR_STEP1_4_10() do{MOTOR_PHASE1=0;MOTOR_PHASE2=0;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=1;}while(0)
#define MOTOR_STEP1_4_11() do{MOTOR_PHASE1=0;MOTOR_PHASE2=0;MOTOR_I01=1;MOTOR_I11=0;MOTOR_I02=1;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_12() do{MOTOR_PHASE1=0;MOTOR_PHASE2=0;MOTOR_I01=0;MOTOR_I11=1;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_13() do{MOTOR_PHASE1=1;MOTOR_PHASE2=0;MOTOR_I01=1;MOTOR_I11=1;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_14() do{MOTOR_PHASE1=1;MOTOR_PHASE2=0;MOTOR_I01=0;MOTOR_I11=1;MOTOR_I02=0;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_15() do{MOTOR_PHASE1=1;MOTOR_PHASE2=0;MOTOR_I01=1;MOTOR_I11=0;MOTOR_I02=1;MOTOR_I12=0;}while(0)
#define MOTOR_STEP1_4_16() do{MOTOR_PHASE1=1;MOTOR_PHASE2=0;MOTOR_I01=0;MOTOR_I11=0;MOTOR_I02=0;MOTOR_I12=1;}while(0)
void Motor_For(void);
void Motor_For1_4(void);
void Motor_Rev1_4(void);
motor.c
#include "motor.h"
void Motor_For(void)
{
static uchar timer=0;
timer++;
switch(timer)
{
case 1: MOTOR_STEP_1();break;
case 2: MOTOR_STEP_2();break;
case 3: MOTOR_STEP_3();break;
case 4: MOTOR_STEP_4();timer=0;break;
}
}
void Motor_For1_4(void)
{
static uchar timer=0;
timer++;
switch(timer)
{
case 1: MOTOR_STEP1_4_1();break;
case 2: MOTOR_STEP1_4_2();break;
case 3: MOTOR_STEP1_4_3();break;
case 4: MOTOR_STEP1_4_4();break;
case 5: MOTOR_STEP1_4_5();break;
case 6: MOTOR_STEP1_4_6();break;
case 7: MOTOR_STEP1_4_7();break;
case 8: MOTOR_STEP1_4_8();break;
case 9: MOTOR_STEP1_4_9();break;
case 10: MOTOR_STEP1_4_10();break;
case 11: MOTOR_STEP1_4_11();break;
case 12: MOTOR_STEP1_4_12();break;
case 13: MOTOR_STEP1_4_13();break;
case 14: MOTOR_STEP1_4_14();break;
case 15: MOTOR_STEP1_4_15();break;
case 16: MOTOR_STEP1_4_16();timer=0;break;
}
}
void Motor_Rev1_4(void)
{
static timer=17;
timer--;
switch(timer)
{
case 1: MOTOR_STEP1_4_1();timer=17;break;
case 2: MOTOR_STEP1_4_2();break;
case 3: MOTOR_STEP1_4_3();break;
case 4: MOTOR_STEP1_4_4();break;
case 5: MOTOR_STEP1_4_5();break;
case 6: MOTOR_STEP1_4_6();break;
case 7: MOTOR_STEP1_4_7();break;
case 8: MOTOR_STEP1_4_8();break;
case 9: MOTOR_STEP1_4_9();break;
case 10: MOTOR_STEP1_4_10();break;
case 11: MOTOR_STEP1_4_11();break;
case 12: MOTOR_STEP1_4_12();break;
case 13: MOTOR_STEP1_4_13();break;
case 14: MOTOR_STEP1_4_14();break;
case 15: MOTOR_STEP1_4_15();break;
case 16: MOTOR_STEP1_4_16();break;
}
}
main.c
#include "system.h"
#include "key.h"
#include "motor.h"
sbit sw1 = P3^7;
extern uchar flag_1ms; // Look for its definition in other modules . in addition ,extern It can also be used to specify Links .
static unsigned int key_timer=0;
void main(void) // The main function of a program
{
Timer0_Init(); Timer 0 Function call
Motor_Active();
while(1) // What's in braces , Executed in a constant loop
{
//1ms Enter once
if(flag_1ms==1) // With me , What's in my braces , Be regularly 1ms Do it once , Repeat
{
flag_1ms=0;
if(sw1==0)
{
key_timer++;
if(key_timer==100)
{
Motor_For();
}
else if(key_timer==900)
{
Motor_For();
}
else if(key_timer>=910)
{
key_timer=899;
}
}
else
{
key_timer=0;
}
}
}
}
边栏推荐
- Raspberry pie 4B Getting Started Guide
- Ad usage notes
- 记录开发过程中无法使用管理员身份修改系统文件问题
- Detailed methods for copying local computer files to virtual machine system
- Vs2019 and SQL
- 1、 Output debugging information: makefile file debugging information $(warning "tests" $(mkfile\u path)); makefile file path
- 网络安全-抓包和IP包头分析
- El input can only input numbers and has a decimal point. At most two digits can be reserved
- Network security - layer 3 switching technology and internal network planning
- C language implements sequential queue, circular queue and chain queue
猜你喜欢

app quits unexpectedly

Cubemx completes STM32F103 dual serial port 485 transceiver transmission

记录开发过程中无法使用管理员身份修改系统文件问题

Go common commands

Basic knowledge of compiling learning records

Basic knowledge of system software development

踩坑记录:supervisor 日志返回信息:redis扩展未安装

Binary tree traversal

C language - student achievement management system

Introduction to go project directory structure
随机推荐
[resolved] MySQL exception: error 1045 (28000): unknown error 1045, forgetting the initial password
Realization of dissolve effect in unity and its principle analysis
网络安全-路由原理
Cubemx completes STM32F103 dual serial port 485 transceiver transmission
01 - embedded learning route and career planning: embedded basic knowledge and development process
网络安全-单臂路由、DHCP中继和ICMP协议
Mailbox application routine of running wild fire RT thread
The first up Master of station B paid to watch the video still came! Price "Persuading" netizens
Local unloading traffic of 5g application
Assembly learning register
SQL Server2005中SUM函数内嵌套IF语句
Double click the idea to solve the problem of downloading again
Basic knowledge of system software development
【已解决】MySQL异常:ERROR 1045 (28000): Unknown error 1045,忘记初始密码
Halcon: read the camera and binary it
grep命令用法
Stm32g0 and FreeRTOS learning summary
Resolved: initialize specified but the data directory has files in it Aborting
C51 minimum system board infrared remote control LED light on and off
TC397 QSPI(CPU)