stm32f407 定时器TIM4输出4通道PWM波,其中两个通道不能调节占空比
//main.c函数
#include "stm32f4xx.h"
#include "sys.h"
#include "pwm.h"
#include "delay.h"
#include "led.h"
//′®¿ú3õê¼»ˉ
void My_usart1_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//ê1Äü′®¿ú1μÄê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //êÇÄüGPIOA
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);//io¸′óÃó3éä
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);//io¸′óÃó3éä
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed=GPIO_High_Speed;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed=GPIO_High_Speed;
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
//֮ǰ×′ì¬preState, ÏÖÔú×′ì¬curState
void pwms_states1(u8 preState, u8 curState)
{
u16 ccrEnd = 100;
u16 ccrBeg = 0;
u8 flagOne = 1;
if(preState != curState)
{
while(ccrBeg++ < ccrEnd)
{
delay_ms(3);
//′óÄ′Ö¸¿ØÖÆ
flagOne = flagOne << 0;
if((preState & flagOne) < (curState & flagOne))
TIM_SetCompare1(TIM14,ccrEnd-ccrBeg);
if((preState & flagOne) > (curState & flagOne))
TIM_SetCompare1(TIM14,ccrBeg);
//ê3Ö¸¿ØÖÆ
flagOne = flagOne << 1;
if((preState & flagOne) < (curState & flagOne))
TIM4->CCR1 = ccrBeg;
if((preState & flagOne) > (curState & flagOne))
TIM4->CCR1 = ccrEnd-ccrBeg;
//ÖDÖ¸¿ØÖÆ
flagOne = flagOne << 2;
if((preState & flagOne) < (curState & flagOne))
TIM4->CCR2 = ccrBeg;
if((preState & flagOne) > (curState & flagOne))
TIM4->CCR2 = ccrEnd-ccrBeg;
//ÎTÃûÖ¸¿ØÖÆ
flagOne = flagOne << 3;
if((preState & flagOne) < (curState & flagOne))
TIM4->CCR3 = ccrBeg;
if((preState & flagOne) > (curState & flagOne))
TIM4->CCR3 = ccrEnd-ccrBeg;
//D¡Ä′Ö¸¿ØÖÆ
flagOne = flagOne << 4;
if((preState & flagOne) < (curState & flagOne))
TIM4->CCR4 = ccrBeg;
if((preState & flagOne) > (curState & flagOne))
TIM4->CCR4 = ccrEnd-ccrBeg;
}
}
}
int main(void)
{
u8 res;
u8 prev_flag = 0; //hand open when start.
u8 five_flag = 0;//the 5 low bits control 5 channels pwm
LED_Init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_usart1_init();
delay_init(168);
TIM4_PWM_Init(100-1, 84-1);
TIM14_PWM_Init(100-1,84-1);
while(1)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
{
res = USART_ReceiveData(USART1);
if(res == '0')
five_flag = 31;
else if(res == '1')
five_flag = 0;
else if(res == '2')
five_flag = 3;
else if(res == '3')
five_flag =13;
else
five_flag = 14;
}
pwms_states1(prev_flag, five_flag);
prev_flag = five_flag;
}
}
//pwm.c
#include "pwm.h"
#include "led.h"
#include "usart.h"
void TIM14_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPEê1Äü
TIM_Cmd(TIM14, ENABLE);
}
void TIM4_PWM_Init(u32 arr, u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_TIM4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4,ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
一周热门 更多>