typedef struct sTWI uint8_t size uint8_t cnt uint8_t wbuf uint8_t rbuf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
typedef struct sTWI {
uint8_t size;
uint8_t cnt;
uint8_t *wbuf;
uint8_t *rbuf;
bool ok;
} TWI;
volatile TWI i2c;
unsigned char i2cBuffByte[10] = {0,1,2,3,4,5,6,7,8,9};
ISR(TWIC_TWIM_vect) {
i2c.cnt++;
if (TWIC_MASTER_STATUS & (1<<6)) {
if(i2c.cnt < i2c.size) {
TWIC_MASTER_DATA = i2cBuffByte[i2c.cnt];
}
TWIC_MASTER_STATUS |= (1<<6);
}
if(TWIC_MASTER_STATUS & (1<<4 )){
TWIC_MASTER_CTRLC = 0b00000111;
}
if (TWIC_MASTER_STATUS & (1<<7)) {
if(i2c.cnt < i2c.size)
i2cBuffByte[i2c.cnt] = TWIC_MASTER_DATA;
TWIC_MASTER_STATUS |= (1<<7);
TWIC_MASTER_CTRLC = 0b00000010;
}
if(i2c.cnt == i2c.size) {
TWIC_MASTER_CTRLC = i2cBuffByte[i2c.cnt];
i2c.ok = true;
TWIC_MASTER_CTRLA = 0b00111000;
}
}
void initTWI_i(void) {
TWIC_MASTER_BAUD = F_CPU/(2*50000)-5;
TWIC_MASTER_CTRLB = 0b00000100;
TWIC_MASTER_CTRLA = 0b10111000;
TWIC_MASTER_STATUS |= 0b00000001;
}
void testTwi(){
i2cBuffByte[0] = slaveAddr+0;
i2cBuffByte[1] = 0x01;
i2c.size = 2;
i2c.wbuf = i2cBuffByte;
i2c.cnt = 0;
i2c.ok = 0;
TWIC_MASTER_ADDR = i2cBuffByte[i2c.cnt];
TWIC_MASTER_CTRLA = 0b10111000;
//Здесь необходим RESTART
i2cBuffByte[0] = slaveAddr +1 ;
i2cBuffByte[1] = 0;
i2cBuffByte[2] = 0;
i2cBuffByte[3] = 0b00000011;
i2c.size = 4;
i2c.wbuf = i2cBuffByte;
i2c.cnt = 0;
i2c.ok = 0;
TWIC_MASTER_ADDR = i2cBuffByte[i2c.cnt];
TWIC_MASTER_CTRLA = 0b10111000;
_delay_ms(20);
//обработка данных, полученных от slave...
}