PWM in C mit dem mySTM32 Board Light

//---------------------------------------------------------------------------
// Title     : simple PWM Solution, ARM C application in SiSy
//---------------------------------------------------------------------------
// Function  : ...
// Wiring    : ...
//---------------------------------------------------------------------------
// Hardware  : ...
// Clock     : ... MHz
// Language  : ARM C
// Date      : ...
// Version   : ...
// Author    : ...
//---------------------------------------------------------------------------
#include <stddef.h>
#include <stdlib.h>
#include "hardware.h"
 
GPIO_InitTypeDef GPIO_InitStructure;
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_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 
	/* Configure PD3 = red LED in output pushpull mode */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB, &GPIO_InitStructure);	
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_0);
 
	/* PWM frequency at 15 Khz */
	timerPeriod = ((SystemCoreClock/10) / F_PWM ) - 1;
 
 
	/* TIM3 clock enable */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , 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(TIM3, &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(TIM3, &TIM_OCInitStructure);
 
	/* TIM4 counter enable */
	TIM_Cmd(TIM3, ENABLE);
 
	/* TIM4 Main Output Enable */
	TIM_CtrlPWMOutputs(TIM3, 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(TIM3, &TIM_OCInitStructure);
	waitMs(1);
 
	} while (true);
	return 0;
}
 
// Application SysTick
extern "C" void SysTick_Handler(void)
{
 
}
//------------------------------------------------------------------------------

Weiter mit