PWM

//----------------------------------------------------------------------
// Titel     : einfaches PWM Beispiel für das STM32F4 Discovery
//----------------------------------------------------------------------
// Funktion  : dimmt die orange LED
// Schaltung : orange LED an PD13
//----------------------------------------------------------------------
// Hardware  : STM32F4 Discovery
// MCU       : STM32F407VGT6
// Takt      : 168 MHz
// Sprache   : ARM C
// Datum     : 21.01.2013
// Version   : 1
// Autor     : Alexander Huwaldt
//----------------------------------------------------------------------
#include <stddef.h>
#include <stdlib.h>
#include "hardware.h"
 
GPIO_InitTypeDef GPIO_InitStructure;
#define LED_ORANGE 	(1<<13)
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
#define F_PWM 5000
volatile uint16_t timerPeriod = 0;
volatile uint16_t pwmLevel = 0; // 0-F_PWM
 
 
void initApplication()
{
	SysTick_Config(SystemCoreClock/100);
	// weitere Initialisierungen durchführen
 
	/* red LED Periph clock enable */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
 
	/* Configure PD3 = red LED in output pushpull mode */
	GPIO_InitStructure.GPIO_Pin = LED_ORANGE;
	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_NOPULL;
	GPIO_Init(GPIOD, &GPIO_InitStructure);	
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
 
	/* PWM frequency at 15 Khz */
	timerPeriod = ((SystemCoreClock/10) / F_PWM ) - 1;
 
 
	/* TIM4 clock enable */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
 
	/* Timer 1 Base configuration */
	TIM_TimeBaseStructure.TIM_Prescaler = 4;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period = timerPeriod;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
 
	/* 50% for PWM channel 1 */
	pwmLevel = F_PWM/2;
 
	/* PWM Channel 2 Coinfiguration */
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
	TIM_OCInitStructure.TIM_Pulse = pwmLevel;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
	TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
	TIM_OC2Init(TIM4, &TIM_OCInitStructure);
 
	/* TIM4 counter enable */
	TIM_Cmd(TIM4, ENABLE);
 
	/* TIM4 Main Output Enable */
	TIM_CtrlPWMOutputs(TIM4, ENABLE);
 
 
}
 
 
int main(void)
{
	SystemInit();
	initApplication();
	bool dimUp=true;
	do{
 
		if (dimUp)
		{
			// nur bis 50% aufdimmen
			if(pwmLevel<(F_PWM/2))
				pwmLevel++;
			else
				dimUp=false;
		}
		else
		{
			if(pwmLevel>0)
				pwmLevel--;
			else
				dimUp=true;
		}
 
	TIM_OCInitStructure.TIM_Pulse = pwmLevel;
	TIM_OC2Init(TIM4, &TIM_OCInitStructure);
	waitMs(1);
 
	} while (true);
	return 0;
}
 
// Application SysTick
extern "C" void SysTick_Handler(void)
{
 
}

zurück zur Übersicht