forked from keyme/grbl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounters.c
117 lines (93 loc) · 3.18 KB
/
counters.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
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
/*
counters.c - code pertaining to encoders and other counting methods
Part of Grbl
Copyright (c) 2014 Adam Shelly
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system.h"
#include "counters.h"
uint32_t alignment_debounce_timer=0;
#define PROBE_DEBOUNCE_DELAY_MS 25
counters_t counters;
// Counters pin initialization routine.
void counters_init()
{
//encoders and feedback //TODO: move to new file
FDBK_DDR &= ~(FDBK_MASK); // Configure as input pins
FDBK_PORT |= FDBK_MASK; // Enable internal pull-up resistors. Normal high operation.
counters.state = FDBK_PIN&FDBK_MASK; //record initial state
counters_enable(0); //default to no encoder
}
void counters_enable(int enable)
{
if (enable) {
FDBK_PCMSK |= FDBK_MASK; // Enable specific pins of the Pin Change Interrupt
PCICR |= (1 << FDBK_INT); // Enable Pin Change Interrupt
}
else {
FDBK_PCMSK &= ~FDBK_MASK; // Disable specific pins of the Pin Change Interrupt
PCICR &= ~(1 << FDBK_INT); // Disable Pin Change Interrupt
}
}
// Resets the counts for an axis
void counters_reset(uint8_t axis)
{
counters.counts[axis]=0;
if (axis == Z_AXIS) { counters.idx=0; }
}
// Returns the counters pin state. Triggered = true. and counters state monitor.
count_t counters_get_count(uint8_t axis)
{
return counters.counts[axis];
}
int debounce(uint32_t* bounce_clock, uint16_t lockout_ms) {
uint32_t clock = masterclock;
//allow another reading if lockout has expired
if ( (uint32_t)(clock - *bounce_clock) >= lockout_ms ) {
*bounce_clock = clock;
return 1;
}
return 0;
}
ISR(FDBK_INT_vect) {
uint8_t state = FDBK_PIN&FDBK_MASK;
uint8_t change = (state^counters.state);
// Encoder was removed in rev 4 board
// Leaving this only as example code for how to read
// encoder sensors in the future, if we ever add them back
/*
int8_t dir=0;
//look for encoder change
if (change & ((1<<Z_ENC_CHA_BIT)|(1<<Z_ENC_CHB_BIT))) { //if a or b changed
counters.anew = (state>>Z_ENC_CHA_BIT)&1;
dir = counters.anew^counters.bold ? 1 : -1;
counters.bold = (state>>Z_ENC_CHB_BIT)&1;
counters.counts[Z_AXIS] += dir;
}
//count encoder indexes
if (change & (1<<Z_ENC_IDX_BIT)) { //idx changed
uint8_t idx_on = ((state>>Z_ENC_IDX_BIT)&1);
if (idx_on) {
counters.idx += dir;
}
}
*/
//count conveyor axis alignment pulses.
if (change & (1<<ALIGN_SENSE_BIT)) { //sensor changed
if (debounce(&alignment_debounce_timer, PROBE_DEBOUNCE_DELAY_MS)){
if (!(state & MAGAZINE_ALIGNMENT_MASK)) { //low is on.
counters.counts[C_AXIS]++;
}
}
}
counters.state = state;
}