1 |   //***************************************************************************** |
2 |   // |
3 |   //  File  Name    :  'eeprom.c' |
4 |   //  Title                :  Itronove  hodiny  |
5 |   #define  __PIC18F8720A__ |
6 |   //  Author              :  http://www.prochazka.zde.cz  -&rsaquo   hacesoft  2017 |
7 |   //  Created            :  26-09-2017,  15:08 |
8 |   //  Revised            :  27-09-2017,  11:27 |
9 |   //  Version            :  1.0 |
10 |   //  Target  MCU   :  PIC  18F8720 |
11 |   // |
12 |   //  This  code  is  distributed  under  the  GNU  Public  License |
13 |   //  Vsechny  informace  jsou  zahrnuty  pod  GPL  licenci,  pokud  není  explicitne  uveden  jiný  typ  licence. |
14 |   //  Pouzivání  techto  stránek  ke  komercním  úcelum  lze  jen  se  souhlasem  autora. |
15 |   //  Vsechna  práva  vyhrazena  (c)  1997  -  2017  hacesoft. |
16 |   // |
17 |   //***************************************************************************** |
18 |   |
19 |   #include &lsaquo xc.h&rsaquo |
20 |   #include &lsaquo stddef.h&rsaquo   //nefinuje  NULL |
21 |   #include  "eeprom.h" |
22 |   #include &lsaquo stdint.h&rsaquo   //uint8_t,  uint16_t,  uint32_t |
23 |   #include  "ledI2C.h" |
24 |   |
25 |   //***************************************************************************** |
26 |   void  ReadEEprom  (void){ |
27 |        unsigned  int  address  =  0x0; |
28 |        |
29 |        PoleLedHSL.IC2_PWM.h   =  _eeprom_read  (address++); |
30 |        PoleLedHSL.IC2_PWM.s   =  _eeprom_read  (address++); |
31 |        PoleLedHSL.IC2_PWM.v   =  _eeprom_read  (address++); |
32 |        |
33 |        PoleLedHSL.IC3_PWM.h   =  _eeprom_read  (address++); |
34 |        PoleLedHSL.IC3_PWM.s   =  _eeprom_read  (address++); |
35 |        PoleLedHSL.IC3_PWM.v   =  _eeprom_read  (address++); |
36 |        |
37 |        address  =  address  +  2; |
38 |        |
39 |        PoleLedHSL.IC4_PWM.h   =  _eeprom_read  (address++); |
40 |        PoleLedHSL.IC4_PWM.s   =  _eeprom_read  (address++); |
41 |        PoleLedHSL.IC4_PWM.v   =  _eeprom_read  (address++); |
42 |        |
43 |        PoleLedHSL.IC5_PWM.h   =  _eeprom_read  (address++); |
44 |        PoleLedHSL.IC5_PWM.s   =  _eeprom_read  (address++); |
45 |        PoleLedHSL.IC5_PWM.v   =  _eeprom_read  (address++); |
46 |        |
47 |        address  =  address  +  2;     |
48 |        |
49 |        PoleLedHSL.IC6_PWM.h   =  _eeprom_read  (address++); |
50 |        PoleLedHSL.IC6_PWM.s   =  _eeprom_read  (address++); |
51 |        PoleLedHSL.IC6_PWM.v   =  _eeprom_read  (address++); |
52 |        |
53 |        PoleLedHSL.IC7_PWM.h   =  _eeprom_read  (address++); |
54 |        PoleLedHSL.IC7_PWM.s   =  _eeprom_read  (address++); |
55 |        PoleLedHSL.IC7_PWM.v   =  _eeprom_read  (address++);      |
56 |   } |
57 |   //***************************************************************************** |
58 |   void  WriteEEprom  (void){ |
59 |        unsigned  int  address  =  0x0; |
60 |        |
61 |        _eeprom_write(address++,  PoleLedHSL.IC2_PWM.h); |
62 |        _eeprom_write(address++,  PoleLedHSL.IC2_PWM.s); |
63 |        _eeprom_write(address++,  PoleLedHSL.IC2_PWM.v); |
64 |        |
65 |        _eeprom_write(address++,  PoleLedHSL.IC3_PWM.h); |
66 |        _eeprom_write(address++,  PoleLedHSL.IC3_PWM.s); |
67 |        _eeprom_write(address++,  PoleLedHSL.IC3_PWM.v); |
68 |        |
69 |        address  =  address  +  2; |
70 |        |
71 |        _eeprom_write(address++,  PoleLedHSL.IC4_PWM.h); |
72 |        _eeprom_write(address++,  PoleLedHSL.IC4_PWM.s); |
73 |        _eeprom_write(address++,  PoleLedHSL.IC4_PWM.v); |
74 |        |
75 |        _eeprom_write(address++,  PoleLedHSL.IC5_PWM.h); |
76 |        _eeprom_write(address++,  PoleLedHSL.IC5_PWM.s); |
77 |        _eeprom_write(address++,  PoleLedHSL.IC5_PWM.v); |
78 |        |
79 |        address  =  address  +  2;     |
80 |        |
81 |        _eeprom_write(address++,  PoleLedHSL.IC6_PWM.h); |
82 |        _eeprom_write(address++,  PoleLedHSL.IC6_PWM.s); |
83 |        _eeprom_write(address++,  PoleLedHSL.IC6_PWM.v); |
84 |        |
85 |        _eeprom_write(address++,  PoleLedHSL.IC7_PWM.h); |
86 |        _eeprom_write(address++,  PoleLedHSL.IC7_PWM.s); |
87 |        _eeprom_write(address++,  PoleLedHSL.IC7_PWM.v); |
88 |        |
89 |   } |
90 |   //***************************************************************************** |
91 |   uint8_t  _eeprom_read  (unsigned  int  address){ |
92 |        EEADR                =    address;  //  load  address  of  EEPROM  to  read |
93 |        EECON1bits.EEPGD     =    0;  //  access  EEPROM  data  memory |
94 |        EECON1bits.CFGS      =    0;  //  do  not  access  configuration  registers |
95 |        EECON1bits.RD        =    1;  //  initiate  read  |
96 |        return  EEDATA;               //  return  EEPROM  byte |
97 |   } |
98 |   //***************************************************************************** |
99 |   //  Write  Byte  to  internal  EEPROM |
100 |   uint8_t  _eeprom_write  (unsigned  int  address,  uint8_t  data){ |
101 |        EECON1bits.WREN      =    1;   //  allow  EEPROM  writes |
102 |        EEADR                =    address;  //  load  address  of  write  to  EEPROM |
103 |        EEDATA               =    data;  //  load  data  to  write  to  EEPROM |
104 |        EECON1bits.EEPGD     =    0;   //  access  EEPROM  data  memory |
105 |        EECON1bits.CFGS      =    0;   //  do  not  access  configuration  registers |
106 |        INTCONbits.GIE       =    0;   //  disable  interrupts  for  critical  EEPROM  write  sequence |
107 |        //===============// |
108 |        EECON2               =    0x55; |
109 |        EECON2               =    0xAA; |
110 |        EECON1bits.WR        =    1; |
111 |        //==============// |
112 |        INTCONbits.GIE       =    1;   //  enable  interrupts,  critical  sequence  complete |
113 |        while  (EECON1bits.WR  ==  1);    //  wait  for  write  to  complete |
114 |        EECON1bits.WREN      =    0;   //  do  not  allow  EEPROM  writes |
115 |        //Verify  write  operation |
116 |        if  (_eeprom_read(address)  ==  data)  //  read  the  byte  we  just  wrote  to  EEPROM |
117 |        return  _true                 ;  //  write  was  successful |
118 |        else |
119 |        return  _false;                   //  write  failed |
120 |   } |
121 |   //***************************************************************************** |
122 |   //  Secondary  Data  Type  with  eeprom  qualifier  |
123 |   /* |
124 |   __EEPROM_DATA  (0,  0,  0,  0,  0,  0,  255,  255); |
125 |   __EEPROM_DATA  (0,  0,  0,  0,  0,  0,  255,  255); |
126 |   __EEPROM_DATA  (0,  0,  0,  0,  0,  0,  255,  255); |
127 |   __EEPROM_DATA  (0,  0,  0,  0,  0,  0,  255,  255); |
128 |   __EEPROM_DATA  ("V",  "e",  "r",  "s",  "i",  "o",  "n",  ":"); |
129 |   __EEPROM_DATA  ("1",  ".",  "1",  "3",  10,  13,  "D",  "a"); |
130 |   __EEPROM_DATA  ("t",  "e",  ":",  "2",  "7",  ".",  "0",  "9"); |
131 |   __EEPROM_DATA  (".",  "2",  "0",  "1",  "7",  10,  13,  "E"); |
132 |   __EEPROM_DATA  ("m",  "a",  "i",  "l",  ":",  20,  "h",  "a"); |
133 |   __EEPROM_DATA  ("c",  "e",  "s",  "o",  "f",  "t",  "@",  "m"); |
134 |   __EEPROM_DATA  ("u",  "j",  "m",  "a",  "i",  "l",  ".",  "c"); |
135 |   __EEPROM_DATA  ("z",  10,  13,  "w",  "e",  "b",  ":","w"); |
136 |   __EEPROM_DATA  ("w",  "w",  ".",  "p",  "r",  "o",  "c",  "h"); |
137 |   __EEPROM_DATA  ("a",  "z",  "k",  "a",  ".",  "z",  "d",  "e"); |
138 |   __EEPROM_DATA  (".",  "c"  ,  "z"  ,10,  13,  0,  0,  0); |
139 |     |
140 |   */ |
141 |   |
142 |   |