找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 44|回复: 2
打印 上一主题 下一主题
收起左侧

为什么此代码运行只能驱动一个电机,如要成功实现驱动两个,需要需要修改,急求

[复制链接]
跳转到指定楼层
楼主
ID:1155523 发表于 2025-7-16 11:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
PWM.C
#include "stm32f10x.h"                  // Device header

void PWM_Init(void)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
       

        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
       
        TIM_InternalClockConfig(TIM2);
       
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;                //ARR
        TIM_TimeBaseInitStructure.TIM_Prescaler = 36 - 1;                //PSC
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
       
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_OCStructInit(&TIM_OCInitStructure);
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 0;                //CCR
        TIM_OC3Init(TIM2, &TIM_OCInitStructure);
        TIM_OC1Init(TIM2, &TIM_OCInitStructure);
       
        TIM_Cmd(TIM2, ENABLE);
}

void PWM_SetCompare3(uint16_t Compare)
{
        TIM_SetCompare3(TIM2, Compare);
}
void PWM_SetCompare1(uint16_t Compare)
{
        TIM_SetCompare1(TIM2, Compare);
}
PWM.H
#ifndef __PWM_H
#define __PWM_H

void PWM_Init(void);
void PWM_SetCompare3(uint16_t Compare);
void PWM_SetCompare1(uint16_t Compare);
#endif
Motor.c
#include "stm32f10x.h"                  // Device header
#include "PWM.h"

void Motor_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;  // 推挽 高低都亮,改OD为开漏  高灭低亮
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;  // 推挽 高低都亮,改OD为开漏  高灭低亮
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
       
        PWM_Init();                                                                          
}

void Motor_SetSpeed(int8_t Speed1,int8_t Speed2)
{
        if(Speed1 >= 0)
        {
                GPIO_SetBits(GPIOA,GPIO_Pin_4);
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);
                PWM_SetCompare3(Speed1);
        }
        else
        {
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);
                GPIO_SetBits(GPIOA,GPIO_Pin_5);
                PWM_SetCompare3(-Speed1);
        }
        if(Speed2 >= 0)
        {
                GPIO_SetBits(GPIOB,GPIO_Pin_13);
                GPIO_ResetBits(GPIOB,GPIO_Pin_14);
                PWM_SetCompare1(Speed2);
        }
        else
        {
                GPIO_ResetBits(GPIOB,GPIO_Pin_13);
                GPIO_SetBits(GPIOB,GPIO_Pin_14);
                PWM_SetCompare1(-Speed2);
        }
}
Motor.h
#ifndef __Motor_H
#define __Motor_H

void Motor_Init(void);
void Motor_SetSpeed(int8_t Speed1,int8_t Speed2);

#endif

main.c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Motor.h"
#include "Key.h"

uint8_t KeyNum;
int8_t Speed1,Speed2;

int main(void)
{
        OLED_Init();
        Motor_Init();
        Key_Init();
       
    OLED_ShowString(1,1,"Speed1:");
        OLED_ShowString(2,1,"Speed2:");
       
        while (1)
        {
                KeyNum = Key_GetNum();
                if(KeyNum == 1)
                {
                        Speed1 += 20;
                        if(Speed1> 100)
                    {
                                Speed1 = 0;
                        }
                }
                else if(KeyNum == 2)
                {
                        Speed2 += 20;
                        if(Speed2> 100)
                    {
                                Speed2 = 0;
                }
        }
                                Motor_SetSpeed(Speed1,Speed2);
                        OLED_ShowSignedNum(1,7,Speed1,3);
                    OLED_ShowSignedNum(2,7,Speed1,3);
        }
}

Key.c
#include "stm32f10x.h"    // Device header
#include "Delay.H"

void Key_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
       
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1 | GPIO_Pin_11 ;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
}

uint8_t Key_GetNum(void)
{
        uint8_t Key_GetNum = 0;
        uint8_t KeyNum = 0;
        if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
        {
                Delay_ms(20);
                while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0);
                Delay_ms(20);
                KeyNum = 1;
        }
       
        return KeyNum;
}

Key.h
#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);
uint8_t Key_GetNum(void);

#endif



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:994000 发表于 2025-7-16 15:40 | 只看该作者
你这个按键每次按下去的返回值都是1啊,没有变成2的时候,另外一路电机没有速度啊。
回复

使用道具 举报

板凳
ID:326998 发表于 2025-7-16 17:05 | 只看该作者
现在是哪个电机驱动成功了?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表