1 Experimental Phenomenon
The running direction and speed of the 28BYJ48 stepper motor are controlled by the ULN2003 driver module. When the KEY1 key is pressed, the motor rotation direction can be adjusted; when the KEY2 key is pressed, the motor accelerates (5 gears); when the KEY3 key is pressed, the motor decelerates (5 gears); at the same time, the motor speed gear is displayed on the digital tube.
2 Experimental Principle
(1) A stepper motor must be driven to operate. The drive signal must be a pulse signal. When there is no pulse, the stepper motor is disabled. When an appropriate pulse signal is added, it will rotate at a certain angle (called the step angle). The speed of rotation is proportional to the frequency of the pulse.
(2) Stepper motors have the superior characteristics of instant starting and rapid stopping;
(3) Changing the order of pulses can easily change the direction of rotation.
3 System Block Diagram
4 Hardware Design
See "Puzhong-2 & Puzhong-3 & Puzhong-4 Development Board Schematic Diagram": independent key module, single-chip microcomputer core, dynamic digital tube module (including 74HC138), five-wire four-phase stepper motor module.
5 Software Design
5.1 Main function
#include
#include "key.h"
#include "delayms.h"
#include "Nixie.h"
#include "stepmotor.h"
// Define the stepper motor speed, the smaller the value, the faster the speed
#define MAX 1 //delay time, pulse period
#define MIN 5 //delay time, pulse period
unsigned char key_num=0;
unsigned char dir=0; //Default counterclockwise direction
unsigned char step=0;
unsigned char speed=5;
void main()
{
while(1)
{
key_num=key();
if(key_num==1) //Reverse
{
dir=!dir;
}
else if(key_num==2) //Speed up, the smaller the delay speed, the faster the speed
{
if(speed>MAX) speed-=1;
}
else if(key_num==3) //Deceleration, the greater the delay speed, the slower the speed
{
if(speed
}
Nixie(1,6-speed); //The larger the displayed value, the faster the speed
stepmotor_send_pulse(step++,dir);
if(step>8) step=0;
delayms(speed);
}
}
5.2 Stepper Motor Control Function
#include
//Define ULN2003 control stepper motor pin
sbit IN_D=P1^0;
sbit IN_C=P1^1;
sbit IN_B=P1^2;
sbit IN_A=P1^3;
/**
* @brief Output a data to ULN2003 to send a pulse to the stepper motor
* @param step specifies the step number, the optional value is 0~7
* @param dir: direction selection, 1: clockwise, 0: counterclockwise
* @retval None
*/
void stepmotor_send_pulse(unsigned char step,dir)
{
unsigned char temp;
if(dir==0) temp=7-step; //If it rotates counterclockwise, change the beat signal
else temp=step;
switch(temp) //8 beats control: A->AB->B->BC->C->CD->D->DA
{
case 0: IN_A=0;IN_B=0;IN_C=0;IN_D=1;break;
case 1: IN_A=0;IN_B=0;IN_C=1;IN_D=1;break;
case 2: IN_A=0;IN_B=0;IN_C=1;IN_D=0;break;
case 3: IN_A=0;IN_B=1;IN_C=1;IN_D=0;break;
case 4: IN_A=0;IN_B=1;IN_C=0;IN_D=0;break;
case 5: IN_A=1;IN_B=1;IN_C=0;IN_D=0;break;
case 6: IN_A=1;IN_B=0;IN_C=0;IN_D=0;break;
case 7: IN_A=1;IN_B=0;IN_C=0;IN_D=1;break;
default:IN_A=0;IN_B=0;IN_C=0;IN_D=0;break;//Stop phase sequence
}
}
#ifndef _stepmotor_h_
#define _stepmotor_h_
void stepmotor_send_pulse(unsigned char step,dir);
#endif
5.3 Key Scan Function
#include
#include "delayms.h"
sbit key1 = P3^1;
sbit key2 = P3^0;
sbit key3 = P3^2;
sbit key4 = P3^3;
/**
* @brief Get the key code of an independent key
* @param None
* @retval The key code of the pressed key, range: 0~4, the return value is 0 when no key is pressed
*/
unsigned char key()
{
unsigned char KeyNumber = 0;
if(key1==0){delayms(20);while(key1==0);delayms(20);KeyNumber=1;}
if(key2==0){delayms(20);while(key2==0);delayms(20);KeyNumber=2;}
if(key3==0){delayms(20);while(key3==0);delayms(20);KeyNumber=3;}
if(key4==0){delayms(20);while(key4==0);delayms(20);KeyNumber=4;}
return KeyNumber;
}
#ifndef _key_h_
#define _key_h_
unsigned char key();
#endif
5.4 Digital tube driver function
#include
#include "delayms.h"
sbit HC138_A=P2^2;
sbit HC138_B=P2^3;
sbit HC138_C=P2^4;
//Digital tube segment code table, 0-9
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
/**
* @brief Digital tube display
* @param Location The location to be displayed, range: 1~8
* @param Number The number to be displayed, range: segment table index range
* @retval None
*/
void Nixie(unsigned char Location,Number)
{
switch(Location) //Bitcode output
{
case 1:HC138_C=1;HC138_B=1;HC138_A=1;break;
case 2:HC138_C=1;HC138_B=1;HC138_A=0;break;
case 3:HC138_C=1;HC138_B=0;HC138_A=1;break;
case 4:HC138_C=1;HC138_B=0;HC138_A=0;break;
case 5:HC138_C=0;HC138_B=1;HC138_A=1;break;
case 6:HC138_C=0;HC138_B=1;HC138_A=0;break;
case 7:HC138_C=0;HC138_B=0;HC138_A=1;break;
case 8:HC138_C=0;HC138_B=0;HC138_A=0;break;
}
P0=NixieTable[Number]; //Segment code output
delayms(1); //display for a period of time
P0=0x00; //Segment code cleared to 0, clearing the image
}
#ifndef __NIXIE_H__
#define __NIXIE_H__
void Nixie(unsigned char Location,Number);
#endif
5.5 Delay Function
#include
void delayms(unsigned int xms) //@11.0592MHz
{
unsigned char i, j;
while(xms--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
#ifndef _delayms_h_
#define _delayms_h_
delayms(unsigned int xms);
#endif
Previous article:Infrared remote control receiver module + Puzhong 51 single chip microcomputer + Jiangke University Automation Association
Next article:DC motor control (PWM) + Puzhong 51 single-chip microcomputer + Jiangke University Automation
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Industry first! Xiaopeng announces P7 car chip crowdfunding is completed: upgraded to Snapdragon 8295, fluency doubled
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Why does the newly soldered STM32 main control board run about 10 times slower than normal after programming?
- [Discussion] There is no place to make money during the seven days of National Day holiday
- Linux-3.14.52 Compiler Reference Manual v2.0
- ESP32 firmware is divided into two versions: ESP-IDF v3.x/4.x
- Two methods of programming TMS320C6748
- Enabling higher-performance front-end radar to make Vision Zero a reality
- Power supply control principle of three-phase fuel pump used in Mercedes-Benz passenger cars
- Review summary: Free review of Fudan Micro FM33LG0 series, Winsilver chip
- ESP32-S2-Saola-1 calculates pi
- DCDC H-bridge circuit, what is the principle of boost and buck?