Subversion Repositories FlightCtrl

Rev

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

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