这是我写的初始化pwm_init()函数#include "pwm.h"
#include "stm32f10x.h"
u32 w1,w2,w3,w4;
void pwm_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_TIM8,ENABLE);
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode =TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period =999;
TIM_TimeBaseInitStruct.TIM_Prescaler =0;
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode =TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCPolarity =TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse =w1;
TIM_OC1Init(TIM8,&TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);
TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse =w2;
TIM_OC2Init(TIM8,&TIM_OCInitStruct);
TIM_OC2PreloadConfig(TIM8,TIM_OCPreload_Enable);
TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse =w3;
TIM_OC3Init(TIM8,&TIM_OCInitStruct);
TIM_OC3PreloadConfig(TIM8,TIM_OCPreload_Enable);
TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse =w4;
TIM_OC4Init(TIM8,&TIM_OCInitStruct);
TIM_OC4PreloadConfig(TIM8,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8,ENABLE);
TIM_Cmd(TIM8,ENABLE);
}
与TIM3输出PWM配置除了引脚和时钟不同,其他应该都是一样的。
主函数
#include "stm32f10x.h"
#include "lcd.h"
#include "delay.h"
#include "usart.h"
#include "pwm.h"
extern u32 w1,w2,w3,w4; //w1Ç°×óÂÖ£¬w2oó×óÂÖ£¬w3Ç°óòÂÖ£¬w4oóóòÂÖ
int main (void)
{
delay_init();
uart_init(115200);
LCD_Init();
pwm_init();
POINT_COLOR=RED;
LCD_Clear(GRAY);
POINT_COLOR=RED;
LCD_ShowString(20,170,200,16,16,"W1:");
LCD_ShowString(20,190,200,16,16,"W2:");
LCD_ShowString(20,210,200,16,16,"W3:");
LCD_ShowString(20,230,200,16,16,"W4:");
w1=w2=w3=w4=500;
while(1)
{
w1+=5;
w2+=10;
w3-=5;
w4-=10;
LCD_ShowNum(60,170,w1,16,16);
LCD_ShowNum(60,190,w2,16,16);
LCD_ShowNum(60,210,w3,16,16);
LCD_ShowNum(60,230,w4,16,16);
pwm_init();
delay_ms(100);
if(w1>950|w2>950|w3<50|w4<50)
{
if(w1>950)w1=500;
if(w2>950)w2=500;
if(w3<50)w3=500;
if(w4<50)w4=500;
}
}
}
LCD程序是原子哥给的历程里面移植过来的,显示能够正常显示W1,W2,W3,W4四个值得变化,但是输出的电机就是不转。
之前使用TIM3输出PWM控制电机转速的 但是TIM3有一个通道与LCD的引脚共用了
为了不与LCD冲突,通过查看精英版的芯片引脚,发现TIM8很合适,所以才决定使用TIM8输出PWM控制电机,现在的问题就是使用TIM8输出PWM波,电机没反应,如果用TIM3输出PWM的话能够正常转动,但就是会影响LCD的亮度。
难道高级定时器的配置与通用定时器的配置不一样吗?
我上传了精英版芯片引脚图,望大神能够帮忙解答,谢谢
一周热门 更多>