STM32F103C8T6 L298N驱动代码
时间: 2023-07-16 19:13:31 浏览: 51
以下是基于STM32F103C8T6与L298N的驱动代码,供参考:
```
#include "stm32f10x.h"
#define IN1 GPIO_Pin_0
#define IN2 GPIO_Pin_1
#define IN3 GPIO_Pin_2
#define IN4 GPIO_Pin_3
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = IN1 | IN2 | IN3 | IN4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void delay(uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void L298N_SetOutput(uint16_t out)
{
if((out & 0x01) == 0x01) GPIO_SetBits(GPIOA, IN1);
else GPIO_ResetBits(GPIOA, IN1);
if((out & 0x02) == 0x02) GPIO_SetBits(GPIOA, IN2);
else GPIO_ResetBits(GPIOA, IN2);
if((out & 0x04) == 0x04) GPIO_SetBits(GPIOA, IN3);
else GPIO_ResetBits(GPIOA, IN3);
if((out & 0x08) == 0x08) GPIO_SetBits(GPIOA, IN4);
else GPIO_ResetBits(GPIOA, IN4);
}
int main(void)
{
GPIO_Configuration();
while(1)
{
L298N_SetOutput(0x05); //正转
delay(50000);
L298N_SetOutput(0x0A); //反转
delay(50000);
}
}
```
注:以上代码仅供参考,具体实现方式需要根据具体需求进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)