完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
前言
注意:浇水由LED1的亮灭进行模拟 源码参考: 步进电机28BYJ-48的驱动程序(stm32f103c8t6) 正点原子按键实验 开发板:正点原子 STM32F103 精英版 语言:C语言 开发环境:Keil5 开发板使用了 LED KEY 步进电机28BYJ-48 ULN2003驱动 代码下载: 码云 GitHub 功能介绍: LED0约1秒1反转。 按KEY0,翻转LED1,电机反转1圈 按KEY1,翻转LED1,电机正转1圈 按KEY_UP,翻转LED1,电机正转半圈 拓展应用 STM32F103+步进电机28BYJ-48 简单应用之摇头、转圈、自定义模式demo STM32F103+DHT11模块+步进电机28BYJ-48 简单实现 智能浇水系统demo 参考资料 接线 + —> 5V- —> GNDIN1 —> PF1IN2 —> PF2IN3 —> PF3IN4 —> PF4 效果图 按下KEY0,LED1翻转,电机反转一圈。风车折法参考 传送门 #include "led.h" #include "delay.h" #include "key.h" #include "sys.h" #include "beep.h" #include "step.h" //IN4: PB6 d //IN3: PB5 c //IN2: PB4 b //IN1: PB3 a int main(void) { vu8 key = 0; uint8_t time = 0; delay_init(); //延时函数初始化 LED_Init(); //初始化与LED连接的硬件接口 BEEP_Init(); //初始化蜂鸣器端口 KEY_Init(); //初始化与按键连接的硬件接口 Step_Motor_GPIO_Init(); LED0 = 0; BEEP = 0; while (1) { key = KEY_Scan(0); //得到键值 if (key) { switch (key) { case WKUP_PRES: // 翻转LED1,电机正转半圈 LED1 = !LED1; /* 功能:转1/64圈 步距角5.625 360/5.625=64 减速比1/64 故64*64个脉冲转一圈 n 圈数 direction 方向 1正转 非1反转 delay delay时长ms >= 2 */ motor_circle(32, 1, 2); break; case KEY1_PRES: // 翻转LED1,电机正转1圈 LED1 = !LED1; motor_circle(64, 1, 2); break; case KEY0_PRES: // 翻转LED1,电机反转1圈 LED1 = !LED1; motor_circle(64, 0, 2); break; } } time++; if(time % 100 == 0) { LED0 = !LED0; } delay_ms(10); } } step.h #ifndef __STEP_H__ #define __STEP_H__ #include "stm32f10x.h" void Step_Motor_GPIO_Init(void); /* 功能:转1/64圈 步距角5.625 360/5.625=64 减速比1/64 故64*64个脉冲转一圈 n 圈数 direction 方向 1正转 非1反转 delay delay时长 >= 2 */ void motor_circle(int n, int direction, int delay); #endif step.c #include "sys.h" #include "delay.h" #include "step.h" //IN4: PF4 d //IN3: PF3 c //IN2: PF2 b //IN1: PF1 a u8 forward[4] = {0x03,0x06,0x0c,0x09}; // 正转 u8 reverse[4]= {0x03,0x09,0x0c,0x06}; // 反转 //引脚初始化 void Step_Motor_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1; GPIO_Init(GPIOF, &GPIO_InitStructure); } //根据输入数据改变引脚电平 void SetMotor(unsigned char InputData) { if(InputData == 0x03) { GPIO_SetBits(GPIOF,GPIO_Pin_1); GPIO_SetBits(GPIOF,GPIO_Pin_2); GPIO_ResetBits(GPIOF,GPIO_Pin_3); GPIO_ResetBits(GPIOF,GPIO_Pin_4); } else if(InputData == 0x06) { GPIO_ResetBits(GPIOF,GPIO_Pin_1); GPIO_SetBits(GPIOF,GPIO_Pin_2); GPIO_SetBits(GPIOF,GPIO_Pin_3); GPIO_ResetBits(GPIOF,GPIO_Pin_4); } else if(InputData == 0x09) { GPIO_SetBits(GPIOF,GPIO_Pin_1); GPIO_ResetBits(GPIOF,GPIO_Pin_2); GPIO_ResetBits(GPIOF,GPIO_Pin_3); GPIO_SetBits(GPIOF,GPIO_Pin_4); } else if(InputData == 0x0c) { GPIO_ResetBits(GPIOF,GPIO_Pin_1); GPIO_ResetBits(GPIOF,GPIO_Pin_2); GPIO_SetBits(GPIOF,GPIO_Pin_3); GPIO_SetBits(GPIOF,GPIO_Pin_4); } else if(InputData == 0x00) { GPIO_ResetBits(GPIOF,GPIO_Pin_1); GPIO_ResetBits(GPIOF,GPIO_Pin_2); GPIO_ResetBits(GPIOF,GPIO_Pin_3); GPIO_ResetBits(GPIOF,GPIO_Pin_4); } } /* 功能:转1/64圈 步距角5.625 360/5.625=64 减速比1/64 故64*64个脉冲转一圈 n 圈数 direction 方向 1正转 非1反转 delay delay时长ms >= 2 */ void motor_circle(int n, int direction, int delay) { int i, j; for(i = 0; i < n * 8; i++) { for(j = 0; j < 4; j++) { if(1 == direction) { SetMotor(0x00); SetMotor(forward[j]); } else { SetMotor(0x00); SetMotor(reverse[j]); } delay_ms(delay > 2 ? delay : 2); } } } |
|
|
|
只有小组成员才能发言,加入小组>>
2163 浏览 0 评论
7829 浏览 4 评论
35665 浏览 18 评论
4770 浏览 0 评论
23230 浏览 34 评论
900浏览 2评论
1155浏览 1评论
1370浏览 1评论
1003浏览 0评论
1444浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-15 02:14 , Processed in 1.360004 second(s), Total 76, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号