nitin110051
Newbie level 3
- Joined
- Jul 24, 2013
- Messages
- 3
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 64
Hi i am using stm32f103 microcroller provide by st microelectronics i want to run a bldc motor of NIDEC in which there is a o/p which tells at how much speed it is running in terms of frequency & having an i/p pwm & duty cylcle of pwm varies speed of motor . i tired but i am not getting a good o/p . code which i am writing is below here can anyone help me to sort out this problem
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 #include <stm32f10x.h> #include<stdio.h> GPIOSpeed_TypeDef gpio_speed; GPIOMode_TypeDef gpio_mode; GPIO_InitTypeDef gpio_init; TIM_TimeBaseInitTypeDef time_init; TIM_OCInitTypeDef timer_output_compare; uint16_t Capture = 0; volatile uint16_t TIM5Freq = 0; uint16_t CCR1_Val= 232; void gpio() { //Configure GPIOD Pin 12 as Input gpio_init.GPIO_Pin = GPIO_Pin_12; gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &gpio_init); //Configure GPIOA Pin 6 as pwm o/p gpio_init.GPIO_Pin = GPIO_Pin_6; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &gpio_init); } void rcc() { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4|RCC_APB1Periph_TIM3, ENABLE); /* GPIOA and GPIOD clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO , ENABLE); } void timer3 () //PORT A //PIN6 { time_init.TIM_Period = 400; time_init.TIM_Prescaler = (uint16_t) (36000000 /4000000) - 1; time_init.TIM_ClockDivision = 0; time_init.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &time_init); /* PWM1 Mode configuration: Channel1 */ timer_output_compare.TIM_OCMode = TIM_OCMode_PWM1; timer_output_compare.TIM_OutputState = TIM_OutputState_Enable; timer_output_compare.TIM_Pulse = CCR1_Val; timer_output_compare.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &timer_output_compare); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); } void timer4 () // for running a free timer to count how many no. of pulse passed in high & low of motor o/p signal to calculate its frequency { TIM_TimeBaseStructInit(&time_init); time_init.TIM_Prescaler =((uint16_t) (36000000 /1000000) - 1); time_init.TIM_Period =65535; time_init.TIM_ClockDivision = 0; time_init.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &time_init); TIM_Cmd(TIM4, ENABLE); } int main() { uint16_t tim1,tim2; uint8_t count1=0; uint16_t gpiocheck; rcc(); gpio(); timer3(); timer4(); TIM_OC1Init(TIM3, &timer_output_compare); while(1) gpiocheck=GPIOD->IDR & 0X1000; if((gpiocheck==0X1000)&& count1==0) { tim1=TIM4->CNT; count1=1; } else if ((gpiocheck==0X0000)&&count1==1 ) { tim2 = TIM4->CNT; if (tim2 > tim1) { Capture = (tim2 - tim1); } else Capture = ((0xFFFF - tim1) + tim2); TIM5Freq = (uint32_t) 1000000 / Capture; count1=0; } if(TIM5Freq<420) { volatile uint16_t difference=0; if(TIM5Freq<440) difference=(440-TIM5Freq); else difference=(TIM5Freq-440); if(difference>=50) { timer_output_compare.TIM_Pulse = 400; TIM_OC1Init(TIM3, &timer_output_compare); } } } }
Last edited by a moderator: