完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近在调直流电机,用的是L298N驱动板,用光电管返回信号给A0脚,来控制A4和A5脚,A4和A5脚分别产生高低电平给L298N的IN1和IN2,IN1和IN2控制正反转,使能控制转速,我现在知道如何用定时器产生PWM来控制转速,但在给IN1和IN2输出高低电平来控制正反转时,电机一直只向一个方向来转,不执行正反转功能,光电管没有控制4和5的电平。
就是这个问题 二楼附上代码,求各位大神指教我到底哪里出错了!!
|
|
相关推荐
7个回答
|
|
#include "bsp_breathing.h"
uint16_t indexWave[] = {400,600,800,1000,1500,1500,1000,800,400}; /** * @brief ÅäÖÃTIM3¸´ÓÃÊä³öPWMʱÓõ½µÄI/O * @param ÎÞ * @retval ÎÞ */ static void TIMx_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOB clock enable */ // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); macTIM_GPIO_APBxClock_FUN ( macTIM_GPIO_CLK, ENABLE ); //¿ªGPIOBµÄʼʱÖÓ /* ÅäÖÃPWMÓõ½µÄPB0Òý½Å */ GPIO_InitStructure.GPIO_Pin = macTIM_LED_PIN ; //PB0 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( macTIM_LED_PORT, &GPIO_InitStructure ); } static void NVIC_Config_PWM(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* ÅäÖÃTIM3_IRQÖжÏΪÖжÏÔ´ */ NVIC_InitStructure.NVIC_IRQChannel = macTIMx_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /* * TIMxCLK/CK_PSC --> TIMxCNT --> TIMx_ARR --> ÖÐ¶Ï & TIMxCNT ÖØмÆÊý * TIMx_CCR(µçƽ·¢Éú±ä»¯) * * ÐźÅÖÜÆÚ=(TIMx_ARR +1 ) * ʱÖÓÖÜÆÚ * */ static void TIMx_Mode_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* ÉèÖÃTIM3CLK ʱÖÓΪ72MHZ */ // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ʹÄÜTIM3ʱÖÓ macTIM_APBxClock_FUN ( macTIM_CLK, ENABLE ); /* »ù±¾¶¨Ê±Æ÷ÅäÖà */ TIM_TimeBaseStructure.TIM_Period = 2000-1; //µ±¶¨Ê±Æ÷´Ó0¼ÆÊý1999£¬¼´Îª2000´Î£¬ÎªÒ»¸ö¶¨Ê±ÖÜÆÚ TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //ÉèÖÃÔ¤·ÖƵ£º TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ(ÕâÀïÓò»µ½) TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ TIM_TimeBaseInit(macTIMx, &TIM_TimeBaseStructure); /* PWMģʽÅäÖà */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ʹÄÜÊä³ö TIM_OCInitStructure.TIM_Pulse = 0; //ÉèÖóõʼPWMÂö³å¿í¶ÈΪ0 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪµÍµçƽ macTIM_OCxInit ( macTIMx, &TIM_OCInitStructure ); //ʹÄÜͨµÀ3 macTIM_OCxPreloadConfig ( macTIMx, TIM_OCPreload_Enable ); //ʹÄÜԤװÔØ TIM_ARRPreloadConfig(macTIMx, ENABLE); //ʹÄÜTIM3ÖØÔؼĴæÆ÷ARR /* TIM3 enable counter */ TIM_Cmd(macTIMx, ENABLE); //ʹÄܶ¨Ê±Æ÷3 TIM_ITConfig(macTIMx, TIM_IT_Update, ENABLE); //ʹÄÜupdateÖÐ¶Ï NVIC_Config_PWM(); //ÅäÖÃÖжÏÓÅÏȼ¶ } void GDG_GPIO_Config(void) //¹âµç¹Ü¼ì²âÒý½Å { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void MOTORA_L_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_ResetBits(GPIOA, GPIO_Pin_4); GPIO_SetBits(GPIOA, GPIO_Pin_5); } void MOTORA_R_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_4); GPIO_ResetBits(GPIOA, GPIO_Pin_5); } void Delay(__IO u32 nCount) { for(; nCount != 0; nCount--); } void jiance(void){ if(GPIO_Pin_0!=0){ MOTORA_L_GPIO_Config(); Delay(0x0FFFFF); } else { MOTORA_R_GPIO_Config(); Delay(0x0FFFFF); } } void TIMx_Breathing_Init(void) { TIMx_GPIO_Config(); TIMx_Mode_Config(); jiance(); } |
|
|
|
求看哪里有问题!
|
|
|
|
|
|
|
|
我看了L298N的威廉希尔官方网站 资料,明白高低电平来控制转动方向,我的问题就是编程的问题,4和5程序里让他产生两种信号,但只产生一种,想知道哪里出问题了_(:з」∠)_ |
|
|
|
希望大神指教程序哪里有问题!
|
|
|
|
|
|
|
|
兄dei,我也遇到你这种问题,想知道你是怎么解决的?
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2404 浏览 0 评论
STM32配合可编程加密芯片SMEC88ST的防抄板加密方案设计
1259 浏览 0 评论
3258 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
3034 浏览 4 评论
5571 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-1-23 21:30 , Processed in 1.227017 second(s), Total 90, Slave 71 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号