MCU serial communication control stepper motor

Publisher:RainbowGardenLatest update time:2015-04-29 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This is a source program from a microcontroller development board. The schematic diagram can be downloaded from:  http://www.51hei.com/f/ks51.pdf

Below is the source code:
/**
  *********************************************************************************************
  * @file main.c
  * @author xr
  * @date March 18, 2014 20:00:03
  * @note Parameters of the stepper motor: Reduction ratio: 1:64 Step angle: 5.625/64 Starting frequency: >=550 Minimum starting time: 1.08ms
  * @brief Serial communication control of stepper motor Microcontroller STC89C52RC MCU Crystal oscillator 11.0592MHZ
  *************************************************************************************************
  */
  
#include

typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long ulong;

//Stepper motor eight-beat mode phase value encoding A-AB-B-BC-C-CD-D-DA
uchar code table[] = {
0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6
};
uint tmp = 0;
bit dir = 0;
ulong beats = 0; //Number of beats
uchar angle = 0; //Number of turns

void timer0_Config();
void motor_Config(ulong angle);
void Uart_Config(uint baud);

void main()
{
timer0_Config();//The startup time is set to 2ms
Uart_Config(9600);
while (1)
{
switch (angle)
{
case 2:
{
motor_Config(2*360);
angle = 0;
break;
}
case 3:
{
motor_Config(3*360);
angle = 0;
break;
}
case 4:
{
motor_Config(4*360);
angle = 0;
break;
}
case 5:
{
motor_Config(5*360);
angle = 0 ;
break;
}
case 6:
{
motor_Config(6*360);
angle = 0;
break;
}
case 7:
{
motor_Config(7*360);
angle = 0;
break;
}
case 8:
{
motor_Config(8*360) ;
angle = 0;
break;
}
case 9:
{
P1 |= 0x0F;
beats = 0;
angle = 0;
break;
}
default:
break;
}
}
}[page]

/**
  * @brief Stepper motor configuration (determine the beats of the rotor according to the number of turns of the output shaft)
  * @param None
  * @retval None
  */
void motor_Config(ulong angle)
{
//Disable interrupts before calculation
EA = 0;
beats = (angle*64*8*8)/360;
EA = 1;
}

/**
  * @brief Timer T0 timing configuration
  * @param uint xms
  * @retval None
  */
void timer0_Config()
{
TMOD &= 0xF0; // Clear T0 control bit
TMOD |= 0x01; // T1 mode 1
TH0 = 0xF8;
TL0 = 0xCC; // 2ms
TR0 = 1;
ET0 = 1;
EA = 1;
}

/**
  * @brief Serial communication configuration
  * @param None
  * @retval None
  */
void Uart_Config(uint baud)
{
SCON |= 0x50; //SM0-SM2 REN TB8 RB8 TI RI
TMOD &= 0x0F; //Clear T1 control bit
TMOD |= 0x20; //Set timer T1 to eight-bit auto-reload mode, count and set serial port baud rate
TH1 = 256-(11059200/12/32/baud); //Baud rate = 2^SMOD/32 * T1(overflow rate) T1(overflow rate) = 1/[(256-X)*(12/11059200)]
TL1 = TH1;
TR1 = 1;
ET1 = 0; //Disable T1 timer interrupt
ES = 1; //Enable serial port interrupt
EA = 1; //Enable total interrupt
}

/**
  * @brief T0 interrupt service subroutine
  * @param None
  * @retval None
  */
void timer0_Int() interrupt 1 using 3
{
static uchar step = 0;
uchar buf = 0;//Store phase value
TH0 = 0xF8;
TL0 = 0xCC;
if (beats)
{
if (!dir)
{
buf = (P1 & 0xF0);//buf temporarily stores the high byte of P1, clears the low byte
buf |= table[step++];//Store the phase value in buf
step &= 0x07;//And the method to achieve 8 zeroing
P1 = buf;
beats--;
}
else
{
buf = (P1 & 0xF0);
buf |= table[step--];
if (step <= 0)
{
step = 7;
}
P1 = buf;
beats--;
}
}
else
{
P1 |= 0x0F; // Turn off all phases, that is, the stepper motor stops rotating
}
}

/**
  * @brief Serial port interrupt service subroutine
  * @param None
  * @retval None
  */
void Uart_Int() interrupt 4 using 0
{
if (RI)
{
RI = 0;
if (SBUF > 1)
{
angle = SBUF;
}
else
{
dir = SBUF;
}
}
}

Keywords:MCU Reference address:MCU serial communication control stepper motor

Previous article:Three ways to implement dynamic scanning of digital tubes
Next article:MCU matrix key timer debounce program source code

Recommended ReadingLatest update time:2024-11-16 14:35

The principle and application of 74HC138 38 decoder
When we design a single-chip microcomputer circuit, the number of IO ports of the single-chip microcomputer is limited, and sometimes it cannot meet our design requirements. For example, our STC89C52 has a total of 32 IO ports, but in order to control more devices, we have to use some peripheral digital chips. This ki
[Microcontroller]
The principle and application of 74HC138 38 decoder
Design of solar photovoltaic DC controller using AVR microcontroller control
Energy shortage and environmental pollution are becoming increasingly serious in the world today, forcing people to find and use new alternative energy sources. With the improvement of electronic technology and solar panel production technology, the use of solar energy is becoming more and more common. Solar energy ha
[Microcontroller]
Design of solar photovoltaic DC controller using AVR microcontroller control
51 microcontroller timer controls LED lights
The exam was over and I was bored. I happened to have a 51 study board on hand to tinker with. The control tasks are as follows: P1.0 controls an LED light, on for 0.5s and off for 0.5s. Design idea: Here we only use timers , not soft delays. The 51 timer can time up to 60ms, so we set the timer to interrupt every 5
[Microcontroller]
51 microcontroller timer controls LED lights
A matrix keyboard program under the stc89c52 microcontroller
    The following is a matrix keyboard program for the stc89c52 microcontroller. The keyboard is connected to port P0 and displayed on port P2.      #include reg52.h  #define uchar unsigned char  #define uint unsigned int  sbit key1=P3^2;  sbit key2=P3^3; uchar code tab ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
[Microcontroller]
Design and Implementation of 16-bit Microcontroller
With the development of information technology, the popularization of network communication, information security and information home appliances, embedded MCU is an indispensable component in all these information products. At present, some domestic scientific research institutions and semiconductor companies are c
[Embedded]
51 MCU ds1302 driver
////////////////////////////////////////////////////////////head File///////////////////////////////////////////////// /****************************************************** **************************** * Title: ds1302 clock chip driver * * Files: ds1302.h ds1302.c * * Date: 2010-3-12 *    * Version: 1.12 (te
[Microcontroller]
Design of clothing production station machine based on LPC11C14 microcontroller that can read RFID tags
    The clothing manufacturing industry is a typical labor-intensive industry, and information-based production management is usually lacking, especially workshop management. Modern enterprises expect to use high-tech to enhance the competitiveness of products and reduce costs, and maximize production capacity by effe
[Microcontroller]
Design of clothing production station machine based on LPC11C14 microcontroller that can read RFID tags
51 MCU Tutorial from Scratch - Timer/Counter Control Word
From the previous section, we have learned that the timer/counter in the microcontroller can have multiple uses, so how can I make them work for the purpose I need? This requires setting the timer/counter control word. The timer/counter has four working modes There are two special function register
[Microcontroller]
51 MCU Tutorial from Scratch - Timer/Counter Control Word
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号