Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2205 - 1
/**************************************************************************************************************************************
2
* File:                 I2Cmaster.c
3
*
4
* Purpose:              controlling I2C
5
*
6
* Functions:    int     main(void)
7
*
8
* hardware:             AVR-Ctrl experimental board without LCD display as I2C bus is used
9
*
10
* Created:              Jan 2013
11
*
12
* Version:              1.00    experimental version
13
*
14
* Copyright:    (c)2013 dk9nw -at- darc.de
15
*                               All rights reserved. This software is available only for non-commercial amateur radio or educational applications.  
16
*                               Other uses are prohibited. This software may be modified only if
17
*                               the resulting code be made available publicly and the original author(s) given credit.
18
*
19
******************************************************************************************************************************************/
20
#include "I2Cmaster.h"
21
 
22
unsigned char twi_state = 0;
23
volatile unsigned char Motor[4];                                // turn rate of the connected electrical motor 0 - 255
24
 
25
 
26
// **************************************************************************************************************************
27
//      initialize the I2C Bus (TWI)                         
28
//
29
void i2c_init(void)
30
{
31
        static unsigned char sreg;
32
        sreg = SREG;                                                            // save backup status register , to be reseted later
33
        cli();                                                                          // switch off I-Bit Global Interrupt
34
 
35
        //--------------------------------------------------------------------------------------------------------------------
36
        // TWSR = TWI Status Register containing: TWS7 TWS6 TWS5 TWS4 TWS3 – TWPS1 TWPS0
37
        // -------------------------
38
        // TWPS: TWI Prescaler Bits
39
        //
40
        TWSR &= ~((1<<TWPS1)|(1<<TWPS0));                       // TWI Status Register Bits 1..0 – TWPS: TWI Prescaler Bits 00=1:1 01=1:4 10=1:16 11=1:64
41
 
42
        //--------------------------------------------------------------------------------------------------------------------
43
        // TWBR = TWI Bit Rate Register containing: TWBR7 TWBR6 TWBR5 TWBR4 TWBR3 TWBR2 TWBR1 TWBR0
44
        // set TWI Bit Rate Register                            // SCL_frequency = ( CPU_Clock_frequency / ( 16 + 2 * TWBR ) * 4**TWPS )
45
        //                                                                                      // 
46
        TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;                       // TWI Bit Rate (formula is only valid if TWSR=0)
47
 
48
        twi_state       = 0;
49
 
50
        i2c_start();
51
 
52
        SREG = sreg;                                                            // write back to old state, 
53
        sei();                                                                          // Interrupt is activated again
54
}
55
// **************************************************************************************************************************
56
 
57
 
58
 
59
// **************************************************************************************************************************
60
//      reset I2C Bus
61
//
62
void i2c_reset(void)
63
{      
64
        i2c_stop();                                                                     // stop i2c Bus
65
        twi_state = 0;
66
        TWCR = (1<<TWINT);                                              // reset to original state incl. interrupt flag reset
67
        TWAR = 0;
68
        TWDR = 0;
69
        TWSR = 0;
70
        TWBR = 0;
71
        i2c_init();                                                                     // initialize the I2C Bus (TWI)  
72
        i2c_start();
73
        i2c_write_byte(0);
74
}
75
// **************************************************************************************************************************
76
 
77
 
78
 
79
// **************************************************************************************************************************
80
//      start of I2C Bus
81
//
82
void i2c_start(void)
83
{
84
        // TWCR = TWI Control Register -> TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE
85
        // --------------------------------------------------------------------
86
        // TWINT = TWI interrupt flag - to be set to one, to reset the TWINT Flag .
87
        // TWSTA = TWI start and should be set to one, that TWI is started as Master.
88
        // TWEN = TWI enable -> now TWI/I2C is working  properly.
89
        // TWIE = TWI Interrupt enable is the TWIE Bit -> should be set
90
        TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)|(1<<TWIE);
91
 
92
        //while( !(TWCR & (1<<TWINT) ) );       // wait, that TWINT is zero again, that means that the start conditions have been transferred
93
}
94
// **************************************************************************************************************************
95
 
96
 
97
 
