![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Lab work with servo
main.cpp@1:349234492517, 2017-10-30 (annotated)
- Committer:
- codebreaker7
- Date:
- Mon Oct 30 11:05:17 2017 +0000
- Revision:
- 1:349234492517
- Parent:
- 0:e0dc7a04fc82
Corrected for two servos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
codebreaker7 | 0:e0dc7a04fc82 | 1 | #include "mbed.h" |
codebreaker7 | 0:e0dc7a04fc82 | 2 | #include "stm32l0xx.h" |
codebreaker7 | 0:e0dc7a04fc82 | 3 | #include "stm32l0xx_hal_tim.h" |
codebreaker7 | 0:e0dc7a04fc82 | 4 | |
codebreaker7 | 0:e0dc7a04fc82 | 5 | TIM_HandleTypeDef timer; |
codebreaker7 | 0:e0dc7a04fc82 | 6 | TIM_OC_InitTypeDef octim; |
codebreaker7 | 0:e0dc7a04fc82 | 7 | int pulse = 1599; |
codebreaker7 | 0:e0dc7a04fc82 | 8 | |
codebreaker7 | 0:e0dc7a04fc82 | 9 | DigitalOut led(LED1); |
codebreaker7 | 0:e0dc7a04fc82 | 10 | |
codebreaker7 | 0:e0dc7a04fc82 | 11 | void HAL_TIM_Base_MspInit(TIM_HandleTypeDef * handle) |
codebreaker7 | 0:e0dc7a04fc82 | 12 | { |
codebreaker7 | 0:e0dc7a04fc82 | 13 | GPIO_InitTypeDef gpio; |
codebreaker7 | 0:e0dc7a04fc82 | 14 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
codebreaker7 | 0:e0dc7a04fc82 | 15 | __HAL_RCC_TIM2_CLK_ENABLE(); |
codebreaker7 | 0:e0dc7a04fc82 | 16 | gpio.Mode = GPIO_MODE_AF_PP; |
codebreaker7 | 1:349234492517 | 17 | gpio.Pin = GPIO_PIN_1 | GPIO_PIN_0; |
codebreaker7 | 0:e0dc7a04fc82 | 18 | gpio.Pull = GPIO_PULLUP; |
codebreaker7 | 0:e0dc7a04fc82 | 19 | gpio.Speed = GPIO_SPEED_HIGH; |
codebreaker7 | 0:e0dc7a04fc82 | 20 | gpio.Alternate = GPIO_AF2_TIM2; |
codebreaker7 | 0:e0dc7a04fc82 | 21 | HAL_GPIO_Init(GPIOA, &gpio); |
codebreaker7 | 0:e0dc7a04fc82 | 22 | } |
codebreaker7 | 0:e0dc7a04fc82 | 23 | |
codebreaker7 | 0:e0dc7a04fc82 | 24 | void InitTimer(void) |
codebreaker7 | 0:e0dc7a04fc82 | 25 | { |
codebreaker7 | 0:e0dc7a04fc82 | 26 | timer.Instance = TIM2; |
codebreaker7 | 0:e0dc7a04fc82 | 27 | timer.Init.CounterMode = TIM_COUNTERMODE_UP; |
codebreaker7 | 0:e0dc7a04fc82 | 28 | timer.Init.ClockDivision = 0; |
codebreaker7 | 0:e0dc7a04fc82 | 29 | timer.Init.Period = 64000 - 1; |
codebreaker7 | 0:e0dc7a04fc82 | 30 | timer.Init.Prescaler = (uint32_t)(SystemCoreClock / 3200000) - 1; |
codebreaker7 | 0:e0dc7a04fc82 | 31 | HAL_TIM_Base_Init(&timer); |
codebreaker7 | 0:e0dc7a04fc82 | 32 | HAL_TIM_PWM_Init(&timer); |
codebreaker7 | 0:e0dc7a04fc82 | 33 | |
codebreaker7 | 0:e0dc7a04fc82 | 34 | octim.OCMode = TIM_OCMODE_PWM1; |
codebreaker7 | 0:e0dc7a04fc82 | 35 | octim.OCPolarity = TIM_OCPOLARITY_HIGH; |
codebreaker7 | 0:e0dc7a04fc82 | 36 | octim.OCFastMode = TIM_OCFAST_ENABLE; |
codebreaker7 | 0:e0dc7a04fc82 | 37 | octim.Pulse = pulse; |
codebreaker7 | 0:e0dc7a04fc82 | 38 | |
codebreaker7 | 1:349234492517 | 39 | HAL_TIM_PWM_ConfigChannel(&timer, &octim, TIM_CHANNEL_1); |
codebreaker7 | 1:349234492517 | 40 | HAL_TIM_PWM_Start(&timer, TIM_CHANNEL_1); |
codebreaker7 | 1:349234492517 | 41 | |
codebreaker7 | 0:e0dc7a04fc82 | 42 | HAL_TIM_PWM_ConfigChannel(&timer, &octim, TIM_CHANNEL_2); |
codebreaker7 | 0:e0dc7a04fc82 | 43 | HAL_TIM_PWM_Start(&timer, TIM_CHANNEL_2); |
codebreaker7 | 0:e0dc7a04fc82 | 44 | } |
codebreaker7 | 0:e0dc7a04fc82 | 45 | |
codebreaker7 | 0:e0dc7a04fc82 | 46 | int cur_pw = 500; |
codebreaker7 | 0:e0dc7a04fc82 | 47 | |
codebreaker7 | 0:e0dc7a04fc82 | 48 | int main() |
codebreaker7 | 0:e0dc7a04fc82 | 49 | { |
codebreaker7 | 0:e0dc7a04fc82 | 50 | HAL_Init(); |
codebreaker7 | 0:e0dc7a04fc82 | 51 | InitTimer(); |
codebreaker7 | 0:e0dc7a04fc82 | 52 | int dir = 0; |
codebreaker7 | 0:e0dc7a04fc82 | 53 | int count = 0; |
codebreaker7 | 0:e0dc7a04fc82 | 54 | |
codebreaker7 | 0:e0dc7a04fc82 | 55 | while(1) { |
codebreaker7 | 0:e0dc7a04fc82 | 56 | led = !led; |
codebreaker7 | 1:349234492517 | 57 | wait(0.05f); |
codebreaker7 | 0:e0dc7a04fc82 | 58 | if (dir) { |
codebreaker7 | 0:e0dc7a04fc82 | 59 | pulse -= 100; |
codebreaker7 | 0:e0dc7a04fc82 | 60 | count--; |
codebreaker7 | 0:e0dc7a04fc82 | 61 | if (count == 0) { |
codebreaker7 | 0:e0dc7a04fc82 | 62 | dir = 0; |
codebreaker7 | 0:e0dc7a04fc82 | 63 | } |
codebreaker7 | 0:e0dc7a04fc82 | 64 | } else { |
codebreaker7 | 0:e0dc7a04fc82 | 65 | pulse += 100; |
codebreaker7 | 0:e0dc7a04fc82 | 66 | count++; |
codebreaker7 | 0:e0dc7a04fc82 | 67 | if (count== 58) { |
codebreaker7 | 0:e0dc7a04fc82 | 68 | dir = 1; |
codebreaker7 | 0:e0dc7a04fc82 | 69 | } |
codebreaker7 | 0:e0dc7a04fc82 | 70 | } |
codebreaker7 | 0:e0dc7a04fc82 | 71 | octim.Pulse = pulse; |
codebreaker7 | 1:349234492517 | 72 | |
codebreaker7 | 1:349234492517 | 73 | HAL_TIM_PWM_ConfigChannel(&timer, &octim, TIM_CHANNEL_1); |
codebreaker7 | 1:349234492517 | 74 | HAL_TIM_PWM_Start(&timer, TIM_CHANNEL_1); |
codebreaker7 | 1:349234492517 | 75 | |
codebreaker7 | 0:e0dc7a04fc82 | 76 | HAL_TIM_PWM_ConfigChannel(&timer, &octim, TIM_CHANNEL_2); |
codebreaker7 | 0:e0dc7a04fc82 | 77 | HAL_TIM_PWM_Start(&timer, TIM_CHANNEL_2); |
codebreaker7 | 0:e0dc7a04fc82 | 78 | } |
codebreaker7 | 0:e0dc7a04fc82 | 79 | } |