-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.h
64 lines (53 loc) · 1.57 KB
/
motor.h
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
#include<stdio.h>
#include<stdint.h>
class Motor{
static const int MOTOR_RUN=0x01; //Run flag
static const int MOTOR_IDLE=0x00; //Idle flag
private:
volatile uint8_t* Port2DDR(volatile uint8_t *port){return port-1;}
volatile uint8_t *m_pport_a, *m_pport_b;
uint8_t m_dq_a,m_dq_b;
uint8_t m_status;
public:
void Init(volatile uint8_t *m_pport_pos,uint8_t m_dq_pos,
volatile uint8_t *m_port_neg,uint8_t m_dq_neg);
void start();
void stop();
bool is_running();
};
//Class initialization
void Motor::Init(volatile uint8_t *m_port_pos, uint8_t m_dq_pos,
volatile uint8_t *m_port_neg,uint8_t m_dq_neg)
{
//save globals
m_pport_a=m_port_pos;
m_dq_a=m_dq_pos;
m_pport_b=m_port_neg;
m_dq_b=m_dq_neg;
//init data and clk ports
*m_pport_a &= ~(1<<m_dq_a); //set pins to LOW
*m_pport_b &= ~(1<<m_dq_b); //set pins to LOW
*Port2DDR(m_pport_a) |= (1<<m_dq_a); //Enable pins as outputs
*Port2DDR(m_pport_b) |= (1<<m_dq_b); //Enable pins as outputs
m_status=MOTOR_IDLE;
}
//Motor start
void Motor::start()
{
*m_pport_a |= (1<<m_dq_a); //set A high
*m_pport_b &= ~(1<<m_dq_b); //set B low
m_status=MOTOR_RUN;
}
//Motor stop
void Motor::stop()
{
*m_pport_a &= ~(1<<m_dq_a); //set A low
*m_pport_b &= ~(1<<m_dq_b); //set B low
m_status=MOTOR_IDLE;
}
//status of motor
bool Motor::is_running()
{
if(m_status==MOTOR_RUN) return true;
return false;
}