98
// **************************************************************************************************************************
99
//      stop the I2C Bus
100
//
101
void i2c_stop(void)
102
{
103
        // TWCR = TWI Control Register -> TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE
104
        // --------------------------------------------------------------------
105
        // TWINT = TWI interrupt flag - to be set to one, to reset the TWINT Flag .
106
        // TWSTO TWI STOP Condition Bit (TWSTO = 1) -> now will be stoped
107
        // TWEN = TWI enable -> now TWI/I2C is working  properly.
108
        TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
109
}
110
// **************************************************************************************************************************
111
 
112
 
113
 
114
 
115
// **************************************************************************************************************************
116
//      Write to I2C                      
117
// 
118
void i2c_write_byte(unsigned char byte)
119
{
120
        //TWSR = 0x00;                                                                  // TWI Status Register
121
 
122
        TWDR = byte;                                                                    // move byte to send into TWI Data Register
123
 
124
        // TWCR = TWI Control Register -> TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE
125
        // --------------------------------------------------------------------
126
        // TWINT = TWI interrupt flag - to be set to one, to reset the TWINT Flag .
127
        // TWEN = TWI enable -> now TWI/I2C is working  properly.
128
        // TWIE = TWI Interrupt enable is the TWIE Bit -> should be set
129
        TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE);
130
}
131
// **************************************************************************************************************************
132
 
133
 
134
 
135
// **************************************************************************************************************************
136
//      Receive byte and send ACK
137
//
138
void I2C_ReceiveByte(void)
139
{
140
        // TWCR = TWI Control Register -> TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE
141
        // ---------------------------------------------------------------------------
142
        // TWINT = TWI interrupt flag - to be set to one, to reset the TWINT Flag .
143
        // TWEA = TWI Acknowledge Bit (TWEA = 1)
144
        // TWEN = TWI enable -> now TWI/I2C is working  properly.
145
        // TWIE = TWI Interrupt enable is the TWIE Bit -> should be set
146
        //
147
        TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN)|(1<<TWIE) ;
148
}
149
// **************************************************************************************************************************
150
 
151
 
152
 
153
// **************************************************************************************************************************
154
//      I2C receive last byte and send no ACK
155
//
156
void I2C_ReceiveLastByte(void)
157
{
158
        // TWCR = TWI Control Register -> TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE
159
        // ---------------------------------------------------------------------------
160
        // TWINT = TWI interrupt flag - to be set to one, to reset the TWINT Flag .
161
        // TWEN = TWI enable -> now TWI/I2C is working  properly.
162
        // TWIE = TWI Interrupt enable is the TWIE Bit -> should be set
163
        TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE);
164
}
165
// **************************************************************************************************************************
166
 
167
 
168
 
169
// **************************************************************************************************************************
170
// Interrupt I2C
171
//---------------------------------------------------------------------------------------------------------------------------
172
ISR(TWI_vect)
173
{
174
        switch(twi_state++)
175
        {
176
                //-------------------------------------------------------------------------------------------------------------------
177
                //      write Motor-Data to BL-Ctrl
178
                //-------------------------------------------------------------------------------------------------------------------
179
                case 0:
180
                        i2c_write_byte(0x52);  
181
                break;
182
 
183
                case 1:
184
                        i2c_write_byte(Motor[0]);
185
                break;
186
 
187
                case 2:
188
                        i2c_stop();
189
                        i2c_start();
190
                break;
191
 
192
                case 3:
193
                        i2c_write_byte(0x54);  
194
                break;
195
 
196
                case 4:
197
                        i2c_write_byte(Motor[1]);
198
                break;
199
 
200
                case 5:
201
                        i2c_stop();
202
                        i2c_start();
203
                break;
204
 
205
                case 6:
206
                        i2c_write_byte(0x56);  
207
                        break;
208
 
209
                case 7:
210
                        i2c_write_byte(Motor[2]);
211
                break;
212
 
213
                case 8:
214
                        i2c_stop();
215
                        i2c_start();
216
                break;
217
 
218
                case 9:
219
                        i2c_write_byte(0x58);  
220
                        break;
221
 
222
                case 10:
223
                        i2c_write_byte(Motor[3]);
224
                break;
225
 
226
                case 11:
227
                        i2c_stop();
228
                        twi_state = 0;
229
                        i2c_start();
230
                break;
231
 
232
                default:
233
                        twi_state = 0;
234
                break;        
235
        }
236
}
237
// EOF ISR(TWI_vect) *****************************************************************************************************