Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / autonomous_recharging / archs / I2c test / i2c.c @ 383

History | View | Annotate | Download (2.49 KB)

1
/* 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
  PORTB&=~_BV(PB4);
43
  
44
  //Send packet. Will open line, transmit, and close line.
45
  USI_TWI_Start_Transceiver_With_Data(packet, (data_size + 1));
46
  
47
  PORTB&=~_BV(PB5);
48

    
49
}
50

    
51
int i2c_getpacket(char *c) {
52
  if (USI_TWI_Data_In_Receive_Buffer()) {
53
    *c =  USI_TWI_Receive_Byte();
54
    return 0;
55
  } else{
56
      return -1;
57
  
58
  }
59
}
60

    
61
void delay_ms(int ms) 
62
{
63
        for(; ms > 15; ms-=15)
64
                _delay_ms(15);
65
        _delay_ms(ms);
66
}
67

    
68

    
69
// Note this will fail. You need to fix the switch from slave -> master before it will succeed.
70
void i2c_test(void) {
71
    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'};
72
   char datarx;
73
  
74
  DDRA = _BV(PA3) | _BV(PA4) | _BV(PA5) | _BV(PA6) | _BV(PA7);
75
  PORTA = 0;
76
  PORTA |= _BV(PA3);
77
  
78
  //delay_ms(1000);
79
  
80
  sei();
81
  i2c_init();
82
   
83
    while(1) {
84
        delay_ms(1000);
85
    i2c_putpacket(0x01, datatx, 26);
86
        }
87
  
88
  /*
89
    while(1) {
90
      i2c_getpacket(&datarx);
91
      
92
      switch (datarx) {
93
        case 'a':
94
            PORTA |= _BV(PA4);
95
            break;
96
        case 'b':
97
            PORTA |= _BV(PA5);
98
            break;
99
        case 'c':
100
            PORTA |= _BV(PA6);
101
            break;
102
        case 'd':
103
            PORTA |= _BV(PA7);
104
            break;
105
      }
106
      
107
      if (datarx == 'd')
108
        break;
109
  }
110
  
111
   PORTA = 0;
112
 
113

114
  //PORTA |= _BV(PA5);
115
  delay_ms(1000);
116
  i2c_putpacket(0x01, datatx, 26);
117
  */
118
}
119