24c64.c


1
#include <avr/io.h>
2
#include <util/delay.h>
3
4
#include "24c64.h"
5
6
void EEOpen()
7
{
8
  //Set up TWI Module
9
  TWBR = 5;
10
  TWSR &= (~((1<<TWPS1)|(1<<TWPS0)));
11
12
}
13
14
int8_t EEWriteByte(uint16_t address,int8_t data)
15
{
16
  do
17
  {
18
    //Put Start Condition on TWI Bus
19
    TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
20
21
    //Poll Till Done
22
    while(!(TWCR & (1<<TWINT)));
23
24
    //Check status
25
    if((TWSR & 0xF8) != 0x08)
26
      return FALSE;
27
28
    //Now write SLA+W
29
    //EEPROM @ 00h
30
    TWDR=0b10100000;  
31
32
    //Initiate Transfer
33
    TWCR=(1<<TWINT)|(1<<TWEN);
34
35
    //Poll Till Done
36
    while(!(TWCR & (1<<TWINT)));
37
  
38
  }while((TWSR & 0xF8) != 0x18);
39
    
40
41
  //Now write ADDRH
42
  TWDR=(address>>8);
43
44
  //Initiate Transfer
45
  TWCR=(1<<TWINT)|(1<<TWEN);
46
47
  //Poll Till Done
48
  while(!(TWCR & (1<<TWINT)));
49
50
  //Check status
51
  if((TWSR & 0xF8) != 0x28)
52
    return FALSE;
53
54
  //Now write ADDRL
55
  TWDR=(address);
56
57
  //Initiate Transfer
58
  TWCR=(1<<TWINT)|(1<<TWEN);
59
60
  //Poll Till Done
61
  while(!(TWCR & (1<<TWINT)));
62
63
  //Check status
64
  if((TWSR & 0xF8) != 0x28)
65
    return FALSE;
66
67
  //Now write DATA
68
  TWDR=(data);
69
70
  //Initiate Transfer
71
  TWCR=(1<<TWINT)|(1<<TWEN);
72
73
  //Poll Till Done
74
  while(!(TWCR & (1<<TWINT)));
75
76
  //Check status
77
  if((TWSR & 0xF8) != 0x28)
78
    return FALSE;
79
80
  //Put Stop Condition on bus
81
  TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
82
  
83
  //Wait for STOP to finish
84
  while(TWCR & (1<<TWSTO));
85
86
  //Wait untill Writing is complete
87
  _delay_ms(12);
88
89
  //Return TRUE
90
  return TRUE;
91
92
}
93
94
int8_t EEReadByte(uint16_t address)
95
{
96
  int8_t data;
97
98
  //Initiate a Dummy Write Sequence to start Random Read
99
  do
100
  {
101
    //Put Start Condition on TWI Bus
102
    TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
103
104
    //Poll Till Done
105
    while(!(TWCR & (1<<TWINT)));
106
107
    //Check status
108
    if((TWSR & 0xF8) != 0x08)
109
      return FALSE;
110
111
    //Now write SLA+W
112
    //EEPROM @ 00h
113
    TWDR=0b10100000;  
114
115
    //Initiate Transfer
116
    TWCR=(1<<TWINT)|(1<<TWEN);
117
118
    //Poll Till Done
119
    while(!(TWCR & (1<<TWINT)));
120
  
121
  }while((TWSR & 0xF8) != 0x18);
122
    
123
124
  //Now write ADDRH
125
  TWDR=(address>>8);
126
127
  //Initiate Transfer
128
  TWCR=(1<<TWINT)|(1<<TWEN);
129
130
  //Poll Till Done
131
  while(!(TWCR & (1<<TWINT)));
132
133
  //Check status
134
  if((TWSR & 0xF8) != 0x28)
135
    return FALSE;
136
137
  //Now write ADDRL
138
  TWDR=(address);
139
140
  //Initiate Transfer
141
  TWCR=(1<<TWINT)|(1<<TWEN);
142
143
  //Poll Till Done
144
  while(!(TWCR & (1<<TWINT)));
145
146
  //Check status
147
  if((TWSR & 0xF8) != 0x28)
148
    return FALSE;
149
150
  //*************************DUMMY WRITE SEQUENCE END **********************
151
152
153
  
154
  //Put Start Condition on TWI Bus
155
  TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
156
157
  //Poll Till Done
158
  while(!(TWCR & (1<<TWINT)));
159
160
  //Check status
161
  if((TWSR & 0xF8) != 0x10)
162
    return FALSE;
163
164
  //Now write SLA+R
165
  //EEPROM @ 00h
166
  TWDR=0b10100001;  
167
168
  //Initiate Transfer
169
  TWCR=(1<<TWINT)|(1<<TWEN);
170
171
  //Poll Till Done
172
  while(!(TWCR & (1<<TWINT)));
173
174
  //Check status
175
  if((TWSR & 0xF8) != 0x40)
176
    return FALSE;
177
178
  //Now enable Reception of data by clearing TWINT
179
  TWCR=(1<<TWINT)|(1<<TWEN);
180
181
  //Wait till done
182
  while(!(TWCR & (1<<TWINT)));
183
184
  //Check status
185
  if((TWSR & 0xF8) != 0x58)
186
    return FALSE;
187
188
  //Read the data
189
  data=TWDR;
190
191
  //Put Stop Condition on bus
192
  TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
193
  
194
  //Wait for STOP to finish
195
  while(TWCR & (1<<TWSTO));
196
197
  //Return TRUE
198
  return data;
199
}