-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom_save.c
126 lines (98 loc) · 2.33 KB
/
eeprom_save.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
#include <avr/io.h>
#include <util/delay.h>
#include "twi_drv.h"
#include "eeprom_save.h"
uint8_t Empt[] = "Empty!!!";
uint8_t en = 0; // â "1" - ñ÷èòàíî â áóôåð
void ee_write_block(uint8_t *buf, uint16_t uiAddress)
{
for(uint8_t i = 0; i < 8; i++)
{
while(EECR & (1<<EEWE));
EEAR = uiAddress++;
EEDR = buf[i];
EECR |= (1<<EEMWE);
EECR |= (1<<EEWE);
}
}
void ee_write(uint8_t buf, uint16_t uiAddress)
{
while(EECR & (1<<EEWE));
EEAR = uiAddress++;
EEDR = buf;
EECR |= (1<<EEMWE);
EECR |= (1<<EEWE);
}
uint8_t ee_read(uint16_t uiAddress)
{
while(EECR & (1<<EEWE));
EEAR = uiAddress;
EECR |= (1<<EERE);
return EEDR;
}
void M_Save(void)
{
uint8_t Buf[8];
uint8_t Buf2[512];
for(uint16_t i = 0; i < 512; i += 8)
{
read_str(i, Buf);
for(uint8_t j = 0; j < 8; j++) Buf2[i+j] = Buf[j];
}
for(uint16_t i = 0; i < 512; i += 8)
{
read_str(i, Buf);
if(err) return;
for(uint8_t j = 0; j < 8; j++) if(Buf2[i+j] != Buf[j]) Blink(4); // ïðîâåðêà ñ÷èòûâàíèÿ èç i2c
}
if(err) return;
ee_write_block(Empt, 0x1F8);
DDRC = (1 << PC3);
PORTC = (1 << PC3);
_delay_ms(100);
PORTC = (0 << PC3);
for(uint16_t i = 0; i < 512; i += 8)
{
for(uint8_t j = 0; j < 8; j++) Buf[j] = Buf2[i+j];
ee_write_block(Buf, i);
}
for(uint16_t i = 0; i < 512; i++) if(Buf2[i] != ee_read(i)) Blink(4); // ïðîâåðêà àðõèâàöèè äàííûõ
Blink(8);
}
void M_Restore(void)
{
uint8_t Buf[8], Buf2[8];
uint8_t x = 0;
for(uint8_t i = 0; i < 8; i++) if(Empt[i] == ee_read(i + 0x1F8)) x++;
if(x == 8) Blink(2);
x = 0;
read_str(0x00, Buf);
for(uint16_t i = 0; i < 8; i++) if(Buf[i] == ee_read(i)) x++;
if((x != 8) && (!en))
{
DDRC = (1 << PC3);
PORTC = (1 << PC3);
_delay_ms(250);
PORTC = (0 << PC3);
_delay_ms(250);
DDRC = (1 << PC3);
PORTC = (1 << PC3);
_delay_ms(250);
PORTC = (0 << PC3);
_delay_ms(2000);
en = 1;
}
for(uint16_t i = 0; i < 512; i += 8)
{
for(uint8_t j = 0; j < 8; j++) Buf[j] = ee_read(i+j);
write_str(i, Buf);
if(err) return;
}
for(uint16_t i = 0; i < 512; i += 8)
{
read_str(i, Buf2);
if(err) return;
for(uint8_t j = 0; j < 8; j++) if(Buf2[j] != ee_read(i+j)) Blink(4); // ïðîâåðêà âîññòàíîâëåíèÿ äàííûõ
}
if(!err) Blink(8);
}