-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRbotMain.c
188 lines (162 loc) · 3.58 KB
/
IRbotMain.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* IRbotMain.c
*
* Created: 12/2/2012 2:54:22 PM
* Author: Nukesforbreakfast
*/
#include "IRbotStates.h"
/*
* Global Variables go here
*/
volatile int turn = 0;
volatile int stateVar = 0; //used to switch between states
volatile int degreeVar = 0; //used for seeing which degree the servo is at.
volatile int degreeSideVar = 0; //used for determining left or right, 0 = left, 1 = right
volatile int accum = 0; //used for LED's
/*
* ISR to handle F0 CCA
* used to control sending of 1.2ms on/off LED signal
*/
ISR(TCF0_CCA_vect)
{
if(accum == 24)
{
TCF0_CTRLA = TC_CLKSEL_OFF_gc;
TCF1_CTRLA = TC_CLKSEL_DIV64_gc;
PORTF_OUT &= ~0x01;
accum = 0;
}
else
{
++accum;
}
}
/*
* ISR to handle TCF1 overflow
* used to generate 1.2ms on/off LED signal
*/
ISR(TCF1_OVF_vect)
{
TCF1_CTRLA = TC_CLKSEL_OFF_gc;
TCF0_CTRLA = TC_CLKSEL_DIV64_gc;
}
/*
* TCE0_CCA overflow interrupt vector
* used to swivel the servo back and forth
*/
ISR(TCE0_OVF_vect)
{
switch(stateVar)
{
case 0: //we are in the scan state
if(turn) //if its true that we need to turn back the other way
{
if(!(TCE0_CCA <= 350)) //make sure we aren't going under the minimum value
{
TCE0_CCA -= 5;
PORTH_OUT = TCE0_CCA/10;
}
else //if we are going under the minimum value we need to turn the other way
{
turn = 0; //false
}
}
else //else we are still turning original direction
{
if(!(TCE0_CCA >= 1150)) //make sure we aren't going over the maximum value
{
TCE0_CCA += 5;
PORTH_OUT = TCE0_CCA/10;
}
else //if we are going over the maximum value we need to turn the other way.
{
turn = 1; //true
}
}
break;
case 1: //we are in the acquire state
break;
default:
break;
}
}
/*
* Interrupt for handling TCC1 capture complete event
* If we get a pulse we need to stop everything and attempt
* to acquire the signal
*/
ISR(TCC1_CCA_vect)
{
switch(stateVar)
{
case 0: //we are in scanState
//we first need to stop the servo where it is and hold its position
//do this by turning off the overflow interrupt for TCE0
TCE0_INTCTRLA = 0x00; //all interrupts off
//need to signal to the scan function that we should change state here
stateVar = 1;
break;
case 1: //we are in acquireState
break;
default:
break;
}
}
/*
* Handle pushbutton 0 presses to reset the machine states
*/
ISR(PORTJ_INT0_vect)
{
switch(stateVar)
{
case 0: //we are in scanState
break;
case 1: //we are in acquireState
stateVar = 0; //reset to scanState
break;
default:
break;
}
PORTH_OUT = 0x01;
}
void main(void)
{
unsigned long sClk, pClk;
cli(); //
SetSystemClock(CLK_SCLKSEL_RC32M_gc, CLK_PSADIV_1_gc,
CLK_PSBCDIV_1_1_gc);
GetSystemClocks(&sClk, &pClk);
/*
* Programmable interrupt controller configuration
*/
PMIC_CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm; //enable all levels of interrupts
/*
*Port H configuration
*/
PORTH_DIR = 0xFF; //set all pins to output so we can output onto the LED's
/*
* Port Q configuration for accessing Serial Via USB using USARTD0, analog stuff on PORTB, etc...
*/
//PORTQ_DIR = 0x0F; //port q lower 3 bits control access to usb and other stuff so get access with these two lines
//PORTQ_OUT = 0x05; //if using port F make this hex 5.
/*
* Call setupTransmit here to turn on LED transmit signal going out on portD pin 0
*/
setupTransmit();
sei();
while(1)
{
switch(stateVar)
{
case 0:
scanState();
break;
case 1:
PORTH_OUT = 0x05;
acquireState();
break;
default:
break;
}
}
}