car_fmw: implementation of periodic forward and backward engine rotation

This commit is contained in:
Eugene Batalov
2016-07-05 22:16:58 +03:00
parent 5be1238427
commit 4ada683ad7
2 changed files with 69 additions and 5 deletions
+53
View File
@@ -0,0 +1,53 @@
#pragma once
#include "stm32f4_discovery.h"
#include "stm32f4xx_conf.h"
//#define CAR_ENGINE_ENABLE
#define CAR_ENGINE_GPIO_PORT_CLOCK RCC_AHB1Periph_GPIOB
#define CAR_ENGINE_GPIO_PORT GPIOB
#define CAR_ENGINE_ENABLE_PIN GPIO_Pin_4
#define CAR_ENGINE_DIRECTION_FORWARD_PIN GPIO_Pin_5
#define CAR_ENGINE_DIRECTION_BACKWARD_PIN GPIO_Pin_6
inline void engine_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(CAR_ENGINE_GPIO_PORT_CLOCK, ENABLE);
GPIO_InitStructure.GPIO_Pin = CAR_ENGINE_ENABLE_PIN
| CAR_ENGINE_DIRECTION_FORWARD_PIN
| CAR_ENGINE_DIRECTION_BACKWARD_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(CAR_ENGINE_GPIO_PORT, &GPIO_InitStructure);
}
inline void engine_stop(void)
{
GPIO_ResetBits(CAR_ENGINE_GPIO_PORT, 0
| CAR_ENGINE_DIRECTION_BACKWARD_PIN
| CAR_ENGINE_ENABLE_PIN
| CAR_ENGINE_DIRECTION_FORWARD_PIN);
}
#ifdef CAR_ENGINE_ENABLE
inline void engine_forward(void)
{
engine_stop();
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN);
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_DIRECTION_FORWARD_PIN);
}
inline void engine_backward(void)
{
engine_stop();
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_ENABLE_PIN);
GPIO_SetBits(CAR_ENGINE_GPIO_PORT, CAR_ENGINE_DIRECTION_BACKWARD_PIN);
}
#else
#define engine_forward(...);
#define engine_backward(...);
#endif // CAR_ENGINE_ENABLE
+16 -5
View File
@@ -1,4 +1,5 @@
#include "car_leds.h"
#include "car_engine.h"
#include "delay.h"
int main(void)
@@ -11,21 +12,31 @@ int main(void)
*/
leds_init();
engine_init();
const int DELAY = 0x3FFFFF;
while (1)
{
led_set(LED_GREEN, true);
Delay(0x3FFFFF);
Delay(DELAY);
engine_forward();
led_set(LED_ORANGE, true);
Delay(0x3FFFFF);
Delay(DELAY);
engine_stop();
led_set(LED_RED, true);
Delay(0x3FFFFF);
Delay(DELAY);
engine_backward();
led_set(LED_BLUE, true);
Delay(0x7FFFFF);
Delay(DELAY);
engine_stop();
led_set(LED_GREEN, false);
led_set(LED_ORANGE, false);
led_set(LED_RED, false);
led_set(LED_BLUE, false);
Delay(0xFFFFFF);
Delay(DELAY);
}
}