-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.c
50 lines (40 loc) · 1.56 KB
/
motor.c
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
/*****************************************************************************************
* Module: DC motor
* description: source file for DC motor drive
* file name:motor.c
* Author: Mustafa Mahmoud
*****************************************************************************************/
#include"gpio.h"
#include"common_macros.h"
#include"motor.h"
#include"pwm.h"
/*************************************************************************
*function definitions *
*************************************************************************/
void DcMotor_Init(void)
{
/* to set up the two pins of the motor as ouTput pins*/
GPIO_setUpPinDirection(PORT_MOTOR_INPUT,PIN1_MOTOR_INPUT_ID,PIN_OUTPUT);
GPIO_setUpPinDirection(PORT_MOTOR_INPUT,PIN2_MOTOR_INPUT_ID,PIN_OUTPUT);
/* to stop the motor at the start*/
GPIO_writePin(PORT_MOTOR_INPUT,PIN1_MOTOR_INPUT_ID,LOGIC_LOW);
GPIO_writePin(PORT_MOTOR_INPUT,PIN2_MOTOR_INPUT_ID,LOGIC_LOW);
}
void DcMotor_Rotate(DCMotor_State state,uint8 speed)
{
PWM_Timer0_Start(speed);
switch (state)
{
case STATE_CW:
GPIO_writePin(PORT_MOTOR_INPUT,PIN1_MOTOR_INPUT_ID,LOGIC_HIGH);
GPIO_writePin(PORT_MOTOR_INPUT,PIN2_MOTOR_INPUT_ID,LOGIC_LOW);
break;
case STATE_ACW:
GPIO_writePin(PORT_MOTOR_INPUT,PIN1_MOTOR_INPUT_ID,LOGIC_LOW);
GPIO_writePin(PORT_MOTOR_INPUT,PIN2_MOTOR_INPUT_ID,LOGIC_HIGH);
break;
case STATE_STOP:
GPIO_writePin(PORT_MOTOR_INPUT,PIN1_MOTOR_INPUT_ID,LOGIC_LOW);
GPIO_writePin(PORT_MOTOR_INPUT,PIN2_MOTOR_INPUT_ID,LOGIC_LOW);
}
}