Вы находитесь на странице: 1из 3

#include

#include
#include
#include
#include
#include
#include
#include
#include

<stdint.h>
<stdbool.h>
<stdio.h>
"inc/hw_types.h"
"inc/hw_memmap.h"
"driverlib/sysctl.h"
"driverlib/gpio.h"
"driverlib/interrupt.h"
"utils/uartstdio.h"

#include
#include
#include
#include

"ES_Configure.h"
"ES_Framework.h"
"ES_Port.h"
"termio.h"

#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "PWM10Tiva.h"
#include "MotorService.h"
#ifndef ALL_BITS
#define ALL_BITS (0xff<<2)
#endif
#define
#define
#define
#define
#define

MOTOR_TIME 1000
MOTOR_CLOSE_TIME 1500
MOTOR_INPUT_1 GPIO_PIN_2
MOTOR_INPUT_2 GPIO_PIN_3
CURTAIN_PIN_MOTOR GPIO_PIN_7

static uint8_t MyPriority;


static MSState_t CurrentState;
static void Motor(uint8_t input1, uint8_t input2);
static void MotorHardwareInit(void);
bool MotorInit (uint8_t Priority){
//
set local priority
MyPriority=Priority;
//
setup motor hardware
MotorHardwareInit();
//
set current state to ready
CurrentState=Motor_Waiting;
//
return es_init event
ES_Event ThisEvent;
ThisEvent.EventType=ES_INIT;
return ES_PostToService(MyPriority, ThisEvent);
}
bool PostMotor(ES_Event ThisEvent){
//
set up post function
return ES_PostToService(MyPriority, ThisEvent);
}
ES_Event RunMotor(ES_Event ThisEvent){
ES_Event ReturnEvent;
ReturnEvent.EventType=ES_NO_EVENT;
//
set next state to current state
MSState_t NextState=CurrentState;
switch (CurrentState) {
//
if the state is ready:
case (Motor_Waiting): {

//
if the event is ES_OPEN
if (ThisEvent.EventType==ES_OPEN) {
//
set the motor forward
Motor(1, 0);
//
set the motor timer
ES_Timer_InitTimer(MOTOR_TIMER, (uint16_t)MOTOR_TIME);
//
next state is moving
NextState=Motor_Moving;
}
//
if the event is ES_RESET
else if (ThisEvent.EventType==ES_RESET) {
// check the current curtain state
uint8_t CurtainState = ((HWREG(GPIO_PORTC_BASE+
(GPIO_O_DATA+ALL_BITS))&CURTAIN_PIN_MOTOR)==CURTAIN_PIN_MOTOR);
// if the curtain is not already closed
if (CurtainState==1) {
//
set the motor backward
Motor(0, 1);
//
set the motor timer
ES_Timer_InitTimer(MOTOR_TIMER,
(uint16_t)MOTOR_CLOSE_TIME);
//
next state is moving
NextState=Motor_Moving;
}
else {
NextState=Motor_Waiting;
}
}
break;
}
//
if the state is moving:
case (Motor_Moving): {
//
if the event is ES_TIMEOUT or curtain button down
if ((ThisEvent.EventType==ES_TIMEOUT)||
(ThisEvent.EventType==ES_BUTTON_DOWN)) {
//
set the motor stop
Motor(0, 0);
//
next state is ready
NextState=Motor_Waiting;
}
break;
}
}
//
set the current state to next state
CurrentState=NextState;
return ReturnEvent;
}
static void Motor(uint8_t input1, uint8_t input2) {
// if input 1 and input 2 are both off
if ((input1==0)&&(input2==0)) {
//
stop the motor
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))&=~(MOTOR_INPUT_1|
MOTOR_INPUT_2);
}
//
else if input 1 is high but input 2 is low
else if ((input1==1)&&(input2==0)) {
//
run the motor forward
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))&=~MOTOR_INPUT_2;
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))|=MOTOR_INPUT_1;
}
//
else if input 1 is low and input 2 is high
else if ((input1==0)&&(input2==1)) {

//
run the motor backwards
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))&=~MOTOR_INPUT_1;
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))|=MOTOR_INPUT_2;

}
static void MotorHardwareInit(void) {
uint16_t freq=100;
uint8_t group=3;
uint8_t dutyCycle=75;
uint8_t channel = 6;
//
set up pins for input 1 and 2 as digital output initially at 0
HWREG(SYSCTL_RCGCGPIO)|=SYSCTL_RCGCGPIO_R5;
while ((HWREG(SYSCTL_PRGPIO)&SYSCTL_PRGPIO_R5)!=SYSCTL_PRGPIO_R5) {}
HWREG(GPIO_PORTF_BASE+GPIO_O_DEN)|=(MOTOR_INPUT_1|MOTOR_INPUT_2);
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR)|=(MOTOR_INPUT_1|MOTOR_INPUT_2);
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))&=~(MOTOR_INPUT_1|
MOTOR_INPUT_2);
//
set pwm for enable pin
PWM_TIVA_SetFreq(freq, group);
PWM_TIVA_SetDuty(dutyCycle, channel);

// Initialize the curtain button input pin and set to digital input
HWREG(SYSCTL_RCGCGPIO)|=SYSCTL_RCGCGPIO_R2;
while ((HWREG(SYSCTL_PRGPIO)&SYSCTL_PRGPIO_R2)!=SYSCTL_PRGPIO_R2) {}
HWREG(GPIO_PORTC_BASE+GPIO_O_DEN)|=CURTAIN_PIN_MOTOR;
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR)&=~(CURTAIN_PIN_MOTOR);

Вам также может понравиться