2017-11-25 03:54 AM
Hello,
I have problem to start motor with sm32f0 and single motor driver mc33926. I was in pololu forum and they sad that everything connection is ok, but still dont work. I have joystick to control speed motor. Adc reading it's ok, I check this in stmstudio. Please, help me
&sharpinclude <stm32f0xx_gpio.h>
&sharpinclude <stm32f0xx_rcc.h>&sharpinclude 'stm32f0xx_adc.h'&sharpinclude 'stm32f0xx_tim.h'GPIO_InitTypeDef Gp;ADC_InitTypeDef ADC_INIT;TIM_TimeBaseInitTypeDef timerInitStructure;TIM_OCInitTypeDef outputChannelInit;&sharpdefine GreenLED_Pin GPIO_Pin_9&sharpdefine BlueLED_Pin GPIO_Pin_8&sharpdefine przycisk_Pin GPIO_Pin_0int odczyt_adc;int i;uint8_t ButtonRead = 0;uint8_t blad = 1;&sharpdefine IN2 GPIO_Pin_14 //PB14&sharpdefine IN1 GPIO_Pin_13 //PB13&sharpdefine EN_P GPIO_Pin_6 //PC6&sharpdefine SLEW GPIO_Pin_7 //PC7&sharpdefine SF_pin GPIO_Pin_7 //PA7&sharpdefine PWM_pin GPIO_Pin_3 //PA3void wyjscia_init(void){ Gp.GPIO_Pin =GreenLED_Pin | BlueLED_Pin | EN_P | SLEW; Gp.GPIO_Mode = GPIO_Mode_OUT; Gp.GPIO_OType = GPIO_OType_PP; Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; Gp.GPIO_Speed = GPIO_Speed_Level_1; GPIO_Init(GPIOC, &Gp); Gp.GPIO_Pin = IN1 | IN2; Gp.GPIO_Mode = GPIO_Mode_OUT; Gp.GPIO_OType = GPIO_OType_PP; Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; Gp.GPIO_Speed = GPIO_Speed_Level_1; GPIO_Init(GPIOB, &Gp); }void wejscia_init(void){ Gp.GPIO_Pin = SF_pin | przycisk_Pin; Gp.GPIO_Mode = GPIO_Mode_IN; Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &Gp); }void ADC_init(void){ ADC_DeInit(ADC1); Gp.GPIO_Mode = GPIO_Mode_AN; Gp.GPIO_OType = GPIO_OType_PP; Gp.GPIO_Pin = GPIO_Pin_1; Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; Gp.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&Gp); ADC_INIT.ADC_Resolution = ADC_Resolution_12b; ADC_INIT.ADC_ContinuousConvMode = ENABLE; ADC_INIT.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_INIT.ADC_DataAlign = ADC_DataAlign_Right; ADC_INIT.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_INIT); ADC_Cmd(ADC1, ENABLE); ADC_ChannelConfig(ADC1,ADC_Channel_1, ADC_SampleTime_71_5Cycles); ADC_StartOfConversion(ADC1);}void PWM_INIT(void){ Gp.GPIO_Pin = PWM_pin; Gp.GPIO_Mode = GPIO_Mode_AF; Gp.GPIO_OType = GPIO_OType_PP; Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; Gp.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&Gp); GPIO_PinAFConfig(GPIOA, PWM_pin, GPIO_AF_2); timerInitStructure.TIM_Prescaler = 48000; timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up; timerInitStructure.TIM_Period = 4000; timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; timerInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &timerInitStructure); outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1; outputChannelInit.TIM_OutputState = TIM_OutputState_Enable; outputChannelInit.TIM_Pulse =3000; outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC4Init(TIM2, &outputChannelInit); TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_Cmd(TIM2, ENABLE);}int main(void){ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); wyjscia_init(); wejscia_init(); ADC_init(); PWM_INIT(); while(1) { GPIO_SetBits(GPIOB, IN1); GPIO_ResetBits(GPIOB, IN2); GPIO_SetBits(GPIOC, EN_P); GPIO_SetBits(GPIOC, SLEW); odczyt_adc=ADC_GetConversionValue(ADC1); TIM_SetCompare1(TIM2,odczyt_adc); ButtonRead = GPIO_ReadInputDataBit(GPIOA, przycisk_Pin); blad = GPIO_ReadInputDataBit(GPIOA, SF_pin);if(ButtonRead) GPIO_SetBits(GPIOC, GreenLED_Pin);if(blad){ GPIO_SetBits(GPIOC, BlueLED_Pin); for(i=0;i<1000000;i++); GPIO_ResetBits(GPIOC, BlueLED_Pin); for(i=0;i<1000000;i++);} } }#pwm-stm32f0 #pwm