Schematic diagram of L297, L298 driving stepper motor, tested and available
The 51 single-chip microcomputer program for L297 and L298 to drive the stepper motor is as follows:
#include "AT89X51.h"
int delay();
void inti_lcd();
void show_lcd(int);
void cmd_wr();
void ShowState();
void clock(unsigned int Delay) ;
void DoSpeed(); //Calculate speed
//Positive rotation value
#define RIGHT_RUN 1
//Reverse value
#define LEFT_RUN 0
sbit RS=0xA0;
sbit RW=0xA1;
sbit E=0xA2;
char SpeedChar[]="SPEED(n/min):";
char StateChar[]="RUN STATE:";
char STATE_CW[]="CW";
char STATE_CCW[]="CCW";
char SPEED[3]="050";
unsigned int RunSpeed=50; //speed
unsigned char RunState=RIGHT_RUN; //Running status
main()
{
/*Timer settings*/
TMOD=0x66; //Timer 0, 1 are both in counting mode; mode 2;
EA=1; //Enable interrupt
TH0=0xff; //Timer 0 initial value FFH;
TL0=0xff;
ET0=1;
TR0=1;
TH1=0xff; //Timer 1 initial value FFH;
TL1=0xff;
ET1=1;
TR1=1;
IT0=1; //Pulse mode
EX0=1; //Open external interrupt 0: speed up
IT1=1; //Pulse mode
EX1=1; //Open external interrupt 1: deceleration
inti_lcd();
DoSpeed();
ShowState();
while(1)
{
clock(RunSpeed);
P0_1=P0_1^0x01;
}
}
//Timer 0 interrupt program: forward
void t_0(void) interrupt 1
{
RunState=RIGHT_RUN;
P0_0=1;
P1=0x01;
cmd_wr();
ShowState();
}
//Timer 1 interrupt: reverse
void t_1(void) interrupt 3
{
RunState=LEFT_RUN;
P0_0=0;
P1=0x01;
cmd_wr();
ShowState();
}
//Interrupt 0: speed up the program
void SpeedUp() interrupt 0
{
if(RunSpeed>=12)
RunSpeed=RunSpeed-2;
DoSpeed();
P1=0x01;
cmd_wr();
ShowState();
}
//Interrupt 1: deceleration program
void SpeedDowm() interrupt 2
{
if(RunSpeed<=100)
RunSpeed=RunSpeed+2;
DoSpeed();
P1=0x01;
cmd_wr();
ShowState();
}
int delay() //Judge whether LCD is busy
{
int a;
start:
RS=0;
RW=1;
E=0;
for(a=0;a<2;a++);
E=1;
P1=0xff;
if(P1_7==0)
return 0;
else
goto start;
}
void inti_lcd() //Set LCD mode
{
P1=0x38;
cmd_wr();
delay();
P1=0x01; //Clear
cmd_wr();
delay();
P1=0x0f;
cmd_wr();
delay();
P1=0x06;
cmd_wr();
delay();
P1=0x0c;
cmd_wr();
delay();
}
void cmd_wr() //write control word
{
RS=0;
RW=0;
E=0;
E=1;
}
void show_lcd(int i) //LCD display subroutine
{
P1=i;
RS=1;
RW=0;
E=0;
E=1;
}
void ShowState() //Display status and speed
{
int i=0;
while(SpeedChar[i]!='')
{
delay();
show_lcd(SpeedChar[i]);
i++;
}
delay();
P1=0x80 | 0x0d;
cmd_wr();
i=0;
while(SPEED[i]!='')
{
delay();
show_lcd(SPEED[i]);
i++;
}
delay();
P1=0xC0;
cmd_wr();
i=0;
while(StateChar[i]!='')
{
delay();
show_lcd(StateChar[i]);
i++;
}
delay();
P1=0xC0 | 0x0A;
cmd_wr();
i=0;
if(RunState==RIGHT_RUN)
while(STATE_CW[i]!='')
{
delay();
show_lcd(STATE_CW[i]);
i++;
}
else
while(STATE_CCW[i]!='')
{
delay();
show_lcd(STATE_CCW[i]);
i++;
}
}
void clock(unsigned int Delay) //1ms delay program
{ unsigned int i;
for(;Delay>0;Delay--)
for(i=0;i<124;i++);
}
void DoSpeed()
{
SPEED[0]=(1000*6/RunSpeed/100)+48;
SPEED[1]=1000*6/RunSpeed%100/10+48;
SPEED[2]=1000*6/RunSpeed%10+48;
}
Previous article:Based on the internal EEPROM read and write interface function of STC15W408AS microcontroller
Next article:51 MCU driver for proximity switch
- Popular Resources
- Popular amplifiers
- Communication Electronic Circuits (Third Edition) (Yan Guoping)
- The Nature of Code: Using Programming to Simulate Natural Systems
- Linux open source storage full stack detailed explanation from Ceph to container storage
- SLAM technology in autonomous driving and robotics: from theory to practice (Gao Xiang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- DC-DC chip LM2596SX-5.0/NOPB
- How does the lwip client let the server know that it has been shut down?
- Small car front-end 16W power supply design
- Bear Pie Huawei IoT operating system LiteOS bare metal driver transplantation 05-E53_SF1 expansion board driver and use
- TI's TPS61200 boost chip can only boost voltage but not reduce voltage during actual testing. Please help.
- Learn to make a flyback switching power supply-2
- Embedded Learning丨4412 Development Board-uboot Source Code-Assembly-Source Code Analysis (I)
- Using the I2C Bus
- 【Qinheng RISC-V core CH582】Learning material collection
- CCS cannot connect to F280049C Launchpad