1 | //--------------------------------------------------------
|
2 | // Intialize the I2C-Bus Interface
|
3 | // as Master with SCL 100kHz, non interrupt driven
|
4 | //--------------------------------------------------------
|
5 |
|
6 | #define F_SCL 100000UL
|
7 |
|
8 | void TWI_init(void)
|
9 | {
|
10 | //--------------------------------------------------------
|
11 | // SCL = F_CPU/(16+2*TWBR*4^TWSR)
|
12 | // TWSR=0 => TWBR = F_CPU/2/F_SCL-8
|
13 | //--------------------------------------------------------
|
14 | TWBR = (u08)(F_CPU/2/F_SCL-7.5);
|
15 | TWSR = 0x00; // TWI-clock prescaler = (TWSR+1) = 1
|
16 | TWCR = (1<<TWEA)|(1<<TWEN); // no interrupt mode
|
17 | }
|
18 |
|
19 |
|
20 | u08 TWI_read(u08 TWI_adr, u08 *val, u08 count)
|
21 | //------------------------------------------------
|
22 | // Read n=count bytes from I2C-Bus in polled mode
|
23 | // needs about 200us for reading addr + one byte
|
24 | //------------------------------------------------
|
25 | {
|
26 | u08 i=0, rc=0;
|
27 | TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // send START condition
|
28 | while (!(TWCR & (1<<TWINT))); // wait for response
|
29 | if ((TWSR & 0xF8) == TW_START) // if START was successful
|
30 | {
|
31 | TWDR = (TWI_adr | 0x01); // put slave address+READ to TWDR
|
32 | TWCR = (1<<TWINT)|(1<<TWEN); // and start transmission
|
33 | while (!(TWCR & (1<<TWINT))); // wait for response
|
34 | if ((TWSR & 0xF8) == TW_MR_SLA_ACK) // if acknowledge ok
|
35 | {
|
36 | while (--count>0) // while more than one byte to read
|
37 | {
|
38 | TWCR=(1<<TWINT)|(1<<TWEA)|(1<<TWEN); // then start transfer with acknowledge
|
39 | while (!(TWCR & (1<<TWINT))); // wait for response
|
40 | val[i++]=TWDR; // read databyte from TWDR
|
41 | }
|
42 | TWCR=(1<<TWINT)|(1<<TWEN); // start last byte transfer without acknowledge
|
43 | while (!(TWCR & (1<<TWINT))); // wait for response
|
44 | val[i]=TWDR; // read databyte from TWDR
|
45 | }
|
46 | else
|
47 | {
|
48 | //printl_I("Error: I2C[R] Addr");
|
49 | rc = 0x10;
|
50 | }
|
51 | }
|
52 | else
|
53 | {
|
54 | //printl_I("Error: I2C[R] Start");
|
55 | rc = 0x20;
|
56 | }
|
57 | TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN); // transmit STOP condition
|
58 | _delay_us(10); // wait until STOP is transmitted
|
59 | return rc;
|
60 | }
|
61 |
|
62 | u08 TWI_write(u08 TWI_adr, u08 *val, u08 count)
|
63 | //------------------------------------------------
|
64 | // write n=count bytes to I2C-Bus in polled mode
|
65 | // needs about 200us for writing Addr + one byte
|
66 | //------------------------------------------------
|
67 | {
|
68 | u08 rc=0;
|
69 | TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // send START condition
|
70 | while (!(TWCR & (1<<TWINT))); // wait for response
|
71 | if ((TWSR & 0xF8) == TW_START) // if START was successful
|
72 | {
|
73 | TWDR = (TWI_adr & 0xFE); // put slave address+WRITE to TWDR
|
74 | TWCR = (1<<TWINT)|(1<<TWEN); // and start transmission
|
75 | for (u08 i=0; i<count; i++)
|
76 | {
|
77 | while (!(TWCR & (1<<TWINT))); // wait for response
|
78 | if ((TWSR & 0xC8) == TW_MT_ACK) // if acknowledge ok
|
79 | {
|
80 | TWDR = val[i]; // put databyte to TWDR
|
81 | TWCR = (1<<TWINT)|(1<<TWEN); // and start transmission
|
82 | }
|
83 | else
|
84 | {
|
85 | //printl_I("Error: I2C[W] Data");
|
86 | rc=0x10;
|
87 | }
|
88 | }
|
89 | }
|
90 | else
|
91 | {
|
92 | //printl_I("Error: I2C[W] Start");
|
93 | rc=0x20;
|
94 | }
|
95 | while (!(TWCR & (1<<TWINT))); // wait for response
|
96 | TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN); // transmit STOP condition
|
97 | _delay_us(10); // wait until STOP is transmitted
|
98 | return rc;
|
99 | }
|