definition:
TB6612FNG is a DC motor driving device having high current MOSFET-H-bridge configuration, dual-channel circuit outputs to drive two motors simultaneously.
Continuous driving current TB6612FNG highest output per channel to 1.2 A, starting peak current of 2A / 3.2 A (successive pulses / single pulse);
four kinds of motor control modes:
forward / reverse / stop / stop; the PWM support frequencies up to 100 kHz;
Function pin diagram:
**
Logic truth table
Instructions:
TB6612FNG pin main functions: AINl / AIN2, BIN1 / BIN2 , PWMA / PWMB to a control signal input terminal;
: Here's GPIO port pin is connected to several stm32 Development Board
will soon GPIO port is initialized to output, and output two pwm received on 6612 by a timer.
AO1 / A02, B01 / B02 is a 2-way control output of the motor;
;: A, B two control two DC motors
STBY control pin to the normal operating / standby state;
the VM (4.5 of 5 ~ 15 V) and VCC (2.7 ~ 5.5 V), respectively the motor drive voltage input and a logic level input.
: VM pin is connected to 12v DC power supply, VCC VCC stm32 connection board.
**
Physical map
Based on the foregoing, I drew a computer-controlled motor unit FIG follows :
**
Software section
Then you began to connect Stm32 and TB6612 modules:
First, we need to complete software support, namely control stm32 output pwm signal and the motor drive signal. I use keil5 to write programs.
Through access to information, pwm signal by GPIOB pb6 and pb7 output port, so I chose to use GPIOB selected as an output. With pb1, pb2, pb3, pb4 motor drive signal as a control. Here is the code to achieve:
void tim4_pwm_init(u16 arr,u16 psc)
{
RCC->APB1ENR |= 1<<2; //时钟复位TIM4
TIM4->CR1=0x0080; //ARPE 使能,Timx_ARR寄存器被装入缓冲器
TIM4->CR1|=0x01; //使能定时器 4
RCC->APB2ENR |= 1<<3; //时钟使能GPIOB 由电路图知GPIO6和TIM4的通道一复用
GPIOB->CRL&=0X00F00000;
GPIOB->CRL|=0XBB033333; //pb6 7复用功能输出 PB6输出ch1,pb7输出ch2
TIM4->ARR = arr; //设置计数器自动重装载值
TIM4->PSC = psc; //预分频器设计
TIM4->CCMR1 =0X6060; //配置Tim4输入捕获模式
TIM4->CCMR1|=1<<3; //CH1 预装载使能
TIM4->CCMR1|=1<<11; //CH 2 预装载使能
TIM4->CCER |=1<<0; //OC1 输出使能
TIM4->CCER |=1<<4; //OC2 输出使能
}
这里用到的寄存器全部可以在stm32中文手册里查到。
The timer configuration TIM4 GPIOB port and complete, and then write the drive motor functions as follows:
```void go(int pwmnum)
{
GPIOB->ODR&=(~1<<3);//pb3
GPIOB->ODR|=1<<2;//pb2
GPIOB->ODR&=~(1<<1); //pb1
GPIOB->ODR|=1<<4; //pb4
MOTOR_Pwm_Valu=pwmnum;
MOTOR_Pwm_Valu2=pwmnum;
}
Write basic code has been completed so far, we only need to call the go () function in the main function, used to change the value of pwmnum can change the speed of the motor.
#include "sys.h"
#include "pwm.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "key.h"
int main(void)
{
Stm32_Clock_Init(9);//系统时钟设置
delay_init(72); //延时初始化
uart_init(72,9600); //串口初始化
LED_Init(); //初始化与LED连接的硬件接口
tim4_pwm_init(899,0);
KEY_Init();
LED0=0; //点亮LED
while(1)
{
go(5000);//在这里改变参数值
}
}
Results Figure
Please bigwigs also pointed out shortcomings.