Ich habe eine simple Testroutine zum Ansteuern eines externen I2C
Treiberbausteines erstellt. Leider reagiert der Baustein überhaupt nicht
auf die Ansteuerung...
Ich habe die Kommunikation auf dem Bus mal aufgezeichnet und angehängt.
1 | #define ADDRESS 0x20
|
2 |
|
3 | void Init_TWI (void)
|
4 | {
|
5 | TWBR = 72; //set Bus speed 100kHz @16Mhz MCKL
|
6 | }
|
7 |
|
8 | void TWI_send_start (void)
|
9 | {
|
10 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); //Send START condition
|
11 | while (!(TWCR & (1<<TWINT))); //Wait for TWINT Flag set.
|
12 | }
|
13 |
|
14 | void TWI_send_add_rw (unsigned char address, unsigned char rw)
|
15 | {
|
16 | unsigned char addr_byte = 0;
|
17 |
|
18 | addr_byte = address << 1; //shift address one bit left
|
19 | addr_byte |= rw; //set RW-Bit, if necessary
|
20 |
|
21 | TWDR = addr_byte;
|
22 | TWCR = (1<<TWINT) | (1<<TWEN); //send content of TWDR
|
23 | while (!(TWCR & (1<<TWINT))); //wait, until address has been sent
|
24 | }
|
25 |
|
26 | unsigned char TWI_read_byte (void)
|
27 | {
|
28 | TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
29 | while (!(TWCR & (1<<TWINT))); //wait, until byte has been received
|
30 | return TWDR;
|
31 | }
|
32 |
|
33 | void TWI_send_byte (unsigned char byte)
|
34 | {
|
35 | TWDR = byte;
|
36 |
|
37 | TWCR = (1<<TWINT) | (1<<TWEN); //send content of TWDR
|
38 | while (!(TWCR & (1<<TWINT))); //wait, until byte has been sent
|
39 | }
|
40 |
|
41 | void TWI_send_stop (void)
|
42 | {
|
43 | TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);
|
44 | delay_ms(1); //wait a bit
|
45 | }
|
46 |
|
47 | int main(void) {
|
48 |
|
49 | Init_TWI();
|
50 |
|
51 | while (1) {
|
52 |
|
53 |
|
54 | TWI_send_start ();
|
55 |
|
56 | TWI_send_add_rw (ADDRESS, 0);
|
57 |
|
58 | TWI_send_byte (0x1);
|
59 | TWI_send_byte (0x0);
|
60 | TWI_send_stop ();
|
61 | USART_transmit(input);
|
62 |
|
63 |
|
64 | delay_ms(2000);
|
65 |
|
66 | } //while
|
67 |
|
68 |
|
69 | } //main
|