-
Notifications
You must be signed in to change notification settings - Fork 2
/
i2c_routines.c
144 lines (122 loc) · 4.55 KB
/
i2c_routines.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//************************************************************
// ******** FUNCTIONS FOR I2C COMMUNICATION *******
//************************************************************
//Controller : ATmega32 (Clock: 8 Mhz-internal)
//Compiler : AVR-GCC (winAVR with AVRStudio-4)
//Project Version : DL_1.0
//Author : CC Dharmani, Chennai (India)
// www.dharmanitech.com
//Date : 10 May 2011
//************************************************************
#include <avr/io.h>
#include "i2c_routines.h"
#include "UART_routines.h"
//************************************************
//TWI initialize
// bit rate:18 (freq: 100Khz @16MHz)
//************************************************
void twi_init(void)
{
TWCR= 0x00; //disable twi
TWBR= 0x12; //set bit rate
TWSR= 0x00; //set prescale
//TWCR= 0x44; //enable twi
}
//*************************************************
//Function to start i2c communication
//*************************************************
unsigned char i2c_start(void)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Send START condition
while (!(TWCR & (1<<TWINT))); //Wait for TWINT flag set. This indicates that the
//START condition has been transmitted
if ((TWSR & 0xF8) == START) //Check value of TWI Status Register
return(0);
else
return(1);
}
//*************************************************
//Function for repeat start condition
//*************************************************
unsigned char i2c_repeatStart(void)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Send START condition
while (!(TWCR & (1<<TWINT))); //Wait for TWINT flag set. This indicates that the
//START condition has been transmitted
if ((TWSR & 0xF8) == REPEAT_START) //Check value of TWI Status Register
return(0);
else
return(1);
}
//**************************************************
//Function to transmit address of the slave
//*************************************************
unsigned char i2c_sendAddress(unsigned char address)
{
unsigned char STATUS;
if((address & 0x01) == 0)
STATUS = MT_SLA_ACK;
else
STATUS = MR_SLA_ACK;
TWDR = address;
TWCR = (1<<TWINT)|(1<<TWEN); //Load SLA_W into TWDR Register. Clear TWINT bit
//in TWCR to start transmission of address
while (!(TWCR & (1<<TWINT))); //Wait for TWINT flag set. This indicates that the
//SLA+W has been transmitted, and
//ACK/NACK has been received.
if ((TWSR & 0xF8) == STATUS) //Check value of TWI Status Register
return(0);
else
return(1);
}
//**************************************************
//Function to transmit a data byte
//*************************************************
unsigned char i2c_sendData(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT) |(1<<TWEN); //Load SLA_W into TWDR Register. Clear TWINT bit
//in TWCR to start transmission of data
while (!(TWCR & (1<<TWINT))); //Wait for TWINT flag set. This indicates that the
//data has been transmitted, and
//ACK/NACK has been received.
if ((TWSR & 0xF8) != MT_DATA_ACK) //Check value of TWI Status Register
return(1);
else
return(0);
}
//*****************************************************
//Function to receive a data byte and send ACKnowledge
//*****************************************************
unsigned char i2c_receiveData_ACK(void)
{
unsigned char data;
TWCR = (1<<TWEA)|(1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT))); //Wait for TWINT flag set. This indicates that the
//data has been received
if ((TWSR & 0xF8) != MR_DATA_ACK) //Check value of TWI Status Register
return(ERROR_CODE);
data = TWDR;
return(data);
}
//******************************************************************
//Function to receive the last data byte (no acknowledge from master
//******************************************************************
unsigned char i2c_receiveData_NACK(void)
{
unsigned char data;
TWCR = (1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT))); //Wait for TWINT flag set. This indicates that the
//data has been received
if ((TWSR & 0xF8) != MR_DATA_NACK) //Check value of TWI Status Register
return(ERROR_CODE);
data = TWDR;
return(data);
}
//**************************************************
//Function to end the i2c communication
//*************************************************
void i2c_stop(void)
{
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); //Transmit STOP condition
}