1 | #ifndef _I2C_MASTER_H
|
2 | #define _I2C_MASTER_H
|
3 |
|
4 | /*****************************************************************************
|
5 | ******************************************************************************
|
6 | **** ****
|
7 | **** I²C Groundroutinen für AVR Prozessoren mit hardwareseitigem ****
|
8 | **** I²C Bus. Start, Stop, read und Send Routinen ****
|
9 | **** ****
|
10 | **** Autor: Robert Schilling robert.schilling@gmx.at ****
|
11 | **** Stefan Tschiggerl stefan.tschiggerl@gmx.net ****
|
12 | **** Datum: 26.09.2006 ****
|
13 | **** ****
|
14 | **** Hardware: Ein Avr Mikrocontroller mit hardwareseitigem ****
|
15 | **** I²C Bus. ****
|
16 | **** ****
|
17 | **** Software: i2c_master Library: I²C Basic Routinen ****
|
18 | **** ****
|
19 | ******************************************************************************
|
20 | *****************************************************************************/
|
21 |
|
22 | #include <avr/io.h>
|
23 | #include <stdlib.h>
|
24 |
|
25 | #define SOFTWARE 0x00
|
26 | #define HARDWARE 0x01
|
27 |
|
28 | #define I2C_MODE SOFTWARE
|
29 |
|
30 |
|
31 | #if I2C_MODE == SOFTWARE
|
32 | #define I2C_PORT PORTC
|
33 | #define I2C_PIN PINC
|
34 | #define I2C_SCL PC3
|
35 | #define I2C_SDA PC2
|
36 |
|
37 | #define set_output(port, pin) port |= (1 << pin)
|
38 | #define set_input(port, pin) port &= ~(1 << pin)
|
39 |
|
40 | #define SDA_LOW() I2C_PORT &= ~(1 << I2C_SDA)
|
41 | #define SDA_HIGH() I2C_PORT |= (1 << I2C_SDA)
|
42 |
|
43 | #define SCL_LOW() I2C_PORT &= ~(1 << I2C_SCL)
|
44 | #define SCL_HIGH() I2C_PORT |= (1 << I2C_SCL)
|
45 |
|
46 | #endif
|
47 |
|
48 | #define ACK 0x01
|
49 | #define NACK 0x00
|
50 |
|
51 | /**************************************************************
|
52 | Initialisiert den I²C Bus des AVR
|
53 |
|
54 | Parameter: NONE
|
55 |
|
56 |
|
57 | Rückgabewert: NONE
|
58 | **************************************************************/
|
59 | void i2c_init(void);
|
60 |
|
61 | /**************************************************************
|
62 | Sendet ein Startkommando
|
63 |
|
64 | Parameter: adress Adresse des I²C Telnehmers
|
65 | r_w Lesen oder schreiben
|
66 |
|
67 | Rückgabewert: NONE
|
68 | **************************************************************/
|
69 |
|
70 | void i2c_start(unsigned char adress, uint8_t r_w);
|
71 |
|
72 | /**************************************************************
|
73 | Sendet ein Stopkommando
|
74 |
|
75 | Parameter: NONE
|
76 |
|
77 | Rückgabewert: NONE
|
78 | **************************************************************/
|
79 |
|
80 | void i2c_stop(void);
|
81 |
|
82 | /**************************************************************
|
83 | Sendet ein Byte über den I²C Bus
|
84 |
|
85 | Parameter: data Datenbyte das gesendet werden soll
|
86 |
|
87 | Rückgabewert: NONE
|
88 | **************************************************************/
|
89 |
|
90 | void i2c_send(unsigned char data);
|
91 |
|
92 | /**************************************************************
|
93 | Empfängt ein Byte über den I²C Bus
|
94 |
|
95 | Parameter: NONE
|
96 |
|
97 | Rückgabewert: Das Byte das ausgelsesn wurde
|
98 | **************************************************************/
|
99 |
|
100 | unsigned char i2c_read(uint8_t ackflag);
|
101 |
|
102 | #endif
|