#include "stm32f4xx.h" #include "stm32f4xx_rcc.c" #include "stm32f4xx_gpio.c" #include "stm32f4xx_tim.c" #include "LCD2x16.c" #include "dd.h" // Timer 3 init function - use as PWM void TIM3init_PWM_CenterAligned (void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //int PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 21000000) - 1; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; TIM_TimeBaseInitStructure.TIM_Period = 1024; TIM_TimeBaseInitStructure.TIM_Prescaler = 3; //PrescalerValue; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 409; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 614; TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_Cmd(TIM3, ENABLE); } // initialize port C, motor control, Pin_6 to Pin_9 and Pin_11 as outputs void GPIOCinit_TIM3_PWM (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 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_25MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIOC->BSRRL = BIT_11; GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3); } // initialize port E (switches), Gpio_Pin_3 to GPIO_Pin_6 as inputs void SWITCHinit (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOE, &GPIO_InitStructure); } int main () { int PWMratio = 512, switches; GPIOCinit_TIM3_PWM(); LCD_init(); // init LCD LCD_string("PWM linear motor", 0x00); // display title string LCD_string("%", 0x48); // display title string SWITCHinit (); TIM3init_PWM_CenterAligned(); while (1) { switches = GPIOE->IDR; if (switches & S370) PWMratio++; // S370 if (switches & S371) PWMratio--; // S371 if (PWMratio < 1) PWMratio = 1; if (PWMratio > 1020) PWMratio = 1020; TIM3->CCR1 = PWMratio; TIM3->CCR2 = 1024 - PWMratio; LCD_sInt3DG((int)((PWMratio-512)/512.0*100),0x44,0x05); // write to LCD, signed, 16 bits for (int i = 0; i < 1000000; i++) {}; }; }