-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.c
194 lines (163 loc) · 4.54 KB
/
config.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
189
190
191
192
193
194
// contains functions for configuration
// and initialization
#include <p33FJ128MC802.h>
#include "config.h"
#include "main.h"
#include "motors.h"
#include "adc.h"
#include "delay_T4.h"
#include "decide.h"
int L_IND;
int R_IND;
unsigned int L_SEQ[4];
unsigned int R_SEQ[4];
int DELAY_COUNT; //counter for delay
int MAX_DELAY; // the max delay cycles
int RDY;// flag for when mouse is ready
int ST_COUNT; // counter for steps
int DIR; // direction
int ALG; // algorithm
// initialize the mouse
void init_all(void) {
// initialize global variables
init_globals();
// initialize clock
init_clock();
// configure I/O
config_io();
// initialize ADC
ADC_Init();
// configure interrupts
config_interrupts();
}
// initialize global variables
void init_globals(void) {
/* Initialize global variables */
// Indices
L_IND = 0;
R_IND = 0;
// Sequences
L_SEQ[0] = LS_0;
L_SEQ[1] = LS_1;
L_SEQ[2] = LS_2;
L_SEQ[3] = LS_3;
R_SEQ[0] = RS_0;
R_SEQ[1] = RS_1;
R_SEQ[2] = RS_2;
R_SEQ[3] = RS_3;
// Delay count
DELAY_COUNT = 0;
// Ready
RDY = 0;
// Step count
ST_COUNT = 0;
// Direction
DIR = FORWARD;
ALG = TREMAUX;
}
// configure clock
void init_clock(void) {
// In order to configure the device to operate at 40 MHz, configure the PLL prescaler,
// PLL postscaler, and PLL divisor
PLLFBD = 41; // M = PLLFBD + 2
CLKDIVbits.PLLPOST = 0; // N1 = 2
CLKDIVbits.PLLPRE = 0; // N2 = 2
__builtin_write_OSCCONH(0x01); // New oscillator FRC w/ PLL
__builtin_write_OSCCONL(0x01); // Enable clock switch as per dsPIC oscillator start-up
// guidelines. For more information, refer to
// Section 42. “Oscillator (Part IV)” (DS70307) in the
// “dsPIC33F Family Reference Manual”.
while(OSCCONbits.COSC != 0b001); // Wait for the new Oscillator to become FRC w/ PLL
while(OSCCONbits.LOCK != 1); // Wait for PLL to lock
}
// configure io
void config_io(void) {
TRISA=0b11;
TRISB=0x0003;
AD1PCFGL = 0xFFFF;
}
// configure interrupts
void config_interrupts(void) {
//Set interrupt
// INTCON = 0xC0; //Enable Global,Timer0 Overflow interrupt
INTCON1bits.NSTDIS = 1; // disable nested interrupts
// set 16 bit
// T1CONbits.RD16 = 1;
// set up timer 1 prescaler for 1:2
T1CONbits.TCKPS1 = 0;
T1CONbits.TCKPS0 = 1;
IPC0bits.T1IP2 = 1; // TMR 1 high priority
IPC0bits.T1IP1 = 1; // TMR 1 high priority
IPC0bits.T1IP0 = 1; // TMR 1 high priority
IEC0bits.T1IE = 1; // enable the timer 1 interrupt
IFS0bits.T1IF = 0; // turn off TMR1 flag
T1CONbits.TCS = 0; // internal clock
PR1=PR1_INIT; // max speed using prescale 01
// set 16 bit
// T2CONbits.RD16 = 1;
// set up timer 2 prescaler for 1:2
T2CONbits.TCKPS1 = 0;
T2CONbits.TCKPS0 = 1;
IPC1bits.T2IP2 = 1; // TMR 1 high priority
IPC1bits.T2IP1 = 1; // TMR 1 high priority
IPC1bits.T2IP0 = 1; // TMR 1 high priority
IEC0bits.T2IE = 1; // enable the timer 1 interrupt
IFS0bits.T2IF = 0; // turn off TMR1 flag
T2CONbits.TCS = 0; // internal clock
PR2=PR2_INIT; // max speed using prescale 01
// set 16 bit
// set up timer 4 prescaler for 1:256
T4CONbits.TCKPS1 = 1;
T4CONbits.TCKPS0 = 1;
IPC6bits.T4IP2 = 1; // TMR 4 high priority
IPC6bits.T4IP1 = 1; // TMR 4 high priority
IPC6bits.T4IP0 = 1; // TMR 4 high priority
IEC1bits.T4IE = 1; // enable the timer 4 interrupt
IFS1bits.T4IF = 0; // turn off TMR4 flag
T4CONbits.TCS = 0; // internal clock
DIR = STOP;
// start timers
T1CONbits.TON = 1; // turn on timer 1
T2CONbits.TON = 1; // turn on timer 2
// Wait for lineup signal
while(ADC_Sample(2) < 2000) {
if(ADC_Sample(0) > 2000) {
ALG = (ALG + 3) % 4;
DIR = FORWARD; // move forward for a while
// Use timer 4 to wait
delay_T4(2);
DIR = STOP; // stop
}
if(ADC_Sample(1) > 2000) {
ALG = (ALG + 5) % 4;
DIR = FORWARD; // move forward for a while
// Use timer 4 to wait
delay_T4(2);
DIR = STOP; // stop
}
}
// prevent inital drift
DIR = FORWARD; // move forward for a while
// Use timer 4 to wait
delay_T4(5);
DIR = STOP; // stop
// PR1=PR1_MAP; // reset to mapping speed
// PR2=PR2_MAP; // reset to mapping speed
//
// // Use timer 4 to wait
//
// Wait for go signal
while(ADC_Sample(2) < 2000);
// Use timer 4 to wait again
delay_T4(5);
}
// wait to start the timers
// Use timer 4 for delay/counting
void __attribute__((interrupt, no_auto_psv)) _T4Interrupt(void)
{
DELAY_COUNT ++;
if(DELAY_COUNT >= MAX_DELAY) { // delays 2.5s
RDY = 1;
}
IFS1bits.T4IF = 0; // turn off TMR4 flag
}