-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·164 lines (124 loc) · 3.24 KB
/
main.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* main.cpp
*
* Copyright (c) 2021 Yulay Rakhmangulov.
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.hpp"
#include <stdnoreturn.h>
#include <avr/interrupt.h>
void setup();
static volatile uint8_t gPrevInput;
static volatile uint8_t gTwoOneMode;
static volatile uint8_t gTime;
static volatile uint8_t gSpeedSet;
/* Pin change interrupt handler */
ISR( PCINT0_vect )
{
uint8_t val = InEvents_Read();
uint8_t change = (val ^ gPrevInput);
gPrevInput = val;
if(change & TwoOne_Switch)
{
gTwoOneMode = (val & TwoOne_Switch);
if(!gTwoOneMode)
{
Wire_Out();
gTime = 0;
}
}
}
// Timer1 interrupt handler
ISR( TIMER1_COMPA_vect ) //each 1s
{
if(gTwoOneMode)
{
++gTime;
if(gTime < TwoOne_Out) //feeding wire out, welding 2 sec
{
Wire_Out();
}
else if(gTime < TwoOne_In) //retract wire to interrupt welding 0.5 sec
{
Wire_In();
}
else if(gTime < TwoOne_Cooloff) //feed wire back where we were before 0.5 sec
{
Wire_Out();
}
else //start new cycle
{
Wire_Out();
gTime = 0;
}
}
ADC_start();
}
// Timer1 interrupt handler
ISR( TIMER1_COMPB_vect ) //each 1s, phase shifted from TIMER1_COMPA_vect
{
ADC_start();
}
// ADC interrupt service routine
ISR( ADC_vect )
{
uint8_t val = ADCH;
if(gSpeedSet)
{
val >>= 1; //divide by 2
//sliding average
gSpeedSet = (gSpeedSet >> 1) + val;
}
else
{
gSpeedSet = val; //initialize
}
Motor_SetSpeed(gSpeedSet);
}
int main( void )
{
setup(); //setup ports
InEvents_Enable();
gTwoOneMode = (TwoOne_Switch & InEvents_Read());
Wire_Out();
Motor_On();
Motor_SetSpeed(MinSpeed);
for(;;)
{
//TODO sleep, keep Timers and ADC On. Allow pin change and timer interrupts to wake up
}
return 0;
}
inline void setup()
{
DDRB |= _BV(DDB2) | _BV(DDB1) | _BV(DDB0); /* set PBx to output */
//PB5 - Reset (can be reused as separate pin later)
// ADC Voltage Reference: internal 2.56V
// ADC High Speed Mode: Off
ADMUX = ( ADC_Left_Justified | ADC_InSpeed | ADC_VRef_2v56 );
// ADC Enabled
// ADC Clock frequency: prescaler 16, F_OSC/16 = 62.5KHz, conversion time 208us
ADCSRA |= _BV( ADEN ) | _BV( ADIE ) | _BV( ADPS2 );
//Timer0 step signal driver
TCCR0A = _BV(COM0A0) | _BV(WGM01); //PB0 output, Mode 2: CTC
TCCR0B = _BV(CS01); //prescaler 8
OCR0A = 0xff; //compare to TCNT0 to toggle output on PB0
//Timer1 CTC
TCCR1 = _BV(CTC1) | _BV(CS13) | _BV(CS12) | _BV(CS11) | _BV(CS10); //Mode 2; CTC prescaler 16384
TIMSK |= _BV(OCIE1A) | _BV(OCIE1B); //enable Compare Match A and B interrupts
OCR1A = Timer1_A; //to get interrupt A
OCR1B = Timer1_B; //to get interrupt B
OCR1C = Timer1_C; //CTC reload value
sei();
}