-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRead Accelerometer.c
More file actions
151 lines (116 loc) · 3.22 KB
/
Copy pathRead Accelerometer.c
File metadata and controls
151 lines (116 loc) · 3.22 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
/*
* MotorControl.c
*
* Created: 4/9/2024 1:58:47 PM
* Author : Zwe
*/
#include <avr/io.h>
#include <stdlib.h>
#include "uart.h"
#include <avr/interrupt.h>
#define F_CPU 16000000UL // 16MHz clock
#define UART_BAUD_RATE 9600
#define UART_BAUD_PRESCALER (((F_CPU / (UART_BAUD_RATE * 16UL))) - 1)
#define time_step 0.02
double V_naught = 0;
double V_Total = 0;
double X_Total = 0;
void Intialize() {
cli();
//--------------------------------------------------Acclerometer--------------------------//
// Setup for ADC (10bit = 0-1023)
// Clear power reduction bit for ADC
PRR0 &= ~(1 << PRADC);
// Select Vref
ADMUX |= (1 << REFS0);
// Set the ADC clock div by 128
// 16M/128=125kHz
ADCSRA |= (1 << ADPS0);
ADCSRA |= (1 << ADPS1);
ADCSRA |= (1 << ADPS2);
// Select Channel ADC0 (pin C0)
ADMUX &= ~(1 << MUX0);
ADMUX &= ~(1 << MUX1);
ADMUX &= ~(1 << MUX2);
ADMUX &= ~(1 << MUX3);
ADCSRA |= (1 << ADATE); // Autotriggering of ADC
// Free running mode ADTS[2:0] = 000
ADCSRB &= ~(1 << ADTS0);
ADCSRB &= ~(1 << ADTS1);
ADCSRB &= ~(1 << ADTS2);
// Disable digital input buffer on ADC pin
DIDR0 |= (1 << ADC0D);
// Enable ADC
ADCSRA |= (1 << ADEN);
// Start conversion
ADCSRA |= (1 << ADSC);
//------------------------------------------------Motor PWM------------------------------------//
//DDRD |= (1<<DDD6);
//DDRD |= (1<<DDD5);
//
////phase correct mode
//TCCR0A |= (1<<WGM00);
//TCCR0A &= ~(1<<WGM01);
//TCCR0B |= (1<<WGM02);
//
////prescaler 1
//TCCR0B |= (1 << CS00);
//TCCR0B &= ~(1 << CS01);
//TCCR0B &= ~(1 << CS02);
//
////Clear compare match
//TCCR0A |= (1 << COM0A0);
//TCCR0A &= ~(1 << COM0A1);
//
//TCCR0A |= (1 << COM0B0);
//TCCR0A |= (1 << COM0B1);
//
//OCR0A = 40; // Sets frequency, 400kHz
//-------------------------------------Timer 1 OVF-------------------------------------------------//
////Set prescaler by 1024
//TCCR1B &= ~(1 << CS10);
//TCCR1B &= ~(1 << CS11);
//TCCR1B &= ~(1 << CS12);
//
////Normal
//TCCR1A &= ~(1 << WGM10);
//TCCR1A &= ~(1 << WGM11);
//TCCR1B &= ~(1 << WGM12);
////overflow
//TIMSK1 = (1 << TOIE1);
sei();
}
//ISR(TIMER1_OVF_vect) {
//
//
//}
void ResetVelocity(){
V_Total = 0;
}
void ResetDistance(){
X_Total = 0;
}
double Calculate_Velocity(double unit_ratio){
double acceleration = ADC * unit_ratio;
V_Total += acceleration*time_step;
return V_Total;
}
double Calculate_Distance(){
X_Total += ADC * ADC / 2.0 * time_step ;
return X_Total;
}
int main(void)
{
Intialize();
UART_init(UART_BAUD_PRESCALER);
while (1)
{
double currentadcread = Calculate_Velocity(0.01);
char rawadcstringbuffer[10]; // buffer to hold the converted number
itoa(currentadcread, rawadcstringbuffer, 10); // convert integer to string
UART_putstring("Velocity read:\t");
UART_putstring(rawadcstringbuffer);
UART_putstring( "\r\n");
//OCR0B = (0.039 * ADC); // Sets duty cycle,
}
}