Subversion Repositories svnkaklik

Rev

Rev 359 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log

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