본문 바로가기

Development/Embedded

STM32F103 - PWM 소스

STM32F103 / PWM / Cortex M3


STM32F103 에서 PWM 을 생성하는 소스임 Timer 3의 CH 1 / CH 2 / CH 3 / CH 4 에서 PWM 생성

전체 소스에서 PWM 부분만 잘라서 올림

헤더 파일 포함 시키는 것이 맞는지 잘 기억이 안남 소스 내용 자체는 정확하니 참고용으로 쓰시길

#include <STM32F103_PWM.H>
#include <STM32F103_TIM.H>
#include <STM32F103_RCC.H>

u16     PrescalerValue = 0;
double   i_motor1 = 0;
double   i_motor2 = 800;
double   i_motor3 = 800;
double   i_motor4 = 0;

void    PWM_Init(void);
void    RCC_Configuration(void);
void    GPIO_Configuration(void);

TIM_TimeBaseInitTypeDef		TIM_TimeBaseStructure;
TIM_OCInitTypeDef			TIM_OCInitStructure;

int main()
{
	PWM_Init();
	while(1)
	{
	}
}

void PWM_Init()
{
    RCC_Configuration();
    GPIO_Configuration();
    
    PrescalerValue = (u16)(SystemCoreClock / 1000000) - 1;     	//	1000000 for 10us
    
    TIM_TimeBaseStructure.TIM_Period = 20000;        			// 	2000 	for 10us
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    
    //CH1 setting start
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);
    
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
    //CH1 setting end
    
    //CH2 setting start
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = (int)i_motor2;
    
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);
    
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
    //CH2 setting end
    
    //CH3 setting start
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = (int)i_motor3;
    
    TIM_OC3Init(TIM3, &TIM_OCInitStructure);
    
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
    //CH3 setting end
    
    //CH4 setting start
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);
    
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
    //CH4 setting end
    
    TIM_ARRPreloadConfig(TIM3, ENABLE);	
    TIM_Cmd(TIM3, ENABLE);
    
    delay_ms(1000);
}

/*	Name : RCC_Configuration
 *	Function : Ready For PWM */
void RCC_Configuration()
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);	
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
}

/*	Name : GPIO_Configuration
 *	Function : Ready For PWM */
void GPIO_Configuration()
{
    GPIO_InitTypeDef	GPIO_InitStructurePWM;
    
    GPIO_InitStructurePWM.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStructurePWM.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructurePWM.GPIO_Speed = GPIO_Speed_50MHz;
    
    GPIO_Init(GPIOA, &GPIO_InitStructurePWM);
    
    GPIO_InitStructurePWM.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_Init(GPIOB, &GPIO_InitStructurePWM);
}


2014 / 02 / 19 - 22:28 최초 작성

2014 / 02 / 20 - 01:41 태그 정리

2015 / 06 / 12 - 16:03 모바일에서 #include문 표시 문제 수정(모바일 티스토리에서 Syntax Highlight 동작 안함)

'Development > Embedded' 카테고리의 다른 글

Getting Start STM32F103 Dev  (0) 2014.02.28
STM32F103 - QuadCopter 참고 소스  (0) 2014.02.19
STM32F103 - PWM 소스  (0) 2014.02.19
FB155BC 설정 with USB2UART Downloader  (0) 2014.02.19
STM32F103 - Flash Downloader 에러  (0) 2014.02.19
ATmega128 - PWM Mode 14 소스  (0) 2014.02.19