当前位置:网站首页>STM32 single chip microcomputer drive L298N
STM32 single chip microcomputer drive L298N
2022-07-28 22:40:00 【The poorest are beggars and immortals will always come out】
L298N The working principle and circuit wiring of the are explained in the blog in the previous chapter , The differences between the three wiring methods are explained , And suggestions .https://blog.csdn.net/m0_59113542/article/details/121038048?spm=1001.2014.3001.5501
This article mainly explains STM32 Driver program

One 、L298N Logical control of

as everyone knows ,L298N Two motors can be driven , They are channels A、 passageway B.
ENA It's a channel A The enabling end of , That is, the channel A Enabled jumper cap ( The jumper cap is inserted to enable , Don't worry about it );
IN1、IN2: Logic input pin ( The four row pins on the board are , The two on the left are channels A Of IN1、IN2; The one on the right is the channel B Of , When you use that channel, you use the corresponding two pins to control ), By controlling the high and low levels of these two pins according to the table, the forward and reverse rotation of the motor can be controlled ;
Two 、 matters needing attention
L298N You can use the three ways introduced in my article , But it's GND It must be connected with the SCM GND Connected to a , Ensure common land , Thus there is a reference level .
3、 ... and 、STM32 The driver
Use PWM The duty cycle controls its speed ,PA4→IN1、PA5→IN2 The logic level of controls its forward and reverse .
main.c file
#include "delay.h"
#include "usart.h"
#include "timer.h"
int main(void)
{
delay_init(); // Delay function initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Interrupt priority grouping settings
uart_init(115200); // Serial port initialization settings
TIM3_PWM_Init(450,7199); //PWM Output initialization
while(1)
{
// Set up channels 2 The duty cycle of PWM adjust speed , Here is 100, stay 0~450 between , The smaller the speed, the faster
TIM_SetCompare2(TIM3,100);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
delay_ms(3000);
TIM_SetCompare2(TIM3,100);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
delay_ms(3000);
}
}
time.c file
#include "timer.h"
#include "led.h"
#include "usart.h"
//TIM3 PWM Partial initialization
//PWM Output initialization
//arr: Automatic reload load value
//psc Clock prescaler factor
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable timer 3 The clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Can make GPIO Peripheral clock
// initialization IOPA4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
// initialization IOPA5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
// initialization IOPA7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
// initialization TIM3
TIM_TimeBaseStructure.TIM_Period = arr; // Automatic reload load value
TIM_TimeBaseStructure.TIM_Prescaler =psc; // The prescaled coefficients
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // Set the clock split
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // Upcount mode
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
// initialization TIM3 Channel2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // choice PWM Pattern 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Compare output enable
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; // Output polarity
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); // Enable timer 3 passageway 2 Preload registers
TIM_Cmd(TIM3, ENABLE); // Can make TIM3
}
time.h file
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM3_PWM_Init(u16 arr,u16 psc);
#endif
The above is a simple code implementation , You can adjust and modify according to your actual situation .
Welcome to correct and exchange , You can discuss the code together when you are free .
It's not easy to make , Thank you for your support , thank !!!!!!
-------------- A person who is working hard
边栏推荐
- JS convert numbers to letters
- Command line agent: proxychains configuration
- What are the main functions and uses of LCR tester
- Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
- 776. String shift inclusion problem
- JMeter installs third-party plug-ins plugins Manager
- Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
- 职场pua但有道理
- ATT&CK初步了解
- Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
猜你喜欢
![Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]](/img/87/d943cc1e8169bb670414fbf7a322c5.jpg)
Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]

What are the main functions and uses of LCR tester

PC side web page special effects (offset series, obtain the coordinates of the mouse in the box, pop-up drag effect, magnifying glass effect)

Use PCL to batch convert point cloud.Bin files to.Pcd

ATT&CK初步了解

Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)

842. Arrange numbers

Concise history of graphic technology

105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)

(翻译)图技术简明历史
随机推荐
Att & CK Threat Intelligence
c语言实现字符串逆序排列
Sword finger offer II 066. sum of words (medium prefix tree design string)
Win11 how to open software notification
Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
Chrome encountered a problem when debugging the code. After modifying and saving the code in vscode, chrome did not update after refreshing
Binary source code, inverse code, complement code
Leetcode integer exercises integer inversion
There will be a black line on the border when the button in the wechat applet is clicked
Quadruped robot | gem (elevation map) + fast_ Deployment records of Leo (odometry) environment
Static details of static members
Command line agent: proxychains configuration
76. Minimum coverage substring (hard sliding window hash table string)
PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
Awk blank line filtering
ATT&CK 威胁情报
Target segmentation learning
[reprint] the token token is used in the login scenario
For loops and functions