-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgygyro.h
205 lines (146 loc) · 6.15 KB
/
gygyro.h
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
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd.c"
#define SLA_W 0xD2 // Write address for DS1307 selection for writing
#define SLA_R 0xD3
#define MAX_BUFFER_SIZE 16
signed int X_Data,Y_Data,Z_Data=0;
float Displacement;
signed int X_Disp,Y_Disp,Z_Disp=0;
void lcd_port_config(void) //configure all the lcd i/0 ports
{
DDRC = DDRC | 0xF7;
PORTC = PORTC & 0x80;
}
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0H = 0x00; //set baud rate hi
UBRR0L = 0x07; //set baud rate lo
UCSR0B = 0x98;
}
unsigned char chartobcd(unsigned char n) // This function is used to convert the characters to Bcd
{
return ((n / 10) << 4) | (n % 10);
}
void twi_init(void)
{
TWCR = 0x00; //disable twi
TWBR = 0x10; //set bit rate
TWSR = 0x00; //set prescale
TWAR = 0x00; //set slave address
TWCR = 0x04; //enable twi
}
void write_byte(unsigned char data_out,unsigned char address)
{
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // send START condition
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWDR = SLA_W; // load SLA_W into TWDR Register
TWCR = (1<<TWINT) | (0<<TWSTA) | (1<<TWEN); // clear TWINT flag to start tramnsmission of slave address
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWDR = address; // send address of register byte want to access register
TWCR = (1<<TWINT) | (1<<TWEN); // clear TWINT flag to start tramnsmission of address
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWDR = data_out;chartobcd(data_out); // convert the character to equivalent BCD value and load into TWDR
TWCR = (1<<TWINT) | (1<<TWEN); // clear TWINT flag to start transmission of data byte
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN); // send STOP condition
}
unsigned char read_byte(unsigned char address)
{
unsigned char acc_recv_data;
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // send START condition
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(100);
TWDR = SLA_W; // load SLA_W into TWDR Register
TWCR = (1<<TWINT) | (1<<TWEN); // clear TWINT flag to start transmission of slave address
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWDR = address; // send address of register byte want to access register
TWCR = (1<<TWINT) | (1<<TWEN); // clear TWINT flag to start transmission of slave address
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // send RESTART condition
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWDR = SLA_R; // load SLA_R into TWDR Register
TWCR = (1<<TWINT) | (0<<TWSTA) | (1<<TWEN); // clear TWINT flag to start transmission of slave address
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
TWCR = (1<<TWINT) | (1<<TWEN); // clear TWINT flag to read the addressed register
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
acc_recv_data = TWDR;
_delay_ms(10);
TWDR = 00; // lOad the NO-ACK value to TWDR register
TWCR = (1<<TWINT) | (1<<TWEN); // clear TWINT flag to start transmission of NO_ACK signal
while(!(TWCR & (1<<TWINT))); // wait for TWINT Flag set
_delay_ms(10);
return(acc_recv_data); // return the read value to called function
}
void init_devices()
{
cli(); // disable all interrupts
lcd_port_config(); // configure the LCD port
twi_init(); // configurE the I2cC, i.e TWI module
uart0_init();
sei(); // re-enable interrupts
//all peripherals are now initialized
}
int main(void)
{
char xl[5],xh[5],yl=0,yh=0,zl=0,zh=0,x1,x2,y1,y2,z1,z2;
int sign, x_dec,xdisp,xpre;
int raw_x[5],raw_y=0,raw_z=0,n=0;
float xrate[5],yrate,zrate;
cli();
sei();
lcd_port_config();
lcd_init();
init_devices();
lcd_init(); // initialize the LCD with its commands
write_byte(0x5F,0x2E);
write_byte(0x10,0x23);
write_byte(0x8F,0x20); // normal mode and enable all x y z axis
// write_byte(0x40,0x24);
l1: while(1)
{
_delay_ms(500);
lcd_wr_command(0x01); //clear lcd screen
X_Data = read_byte(0x28); //receive 8bit value from 'x low' register
UDR0 = read_byte(0x29); //transmit 8bit value of 'x high' register to usart3
X_Data|= (signed short int)read_byte(0x29) << 8; //convert the two 8bit values to one 16 bit data
Y_Data = read_byte(0x2a); //receive 8bit value from 'y low' register
//UDR0=Y_Data;
Y_Data|= (signed short int)read_byte(0x2b)<< 8; //convert the two 8bit values 'y high and ylow' to one 16 bit data
Z_Data = read_byte(0x2c); //receive 8bit value from 'z low' register
//UDR0=Z_Data;
Z_Data|= (signed short int)read_byte(0x2d) << 8;//convert the two 8bit values 'z high and zlow' to one 16 bit data
// this is a small try to calibrate the gyroscope coordinate values which turns out to be fuzzy.
// 57.0,0.01, and 10 are all constants obtained by trial and error method
Displacement = ((float)X_Data / 57.0) * 0.01 * 10.0;
X_Data = (signed short int)Displacement;
X_Disp=X_Data;
lcd_cursor(1,1);
lcd_string("X = ");
lcd_print(1,4,X_Disp,3);
Displacement = ((float)Y_Data / 57.0) * 0.01 * 10.0;
Y_Data = (signed short int)Displacement;
Y_Disp=Y_Data;
lcd_cursor(1,7);
lcd_string(" y = ");
lcd_print(1,11,Y_Disp,3);
Displacement = ((float)Z_Data / 57.0) * 0.01 * 10.0;
Z_Data = (signed short int)Displacement;
Z_Disp=Z_Data;
lcd_cursor(2,1);
lcd_string("z = ");
lcd_print(2,4,Z_Disp,3);
}
}