37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
![01.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/334ba5f4fe3213722215f3d2fcc8f089.jpg)
![01-.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/f81b5ec499f9e95a85811f056a87bef6.jpg)
![01-2.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/827dfab3835cba6d2576bbe8decee1e0.jpg)
电机规格 :42 步进电机
电机厚度 :33.3 MM
电机轴径 :5 MM圆轴(没有扁位)
输出轴长度 :22 MM
线长 :大约 140 MM(线是送的)
电机重量 :224 克
电机内阻 :1.5 欧
5V 短路电流 :2.7 A
![01-1.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/44140a2fd4dccb431882320db904dc5b.jpg)
42步进电机驱动,使用A4988模块
![02.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/6780d957c83d4b9ce6e98f51f994dea3.jpg)
42步进电机A4988驱动模块扩展板
![02-5.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/d2bb71b0c6851e7628a67203a774dd34.jpg)
![02-6.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/4b189b9b10b1f5658d63fe2372d49981.jpg)
![02-7.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/b3353a50bb80bc36eebb4c5e5347372b.jpg)
![02-8 (1).jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/6858e4f638ab0a15282d180d35220ae1.jpg)
![02-8 (2).jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/3b9c8ef99f0eb51cd7d6e3e3479a9d60.jpg)
![02-9.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/a64b9e3de7bf5e166646be25e794d137.jpg)
实验模块接线示意图
![02-10.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/2e331bf413e2937fdc88bba05f6fa9c2.jpg)
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:日本(Nidec)二相四线 KV4234 步进电机 步进角1.8 度 5MM圆轴
项目程序之一:控制步进电机转动
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机+A4988驱动器
项目程序之一:控制步进电机转动
/*
//包含AccelStepper库
#include <AccelStepper.h>
//定义引脚连接
const int dirPin = 4;
const int stepPin = 2;
int enPin = 8;
//定义电机接口类型
#define motorInterfaceType 1
//创建实例
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
//设置最大速度、加速度系数,
//初始速度和目标位置
pinMode( enPin , OUTPUT);
digitalWrite( enPin , LOW);
// myStepper.setCurrent(0);
// myStepper.setMaxSpeed(1000);
myStepper.setAcceleration(2000);
myStepper.setSpeed(1000);
myStepper.moveTo(1600);
}
void loop() {
//电机到达目标位置后改变方向
if (myStepper.distanceToGo() == 0)
myStepper.moveTo(-myStepper.currentPosition());
//将电机移动一步
myStepper.run();
}
实验场景图
![03.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/88cac160d74b223d834947685686aa3c.jpg)
![03-.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/632d442b286cb35e217a5aa3fd7ba86a.jpg)
实验记录视频
【花雕学编程】Arduino动手做(223)---日本(Nidec)二相四线 KV4234 步进电机 步进角1.8 度 5MM圆轴
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:日本(Nidec)二相四线 KV4234 步进电机 步进角1.8 度 5MM圆轴
项目实验之二:控制步进电机正向与反向转动
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:日本(Nidec)二相四线 KV4234 步进电机 步进角1.8 度 5MM圆轴
项目实验之二:控制步进电机正向与反向转动
*/
//必须要安装 mstimer2的库, 否则编译报错!!!!!
//如果需要用其他定时器驱动, 这里可以换成对应定时器的头文件
#include<MsTimer2.h>
#define t_bit unsigned char
#define t_bool unsigned char
#define t_uint8 unsigned char
#define t_int8 signed char
#define t_uint16 unsigned short
#define t_int16 signed short
#define t_uint32 unsigned long
#define t_int32 signed long
#define t_fix signed long
void cx_NULL_E(void);
void cx_SYS_NullFunc(void);
t_int8 cx__patch_get_char_width_real( t_int32 tmp_ch );
t_uint8 cx___sysbox__0_sys_iointerface_get_uint8( t_uint32 tmp_addr );
void cx___sysbox__0_sys_iointerface_set_uint8( t_uint32 tmp_addr, t_uint8 tmp_d );
t_uint16 cx___sysbox__0_sys_iointerface_get_uint16( t_uint32 tmp_addr );
void cx___sysbox__0_sys_iointerface_set_uint16( t_uint32 tmp_addr, t_uint16 tmp_d );
t_uint32 cx___sysbox__0_sys_iointerface_get_uint32( t_uint32 tmp_addr );
void cx___sysbox__0_sys_iointerface_set_uint32( t_uint32 tmp_addr, t_uint32 tmp_d );
void cx_bujindianjiqudongqi_driver_OS_init(void);
void cx_bujindianjiqudongqi_driver_OS_thread(void);
void cx_bujindianjiqudongqi_driver_DeviceInit(void);
void cx_bujindianjiqudongqi_driver_OS_run100us(void);
void cx_bujindianjiqudongqi_driver_set_speed( t_int32 tmp_s );
void cx_bujindianjiqudongqi_driver_run_right(void);
void cx_bujindianjiqudongqi_driver_run_left(void);
void cx_bujindianjiqudongqi_driver_run_right_t( t_int32 tmp_t );
void cx_bujindianjiqudongqi_driver_run_left_t( t_int32 tmp_t );
void cx_bujindianjiqudongqi_driver_run_to( t_int32 tmp_t );
void cx_bujindianjiqudongqi_driver_stop(void);
void cx_bujindianjiqudongqi_driver_short_stop(void);
t_bool cx_bujindianjiqudongqi_driver_Running(void);
t_bool cx_bujindianjiqudongqi_driver_Stoped(void);
void cx_bujindianjiqudongqi_driver_power_on(void);
void cx_bujindianjiqudongqi_driver_power_off(void);
void cx_bujindianjiqudongqi_driver_accel_on(void);
void cx_bujindianjiqudongqi_driver_accel_off(void);
void cx_yanshiqi_driver_OS_init(void);
void cx_yanshiqi_driver_OS_thread(void);
void cx_yanshiqi_driver_DelayMillisecond( t_int32 tmp_t );
void cx_yanshiqi_driver_DelaySecond( t_fix tmp_t );
void cx_yanshiqi_driver_DelayMinute( t_int32 tmp_t );
void cx_yanshiqi_driver_DelayHour( t_int32 tmp_t );
void cx_yanshiqi_driver_DelayDay( t_int32 tmp_t );
void cx_yanshiqi_driver_Stop(void);
void cx_yanshiqi_driver_StopCurrent(void);
void cx_yanshiqi_driver_StopAll(void);
void cx_kongzhiqiUNO_driver_OS_init(void);
void cx_kongzhiqiUNO_driver_OS_thread(void);
void cx_kongzhiqiUNO_driver_OS_run(void);
void cx_kongzhiqiUNO_driver_LightOpen(void);
void cx_kongzhiqiUNO_driver_LightClose(void);
void cx_kongzhiqiUNO_driver_LightSwap(void);
void cx_kongzhiqiUNO_driver_LightFlashTimes( t_int32 tmp_n );
void cx_kongzhiqiUNO_driver_LightFlash(void);
void cx_kongzhiqiUNO_driver_delay(void);
void cx_kongzhiqiUNO_pinMode( t_int32 tmp_p, t_int32 tmp_m );
void cx_kongzhiqiUNO_digitalWrite( t_int32 tmp_p, t_int32 tmp_d );
t_int32 cx_kongzhiqiUNO_digitalRead( t_int32 tmp_p );
void cx_OS0_REMO_ModuleWrite( t_int32 tmp_d, t_int32 tmp_d1 );
t_int32 cx_OS0_REMO_ModuleRead( t_int32 tmp_d );
void cx_OS0_Delayer_Init(void);
void cx_OS0_Delayer_Run(void);
t_bool cx_OS0_Delayer_Running(void);
t_bool cx_OS0_Delayer_TarRunning( t_uint8 tmp_i );
void cx_OS0_Delayer_Stop( t_uint16 tmp_cid );
void cx_OS0_Delayer_StopAll(void);
void cx_OS0_Delayer_Wait( t_int32 tmp_t );
t_int32 cx_OS0_GetSysTick(void);
t_uint32 cx_OS0_GetSysTickU(void);
void cx_OS0_OS_init(void);
void cx_OS0_CloseWatchdog(void);
void cx_OS0_MACRO_TIMER_ON(void);
void cx_OS0_MACRO_TIMER_OFF(void);
void cx_OS0_TimerInit(void);
void cx_OS0_OS_run(void);
void cx_OS0_DriverInit(void);
void cx_OS0_CreateDriver( void (*tmp_Handle)(void), t_uint8 tmp_MaxTick );
void cx_OS0_CreateDriver100us( void (*tmp_Handle)(void) );
void cx_OS0_RunDriverList(void);
void cx_OS0_RunDriver100usList(void);
void cx_OS0_D_CALL( void (*tmp_Handle)(void) );
void cx_OS0_Start(void);
void cx_OS0_Schedule(void);
void cx_sys_delayer_delay_n_10us( t_uint8 tmp_t );
void cx_sys_delayer_delay_n_1us( t_uint8 tmp_t );
void cx_sys_delayer_delay_ms( t_int32 tmp_t );
void cx_Sim_onInitDefault(void);
void cx_Sim_onDrawDefault(void);
void cx_Sim_onKeyEventDefault( t_bool tmp_down, t_int32 tmp_key );
t_int32 cx_Sim_crux_AddVar( t_int32 tmp_v );
t_int32 cx_Sim_crux_Run( t_int32 tmp_v );
void cx_Sim_OS_init(void);
void cx_Sim_OS_thread(void);
void cx_Sim_Clear(void);
void cx_Sim_DrawCircle( t_int32 tmp_x, t_int32 tmp_y, t_int32 tmp_r );
void cx_Sim_FillCircle( t_int32 tmp_x, t_int32 tmp_y, t_int32 tmp_r );
void cx_main0(void);
void cx_Setup(void);
void cx_Loop(void);
void cx_kongzhiqiUNO_fanfuzhixing(void);
void cx_OS_VarInit(void);
t_int32 cx_SYS_Tick;
t_uint8 cx_SYS_Loop;
t_int32 cx_CInsIndex;
t_uint8 cx_bujindianjiqudongqi_driver_OS_time;
t_int32 cx_bujindianjiqudongqi_driver_angle;
t_int16 cx_bujindianjiqudongqi_driver_myspeed;
t_int16 cx_bujindianjiqudongqi_driver_realspeed;
t_int16 cx_bujindianjiqudongqi_driver_offset;
t_uint8 cx_bujindianjiqudongqi_driver_tDIR;
t_int32 cx_bujindianjiqudongqi_driver_time;
t_bool cx_bujindianjiqudongqi_driver_upspeed;
t_bool cx_bujindianjiqudongqi_driver_downspeed;
t_bool cx_bujindianjiqudongqi_driver_accel;
t_bool cx_bujindianjiqudongqi_driver_runforever;
#define cx_kongzhiqiUNO_driver_RES_SP_LOW SP_LOW
#define cx_kongzhiqiUNO_driver_RES_SP_HIGH SP_HIGH
#define cx_kongzhiqiUNO_driver_RES_SP SP
#define cx_kongzhiqiUNO_driver_RES_SREG SREG
#define cx_kongzhiqiUNO_driver_RES_PINB PINB
#define cx_kongzhiqiUNO_driver_RES_DDRB DDRB
#define cx_kongzhiqiUNO_driver_RES_PORTB PORTB
#define cx_kongzhiqiUNO_driver_RES_PINC PINC
#define cx_kongzhiqiUNO_driver_RES_DDRC DDRC
#define cx_kongzhiqiUNO_driver_RES_PORTC PORTC
#define cx_kongzhiqiUNO_driver_RES_PIND PIND
#define cx_kongzhiqiUNO_driver_RES_DDRD DDRD
#define cx_kongzhiqiUNO_driver_RES_PORTD PORTD
#define cx_kongzhiqiUNO_driver_RES_WDTCSR WDTCSR
#define cx_kongzhiqiUNO_driver_RES_MCUSR MCUSR
#define cx_kongzhiqiUNO_driver_RES_TCCR0A TCCR0A
#define cx_kongzhiqiUNO_driver_RES_TCCR0B TCCR0B
#define cx_kongzhiqiUNO_driver_RES_TCNT0 TCNT0
#define cx_kongzhiqiUNO_driver_RES_OCR0A OCR0A
#define cx_kongzhiqiUNO_driver_RES_OCR0B OCR0B
#define cx_kongzhiqiUNO_driver_RES_TIMSK0 TIMSK0
#define cx_kongzhiqiUNO_driver_RES_TIFR0 TIFR0
#define cx_kongzhiqiUNO_driver_RES_ADCL ADCL
#define cx_kongzhiqiUNO_driver_RES_ADCH ADCH
#define cx_kongzhiqiUNO_driver_RES_ADCSRA ADCSRA
#define cx_kongzhiqiUNO_driver_RES_ADCSRB ADCSRB
#define cx_kongzhiqiUNO_driver_RES_ADMUX ADMUX
#define cx_kongzhiqiUNO_driver_RES_DIDR0 DIDR0
#define cx_kongzhiqiUNO_driver_RES_EICRA EICRA
#define cx_kongzhiqiUNO_driver_RES_EIMSK EIMSK
#define cx_kongzhiqiUNO_driver_RES_EIFR EIFR
#define cx_kongzhiqiUNO_driver_RES_OCR1BH OCR1BH
#define cx_kongzhiqiUNO_driver_RES_OCR1BL OCR1BL
#define cx_kongzhiqiUNO_driver_RES_OCR1AH OCR1AH
#define cx_kongzhiqiUNO_driver_RES_OCR1AL OCR1AL
#define cx_kongzhiqiUNO_driver_RES_TCNT1H TCNT1H
#define cx_kongzhiqiUNO_driver_RES_TCNT1L TCNT1L
#define cx_kongzhiqiUNO_driver_RES_TCCR1C TCCR1C
#define cx_kongzhiqiUNO_driver_RES_TCCR1B TCCR1B
#define cx_kongzhiqiUNO_driver_RES_TCCR1A TCCR1A
#define cx_kongzhiqiUNO_driver_RES_TIMSK1 TIMSK1
#define cx_kongzhiqiUNO_driver_RES_TIFR1 TIFR1
#define cx_kongzhiqiUNO_driver_RES_OCR2B OCR2B
#define cx_kongzhiqiUNO_driver_RES_OCR2A OCR2A
#define cx_kongzhiqiUNO_driver_RES_TCNT2 TCNT2
#define cx_kongzhiqiUNO_driver_RES_TCCR2B TCCR2B
#define cx_kongzhiqiUNO_driver_RES_TCCR2A TCCR2A
#define cx_kongzhiqiUNO_driver_RES_TIMSK2 TIMSK2
#define cx_kongzhiqiUNO_driver_RES_TIFR2 TIFR2
#define cx_kongzhiqiUNO_driver_RES_UCSR0A UCSR0A
#define cx_kongzhiqiUNO_driver_RES_UCSR0B UCSR0B
#define cx_kongzhiqiUNO_driver_RES_UCSR0C UCSR0C
#define cx_kongzhiqiUNO_driver_RES_UBRR0L UBRR0L
#define cx_kongzhiqiUNO_driver_RES_UBRR0H UBRR0H
#define cx_kongzhiqiUNO_driver_RES_UDR0 UDR0
#define cx_kongzhiqiUNO_driver_RES_EECR EECR
#define cx_kongzhiqiUNO_driver_RES_EEDR EEDR
#define cx_kongzhiqiUNO_driver_RES_EEARL EEARL
#define cx_kongzhiqiUNO_driver_RES_EEARH EEARH
#define cx_kongzhiqiUNO_driver_RES_SPCR SPCR
#define cx_kongzhiqiUNO_driver_RES_SPSR SPSR
#define cx_kongzhiqiUNO_driver_RES_SPDR SPDR
#define cx_kongzhiqiUNO_driver_RES_SMCR SMCR
#define cx_kongzhiqiUNO_driver_RES_MCUCSR MCUCSR
#define cx_kongzhiqiUNO_driver_RES_MCUCR MCUCR
t_uint8 cx_kongzhiqiUNO_driver_OS_time;
t_uint8 cx_kongzhiqiUNO_driver_OS_EventFlag;
t_uint8 cx_kongzhiqiUNO_driver_swap;
t_int32 cx_kongzhiqiUNO_driver_FlashNumber;
t_bool cx_kongzhiqiUNO_driver_FlashOpen;
t_bool cx_kongzhiqiUNO_driver_FlashForever;
t_bit cx_kongzhiqiUNO_GND_D0_DIR;
t_bit cx_kongzhiqiUNO_GND_D0_IN;
t_bit cx_kongzhiqiUNO_GND_D0_OUT;
t_bit cx_kongzhiqiUNO_GND_D0_PUL;
t_bit cx_kongzhiqiUNO_GND1_D0_DIR;
t_bit cx_kongzhiqiUNO_GND1_D0_IN;
t_bit cx_kongzhiqiUNO_GND1_D0_OUT;
t_bit cx_kongzhiqiUNO_GND1_D0_PUL;
t_bit cx_kongzhiqiUNO_GND2_D0_DIR;
t_bit cx_kongzhiqiUNO_GND2_D0_IN;
t_bit cx_kongzhiqiUNO_GND2_D0_OUT;
t_bit cx_kongzhiqiUNO_GND2_D0_PUL;
t_bit cx_kongzhiqiUNO_VCC1_D0_DIR;
t_bit cx_kongzhiqiUNO_VCC1_D0_IN;
t_bit cx_kongzhiqiUNO_VCC1_D0_OUT;
t_bit cx_kongzhiqiUNO_VCC1_D0_PUL;
t_bit cx_kongzhiqiUNO_VCC2_D0_DIR;
t_bit cx_kongzhiqiUNO_VCC2_D0_IN;
t_bit cx_kongzhiqiUNO_VCC2_D0_OUT;
t_bit cx_kongzhiqiUNO_VCC2_D0_PUL;
t_uint16 cx_OS0_ID_list[1];
t_int32 cx_OS0_time_list[1];
t_bool cx_OS0_enable_list[1];
t_uint16 cx_OS0_Delayer_tempID;
t_bool cx_OS0_EnableSchedule;
t_uint16 cx_OS0_Delayer_ActiveTaskIndex;
t_bool cx_OS0_Delayer_Pause;
t_uint32 cx_OS0_SysTick;
t_uint16 cx_OS0_CurrentTaskIndex;
t_uint8 cx_OS0_TickList[1];
t_uint8 cx_OS0_MaxTickList[1];
void (*cx_OS0_DriverHandleList[1])(void);
t_uint8 cx_OS0_DriverNumber;
void (*cx_OS0_Driver100usHandleList[1])(void);
t_uint8 cx_OS0_Driver100usNumber;
t_uint8 cx_OS0_tick;
t_uint8 cx_OS0_DebugTick;
void (*cx_OS0_s_Handle)(void);
t_bool cx_kongzhiqiUNO_fanfuzhixing_en_;
t_uint8 cx_kongzhiqiUNO_fanfuzhixing_flg_;
void cx_NULL_E(void)
{
}
void cx_SYS_NullFunc(void)
{
}
t_int8 cx__patch_get_char_width_real( t_int32 tmp_ch )
{
return 1;
}
t_uint8 cx___sysbox__0_sys_iointerface_get_uint8( t_uint32 tmp_addr )
{
return 0;
}
void cx___sysbox__0_sys_iointerface_set_uint8( t_uint32 tmp_addr, t_uint8 tmp_d )
{
}
t_uint16 cx___sysbox__0_sys_iointerface_get_uint16( t_uint32 tmp_addr )
{
return 0;
}
void cx___sysbox__0_sys_iointerface_set_uint16( t_uint32 tmp_addr, t_uint16 tmp_d )
{
}
t_uint32 cx___sysbox__0_sys_iointerface_get_uint32( t_uint32 tmp_addr )
{
return 0;
}
void cx___sysbox__0_sys_iointerface_set_uint32( t_uint32 tmp_addr, t_uint32 tmp_d )
{
}
void cx_bujindianjiqudongqi_driver_OS_init(void)
{
cx_kongzhiqiUNO_driver_RES_DDRD |= 4;
cx_kongzhiqiUNO_driver_RES_PORTD &= 251;
cx_kongzhiqiUNO_driver_RES_DDRD |= 16;
cx_kongzhiqiUNO_driver_RES_PORTD &= 239;
cx_kongzhiqiUNO_driver_RES_DDRB |= 1;
cx_kongzhiqiUNO_driver_RES_PORTB &= 254;
cx_bujindianjiqudongqi_driver_tDIR = 0;
cx_bujindianjiqudongqi_driver_offset = 0;
cx_bujindianjiqudongqi_driver_realspeed = 0;
cx_bujindianjiqudongqi_driver_time = 0;
cx_bujindianjiqudongqi_driver_runforever = 0;
cx_bujindianjiqudongqi_driver_upspeed = 0;
cx_bujindianjiqudongqi_driver_downspeed = 0;
cx_bujindianjiqudongqi_driver_myspeed = (t_int16)500;
cx_bujindianjiqudongqi_driver_accel = 1;
cx_bujindianjiqudongqi_driver_DeviceInit();
}
void cx_bujindianjiqudongqi_driver_OS_thread(void)
{
}
void cx_bujindianjiqudongqi_driver_DeviceInit(void)
{
}
void cx_bujindianjiqudongqi_driver_OS_run100us(void)
{
if( cx_bujindianjiqudongqi_driver_tDIR == 0 ) {
return;
}
if( cx_bujindianjiqudongqi_driver_accel ) {
if( cx_bujindianjiqudongqi_driver_upspeed ) {
if( cx_bujindianjiqudongqi_driver_realspeed < cx_bujindianjiqudongqi_driver_myspeed ) {
cx_bujindianjiqudongqi_driver_realspeed = cx_bujindianjiqudongqi_driver_realspeed + (1);
}
else {
cx_bujindianjiqudongqi_driver_upspeed = 0;
}
}
if( !cx_bujindianjiqudongqi_driver_runforever ) {
if( cx_bujindianjiqudongqi_driver_downspeed ) {
if( cx_bujindianjiqudongqi_driver_realspeed > 1 ) {
cx_bujindianjiqudongqi_driver_realspeed = cx_bujindianjiqudongqi_driver_realspeed - (1);
}
}
}
cx_bujindianjiqudongqi_driver_offset = cx_bujindianjiqudongqi_driver_offset + (cx_bujindianjiqudongqi_driver_realspeed);
}
else {
cx_bujindianjiqudongqi_driver_offset = cx_bujindianjiqudongqi_driver_offset + (cx_bujindianjiqudongqi_driver_myspeed);
}
if( cx_bujindianjiqudongqi_driver_offset < 10000 ) {
return;
}
cx_bujindianjiqudongqi_driver_offset = cx_bujindianjiqudongqi_driver_offset - (10000);
if( !cx_bujindianjiqudongqi_driver_runforever ) {
if( cx_bujindianjiqudongqi_driver_time > 0 ) {
cx_bujindianjiqudongqi_driver_time = cx_bujindianjiqudongqi_driver_time - (1);
if( !cx_bujindianjiqudongqi_driver_downspeed ) {
t_int32 tmp_dline = cx_bujindianjiqudongqi_driver_realspeed;
tmp_dline = tmp_dline * cx_bujindianjiqudongqi_driver_realspeed / 20000;
if( cx_bujindianjiqudongqi_driver_time < tmp_dline ) {
cx_bujindianjiqudongqi_driver_downspeed = 1;
cx_bujindianjiqudongqi_driver_upspeed = 0;
}
}
}
else {
cx_bujindianjiqudongqi_driver_tDIR = 0;
return;
}
}
if( cx_bujindianjiqudongqi_driver_tDIR == 1 ) {
cx_kongzhiqiUNO_driver_RES_PORTD &= 239;
cx_bujindianjiqudongqi_driver_angle = cx_bujindianjiqudongqi_driver_angle + (1);
}
else {
cx_kongzhiqiUNO_driver_RES_PORTD |= 16;
cx_bujindianjiqudongqi_driver_angle = cx_bujindianjiqudongqi_driver_angle - (1);
}
cx_kongzhiqiUNO_driver_RES_PORTD |= 4;
cx_kongzhiqiUNO_driver_RES_PORTD &= 251;
}
void cx_bujindianjiqudongqi_driver_set_speed( t_int32 tmp_s )
{
cx_bujindianjiqudongqi_driver_myspeed = (t_int16)tmp_s;
}
void cx_bujindianjiqudongqi_driver_run_right(void)
{
if( cx_bujindianjiqudongqi_driver_realspeed != 0 && cx_bujindianjiqudongqi_driver_tDIR == 2 ) {
cx_bujindianjiqudongqi_driver_realspeed = 0;
}
cx_bujindianjiqudongqi_driver_tDIR = 1;
cx_bujindianjiqudongqi_driver_time = 0;
cx_bujindianjiqudongqi_driver_runforever = 1;
cx_bujindianjiqudongqi_driver_upspeed = 1;
cx_bujindianjiqudongqi_driver_downspeed = 0;
}
void cx_bujindianjiqudongqi_driver_run_left(void)
{
if( cx_bujindianjiqudongqi_driver_realspeed != 0 && cx_bujindianjiqudongqi_driver_tDIR == 1 ) {
cx_bujindianjiqudongqi_driver_realspeed = 0;
}
cx_bujindianjiqudongqi_driver_tDIR = 2;
cx_bujindianjiqudongqi_driver_time = 0;
cx_bujindianjiqudongqi_driver_runforever = 1;
cx_bujindianjiqudongqi_driver_upspeed = 1;
cx_bujindianjiqudongqi_driver_downspeed = 0;
}
void cx_bujindianjiqudongqi_driver_run_right_t( t_int32 tmp_t )
{
if( cx_bujindianjiqudongqi_driver_realspeed != 0 && cx_bujindianjiqudongqi_driver_tDIR == 2 ) {
cx_bujindianjiqudongqi_driver_realspeed = 0;
}
cx_bujindianjiqudongqi_driver_time = tmp_t;
cx_bujindianjiqudongqi_driver_tDIR = 1;
cx_bujindianjiqudongqi_driver_runforever = 0;
cx_bujindianjiqudongqi_driver_upspeed = 1;
cx_bujindianjiqudongqi_driver_downspeed = 0;
}
void cx_bujindianjiqudongqi_driver_run_left_t( t_int32 tmp_t )
{
if( cx_bujindianjiqudongqi_driver_realspeed != 0 && cx_bujindianjiqudongqi_driver_tDIR == 1 ) {
cx_bujindianjiqudongqi_driver_realspeed = 0;
}
cx_bujindianjiqudongqi_driver_time = tmp_t;
cx_bujindianjiqudongqi_driver_tDIR = 2;
cx_bujindianjiqudongqi_driver_runforever = 0;
cx_bujindianjiqudongqi_driver_upspeed = 1;
cx_bujindianjiqudongqi_driver_downspeed = 0;
}
void cx_bujindianjiqudongqi_driver_run_to( t_int32 tmp_t )
{
t_int32 tmp_t_angle = cx_bujindianjiqudongqi_driver_angle;
if( tmp_t == tmp_t_angle ) {
return;
}
if( tmp_t > tmp_t_angle ) {
cx_bujindianjiqudongqi_driver_run_right_t( tmp_t - cx_bujindianjiqudongqi_driver_angle );
}
else {
cx_bujindianjiqudongqi_driver_run_left_t( cx_bujindianjiqudongqi_driver_angle - tmp_t );
}
}
void cx_bujindianjiqudongqi_driver_stop(void)
{
if( !cx_bujindianjiqudongqi_driver_accel ) {
cx_bujindianjiqudongqi_driver_tDIR = 0;
cx_bujindianjiqudongqi_driver_runforever = 0;
cx_bujindianjiqudongqi_driver_downspeed = 0;
cx_bujindianjiqudongqi_driver_upspeed = 0;
cx_bujindianjiqudongqi_driver_time = 0;
cx_bujindianjiqudongqi_driver_realspeed = 0;
}
else {
cx_bujindianjiqudongqi_driver_runforever = 0;
cx_bujindianjiqudongqi_driver_downspeed = 1;
cx_bujindianjiqudongqi_driver_upspeed = 0;
cx_bujindianjiqudongqi_driver_time = cx_bujindianjiqudongqi_driver_realspeed;
cx_bujindianjiqudongqi_driver_time = cx_bujindianjiqudongqi_driver_time * cx_bujindianjiqudongqi_driver_realspeed / 20000;
}
}
void cx_bujindianjiqudongqi_driver_short_stop(void)
{
cx_bujindianjiqudongqi_driver_tDIR = 0;
cx_bujindianjiqudongqi_driver_runforever = 0;
cx_bujindianjiqudongqi_driver_downspeed = 0;
cx_bujindianjiqudongqi_driver_upspeed = 0;
cx_bujindianjiqudongqi_driver_time = 0;
cx_bujindianjiqudongqi_driver_realspeed = 0;
}
t_bool cx_bujindianjiqudongqi_driver_Running(void)
{
return cx_bujindianjiqudongqi_driver_tDIR != 0;
}
t_bool cx_bujindianjiqudongqi_driver_Stoped(void)
{
return cx_bujindianjiqudongqi_driver_tDIR == 0;
}
void cx_bujindianjiqudongqi_driver_power_on(void)
{
cx_kongzhiqiUNO_driver_RES_PORTB &= 254;
}
void cx_bujindianjiqudongqi_driver_power_off(void)
{
cx_kongzhiqiUNO_driver_RES_PORTB |= 1;
}
void cx_bujindianjiqudongqi_driver_accel_on(void)
{
cx_bujindianjiqudongqi_driver_accel = 1;
}
void cx_bujindianjiqudongqi_driver_accel_off(void)
{
cx_bujindianjiqudongqi_driver_accel = 0;
}
void cx_yanshiqi_driver_OS_init(void)
{
}
void cx_yanshiqi_driver_OS_thread(void)
{
}
void cx_yanshiqi_driver_DelayMillisecond( t_int32 tmp_t )
{
t_int32 tmp_tt = tmp_t;
if( tmp_tt != 0 ) {
cx_OS0_Delayer_tempID = 2;
cx_OS0_Delayer_Wait( tmp_tt );
}
}
void cx_yanshiqi_driver_DelaySecond( t_fix tmp_t )
{
t_uint32 tmp_d = (t_uint32)tmp_t;
tmp_d = tmp_d * 1000 / 1024;
cx_yanshiqi_driver_DelayMillisecond( (t_int32)tmp_d );
}
void cx_yanshiqi_driver_DelayMinute( t_int32 tmp_t )
{
cx_yanshiqi_driver_DelayMillisecond( tmp_t * 60000 );
}
void cx_yanshiqi_driver_DelayHour( t_int32 tmp_t )
{
cx_yanshiqi_driver_DelayMillisecond( tmp_t * 3600000 );
}
void cx_yanshiqi_driver_DelayDay( t_int32 tmp_t )
{
cx_yanshiqi_driver_DelayMillisecond( tmp_t * 86400000 );
}
void cx_yanshiqi_driver_Stop(void)
{
cx_OS0_Delayer_Stop( 2 );
}
void cx_yanshiqi_driver_StopCurrent(void)
{
cx_OS0_Delayer_Stop( 2 );
}
void cx_yanshiqi_driver_StopAll(void)
{
cx_OS0_Delayer_StopAll();
}
void cx_kongzhiqiUNO_driver_OS_init(void)
{
cx_kongzhiqiUNO_driver_FlashOpen = 0;
cx_kongzhiqiUNO_driver_FlashForever = 0;
cx_kongzhiqiUNO_driver_FlashNumber = 0;
cx_kongzhiqiUNO_driver_swap = 0;
cx_kongzhiqiUNO_driver_OS_time = 50;
cx_kongzhiqiUNO_driver_OS_EventFlag = 0;
cx_kongzhiqiUNO_driver_OS_EventFlag |= 1;
t_uint16 _cx_lc_1 = 10;
for( t_uint16 _cx_lc_0 = 0; _cx_lc_0 < _cx_lc_1; _cx_lc_0++ ) {
cx_kongzhiqiUNO_driver_delay();
}
cx_kongzhiqiUNO_driver_RES_DDRB |= 32;
cx_kongzhiqiUNO_driver_RES_PORTB &= 223;
}
void cx_kongzhiqiUNO_driver_OS_thread(void)
{
cx_kongzhiqiUNO_driver_OS_EventFlag |= 2;
}
void cx_kongzhiqiUNO_driver_OS_run(void)
{
if( !cx_kongzhiqiUNO_driver_FlashOpen ) {
return;
}
cx_kongzhiqiUNO_driver_swap = cx_kongzhiqiUNO_driver_swap % (10);
t_bit tmp_OUT;
if( cx_kongzhiqiUNO_driver_swap < 1 ) {
tmp_OUT = 1;
}
else {
tmp_OUT = 0;
}
cx_kongzhiqiUNO_driver_swap = cx_kongzhiqiUNO_driver_swap + (1);
if( cx_kongzhiqiUNO_driver_FlashForever ) {
(tmp_OUT) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 32): (cx_kongzhiqiUNO_driver_RES_PORTB &= 223);
return;
}
if( cx_kongzhiqiUNO_driver_FlashNumber == 0 ) {
cx_kongzhiqiUNO_driver_FlashOpen = 0;
return;
}
if( ((cx_kongzhiqiUNO_driver_RES_PORTB >> 5) & 1) == 1 && tmp_OUT == 0 ) {
cx_kongzhiqiUNO_driver_FlashNumber = cx_kongzhiqiUNO_driver_FlashNumber - (1);
}
(tmp_OUT) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 32): (cx_kongzhiqiUNO_driver_RES_PORTB &= 223);
}
void cx_kongzhiqiUNO_driver_LightOpen(void)
{
cx_kongzhiqiUNO_driver_FlashOpen = 0;
cx_kongzhiqiUNO_driver_RES_PORTB |= 32;
}
void cx_kongzhiqiUNO_driver_LightClose(void)
{
cx_kongzhiqiUNO_driver_FlashOpen = 0;
cx_kongzhiqiUNO_driver_RES_PORTB &= 223;
}
void cx_kongzhiqiUNO_driver_LightSwap(void)
{
cx_kongzhiqiUNO_driver_FlashOpen = 0;
((((cx_kongzhiqiUNO_driver_RES_PORTB >> 5) & 1)^1)) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 32): (cx_kongzhiqiUNO_driver_RES_PORTB &= 223);
}
void cx_kongzhiqiUNO_driver_LightFlashTimes( t_int32 tmp_n )
{
cx_kongzhiqiUNO_driver_FlashNumber = tmp_n;
cx_kongzhiqiUNO_driver_swap = 0;
cx_kongzhiqiUNO_driver_FlashForever = 0;
cx_kongzhiqiUNO_driver_FlashOpen = 1;
}
void cx_kongzhiqiUNO_driver_LightFlash(void)
{
cx_kongzhiqiUNO_driver_FlashNumber = 0;
cx_kongzhiqiUNO_driver_FlashForever = 1;
cx_kongzhiqiUNO_driver_FlashOpen = 1;
}
void cx_kongzhiqiUNO_driver_delay(void)
{
t_uint16 _cx_lc_1 = 250;
for( t_uint16 _cx_lc_0 = 0; _cx_lc_0 < _cx_lc_1; _cx_lc_0++ ) {
t_uint16 _cx_lc_3 = 250;
for( t_uint16 _cx_lc_2 = 0; _cx_lc_2 < _cx_lc_3; _cx_lc_2++ ) {
}
}
}
void cx_kongzhiqiUNO_pinMode( t_int32 tmp_p, t_int32 tmp_m )
{
if( tmp_p > 19 || tmp_p < 0 ) {
return;
}
t_bit tmp_b;
if( tmp_m != 0 ) {
tmp_b = 1;
}
else {
tmp_b = 0;
}
switch( (t_uint8)(t_uint16)(t_uint32)tmp_p ) {
case 0:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 1): (cx_kongzhiqiUNO_driver_RES_DDRD &= 254);
break;
case 1:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 2): (cx_kongzhiqiUNO_driver_RES_DDRD &= 253);
break;
case 2:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 4): (cx_kongzhiqiUNO_driver_RES_DDRD &= 251);
break;
case 3:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 8): (cx_kongzhiqiUNO_driver_RES_DDRD &= 247);
break;
case 4:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 16): (cx_kongzhiqiUNO_driver_RES_DDRD &= 239);
break;
case 5:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 32): (cx_kongzhiqiUNO_driver_RES_DDRD &= 223);
break;
case 6:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 64): (cx_kongzhiqiUNO_driver_RES_DDRD &= 191);
break;
case 7:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRD |= 128): (cx_kongzhiqiUNO_driver_RES_DDRD &= 127);
break;
case 8:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRB |= 1): (cx_kongzhiqiUNO_driver_RES_DDRB &= 254);
break;
case 9:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRB |= 2): (cx_kongzhiqiUNO_driver_RES_DDRB &= 253);
break;
case 10:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRB |= 4): (cx_kongzhiqiUNO_driver_RES_DDRB &= 251);
break;
case 11:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRB |= 8): (cx_kongzhiqiUNO_driver_RES_DDRB &= 247);
break;
case 12:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRB |= 16): (cx_kongzhiqiUNO_driver_RES_DDRB &= 239);
break;
case 13:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRB |= 32): (cx_kongzhiqiUNO_driver_RES_DDRB &= 223);
break;
case 14:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRC |= 1): (cx_kongzhiqiUNO_driver_RES_DDRC &= 254);
break;
case 15:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRC |= 2): (cx_kongzhiqiUNO_driver_RES_DDRC &= 253);
break;
case 16:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRC |= 4): (cx_kongzhiqiUNO_driver_RES_DDRC &= 251);
break;
case 17:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRC |= 8): (cx_kongzhiqiUNO_driver_RES_DDRC &= 247);
break;
case 18:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRC |= 16): (cx_kongzhiqiUNO_driver_RES_DDRC &= 239);
break;
case 19:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_DDRC |= 32): (cx_kongzhiqiUNO_driver_RES_DDRC &= 223);
break;
default:
break;
}
}
void cx_kongzhiqiUNO_digitalWrite( t_int32 tmp_p, t_int32 tmp_d )
{
if( tmp_p > 19 || tmp_p < 0 ) {
return;
}
t_bit tmp_b;
if( tmp_d != 0 ) {
tmp_b = 1;
}
else {
tmp_b = 0;
}
switch( (t_uint8)(t_uint16)(t_uint32)tmp_p ) {
case 0:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 1): (cx_kongzhiqiUNO_driver_RES_PORTD &= 254);
break;
case 1:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 2): (cx_kongzhiqiUNO_driver_RES_PORTD &= 253);
break;
case 2:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 4): (cx_kongzhiqiUNO_driver_RES_PORTD &= 251);
break;
case 3:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 8): (cx_kongzhiqiUNO_driver_RES_PORTD &= 247);
break;
case 4:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 16): (cx_kongzhiqiUNO_driver_RES_PORTD &= 239);
break;
case 5:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 32): (cx_kongzhiqiUNO_driver_RES_PORTD &= 223);
break;
case 6:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 64): (cx_kongzhiqiUNO_driver_RES_PORTD &= 191);
break;
case 7:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTD |= 128): (cx_kongzhiqiUNO_driver_RES_PORTD &= 127);
break;
case 8:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 1): (cx_kongzhiqiUNO_driver_RES_PORTB &= 254);
break;
case 9:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 2): (cx_kongzhiqiUNO_driver_RES_PORTB &= 253);
break;
case 10:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 4): (cx_kongzhiqiUNO_driver_RES_PORTB &= 251);
break;
case 11:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 8): (cx_kongzhiqiUNO_driver_RES_PORTB &= 247);
break;
case 12:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 16): (cx_kongzhiqiUNO_driver_RES_PORTB &= 239);
break;
case 13:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTB |= 32): (cx_kongzhiqiUNO_driver_RES_PORTB &= 223);
break;
case 14:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTC |= 1): (cx_kongzhiqiUNO_driver_RES_PORTC &= 254);
break;
case 15:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTC |= 2): (cx_kongzhiqiUNO_driver_RES_PORTC &= 253);
break;
case 16:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTC |= 4): (cx_kongzhiqiUNO_driver_RES_PORTC &= 251);
break;
case 17:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTC |= 8): (cx_kongzhiqiUNO_driver_RES_PORTC &= 247);
break;
case 18:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTC |= 16): (cx_kongzhiqiUNO_driver_RES_PORTC &= 239);
break;
case 19:
(tmp_b) != 0 ? (cx_kongzhiqiUNO_driver_RES_PORTC |= 32): (cx_kongzhiqiUNO_driver_RES_PORTC &= 223);
break;
default:
break;
}
}
t_int32 cx_kongzhiqiUNO_digitalRead( t_int32 tmp_p )
{
if( tmp_p > 19 || tmp_p < 0 ) {
return 0;
}
t_bit tmp_b;
switch( (t_uint8)(t_uint16)(t_uint32)tmp_p ) {
case 0:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 0) & 1);
break;
case 1:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 1) & 1);
break;
case 2:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 2) & 1);
break;
case 3:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 3) & 1);
break;
case 4:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 4) & 1);
break;
case 5:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 5) & 1);
break;
case 6:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 6) & 1);
break;
case 7:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PIND >> 7) & 1);
break;
case 8:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINB >> 0) & 1);
break;
case 9:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINB >> 1) & 1);
break;
case 10:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINB >> 2) & 1);
break;
case 11:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINB >> 3) & 1);
break;
case 12:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINB >> 4) & 1);
break;
case 13:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINB >> 5) & 1);
break;
case 14:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINC >> 0) & 1);
break;
case 15:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINC >> 1) & 1);
break;
case 16:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINC >> 2) & 1);
break;
case 17:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINC >> 3) & 1);
break;
case 18:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINC >> 4) & 1);
break;
case 19:
tmp_b = ((cx_kongzhiqiUNO_driver_RES_PINC >> 5) & 1);
break;
default:
break;
}
if( tmp_b == 0 ) {
return 0;
}
else {
return 1;
}
return 0;
}
void cx_OS0_REMO_ModuleWrite( t_int32 tmp_d, t_int32 tmp_d1 )
{
}
t_int32 cx_OS0_REMO_ModuleRead( t_int32 tmp_d )
{
return 0;
}
void cx_OS0_Delayer_Init(void)
{
cx_OS0_EnableSchedule = 1;
cx_OS0_Delayer_Pause = 0;
}
void cx_OS0_Delayer_Run(void)
{
if( cx_OS0_Delayer_Pause ) {
return;
}
for( t_uint8 tmp_i = 0; tmp_i < 1; ) {
if( cx_OS0_enable_list[tmp_i] ) {
cx_OS0_time_list[tmp_i] = cx_OS0_time_list[tmp_i] - (cx_SYS_Tick);
if( cx_OS0_time_list[tmp_i] <= 0 ) {
cx_OS0_enable_list[tmp_i] = 0;
}
}
tmp_i = tmp_i + (1);
}
}
t_bool cx_OS0_Delayer_Running(void)
{
return cx_OS0_enable_list[cx_OS0_CurrentTaskIndex];
}
t_bool cx_OS0_Delayer_TarRunning( t_uint8 tmp_i )
{
if( !cx_OS0_EnableSchedule && cx_OS0_Delayer_ActiveTaskIndex != tmp_i ) {
return 1;
}
return cx_OS0_enable_list[tmp_i];
}
void cx_OS0_Delayer_Stop( t_uint16 tmp_cid )
{
for( t_uint8 tmp_i = 0; tmp_i < 1; ) {
if( cx_OS0_ID_list[tmp_i] == tmp_cid ) {
cx_OS0_time_list[tmp_i] = 0;
cx_OS0_enable_list[tmp_i] = 0;
}
tmp_i = tmp_i + (1);
}
}
void cx_OS0_Delayer_StopAll(void)
{
for( t_uint8 tmp_i = 0; tmp_i < 1; ) {
cx_OS0_time_list[tmp_i] = 0;
cx_OS0_enable_list[tmp_i] = 0;
tmp_i = tmp_i + (1);
}
}
void cx_OS0_Delayer_Wait( t_int32 tmp_t )
{
cx_OS0_ID_list[cx_OS0_CurrentTaskIndex] = cx_OS0_Delayer_tempID;
cx_OS0_time_list[cx_OS0_CurrentTaskIndex] = tmp_t;
cx_OS0_enable_list[cx_OS0_CurrentTaskIndex] = 1;
cx_OS0_Delayer_ActiveTaskIndex = cx_OS0_CurrentTaskIndex;
}
t_int32 cx_OS0_GetSysTick(void)
{
return (t_int32)(cx_OS0_SysTick & 2147483647);
}
t_uint32 cx_OS0_GetSysTickU(void)
{
return cx_OS0_SysTick;
}
void cx_OS0_OS_init(void)
{
cx_OS0_CloseWatchdog();
cx_OS0_TimerInit();
cx_OS0_DriverInit();
cx_OS0_Delayer_Init();
cx_OS0_tick = 0;
cx_OS0_CurrentTaskIndex = 0;
}
void cx_OS0_CloseWatchdog(void)
{
}
void cx_OS0_MACRO_TIMER_ON(void)
{
}
void cx_OS0_MACRO_TIMER_OFF(void)
{
}
void cx_OS0_TimerInit(void)
{
}
void cx_OS0_OS_run(void)
{
cx_OS0_MACRO_TIMER_OFF();
cx_OS0_RunDriver100usList();
cx_OS0_tick = cx_OS0_tick + (1);
if( cx_OS0_tick >= 10 ) {
cx_OS0_RunDriverList();
cx_OS0_tick = 0;
cx_OS0_DebugTick = cx_OS0_DebugTick + (1);
cx_OS0_SysTick = cx_OS0_SysTick + (1);
}
cx_OS0_MACRO_TIMER_ON();
}
void cx_OS0_DriverInit(void)
{
cx_OS0_DriverNumber = 0;
for( t_uint8 tmp_i = 0; tmp_i < 1; ) {
cx_OS0_TickList[tmp_i] = 0;
cx_OS0_MaxTickList[tmp_i] = 0;
cx_OS0_DriverHandleList[tmp_i] = 0;
tmp_i = tmp_i + (1);
}
cx_OS0_Driver100usNumber = 0;
}
void cx_OS0_CreateDriver( void (*tmp_Handle)(void), t_uint8 tmp_MaxTick )
{
cx_OS0_DriverHandleList[cx_OS0_DriverNumber] = tmp_Handle;
cx_OS0_MaxTickList[cx_OS0_DriverNumber] = tmp_MaxTick;
cx_OS0_DriverNumber = cx_OS0_DriverNumber + (1);
}
void cx_OS0_CreateDriver100us( void (*tmp_Handle)(void) )
{
cx_OS0_Driver100usHandleList[cx_OS0_Driver100usNumber] = tmp_Handle;
cx_OS0_Driver100usNumber = cx_OS0_Driver100usNumber + (1);
}
void cx_OS0_RunDriverList(void)
{
for( t_uint8 tmp_i = 0; tmp_i < cx_OS0_DriverNumber; ) {
cx_OS0_TickList[tmp_i] = cx_OS0_TickList[tmp_i] + (1);
if( cx_OS0_TickList[tmp_i] == cx_OS0_MaxTickList[tmp_i] ) {
cx_OS0_TickList[tmp_i] = 0;
cx_OS0_D_CALL( cx_OS0_DriverHandleList[tmp_i] );
}
tmp_i = tmp_i + (1);
}
cx_OS0_Delayer_Run();
}
void cx_OS0_RunDriver100usList(void)
{
for( t_uint8 tmp_i = 0; tmp_i < cx_OS0_Driver100usNumber; ) {
cx_OS0_D_CALL( cx_OS0_Driver100usHandleList[tmp_i] );
tmp_i = tmp_i + (1);
}
}
void cx_OS0_D_CALL( void (*tmp_Handle)(void) )
{
cx_OS0_s_Handle = tmp_Handle;
(*cx_OS0_s_Handle)();
}
void cx_OS0_Start(void)
{
cx_OS0_MACRO_TIMER_ON();
}
void cx_OS0_Schedule(void)
{
}
void cx_sys_delayer_delay_n_10us( t_uint8 tmp_t )
{
t_uint16 _cx_lc_1 = tmp_t;
for( t_uint16 _cx_lc_0 = 0; _cx_lc_0 < _cx_lc_1; _cx_lc_0++ ) {
cx_sys_delayer_delay_n_1us( 10 );
}
}
void cx_sys_delayer_delay_n_1us( t_uint8 tmp_t )
{
delayMicroseconds( tmp_t );
}
void cx_sys_delayer_delay_ms( t_int32 tmp_t )
{
t_uint16 _cx_lc_1 = tmp_t;
for( t_uint16 _cx_lc_0 = 0; _cx_lc_0 < _cx_lc_1; _cx_lc_0++ ) {
cx_sys_delayer_delay_n_10us( 100 );
}
}
void cx_Sim_onInitDefault(void)
{
}
void cx_Sim_onDrawDefault(void)
{
}
void cx_Sim_onKeyEventDefault( t_bool tmp_down, t_int32 tmp_key )
{
}
t_int32 cx_Sim_crux_AddVar( t_int32 tmp_v )
{
return 0;
}
t_int32 cx_Sim_crux_Run( t_int32 tmp_v )
{
return 0;
}
void cx_Sim_OS_init(void)
{
cx_Sim_onInitDefault();
}
void cx_Sim_OS_thread(void)
{
t_int32 tmp_d = cx_Sim_crux_Run( 0 );
if( tmp_d != 0 ) {
t_int32 tmp_type = tmp_d / 1000;
tmp_d = tmp_d % 1000;
if( tmp_type == 1 ) {
cx_Sim_onKeyEventDefault( 1, tmp_d );
}
if( tmp_type == 2 ) {
cx_Sim_onKeyEventDefault( 0, tmp_d );
}
}
cx_Sim_Clear();
cx_Sim_onDrawDefault();
}
void cx_Sim_Clear(void)
{
t_int32 tmp_d = cx_Sim_crux_Run( 10 );
}
void cx_Sim_DrawCircle( t_int32 tmp_x, t_int32 tmp_y, t_int32 tmp_r )
{
t_int32 tmp_d;
tmp_d = cx_Sim_crux_AddVar( tmp_x );
tmp_d = cx_Sim_crux_AddVar( tmp_y );
tmp_d = cx_Sim_crux_AddVar( tmp_r );
tmp_d = cx_Sim_crux_Run( 11 );
}
void cx_Sim_FillCircle( t_int32 tmp_x, t_int32 tmp_y, t_int32 tmp_r )
{
t_int32 tmp_d;
tmp_d = cx_Sim_crux_AddVar( tmp_x );
tmp_d = cx_Sim_crux_AddVar( tmp_y );
tmp_d = cx_Sim_crux_AddVar( tmp_r );
tmp_d = cx_Sim_crux_Run( 12 );
}
void cx_main0(void)
{
cx_Setup();
while( true ) {
cx_Loop();
}
}
void cx_Setup(void)
{
cx_OS_VarInit();
cx_OS0_OS_init();
cx_kongzhiqiUNO_driver_OS_init();
cx_bujindianjiqudongqi_driver_OS_init();
cx_yanshiqi_driver_OS_init();
cx_OS0_CreateDriver100us( &cx_bujindianjiqudongqi_driver_OS_run100us );
cx_OS0_CreateDriver( &cx_kongzhiqiUNO_driver_OS_run, cx_kongzhiqiUNO_driver_OS_time );
cx_OS0_Start();
}
void cx_Loop(void)
{
if( cx_OS0_EnableSchedule ) {
if( ((cx_kongzhiqiUNO_driver_OS_EventFlag >> 1) & 1) == 1 ) {
cx_kongzhiqiUNO_driver_OS_EventFlag &= 253;
if( cx_kongzhiqiUNO_fanfuzhixing_en_ && cx_kongzhiqiUNO_fanfuzhixing_flg_ == 0 ) {
cx_kongzhiqiUNO_fanfuzhixing_flg_ = 1;
cx_kongzhiqiUNO_fanfuzhixing();
}
}
}
cx_bujindianjiqudongqi_driver_OS_thread();
cx_yanshiqi_driver_OS_thread();
cx_kongzhiqiUNO_driver_OS_thread();
if( cx_kongzhiqiUNO_fanfuzhixing_flg_ != 0 ) {
cx_kongzhiqiUNO_fanfuzhixing();
}
cx_SYS_Loop = 1;
}
void cx_kongzhiqiUNO_fanfuzhixing(void)
{
goto cx_kongzhiqiUNO_fanfuzhixing_end;
cx_kongzhiqiUNO_fanfuzhixing_1:;
cx_bujindianjiqudongqi_driver_power_on();
cx_kongzhiqiUNO_driver_LightFlash();
cx_bujindianjiqudongqi_driver_run_right_t( ((96) * (4)) );
cx_OS0_CurrentTaskIndex = 0;
cx_yanshiqi_driver_DelaySecond( ((t_int32)((1)) << 10) );
cx_kongzhiqiUNO_fanfuzhixing_flg_ = 2;
cx_kongzhiqiUNO_fanfuzhixing_2:;
if( cx_OS0_Delayer_TarRunning( 0 ) ) {
return;
}
cx_bujindianjiqudongqi_driver_run_left_t( ((96) * (4)) );
cx_OS0_CurrentTaskIndex = 0;
cx_yanshiqi_driver_DelaySecond( ((t_int32)((1)) << 10) );
cx_kongzhiqiUNO_fanfuzhixing_flg_ = 3;
cx_kongzhiqiUNO_fanfuzhixing_3:;
if( cx_OS0_Delayer_TarRunning( 0 ) ) {
return;
}
cx_bujindianjiqudongqi_driver_power_off();
cx_kongzhiqiUNO_driver_LightClose();
cx_OS0_CurrentTaskIndex = 0;
cx_yanshiqi_driver_DelaySecond( ((t_int32)((1)) << 10) );
cx_kongzhiqiUNO_fanfuzhixing_flg_ = 4;
cx_kongzhiqiUNO_fanfuzhixing_4:;
if( cx_OS0_Delayer_TarRunning( 0 ) ) {
return;
}
cx_kongzhiqiUNO_fanfuzhixing_flg_ = 0;
return;
cx_kongzhiqiUNO_fanfuzhixing_end:;
if( cx_kongzhiqiUNO_fanfuzhixing_flg_ == 1 ) {
goto cx_kongzhiqiUNO_fanfuzhixing_1;
}
if( cx_kongzhiqiUNO_fanfuzhixing_flg_ == 2 ) {
goto cx_kongzhiqiUNO_fanfuzhixing_2;
}
if( cx_kongzhiqiUNO_fanfuzhixing_flg_ == 3 ) {
goto cx_kongzhiqiUNO_fanfuzhixing_3;
}
if( cx_kongzhiqiUNO_fanfuzhixing_flg_ == 4 ) {
goto cx_kongzhiqiUNO_fanfuzhixing_4;
}
}
void cx_OS_VarInit(void)
{
cx_kongzhiqiUNO_fanfuzhixing_flg_ = 0;
cx_kongzhiqiUNO_fanfuzhixing_en_ = 1;
cx_SYS_Tick = 1;
cx_CInsIndex = 8192;
}
//=====================================================================
void setup(void) {
//这里添加用户的初始化, 如各个外设
//...
//初始化linkboy里的所有模块
cx_Setup();
//每经过1毫秒调用一次 lb_OS0_RunDriverList()
//必须要安装 mstimer2的库, 否则编译报错!!!!!
//如果需要用其他定时器驱动, 这里可以换成对应定时器的设置函数
MsTimer2::set( 1, cx_OS0_RunDriverList );
MsTimer2::start();
}
void loop(void) {
//这里应尽可能快的反复调用lb_loop
cx_Loop();
//这里添加用户自己的程序, linkboy里的程序和用户程序同时执行, 互不影响
//...
}
Linkboy仿真编程
![11.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/1ad4386788b60a8f1b76487d469cebfe.jpg)
实验记录视频
【花雕学编程】实验之二:控制步进电机正向与反向转动】 https://www.bilibili.com/video/BV1yo8gezEjg/?share_source=copy_web&vd_source=371a292a55e5ca9be994cbb4a86cc987
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机+A4988驱动器
项目实验之三:正反向控制步进电机快慢转动
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机+A4988驱动器
项目实验之三:正反向控制步进电机快慢转动
*/
//定义引脚连接和电机每转的步数
const int dirPin = 3;
const int stepPin = 2;
const int stepsPerRevolution = 200;
void setup() {
//将引脚声明为输出
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
//顺时针设置电机方向
digitalWrite(dirPin, HIGH);
//缓慢旋转电机
for (int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000); //等一下,延时1秒
//逆时针设置电机方向
digitalWrite(dirPin, LOW);
//快速旋转电机
for (int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // 等一下,延时1秒
}
实验模块接线示意图
![12.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/07e56c970de888a71aed10eae34b96a5.jpg)
代码说明
1、我们首先定义了连接到 A4988 引脚的 Arduino 引脚。然后,我们定义了内容stepsPerRevolution,它包含电机每转完成所需的整数步数。
2、在空置设置中,我们将电机的控制引脚声明为输出。
3、在空隙回路中,我们将 DIR 引脚设置为高电平以顺时针方向旋转电机(要逆时针旋转电机,可以将 DIR 设置为低电平)。由于我们发送到STEP引脚的脉冲频率决定了电机的速度,因此我们将该引脚设置为高,然后设置为低。当脉冲较高时,电机运行得更快。
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之四:步进电机的加速和减速控制
实验说明:步进电机的运动加速和减速,电机将以每秒 200 步的速度和每秒 30 步的加速度来回运行。
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之四:42步进电机的加速和减速控制
*/
//包含AccelStepper库
#include <AccelStepper.h>
//定义引脚连接
#define dirPin 2
#define stepPin 3
//定义电机接口类型
#define motorInterfaceType 1
//创建AccelStepper类的新实例
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup(){
//设置最大速度和加速度:
stepper.setMaxSpeed(200);
stepper.setAcceleration(30);
}
void loop() {
//设置目标位置
stepper.moveTo(600);
//以设定的速度和加速/减速运行到目标位置
stepper.runToPosition();
delay(1000);
// 移回零
stepper.moveTo(0);
stepper.runToPosition();
delay(1000);
}
实验场景图
![15.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/7d05567cafa85c11c0c9c58fd9992c62.jpg)
![16.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/e26196a65957b2e4e31e76c7a1e8d4ec.jpg)
实验记录视频
【花雕学编程】实验之四:42步进电机的加速和减速控制
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之五:使用10K电位器模块控制42步进电机的速度
10K电位器模块
![18.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/19a8ee840acff2a015654600c85d2dde.jpg)
步进电机也可以使用电位器进行控制。这里使用了一个 10K 电位器模块并将其连接到 Arduino Uno 板的 A0 模拟引脚。馈送到Arduino模拟引脚的电压可用作参考电压来控制步进电机的速度。连接示意图如下所示。
![17.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/297ebc47990fabdeda0245b1d816f028.jpg)
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之五:使用10K电位器模块控制42步进电机的速度
*/
//定义引脚编号
const int stepPin = 2;
const int dirPin = 3;
int customDelay, customDelayMapped; //定义变量
void setup() {
//将两个引脚设置为输出
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
//使电机能够沿特定方向移动
digitalWrite(dirPin, HIGH);
}
void loop() {
//从自定义加速函数获取自定义延迟值
customDelayMapped = speedUp();
//根据电位器数值的不同,可以定制延迟脉冲,电机的速度取决于电位器
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
//读取电位计的功能
int speedUp() {
//通过A0读取电位计
int customDelay = analogRead(A0);
//将电位计的读取值从0到1023转换为所需的延迟值(300到4000)
int newCustom = map(customDelay, 0, 1023, 300, 4000);
return newCustom;
}
实验记录视频
【花雕学编程】实验之五:使用10K电位器模块控制42步进电机的速度
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之六:步进电机循环转动200步暂停一秒
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之六:步进电机循环转动200步暂停一秒
*/
int x;
void setup() {
//定义引脚,设为输出
Serial.begin(9600);
Serial.println("步进电机准备就绪");
// Enable
pinMode(6, OUTPUT);
// Step
pinMode(5, OUTPUT);
// Dir
pinMode(4, OUTPUT);
digitalWrite(6, LOW);
}
void loop() {
// Set Enable low
digitalWrite(6, LOW);
// Set Dir high
digitalWrite(4, HIGH);
Serial.println("循环200步(1转)");
for (x = 0; x < 200; x++) // //循环200次
{
digitalWrite(5, HIGH); // 输出高
delay(10); // Wait
digitalWrite(5, LOW); // 输出低
delay(100); // Wait
}
Serial.println("暂停");
delay(1000); //暂停一秒钟
}
实验串口返回情况
![19.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/781313ae9559276a1db0eec21029a589.jpg)
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之七:步进电机速度、位置和加速度的随机变化
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之七:步进电机速度、位置和加速度的随机变化
*/
//包含AccelStepper库
#include <AccelStepper.h>
AccelStepper stepper(1, 5, 4);
//为双线板初始化加速踏板
void setup() {
Serial.begin(9600);
pinMode(6, OUTPUT); // 输出
digitalWrite(6, LOW); // 设置为低
}
void loop() {
digitalWrite(6, LOW); // 设置为低
if (stepper.distanceToGo() == 0)
{
//速度、位置和加速度的随机变化
//确保我们没有零速度或加速度
delay(1000);
stepper.moveTo(rand() % 400);
stepper.setMaxSpeed((rand() % 400) + 200);
stepper.setAcceleration((rand() % 200) + 100);
}
Serial.println(stepper.distanceToGo());
stepper.run(); //实际上使步进器移动
}
实验串口返回情况
![20.jpg](https://makelogimg.dfrobot.com.cn/makelog/5ab819e19e087152393ebf1f/1fc82c5c5263b71dcb7cde7cfbf8d9d1.jpg)
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之八:驱动步进电机顺时针5圈和逆时针5圈
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之八:驱动步进电机顺时针5圈和逆时针5圈
*/
//定义步进电机连接引脚和每转一圈的步数
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
void setup() {
//将引脚声明为输出
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
//顺时针设置旋转方向
digitalWrite(dirPin, HIGH);
//缓慢旋转步进电机1圈
for (int i = 0; i < stepsPerRevolution; i++) {
//这四行结果为1个步骤
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000);
//逆时针设置旋转方向
digitalWrite(dirPin, LOW);
//快速旋转步进电机1圈
for (int i = 0; i < stepsPerRevolution; i++) {
//这四行结果为1个步骤
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000);
//顺时针设置旋转方向
digitalWrite(dirPin, HIGH);
//快速旋转步进电机5圈
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
//这四行结果为1个步骤
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
//逆时针设置旋转方向
digitalWrite(dirPin, LOW);
//快速旋转步进电机5圈
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
//这四行结果为1个步骤
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
}
实验记录视频
【花雕学编程】项目实验之八:驱动步进电机顺时针5圈和逆时针5圈
https://www.bilibili.com/video/BV1dD8XeCEjg/?
share_source=copy_web&vd_source=371a292a55e5ca9be994cbb4a86cc987
代码的工作原理:
从定义步进和方向引脚开始,这里将它们连接到Arduino引脚3和2。
该语句用于为常量值命名。编译程序时,编译器会将对此常量的任何引用替换为定义的值。所以在你提到的任何地方,编译器都会在编译程序时将其替换为值 2。
#definedirPin
这里定义了一个常量。驱动器设置为全步模式,所以将其设置为每转 200 步。如果设置不同,请更改此值 stepsPerRevolution。
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
在代码部分中,所有电机控制引脚都声明为数字输出,其功能为 setup()pinMode() 。
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
在代码部分,让电机在 CW 方向上缓慢旋转一圈,在 CCW 方向上快速旋转一圈。接下来,我们让电机高速向每个方向旋转 5 圈。那么如何控制速度、旋转方向和转数呢?
loop()
digitalWrite(dirPin, HIGH);
for(int i = 0; i < stepsPerRevolution; i++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
控制旋转方向:
为了控制步进电机的旋转方向,我们将 DIR(方向)引脚设置为高电平或低电平。为此,我们使用函数,根据步进电机的连接方式,将 DIR 引脚设置为高电平将使电机转动 CW 或 CCW。
digitalWrite()
控制步数或转数:
在此示例中,for 环路控制步进电机将采取的步数。for 循环中的代码导致步进电机的 1 步进。由于循环中的代码执行了 200 次 (stepsPerRevolution),因此会导致 1 次旋转。在最后两个循环中,for 循环中的代码执行 1000 次,这导致 1000 步或 5 转。
请注意,可以将 for 循环中的第二个项更改为所需的任何步骤数。 将导致 100 步,或半圈。
for(int i = 0; i < 100; i++)
控制速度:
步进电机的速度由发送到STEP引脚的脉冲频率决定。频率越高,电机运行速度越快。可以通过更改代码来控制脉冲的频率。延迟越短,频率越高,电机运行速度越快。
delayMicroseconds()
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之九:使用AccelStepper库控制步进电机的步数或转数
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十三:42步进电机二相四线+A4988驱动模块+设置扩展板
项目实验之九:使用AccelStepper库控制步进电机的步数或转数
*/
//包括AccelStepper库
#include "AccelStepper.h"
//定义步进电机连接和电机接口类型。
//使用驱动器时,电机接口类型必须设置为1
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
//创建AccelStepper类的新实例
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
//以每秒步数设置最大速度
stepper.setMaxSpeed(1000);
}
void loop() {
//将当前位置设置为0
stepper.setCurrentPosition(0);
//以200步/秒的速度向前运行电机,直到电机达到400步(2转)
while (stepper.currentPosition() != 400)
{
stepper.setSpeed(200);
stepper.runSpeed();
}
delay(1000);
//将位置重置为0
stepper.setCurrentPosition(0);
//以600步/秒的速度反向运行电机,直到电机达到-200步(1转)
while (stepper.currentPosition() != -200)
{
stepper.setSpeed(-600);
stepper.runSpeed();
}
delay(1000);
//将位置重置为0
stepper.setCurrentPosition(0);
//以400步/秒的速度向前运行电机,直到电机达到600步(3转)
while (stepper.currentPosition() != 600)
{
stepper.setSpeed(400);
stepper.runSpeed();
}
delay(1500);
}
代码说明:
到 loop() 部分的代码的第一部分与前面的案例完全相同。
在循环中,将 while 循环与函数结合使用。首先,将步进电机的当前位置设置为零 currentPosition()stepper.setCurrentPosition(0)。
//将位置重置为0
stepper.setCurrentPosition(0);
接下来,利用 while 循环。while 循环将连续且无限循环,直到括号内的表达式 () 变为 false。所以,在这种情况下,检查步进电机的当前位置是否不等于 400 步(!= means: 不等于)。虽然情况并非如此,但以设定的恒定速度运行步进电机 setSpeed()。
//以200步/秒的速度向前运行电机,直到电机达到400步(2转)
while(stepper.currentPosition() != 400)
{
stepper.setSpeed(200);
stepper.runSpeed();
在循环的其余部分,做完全相同的事情,只是速度和目标位置不同。
评论