Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / autonomous_recharging / archs / i2c.c @ 955

History | View | Annotate | Download (2.44 KB)

1 80 bneuman
/* I2C.c
2

3
    I2C Communication
4

5
    Problem switching into slave mode. So never run as a slave otherwise you will never be able
6
        to speak again (unless a master tells you to).
7

8
*/
9
10
#include "i2c.h"
11
/*
12
int main(void) {
13
        i2c_test();
14
}
15
*/
16
//Call this to start I2C up
17
void i2c_init(void) {
18
  //Start off in slave mode so you can catch any data that comes your way
19
  //USI_TWI_Slave_Initialise(0x02);
20
21
  //Can't ever be a slave otherwise you will die. Stay as master and never give it up.
22
    USI_TWI_Master_Initialise();
23
24
}
25
26
27
//Call this to send a packet. *data is a char array of data_size. Make sure you put in data_size or we won't send
28
//everything.
29
//Note, mainboard is addr = 0x01.
30
void i2c_putpacket(char addr, char *data, char data_size) {
31
  //The packet that we will transmit over I2C
32
  unsigned char packet[data_size + 1];
33
34
  //First byte is the destination address + R/W (R = 1, W = 0)
35
  packet[0] = addr << 1;    //Shift the address over 1 bit, LSB is 0 which means write
36
37
  //Put data into packet
38
  for (int i = 0; i < data_size; i++) {
39
    packet[i+1] = data[i];
40
  }
41
42
  //Send packet. Will open line, transmit, and close line.
43
  USI_TWI_Start_Transceiver_With_Data(packet, (data_size + 1));
44
45
}
46
47
int i2c_getpacket(char *c) {
48
  if (USI_TWI_Data_In_Receive_Buffer()) {
49
    *c =  USI_TWI_Receive_Byte();
50
    return 0;
51
  } else{
52
      return -1;
53
54
  }
55
}
56
57
void delay_ms(int ms)
58
{
59
        for(; ms > 15; ms-=15)
60
                _delay_ms(15);
61
        _delay_ms(ms);
62
}
63
64
65
// Note this will fail. You need to fix the switch from slave -> master before it will succeed.
66
void i2c_test(void) {
67
    char datatx[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
68
   char datarx;
69
70
  DDRA = _BV(PA3) | _BV(PA4) | _BV(PA5) | _BV(PA6) | _BV(PA7);
71
  PORTA = 0;
72
  PORTA |= _BV(PA3);
73
74
  //delay_ms(1000);
75
76
  sei();
77
  i2c_init();
78
79
    while(1) {
80
        delay_ms(1000);
81
    i2c_putpacket(0x01, datatx, 26);
82
        }
83
84
  /*
85
    while(1) {
86
      i2c_getpacket(&datarx);
87

88
      switch (datarx) {
89
        case 'a':
90
            PORTA |= _BV(PA4);
91
            break;
92
        case 'b':
93
            PORTA |= _BV(PA5);
94
            break;
95
        case 'c':
96
            PORTA |= _BV(PA6);
97
            break;
98
        case 'd':
99
            PORTA |= _BV(PA7);
100
            break;
101
      }
102

103
      if (datarx == 'd')
104
        break;
105
  }
106

107
   PORTA = 0;
108

109

110
  //PORTA |= _BV(PA5);
111
  delay_ms(1000);
112
  i2c_putpacket(0x01, datatx, 26);
113
  */
114
}