赞
踩
左右两边的OUT0~OUT4接130电机两端;
中间蓝色端口接电源,注意:两个电源口和一个接地口都要接上!!!!!!!!
偏左一些的排针用杜邦线接上io口,如果需要PWM控速的话,ENA和ENB的跳线帽需要拔掉,
然后用两根杜邦线分别连到一个io口,给高电平使能,如果是低电平则电机制动。
同时,IN1~IN4连接到pwm对应的GPIO口。
这样,接线部分基本完成。
具体可参考这位博主,
(63条消息) L298N 驱动电机与单片机的线路连接图_l298n驱动接线方法图_高佬君的博客-CSDN博客
1.首先给对应的GPIO口初始化,给用到的GPIO口设置推挽输出
L298N.c
- #include "L298N.h"
- void L298_Init(){
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能PG端口时钟
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5; //IN1->PB4,IN2->PB5推挽输出
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIO
- GPIO_ResetBits(GPIOB,GPIO_Pin_4|GPIO_Pin_5); //PB4,PB5 输出低
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //IN3->PB1,IN4->PB0推挽输出
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1);
-
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2|GPIO_Pin_6; //ENA->PB0,ENB->PB6,推挽输出
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
-
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }

L298N.h
- #ifndef __L298N_H__
- #define __L298N_H__
- #include "sys.h"
- #define IN1 PBout(4)
- #define IN2 PBout(5)
- #define IN3 PBout(0)
- #define IN4 PBout(1)
- #define ENA1 PBout(2)
- #define ENA2 PBout(6)
- void L298_Init();
- #endif
2.定时器初始化,设置对应的PWM
这里选用通用定时器TIM3的通道1~4;
首先,初始化TIM3,然后,设置端口重映射,并设置PWM输出的GPIO口为推挽复用输出!(切记)
而后,设置PWM参数,使能自动重装载寄存器,arr的预装载,选择通道,使能定时器,完成。
代码如下:PWM.c
- #include "PWM.h"
-
- void TIM3_PWM_Init(u16 arr,u16 psc){
- GPIO_InitTypeDef gpio;
- TIM_TimeBaseInitTypeDef time;
- TIM_OCInitTypeDef time_oc;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
-
- //GPIO_PinRemapConfig();
- GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
-
- gpio.GPIO_Mode=GPIO_Mode_AF_PP;
- gpio.GPIO_Pin=GPIO_Pin_5;
- gpio.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&gpio);
-
- gpio.GPIO_Mode=GPIO_Mode_AF_PP;
- gpio.GPIO_Pin=GPIO_Pin_1;
- gpio.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&gpio);
-
- time.TIM_Period=arr;
- time.TIM_Prescaler=psc;
- time.TIM_ClockDivision=0;
- time.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3,&time);
-
- time_oc.TIM_OCMode=TIM_OCMode_PWM1;
- time_oc.TIM_OutputState=TIM_OutputState_Enable;
- time_oc.TIM_OCPolarity=TIM_OCPolarity_High;
-
- TIM_OC1Init(TIM3,&time_oc);
- TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
-
- time_oc.TIM_OCMode=TIM_OCMode_PWM1;
- time_oc.TIM_OutputState=TIM_OutputState_Enable;
- time_oc.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OC2Init(TIM3,&time_oc);
- TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
-
- time_oc.TIM_OCMode=TIM_OCMode_PWM2;
- time_oc.TIM_OutputState=TIM_OutputState_Enable;
- time_oc.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OC3Init(TIM3,&time_oc);
- TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
-
- time_oc.TIM_OCMode=TIM_OCMode_PWM2;
- time_oc.TIM_OutputState=TIM_OutputState_Enable;
- time_oc.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OC4Init(TIM3,&time_oc);
- TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
-
- TIM_ARRPreloadConfig(TIM3,ENABLE);//使能自动重装载寄存器,arr的预装载
- TIM_Cmd(TIM3,ENABLE);
-
-
-
- }

pwm.h
- #ifndef __PWM_H__
- #define __PWM_H__
- #include "sys.h"
-
- void TIM3_PWM_Init(u16 arr,u16 psc);
- #endif
main.c
- #include "stm32f10x.h"
- #include "L298N.h"
- #include "key.h"
- #include "uart.h"
- /************************************************
- ALIENTEK 精英STM32F103开发板实验0
- 工程模板
- 注意,这是手册中的新建工程章节使用的main文件
- 技术支持:www.openedv.com
- 淘宝店铺:http://eboard.taobao.com
- 关注微信公众平台微信号:"正点原子",免费获取STM32资料。
- 广州市星翼电子科技有限公司
- 作者:正点原子 @ALIENTEK
- ************************************************/
-
-
- int main(void)
- {
- u16 led0pwmval=0;
- u8 dir=1;
-
- L298_Init();
- TIM3_PWM_Init(450,7199);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- Uart_Init(115200);
- Key_Init();
-
- delay_init(); //延时函数初始化
- //设置 NVIC 中断分组 2
- ENA1=1;
- ENA2=1;
- //Ind=0;
- TIM_SetCompare2(TIM3,100);//设置通道2的占空比实现PWM调速,这里是100,在0~450间,越小速度越快
- // TIM_SetCompare1(TIM3,400);//设置通道1的占空比实现PWM调速
- // TIM_SetCompare3(TIM3,400);
- TIM_SetCompare4(TIM3,100);
- //Ina=1;
- //IN1=1;
- // IN4=1;
-
- while(1)
- {
-
- printf("%d\r\n", Get_Key(Key_Scan(0)) );
-
- delay_ms(1000);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。