Subversion Repositories svnkaklik

Rev

Rev 409 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 409 Rev 410
1
 
1
 
2
/****************************************************************************
2
/****************************************************************************
3
 Title  :   C  test file for the SRF08 FUNCTIONS library (test_srf08.c)
3
 Title  :   C  test file for the SRF08 FUNCTIONS library (test_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
#define  F_CPU   3686400L
12
#define  F_CPU   3686400L
13
 
13
 
14
#include <io.h>
14
#include <io.h>
15
#include "lcd_io.h"
15
#include "lcd_io.h"
16
#include "srf08.h"
16
#include "srf08.h"
17
 
17
 
18
#define AVG_FLT_SAMPLES   3
18
#define AVG_FLT_SAMPLES   3
19
 
19
 
20
/*#################################################################################################*/
20
/*#################################################################################################*/
21
void delay(unsigned long us)
21
void delay(unsigned long us)
22
{
22
{
23
 
23
 
24
   while ( us ) { us--; }  /* 6 cpu cycles per loop */
24
   while ( us ) { us--; }  /* 6 cpu cycles per loop */
25
 
25
 
26
}
26
}
27
/*#################################################################################################*/
27
/*#################################################################################################*/
28
 
28
 
29
void main(void)
29
void main(void)
30
{
30
{
31
unsigned int range=0;
31
unsigned int range=0;
32
unsigned char counter1=0, gain=0;
32
unsigned char counter1=0, gain=0;
33
 
33
 
34
/* Delay is there to ensure that SRF08 is out of reset and operational */
34
/* Delay is there to ensure that SRF08 is out of reset and operational */
35
/* 
35
/* 
36
delay(1000000);
36
delay(1000000);
37
srf08_change_i2c_address(SRF08_UNIT_1); 
37
srf08_change_i2c_address(SRF08_UNIT_1); 
38
delay(100000);
38
delay(100000);
39
*/
39
*/
40
 
40
 
41
/*
41
/*
42
   By default SRF_UNIT_0 is selected so the below command shows that the change of i2c address
42
   By default SRF_UNIT_0 is selected so the below command shows that the change of i2c address
43
   by the above "srf08_change_i2c_address(SRF08_UNIT_1);" command was succesfull.
43
   by the above "srf08_change_i2c_address(SRF08_UNIT_1);" command was succesfull.
44
   All commands after this, refer to that unit only! 
44
   All commands after this, refer to that unit only! 
45
*/
45
*/
46
srf08_select_unit(SRF08_UNIT_1);   
46
srf08_select_unit(SRF08_UNIT_1);   
47
 
47
 
48
srf08_init();                      /* Only the selected SRF08 unit will be initialized!     */  
48
srf08_init();                      /* Only the selected SRF08 unit will be initialized!     */  
49
gain=SRF08_MAX_GAIN;
49
gain=SRF08_MAX_GAIN;
50
srf08_set_gain(gain);
50
srf08_set_gain(gain);
51
srf08_set_range(SRF08_MAX_RANGE);   /* Set range to 11008 mm */ 
51
srf08_set_range(SRF08_MAX_RANGE);   /* Set range to 11008 mm */ 
52
 
52
 
53
lcd_init();
53
lcd_init();
54
lcd_clrscr();
54
lcd_clrscr();
55
lcd_gotoxy(0,0); lcd_puts_P("RANGE (E1)  = ");
55
lcd_gotoxy(0,0); lcd_puts_P("RANGE (E1)  = ");
56
lcd_gotoxy(0,1); lcd_puts_P("RANGE (E2)  = ");
56
lcd_gotoxy(0,1); lcd_puts_P("RANGE (E2)  = ");
57
lcd_gotoxy(0,2); lcd_puts_P("RANGE (E5)  = ");
57
lcd_gotoxy(0,2); lcd_puts_P("RANGE (E5)  = ");
58
lcd_gotoxy(0,3); lcd_puts_P("LIGHT sensor= ");
58
lcd_gotoxy(0,3); lcd_puts_P("LIGHT sensor= ");
59
 
59
 
60
while(1)
60
while(1)
61
    {
61
    {
62
       
62
       
63
       /* AVERAGING FILTER */
63
       /* AVERAGING FILTER */
64
       for(counter1=0,range=0; counter1<AVG_FLT_SAMPLES; counter1++)
64
       for(counter1=0,range=0; counter1<AVG_FLT_SAMPLES; counter1++)
65
         {
65
         {
66
            range+=srf08_ping(SRF08_CENTIMETERS);
66
            range+=srf08_ping(SRF08_CENTIMETERS);
67
         } 
67
         } 
68
       range /= AVG_FLT_SAMPLES;
68
       range /= AVG_FLT_SAMPLES;
69
 
69
 
70
       /* AUTOMATIC GAIN CONTROL */
70
       /* AUTOMATIC GAIN CONTROL */
71
       if(srf08_read_register(SRF08_ECHO_5)!=0 )
71
       if(srf08_read_register(SRF08_ECHO_5)!=0 )
72
        { 
72
        { 
73
           if(gain>=5) { srf08_set_gain(gain-=5); } else { srf08_set_gain(gain=0); }  
73
           if(gain>=5) { srf08_set_gain(gain-=5); } else { srf08_set_gain(gain=0); }  
74
        } 
74
        } 
75
       else if(srf08_read_register(SRF08_ECHO_2)<=0 && gain!=31) { srf08_set_gain(++gain); } 
75
       else if(srf08_read_register(SRF08_ECHO_2)<=0 && gain!=31) { srf08_set_gain(++gain); } 
76
 
76
 
77
       /* DISPLAY TO LCD */
77
       /* DISPLAY TO LCD */
78
       lcd_gotoxy(14,0); lcd_puti(range,2);
78
       lcd_gotoxy(14,0); lcd_puti(range,2);
79
       lcd_gotoxy(14,1); lcd_puti(srf08_read_register(SRF08_ECHO_2), 2);
79
       lcd_gotoxy(14,1); lcd_puti(srf08_read_register(SRF08_ECHO_2), 2);
80
       lcd_gotoxy(14,2); lcd_puti(srf08_read_register(SRF08_ECHO_5), 2);
80
       lcd_gotoxy(14,2); lcd_puti(srf08_read_register(SRF08_ECHO_5), 2);
81
       lcd_gotoxy(14,3); lcd_puti(srf08_read_register(SRF08_LIGHT),  2);
81
       lcd_gotoxy(14,3); lcd_puti(srf08_read_register(SRF08_LIGHT),  2);
82
 
82
 
83
 
83
 
84
    }
84
    }
85
 
85
 
86
return;
86
return;
87
}
87
}
88
/*#################################################################################################*/
88
/*#################################################################################################*/
89
       
89
       
90
 
90