Subversion Repositories svnkaklik

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
359 kaklik 1
 
2
/****************************************************************************
3
 Title  :   C  file for the SRF08 FUNCTIONS library (srf08.c)
4
 Author:    Chris efstathiou hendrix@otenet.gr
5
 Date:      13/Jul/2002
6
 Software:  AVR-GCC with AVR-AS
7
 Target:    any AVR device
8
 Comments:  This software is FREE.
9
 
10
*****************************************************************************/
11
 
12
#include <io.h>
13
#include <eeprom.h>
14
#include "i2c.h"
15
#include "srf08.h"
16
 
17
/* Global Variables */
18
static unsigned char address=SRF08_UNIT_0;
19
 
20
/*#################################################################################################*/
21
 
22
void srf08_init(void)
23
{
24
unsigned int  range=0;
25
        i2c_init();
26
        I2C_START_TX(address);
27
        i2c_transmit(0);                    
28
        i2c_transmit(0x51);
29
 
30
        do{
31
            i2c_start();
32
            range=i2c_transmit(address);               
33
            i2c_stop();
34
          }while(range != I2C_NO_ERROR);                               
35
 
36
 
37
return;
38
}
39
/*#################################################################################################*/
40
 
41
void srf08_set_gain(unsigned char gain)
42
{
43
    if(gain>31) { gain=31; }
44
    I2C_START_TX(address);         
45
    i2c_transmit(1);                               
46
    i2c_transmit(gain);
47
    i2c_stop();                            
48
 
49
return;
50
}
51
/*#################################################################################################*/
52
 
53
void srf08_set_range(unsigned int millimeters)
54
{
55
    millimeters= (millimeters/43); 
56
    if(millimeters > 0xff ) { millimeters=0xff; }
57
    I2C_START_TX(address);         
58
    i2c_transmit(2);                               
59
    i2c_transmit(millimeters);
60
    i2c_stop();                            
61
 
62
return;
63
}
64
/*#################################################################################################*/
65
 
66
unsigned int srf08_ping(unsigned char metric_unit)
67
{
68
     union i2c_union {
69
                              unsigned int  rx_word; 
70
                              unsigned char rx_byte[2];
71
                     } i2c;
72
 
73
 
74
        I2C_START_TX(address);
75
        i2c_transmit(0);                    
76
        i2c_transmit(metric_unit);
77
 
78
        do{
79
            i2c_start();
80
            i2c.rx_byte[0]=i2c_transmit(address);               
81
            i2c_stop();
82
          }while(i2c.rx_byte[0] != I2C_NO_ERROR);
83
 
84
 
85
        I2C_START_TX(address);
86
        i2c_transmit(SRF08_ECHO_1);
87
        I2C_START_RX(address);
88
        i2c.rx_byte[1]=i2c_receive(I2C_CONTINUE);  /* get high byte msb first */                         
89
        i2c.rx_byte[0]=i2c_receive(I2C_QUIT);      /* get low byte msb first  */                     
90
        i2c_stop();
91
 
92
return(i2c.rx_word);
93
}
94
/*#################################################################################################*/
95
 
96
unsigned int srf08_read_register(unsigned char srf08_register)
97
{
98
union i2c_union {
99
                   unsigned int  rx_word; 
100
                   unsigned char rx_byte[2];
101
                } i2c;
102
 
103
 
104
        I2C_START_TX(address);
105
        i2c_transmit(srf08_register);
106
        I2C_START_RX(address);
107
 
108
        /* get high byte msb first */ 
109
        if(srf08_register>=2) { i2c.rx_byte[1]=i2c_receive(I2C_CONTINUE); }                         
110
 
111
        /* get low byte msb first  */ 
112
        i2c.rx_byte[0]=i2c_receive(I2C_QUIT);                          
113
 
114
        i2c_stop();
115
 
116
return(i2c.rx_word);
117
}
118
/*#################################################################################################*/
119
 
120
void srf08_change_i2c_address(unsigned char new_i2c_address)
121
{
122
 
123
 
124
    /* Start the I2C address changing procedure */
125
    I2C_START_TX(address);         
126
    i2c_transmit(SRF08_COMMAND); 
127
    i2c_transmit(0XA0); 
128
    i2c_stop(); 
129
 
130
    I2C_START_TX(address);         
131
    i2c_transmit(SRF08_COMMAND); 
132
    i2c_transmit(0XAA);                                
133
    i2c_stop();
134
 
135
    I2C_START_TX(address);         
136
    i2c_transmit(SRF08_COMMAND); 
137
    i2c_transmit(0XA5);
138
    i2c_stop();
139
 
140
    I2C_START_TX(address);         
141
    i2c_transmit(SRF08_COMMAND); 
142
    i2c_transmit(new_i2c_address); 
143
    i2c_stop(); 
144
 
145
    /* Make the new i2c address the active one. */
146
    address=new_i2c_address;                           
147
 
148
return;
149
}
150
/*#################################################################################################*/
151
 
152
void srf08_select_unit(unsigned char srf08_address)
153
{
154
    /* New address validity check */
155
 
156
    if( (srf08_address<0xE0 || srf08_address>0XFE) && srf08_address != 0 )  { return; }
157
    if(srf08_address%2) { return; }
158
 
159
    /* Make the new i2c address the active one. */
160
    address=srf08_address;                           
161
 
162
return;
163
}
164
 
165
 
166
/*######################################################################################################*/
167
/*                                         T H E   E N D                                                */
168
/*######################################################################################################*/
169