forked from mkschreder/avr-ultimate-driver-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcmotor.c
83 lines (69 loc) · 1.83 KB
/
dcmotor.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
dcmotor lib 0x01
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include "dcmotor.h"
/*
* setup motor ports here
*/
void dcmotor_setup(void) {
uint8_t i = 0;
//motor 1
i=0;
dcmotor_array[i].ddr = &DDRD;
dcmotor_array[i].port = &PORTD;
dcmotor_array[i].pinA = PD2;
dcmotor_array[i].pinB = PD3;
//motor 2
i=1;
dcmotor_array[i].ddr = &DDRD;
dcmotor_array[i].port = &PORTD;
dcmotor_array[i].pinA = PD4;
dcmotor_array[i].pinB = PD5;
}
/*
* init a motor
*/
void dcmotor_init(void) {
dcmotor_setup();
//set ports
uint8_t i = 0;
for(i=0; i<DCMOTOR_MOTORNUM; i++) {
*dcmotor_array[i].ddr |= (1 << dcmotor_array[i].pinA); //output
*dcmotor_array[i].ddr |= (1 << dcmotor_array[i].pinB); //output
*dcmotor_array[i].port &= ~(1 << dcmotor_array[i].pinA); //off
*dcmotor_array[i].port &= ~(1 << dcmotor_array[i].pinB); //off
}
}
/*
* stop a motor
*/
void dcmotor_gostop(uint8_t motorid) {
if(motorid >= 0 && motorid < DCMOTOR_MOTORNUM) {
*dcmotor_array[motorid].port &= ~(1 << dcmotor_array[motorid].pinA);
*dcmotor_array[motorid].port &= ~(1 << dcmotor_array[motorid].pinB);
}
}
/*
* let a motor go forward
*/
void dcmotor_goforward(uint8_t motorid) {
if(motorid >= 0 && motorid < DCMOTOR_MOTORNUM) {
*dcmotor_array[motorid].port &= ~(1 << dcmotor_array[motorid].pinA);
*dcmotor_array[motorid].port |= (1 << dcmotor_array[motorid].pinB);
}
}
/*
* let a motor go backward
*/
void dcmotor_gobackward(uint8_t motorid) {
if(motorid >= 0 && motorid < DCMOTOR_MOTORNUM) {
*dcmotor_array[motorid].port |= (1 << dcmotor_array[motorid].pinA);
*dcmotor_array[motorid].port &= ~(1 << dcmotor_array[motorid].pinB);
}
}