Stepper motor control + Puzhong 51 single chip microcomputer + Puzhong official

Publisher:Heavenly999Latest update time:2024-08-08 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Reference address:Stepper motor control + Puzhong 51 single chip microcomputer + Puzhong official

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号