forked from mkschreder/avr-ultimate-driver-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbldcsensored.h
142 lines (121 loc) · 6.44 KB
/
bldcsensored.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
bldc sensored driver 0x01
copyright (c) Davide Gironi, 2013
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#ifndef BLDCSENSORED_H_
#define BLDCSENSORED_H_
#include <stdio.h>
#include <avr/io.h>
//hall sensor input 1 (U)
#define BLDCSENSORED_HALLUDDR DDRB
#define BLDCSENSORED_HALLUPORT PORTB
#define BLDCSENSORED_HALLUPIN PINB
#define BLDCSENSORED_HALLUINPUT PB0
//hall sensor input 2 (V)
#define BLDCSENSORED_HALLVDDR DDRB
#define BLDCSENSORED_HALLVPIN PINB
#define BLDCSENSORED_HALLVPORT PORTB
#define BLDCSENSORED_HALLVINPUT PB1
//hall sensor input 3 (W)
#define BLDCSENSORED_HALLWDDR DDRB
#define BLDCSENSORED_HALLWPORT PORTB
#define BLDCSENSORED_HALLWPIN PINB
#define BLDCSENSORED_HALLWINPUT PB2
//fet driver port
#define BLDCSENSORED_FETUHDDR DDRD
#define BLDCSENSORED_FETUHPORT PORTD
#define BLDCSENSORED_FETUHINPUT PD2
#define BLDCSENSORED_FETULDDR DDRD
#define BLDCSENSORED_FETULPORT PORTD
#define BLDCSENSORED_FETULINPUT PD3
#define BLDCSENSORED_FETVHDDR DDRD
#define BLDCSENSORED_FETVHPORT PORTD
#define BLDCSENSORED_FETVHINPUT PD4
#define BLDCSENSORED_FETVLDDR DDRD
#define BLDCSENSORED_FETVLPORT PORTD
#define BLDCSENSORED_FETVLINPUT PD5
#define BLDCSENSORED_FETWHDDR DDRD
#define BLDCSENSORED_FETWHPORT PORTD
#define BLDCSENSORED_FETWHINPUT PD0
#define BLDCSENSORED_FETWLDDR DDRD
#define BLDCSENSORED_FETWLPORT PORTD
#define BLDCSENSORED_FETWLINPUT PD7
//fet driver port status
#define BLDCSENSORED_FETUL1 BLDCSENSORED_FETULPORT |= (1<<BLDCSENSORED_FETULINPUT)
#define BLDCSENSORED_FETUL0 BLDCSENSORED_FETULPORT &= ~(1<<BLDCSENSORED_FETULINPUT)
#define BLDCSENSORED_FETUH1 BLDCSENSORED_FETUHPORT |= (1<<BLDCSENSORED_FETUHINPUT)
#define BLDCSENSORED_FETUH0 BLDCSENSORED_FETUHPORT &= ~(1<<BLDCSENSORED_FETUHINPUT)
#define BLDCSENSORED_FETVL1 BLDCSENSORED_FETVLPORT |= (1<<BLDCSENSORED_FETVLINPUT)
#define BLDCSENSORED_FETVL0 BLDCSENSORED_FETVLPORT &= ~(1<<BLDCSENSORED_FETVLINPUT)
#define BLDCSENSORED_FETVH1 BLDCSENSORED_FETVHPORT |= (1<<BLDCSENSORED_FETVHINPUT)
#define BLDCSENSORED_FETVH0 BLDCSENSORED_FETVHPORT &= ~(1<<BLDCSENSORED_FETVHINPUT)
#define BLDCSENSORED_FETWL1 BLDCSENSORED_FETWLPORT |= (1<<BLDCSENSORED_FETWLINPUT)
#define BLDCSENSORED_FETWL0 BLDCSENSORED_FETWLPORT &= ~(1<<BLDCSENSORED_FETWLINPUT)
#define BLDCSENSORED_FETWH1 BLDCSENSORED_FETWHPORT |= (1<<BLDCSENSORED_FETWHINPUT)
#define BLDCSENSORED_FETWH0 BLDCSENSORED_FETWHPORT &= ~(1<<BLDCSENSORED_FETWHINPUT)
//running operations can also be implemented in 1 instruction, for example setting all a motor output on the sample port, a running instruction can be just a PORT setting
//running free spin
#define BLDCSENSORED_RUNS BLDCSENSORED_FETUH0;BLDCSENSORED_FETUL0; BLDCSENSORED_FETVH0;BLDCSENSORED_FETVL0; BLDCSENSORED_FETWH0;BLDCSENSORED_FETWL0; //00-00-00 (U-V-W)
//running break
#define BLDCSENSORED_RUNB BLDCSENSORED_FETUH0;BLDCSENSORED_FETUL1; BLDCSENSORED_FETVH0;BLDCSENSORED_FETVL1; BLDCSENSORED_FETWH0;BLDCSENSORED_FETWL1; //10-10-10 (U-V-W)
//running phases
#define BLDCSENSORED_RUN1 BLDCSENSORED_FETUH1;BLDCSENSORED_FETUL0; BLDCSENSORED_FETVH0;BLDCSENSORED_FETVL1; BLDCSENSORED_FETWH0;BLDCSENSORED_FETWL0; //10-01-00 (U-V-W)
#define BLDCSENSORED_RUN2 BLDCSENSORED_FETUH1;BLDCSENSORED_FETUL0; BLDCSENSORED_FETVH0;BLDCSENSORED_FETVL0; BLDCSENSORED_FETWH0;BLDCSENSORED_FETWL1; //10-00-01 (U-V-W)
#define BLDCSENSORED_RUN3 BLDCSENSORED_FETUH0;BLDCSENSORED_FETUL0; BLDCSENSORED_FETVH1;BLDCSENSORED_FETVL0; BLDCSENSORED_FETWH0;BLDCSENSORED_FETWL1; //00-10-01 (U-V-W)
#define BLDCSENSORED_RUN4 BLDCSENSORED_FETUH0;BLDCSENSORED_FETUL1; BLDCSENSORED_FETVH1;BLDCSENSORED_FETVL0; BLDCSENSORED_FETWH0;BLDCSENSORED_FETWL0; //01-10-00 (U-V-W)
#define BLDCSENSORED_RUN5 BLDCSENSORED_FETUH0;BLDCSENSORED_FETUL1; BLDCSENSORED_FETVH0;BLDCSENSORED_FETVL0; BLDCSENSORED_FETWH1;BLDCSENSORED_FETWL0; //01-00-10 (U-V-W)
#define BLDCSENSORED_RUN6 BLDCSENSORED_FETUH0;BLDCSENSORED_FETUL0; BLDCSENSORED_FETVH0;BLDCSENSORED_FETVL1; BLDCSENSORED_FETWH1;BLDCSENSORED_FETWL0; //00-01-10 (U-V-W)
//define positions for running phases depending on hall sensor/position CW
#define BLDCSENSORED_101CW BLDCSENSORED_RUN2
#define BLDCSENSORED_100CW BLDCSENSORED_RUN1
#define BLDCSENSORED_110CW BLDCSENSORED_RUN6
#define BLDCSENSORED_010CW BLDCSENSORED_RUN5
#define BLDCSENSORED_011CW BLDCSENSORED_RUN4
#define BLDCSENSORED_001CW BLDCSENSORED_RUN3
//define positions for running phases depending on hall sensor/position CCW
#define BLDCSENSORED_101CCW BLDCSENSORED_RUN1
#define BLDCSENSORED_100CCW BLDCSENSORED_RUN6
#define BLDCSENSORED_110CCW BLDCSENSORED_RUN5
#define BLDCSENSORED_010CCW BLDCSENSORED_RUN4
#define BLDCSENSORED_011CCW BLDCSENSORED_RUN3
#define BLDCSENSORED_001CCW BLDCSENSORED_RUN2
#define BLDCSENSORED_DEBUG 1 //enable or disable debug functions
//direction definitions
#define BLDCSENSORED_DIRECTIONCW 1
#define BLDCSENSORED_DIRECTIONCCW 2
//spin direction definitions
#define BLDCSENSORED_SPINNONE 0
#define BLDCSENSORED_SPINCW 1
#define BLDCSENSORED_SPINCCW 2
//emit startup sound
#define BLDCSENSORED_STARTUPSOUND 1
//startup delay, the startup delay for every step is given by startup delay array*BLDCSENSORED_STARTUPDELAYMULT
#define BLDCSENSORED_STARTUPDELAYS {200, 150, 100, 80, 70, 65, 60}
#define BLDCSENSORED_STARTUPCOMMUTATIONS 6 //number of startup commutations
#define BLDCSENSORED_STARTUPDELAYMULT 50 //startup delay multiplier
//stop steps
#define BLDCSENSORED_STOPSTEPS 500 //number of same position step to declare that motor not spinning
//setup speed
#define BLDCSENSORED_SPEEDMIN 600 //define the minimum speed (from 0 to 0xFFFF), number of cycle to stop stop the motor
//freq = FCPU/1+(prescaler*ICR1)
//note: ICR1 can not be small, because we have some commands to execute during the interrupt routine
//20000Hz = 8000000Hz/(1+(1*399))
#define TIMER1_ICR1 399
#define TIMER1_PRESCALER (1<<CS10) //prescaler
//functions
extern void bldcsensored_init(void);
extern uint8_t bldcsensored_getspindirection(void);
extern uint16_t bldcsensored_getspeed(void);
extern void bldcsensored_setspeed(uint16_t speed);
extern uint8_t bldcsensored_getdirection(void);
extern void bldcsensored_setdirection(uint8_t direction);
extern void bldcsensored_setstop(void);
extern void bldcsensored_setstart(void);
#if BLDCSENSORED_DEBUG == 1
extern volatile uint8_t bldcsensored_debugstate;
extern volatile uint8_t bldcsensored_debugposerror;
extern volatile uint8_t bldcsensored_debugposerrorprev;
#endif
#endif