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