Subversion Repositories svnkaklik

Rev

Rev 358 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log

Rev 358 Rev 385
Line 1... Line 1...
1
/* compass.c
1
/* compass.c
2
 *
2
 *
3
 * Software to read from the CMPS03 magnetic compass. 
3
 * Software to read from the CMPS03 magnetic compass. 
4
 *
4
 *
5
 * By Josh Marshall, joshua dot marshall at studentmail dot newcastle dot edu dot au
-
 
6
 * October 15, 2004.
-
 
7
 *
-
 
8
 * Adapted from code for the SP03 speech synthesiser 
-
 
9
 * By Bram Stolk, b.stolk at chello.nl
-
 
10
 *
-
 
11
 * Bram Stolk's notes:
-
 
12
 * In linux, you can do I2C stuff in user-space, if you enable
-
 
13
 * the device-interface to i2c. You can use plain read() and 
-
 
14
 * write() calls on your /dev/i2c-0 file.
-
 
15
 *
-
 
16
 * NOTE: Check if you have an i2c adapter active by doing:
-
 
17
 * cat /proc/bus/i2c
-
 
18
 * If this comes up empty, then you have no adapters. If the
-
 
19
 * file does not exist at all, you probably lack i2c kernel
-
 
20
 * support alltogether.
-
 
21
 */
-
 
22
 
5
 
23
#include <errno.h>
6
#include <errno.h>
24
#include <string.h>
7
#include <string.h>
25
#include <stdio.h>
8
#include <stdio.h>
26
#include <stdlib.h>
9
#include <stdlib.h>
Line 31... Line 14...
31
/* Note that the documentation for the compass states its address as 0xC0.
14
/* Note that the documentation for the compass states its address as 0xC0.
32
 * However, this includes the low bit which specifies read or write.
15
 * However, this includes the low bit which specifies read or write.
33
 * Linux i2c does not include this bit in this address, so the actual
16
 * Linux i2c does not include this bit in this address, so the actual
34
 * address is 0xC0 shifted down, 0x60.
17
 * address is 0xC0 shifted down, 0x60.
35
 */
18
 */
-
 
19
 
36
#define CMPS03_ADDR	0x60
20
#define CMPS_Addr	(0xC0>>1)
37
#define I2C_SLAVE	0x0B	/* Change slave address	
21
#define I2C_SLAVE	0x0B	/* Change slave address	
38
 
22
 
39
/* The important registers on the compass. Internal/test registers omitted. */
23
/* The important registers on the compass. Internal/test registers omitted. */
40
#define CMPS03_SOFTWARE_REVISION 0x0
24
#define CMPS03_SOFTWARE_REVISION 0x0
41
#define CMPS03_BEARING_BYTE 0x1
25
#define CMPS03_BEARING_BYTE 0x1
42
#define CMPS03_BEARING_WORD_HIGH 0x2
26
#define CMPS03_BEARING_WORD_HIGH 0x2
43
#define CMPS03_BEARING_WORD_LOW 0x3
27
#define CMPS03_BEARING_WORD_LOW 0x3
44
#define CMPS03_CALIBRATE_CMD 0xF
28
#define CMPS03_CALIBRATE_CMD 0xF
45
 
29
 
-
 
30
void I2C_addr (int Addr)  // vybere adresu cidla se kterym se bude komunikovat
-
 
31
{
-
 
32
    if (ioctl(file, I2C_SLAVE, Addr) == -1)
-
 
33
    {
-
 
34
        fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
-
 
35
        exit(-5);
-
 
36
    }
-
 
37
}
-
 
38
 
-
 
39
int i2c_init()   // zinicializuje i2c
-
 
40
{
-
 
41
	file = open("/dev/i2c-0", O_RDWR);
-
 
42
	if (file < 0)
-
 
43
	{
-
 
44
		cerr << "Could not open /dev/i2c-0." << endl;
-
 
45
		return -1;
-
 
46
	}
-
 
47
	return 0;
-
 
48
}
-
 
49
 
-
 
50
unsigned char read_azimut_mag()  // precte azimut z kompasu
-
 
51
{
-
 
52
    char Buf[3];      // promena pro manipulaci s i2c
-
 
53
 
-
 
54
    I2C_addr(CMPS_Addr);
-
 
55
    Buf[0]=1;
-
 
56
    write(file,Buf,1);
-
 
57
    read(file, Buf,1);
-
 
58
    return (Buf[0]-SEVER);
-
 
59
}
-
 
60
 
-
 
61
void calib()  // kalibrace kompasu
-
 
62
{
-
 
63
    char Buf[3];      // promena pro manipulaci s i2c
-
 
64
 
-
 
65
    I2C_addr(CMPS_Addr);
-
 
66
    Buf[0]=15;
-
 
67
    Buf[1]=0xFF;
-
 
68
    write(file,Buf,2);
-
 
69
}
-
 
70
 
46
int main(int argc, char *argv[]) {
71
int main(int argc, char *argv[]) 
-
 
72
{
47
   char *end;
73
   char *end;
48
   int res,file;
74
   int res,file;
49
   int error;
75
   int error;
50
   char filename[20] ;
76
   char filename[20] ;
51
   long funcs;
77
   long funcs;