-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
234 lines (186 loc) · 5.46 KB
/
main.c
File metadata and controls
234 lines (186 loc) · 5.46 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* main.c
*
* Created: 4/4/2019 1:37:23 PM
* Author : Sterling
*/
#define F_CPU 16000000UL // CPU speed
#define BAUD 9600 // baud-rate
#define MYBURR F_CPU/16/BAUD - 1 // value to initialize for baud-rate
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
extern void ZERO();
extern void ONE();
// Initialize ports
void PORT_Init(void);
// Analog to Digital Converter initialization
void ADC_Init(void);
// Analog to Digital read-in
uint8_t ADC_read(uint8_t pin);
// Universal Asynchronous communication initialization
void USART_Init(unsigned int ubrr);
// Universal Asynchronous communication transmission
void USART_Transmit(unsigned char data);
// Universal Asynchronous communication receiving
unsigned char USART_Receive(void);
// Universal Asynchronous communication transmitting whole strings
void USART_String(char *word);
// Convert a value to a string
char *to_string(uint8_t number, char *buffer);
// Color will be shown on LED from axis values
void color_convert(uint8_t *axis_array);
// Convert readouts from axis to actual color
void color(uint8_t value);
// Delay function (no idea on length, just estimate)
void delay(void);
// initialize array for axis data and buffer
uint8_t axis_array[3] = {0, 0, 0};
char buffer[4] = "";
int main(void)
{
// initialize serial connection and analog to digital converter
PORT_Init();
USART_Init(MYBURR);
ADC_Init();
// Start timer using 1024 prescaler
TCCR0B = (1 << CS02);// | (1 << CS00);
// Set counter initial value to 0
TCNT0 = 0x00;
// Timer Interrupt Enabled (8-bit)
TIMSK0 = (1 << TOIE0);
// set global interrupt
sei();
while(1);
return 0;
}
// interrupt service routine
ISR(TIMER0_OVF_vect) {
// z-axis
axis_array[0] = ADC_read(0);
// y-axis
axis_array[1] = ADC_read(1);
// x-axis
axis_array[2] = ADC_read(2);
// transmit data to serial console
USART_String("\n\r");
USART_String(to_string(axis_array[2], buffer));
USART_Transmit(',');
USART_String(to_string(axis_array[1], buffer));
USART_Transmit(',');
USART_String(to_string(axis_array[0], buffer));
// run color conversion from sensor data
color_convert(axis_array);
}
// take each axis and convert it to a single color
void color_convert(uint8_t *axis_array) {
// x-axis (green)
color(*axis_array);
axis_array++;
// y-axis (red)
color(*axis_array);
axis_array++;
// z-axis (blue)
color(*axis_array);
}
// take an 8-bit number and call corresponding
// cycles for neopixel
void color(uint8_t value) {
uint8_t midpoint = 128;
// if no force, then eliminate the color completely
// (128 -> 0)
value = value % midpoint;
// dim the brightness of the neopixel
value &= 0xF0;
// for every bit, call a command
for(int i = 0; i < 8; i++) {
if(value & (1 << i)) {
ONE();
} else {
ZERO();
}
}
}
void PORT_Init(void) {
// PortA is input (acceleromter)
DDRA = 0x00;
// PORTB is output (neopixel)
DDRB = 0xFF;
}
// initialize the analog to digital converter from PORTA
void ADC_Init(void) {
// * Analog to Digital Converter Status Register A *
// Pre-scaler of 64 -> 16MHz/64 = 250KHz
// ADEN -> enable analog to digital conversion
// ADSC -> start the conversion from digital to analog
// ADIF -> interrupt flag
// ADIE -> ADC interrupt enable
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // 0x07 (pre-scaler 16M/128)
ADCSRA |= (1 << ADEN) | (1 << ADIE); // 0x8F
// * Multiplexer Selection Register *
// ADLAR -> ADC Left Adjust Result (high 8-bits)
// First three bits in ADC to be read (X,Y,Z)
ADMUX |= (1 << ADLAR); //0x20
}
// read from a single port and convert to a digital
// value from a signal value
uint8_t ADC_read(uint8_t pin) {
// axis x = 2
// axis y = 1
// axis z = 0
// select the corresponding pins from PORTA 0~7
pin &= 0x07; // restrict to pins 0-7 (error check)
ADMUX = (ADMUX & 0xE0) | pin; // clears the bottom 5 bits before selecting pin
// start analog to digital conversion
ADCSRA |= (1<<ADSC);
// wait for conversion to complete
// ADSC becomes ’0' again
// till then, run loop continuously
while(ADCSRA & (1<<ADSC)) {
continue;
}
delay();
// return value (high-bit, left-justified)
return (ADCH);
}
// initialize serial connection
void USART_Init(unsigned int ubrr) {
// Place corresponding value for baud-rate into UBRR0
UBRR0H = (unsigned char) (ubrr>>8);
UBRR0L = (unsigned char) (ubrr);
// Enable receiver and transmitter
UCSR0B = (1 << TXEN0) | (1 << TXEN0);
// Enable 8 data bits and 2 stop bits
UCSR0C = (1 << USBS0) | (3 << UCSZ00);
}
// receive single character from computer console
unsigned char USART_Receive(void) {
while ( !(UCSR0A & (1 << RXC0)) ) {
continue;
}
return UDRE0;
}
// transmit a single character
void USART_Transmit(unsigned char data) {
// while register is empty for char is empty
while( !(UCSR0A & (1 << UDRE0)) ) {
continue;
}
// return character
UDR0 = data;
}
// transmit a string rather than a single character
void USART_String(char *word) {
// iterate through string transmitting until string is empty
for(int i = 0; word[i] != '\0'; i++) {
USART_Transmit(word[i]);
}
}
// convert string using the utoa function
char *to_string(uint8_t number, char *buffer) {
// uses base-10
utoa(number, buffer, 10);
return buffer;
}
void delay(void) {
}