View difference between Paste ID: 8bF1KnZA and bAnP8KVA
SHOW: | | - or go back to the newest paste.
1
/* compass_robot - an application for the Pololu Orangutan SVP
2
 *
3
 * This application uses the Pololu AVR C/C++ Library.  For help, see:
4
 * -User's guide: http://www.pololu.com/docs/0J20
5
 * -Command reference: http://www.pololu.com/docs/0J18
6
 *
7
 * Created: 5/13/2012 6:00:02 PM
8
 *  Author: Dave
9
 */
10
11
#define F_CPU 20000000UL
12
#include <pololu/orangutan.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include "i2cmaster.h"
16
#define COMPASS 0x3C
17
18
int main()
19
{
20
	play_from_program_space(PSTR(">g32>>c32"));  // Play welcoming notes.
21
	
22
	lcd_goto_xy(0,0);
23
	print("i2c init");
24
	i2c_init();
25
	i2c_start_wait(COMPASS + I2C_WRITE);
26
	i2c_write(0x00);
27
	i2c_write(0x70);
28
29
	i2c_start_wait(COMPASS + I2C_WRITE);
30
	i2c_write(0x01);
31
	i2c_write(0xA0);	
32
33
	i2c_start_wait(COMPASS + I2C_WRITE);
34
	i2c_write(0x02);
35
	i2c_write(0x00);
36
	lcd_goto_xy(0, 0);
37
	print("i2c init done");
38
	delay_ms(1000);
39
40
   	int buffer[6];
41
   	int x, y, z = 0;
42
	short i;
43
	
44-
    	x = (buffer[0] << 8) | buffer[1];
44+
45-
    	z = (buffer[2] << 8) | buffer[3];
45+
46-
    	y = (buffer[4] << 8) | buffer[5];
46+
47
		i2c_start(COMPASS + I2C_READ);
48
		i2c_write(0x06);
49
		
50
		for (i = 0; i < 6; i++) {
51
			buffer[i] = i2c_readNak();
52
		}
53
54
		x = (buffer[0] << 8) | buffer[1];
55
		z = (buffer[2] << 8) | buffer[3];
56
		y = (buffer[4] << 8) | buffer[5];
57
58
		lcd_goto_xy(0, 1);
59
		sprintf(msg, "%d %d %d", x, y, z);
60
		print(msg);
61
		
62
		delay_ms(1000);
63
		lcd_goto_xy(0, 1);
64
		print("                ");
65
		delay_ms(200);
66
	}
67
}