1 | #include "i2cmaster.h"
|
2 | #include "twimaster.c"
|
3 | #include <avr/io.h>
|
4 | #include <util/delay.h>
|
5 | #define SDA 1
|
6 | #define SCL 0
|
7 | #define SDA_PORT PORTD // SDA Port E
|
8 | #define SCL_PORT PORTD // SCL Port E
|
9 | #define US_address 0xE0
|
10 | #define Start_US 0x51
|
11 |
|
12 | int main (void)
|
13 |
|
14 | {
|
15 | uint8_t distance_low;
|
16 | uint8_t distance_high;
|
17 | uint16_t distance;
|
18 | DDRB = 0xff;
|
19 | unsigned char busy;
|
20 | unsigned int i;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | while (1)
|
27 |
|
28 | {
|
29 | // start execution of program
|
30 |
|
31 | i2c_init(); // initialize I2C serial communication
|
32 |
|
33 | i2c_start(US_address + I2C_WRITE);
|
34 | i2c_write(0x00);
|
35 |
|
36 | // address I2C device ultrasonic sensor with write access
|
37 |
|
38 | i2c_write(Start_US); //start ultrasonic measurement
|
39 |
|
40 | i2c_stop(); // release I2C bus
|
41 |
|
42 | for(i=0; i<13; i++) {
|
43 | _delay_ms(5);
|
44 | }
|
45 | busy = i2c_start(US_address + I2C_READ);
|
46 |
|
47 | // address I2C device ultrasonic sensor with read access
|
48 |
|
49 | if (busy == 0) //vorher == 0
|
50 |
|
51 | {
|
52 | distance_low = i2c_readAck(); // read one byte
|
53 | distance_high = i2c_readNak();
|
54 | distance = (distance_low*256)+distance_high;
|
55 | //i2c_stop(); // release I2C bus
|
56 |
|
57 |
|
58 |
|
59 | //i2c_start(US_address + I2C_WRITE);
|
60 | //i2c_write(0x00);
|
61 | // address I2C device ultrasonic sensor with write access
|
62 |
|
63 | //i2c_write(Start_US); //start new ultrasonic measurement
|
64 | //i2c_stop(); // release I2C bus
|
65 | }
|
66 |
|
67 | else
|
68 |
|
69 | {
|
70 | i2c_stop(); // release I2C bus
|
71 | }
|
72 |
|
73 |
|
74 | PORTB = distance;
|
75 |
|
76 | }
|
77 |
|
78 | return 0;
|
79 | }
|