【魔方团】STM32F4之PWM配置
我用的编译器是IAR,至于怎么在IAR下建立工程文件,就按照附件中“我的stm32f4学习历程”跟着建立。另外,如果是用官方的F4-DISCOVERY板子,需注意,板子上的HSE为8M,而我们下载的官方库文件下默认为25M,在启动文件中会自动调用system_init()这个函数讲系统时钟设为168M,我们需要修改的地方有两处
1.system_stm32f4xx.c文件中#define PLL_M 8原本为25。改为8(也可根据自己想设定的总线时钟进行修改,但最高不超过168M)
2.stm32f4xx.h文件中 #define HSE_VALUE ((uint32_t)8000000)原本为25000000现在改为8000000.这样时钟配置就完成了。
接下来看PWM的配置:
使用通用定时器3,4,每个定时器有4个通道,来产生8路PWM。
PS.第一次发帖,可能有些错误,望指出共同学习参考。
附件附件附件附件 看看。。。。。。。。。。 参观一下的路过
飘过。。。。。。。。。。。。 参观一下的路过 这是什么开发环境呢。。。。。。 lsj306 发表于 2013-8-16 13:01
这是什么开发环境呢。。。。。。
iar自己改了改配色 #include <stm32f4xx.h>
volatile u8 state = 0;
const u16 TIM3_CCR1[] = {240,240,60,60,60,60,60,60,60,540};
#define STATE_COUNT (sizeof(TIM3_CCR1)/sizeof(u16 ))
static void TIM3_Config(void);
int main()
{
TIM3_Config();
TIM3->ARR = 65535;//Prescaler
TIM3->PSC = 100;//Period
TIM3->CCMR1 |= 0x3030;//ToggleMode
TIM3->CCMR2 |= 0x3030;//Toggle Mode
TIM3->CCER |= 0x1111;//Capture Compare 1 output enable //CaptureCompare 1 output PolarityHigh
TIM3->CCR1= 240;// Duty
TIM3->CCR2= 140;// Duty
TIM3->CCR3= 540;// Duty
TIM3->CCR4= 640;// Duty
TIM3->CR1 |= 0x0001;//Counter enabled
TIM3->DIER |= 0x001E;//Capture Compare 1 interrupt enable
while(1)
{
}
}
static void TIM3_Config(void)
{
NVIC_InitTypeDef NVIC_TIM3;// NVIC structure
RCC->AHB1ENR |= 0x00000004; //GPIOC Clock
RCC->APB1ENR |= 0x00000002;//TIM3 Clock
GPIOC->MODER |= 0x000AA000;//PC6,7,8,9 AF Mode
GPIOC->OSPEEDR |= 0x000AA000;//PC6,7,8,9 50MHz
GPIOC->OTYPER|= 0x00000000;//PC6,7,8,9 Push-pull
GPIOC->PUPDR |= 0x00055000;//PC6,7,8,9 Pull Up
GPIOC->AFR|= 0x22000000;//AF2 (TIM3)PC6-> TIM3_CH1,PC7->CH2
GPIOC->AFR|= 0x00000022;// PC8->CH3,PC9->CH4
NVIC->ISER|= 0x20000000;
NVIC_TIM3.NVIC_IRQChannelPreemptionPriority = 0; // Only matters for multiple interrupts
NVIC_TIM3.NVIC_IRQChannelSubPriority = 0; // Only matters for multiple interrupts
NVIC_TIM3.NVIC_IRQChannelCmd = ENABLE; // Enabling global interrupt
NVIC_Init(&NVIC_TIM3); // Initializing NVIC structure
}
void TIM3_IRQHandler(void)
{
static uint8_t state = 0;
if(TIM3->SR &0x0002)//CC1
{
TIM3->SR &= ~0x0002;
TIM3->CCR1 += TIM3_CCR1;
if (++state >= STATE_COUNT)
state = 0;
}
if(TIM3->SR &0x0004)//CC2
{
static uint8_t state1 = 0;
TIM3->SR &= ~0x0004;
if (state1 < 2 || state1 > 8)
TIM3->CCR2 += 240;
else
TIM3->CCR2 += 60;
if (++state1 > 9)
state1 = 0;
}
if(TIM3->SR &0x0008)//CC3
{
static uint8_t state2 = 0;
TIM3->SR &= ~0x0008;
if (state2 < 2 || state2 > 8)
TIM3->CCR3 += 240;
else
TIM3->CCR3 += 60;
if (++state2 > 9)
state2 = 0;
}
if(TIM3->SR &0x0010)//CC4
{
static uint8_t state3 = 0;
TIM3->SR &= ~0x0010;
if (state3 < 2 || state3 > 8)
TIM3->CCR4 += 200;
else
TIM3->CCR4 += 100;
if (++state3 > 9)
state3 = 0;
}
}
#include <stm32f4xx.h>
volatile u8 state = 0;
const u16 TIM3_CCR1[] = {240,240,60,60,60,60,60,60,60,540};
#define STATE_COUNT (sizeof(TIM3_CCR1)/sizeof(u16 ))
static void TIM3_Config(void);
int main()
{
TIM3_Config();
TIM3->ARR = 65535;//Prescaler
TIM3->PSC = 100;//Period
TIM3->CCMR1 |= 0x3030;//Toggle Mode
TIM3->CCMR2 |= 0x3030;//Toggle Mode
TIM3->CCER |= 0x1111;//Capture Compare 1 output enable //CaptureCompare 1 output PolarityHigh
TIM3->CCR1= 240;// Duty
TIM3->CCR2= 140;// Duty
TIM3->CCR3= 540;// Duty
TIM3->CCR4= 640;// Duty
TIM3->CR1 |= 0x0001;//Counter enabled
TIM3->DIER |= 0x001E;//Capture Compare 1 interrupt enable
while(1)
{
}
}
static void TIM3_Config(void)
{
NVIC_InitTypeDef NVIC_TIM3;// NVIC structure
RCC->AHB1ENR |= 0x00000004; //GPIOC Clock
RCC->APB1ENR |= 0x00000002;//TIM3 Clock
GPIOC->MODER |= 0x000AA000;//PC6,7,8,9 AF Mode
GPIOC->OSPEEDR |= 0x000AA000;//PC6,7,8,9 50MHz
GPIOC->OTYPER|= 0x00000000;//PC6,7,8,9 Push-pull
GPIOC->PUPDR |= 0x00055000;//PC6,7,8,9 Pull Up
GPIOC->AFR|= 0x22000000;//AF2 (TIM3)PC6-> TIM3_CH1,PC7->CH2
GPIOC->AFR|= 0x00000022;// PC8->CH3,PC9->CH4
NVIC->ISER|= 0x20000000;
NVIC_TIM3.NVIC_IRQChannelPreemptionPriority = 0; // Only matters for multiple interrupts
NVIC_TIM3.NVIC_IRQChannelSubPriority = 0; // Only matters for multiple interrupts
NVIC_TIM3.NVIC_IRQChannelCmd = ENABLE; // Enabling global interrupt
NVIC_Init(&NVIC_TIM3); // Initializing NVIC structure
}
void TIM3_IRQHandler(void)
{
static uint8_t state = 0;
if(TIM3->SR &0x0002)//CC1
{
TIM3->SR &= ~0x0002;
TIM3->CCR1 += TIM3_CCR1;
if (++state >= STATE_COUNT)
state = 0;
}
if(TIM3->SR &0x0004)//CC2
{
static uint8_t state1 = 0;
TIM3->SR &= ~0x0004;
if (state1 < 2 || state1 > 8)
TIM3->CCR2 += 240;
else
TIM3->CCR2 += 60;
if (++state1 > 9)
state1 = 0;
}
if(TIM3->SR &0x0008)//CC3
{
static uint8_t state2 = 0;
TIM3->SR &= ~0x0008;
if (state2 < 2 || state2 > 8)
TIM3->CCR3 += 240;
else
TIM3->CCR3 += 60;
if (++state2 > 9)
state2 = 0;
}
if(TIM3->SR &0x0010)//CC4
{
static uint8_t state3 = 0;
TIM3->SR &= ~0x0010;
if (state3 < 2 || state3 > 8)
TIM3->CCR4 += 200;
else
TIM3->CCR4 += 100;
if (++state3 > 9)
state3 = 0;
}
}
页:
[1]