1 | #include <plib.h>
|
2 | #include "time_date.X/displaytech.h"
|
3 |
|
4 | #if defined (__32MX360F512L__) || (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__)
|
5 | // Configuration Bit settings
|
6 | // SYSCLK = 80 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
|
7 | // PBCLK = 80 MHz (SYSCLK / FPBDIV)
|
8 | // Primary Osc w/PLL (XT+,HS+,EC+PLL)
|
9 | // WDT OFF
|
10 | // Other options are don't care
|
11 | #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
|
12 | #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
|
13 | #define SYS_FREQ (80000000L)
|
14 |
|
15 |
|
16 |
|
17 | // local function prototypes
|
18 | int CheckRtccRunning(int secWait);
|
19 |
|
20 | /*********************************************************************
|
21 | * Function: int main(void)
|
22 | *
|
23 | * PreCondition: None
|
24 | *
|
25 | * Input: None
|
26 | *
|
27 | * Output: 1 if everything went on ok, 0 if failed
|
28 | *
|
29 | * Side Effects: None
|
30 | *
|
31 | * Overview: The function is an example of using the RTCC device.
|
32 |
|
33 | * Note: None
|
34 | ********************************************************************/
|
35 | int main(void)
|
36 | {
|
37 | rtccTime tm, tm1; // time structure
|
38 | rtccDate dt, dt1; // date structure
|
39 |
|
40 | // Configure the device for maximum performance but do not change the PBDIV
|
41 | // Given the options, this function will change the flash wait states, RAM
|
42 | // wait state and enable prefetch cache but will not change the PBDIV.
|
43 | // The PBDIV value is already set via the pragma FPBDIV option above..
|
44 | SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
|
45 |
|
46 | //***************************************************************
|
47 | // Pins für 16/2 Display und Initialisierung
|
48 | //***************************************************************
|
49 |
|
50 | button_in;
|
51 | RS_OUT;
|
52 | E_OUT;
|
53 | DB7_OUT;
|
54 | DB7_0;
|
55 | DB6_OUT;
|
56 | DB6_0;
|
57 | DB5_OUT;
|
58 | DB5_0;
|
59 | DB4_OUT;
|
60 | DB4_0;
|
61 | E_0;
|
62 |
|
63 | DelayMs(100);
|
64 | lcd_init();
|
65 |
|
66 |
|
67 |
|
68 | RtccInit(); // init the RTCC
|
69 | while(RtccGetClkStat()!=RTCC_CLK_ON); // -> HIER BLEIBT DAS PROGRAMM IN DER SCHLEIFE HAENGEN
|
70 |
|
71 | // wait for the SOSC to be actually running and RTCC to have its clock source
|
72 | // could wait here at most 32ms
|
73 |
|
74 |
|
75 |
|
76 | // when using the RtccSetTimeDate() function, the write operation is enabled if needed and then restored to the initial value
|
77 | // so that we don't have to worry about calling RtccWrEnable()/RtccWrDisable() functions
|
78 |
|
79 | // let's start setting the current date
|
80 | {
|
81 | // one way to do it
|
82 | tm.l=0;
|
83 | tm.sec=0x30;
|
84 | tm.min=0x07;
|
85 | tm.hour=0x10;
|
86 |
|
87 | dt.wday=2;
|
88 | dt.mday=0x16;
|
89 | dt.mon=0x01;
|
90 | dt.year=0x07;
|
91 | RtccSetTimeDate(tm.l, dt.l);
|
92 | }
|
93 | // however, much easier to do it should be:
|
94 | RtccSetTimeDate(0x10073000, 0x07011602); // time is MSb: hour, min, sec, rsvd. date is MSb: year, mon, mday, wday.
|
95 | // please note that the rsvd field has to be 0 in the time field!
|
96 |
|
97 | // NOTE: at this point the writes to the RTCC time and date registers are disabled
|
98 |
|
99 | // we can also read the time and date
|
100 | tm1.l=RtccGetTime();
|
101 | dt1.l=RtccGetDate();
|
102 | // can check that the time and date are the ones we just set
|
103 | if(tm.hour!=tm1.hour ||tm.min!=tm1.min)
|
104 | {
|
105 | return 0;
|
106 | }
|
107 | if(dt.l!=dt1.l) // check for proper date
|
108 | {
|
109 | return 0;
|
110 | }
|
111 | // we can read the time and date in a single operation
|
112 | RtccGetTimeDate(&tm1, &dt1);
|
113 |
|
114 | // now that we know the RTCC clock is up and running, it's easier to start from fresh:
|
115 | RtccOpen(tm.l, dt.l, 0); // set time, date and calibration in a single operation
|
116 |
|
117 | // check that the RTCC is running
|
118 | if(!CheckRtccRunning(3))
|
119 | {
|
120 | return 0;
|
121 | }
|
122 |
|
123 | // another way to see the RTCC is tunning: check the SYNC bit
|
124 | while(RtccGetSync()); // wait sync to be low
|
125 | while(!RtccGetSync()); // wait to be high
|
126 | while(RtccGetSync()); // wait sync to be low again
|
127 |
|
128 |
|
129 | // other RTCC operations
|
130 |
|
131 | // adjust the RTCC timing
|
132 | RtccSetCalibration(200); // value to calibrate with at each minute
|
133 |
|
134 | // enabling the RTCC output pin
|
135 | RtccSelectPulseOutput(1); // select the seconds clock pulse as the function of the RTCC output pin
|
136 | RtccSelectPulseOutput(0); // select the alarm pulse as the function of the RTCC output pin
|
137 | RtccOutputEnable(1); // enable the Output pin of the RTCC
|
138 | set_cursor(0, 1);
|
139 | lcd_out("24 Bit AD: ");
|
140 | integerintoasciiconverter(RtccGetTime());
|
141 | while(1)
|
142 | {
|
143 |
|
144 | }
|
145 |
|
146 |
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 | /*********************************************************************
|
152 | * Function: int CheckRtccRunning(int secWait)
|
153 | *
|
154 | * PreCondition: None
|
155 | *
|
156 | * Input: None
|
157 | *
|
158 | * Output: 1(true) if test succeeded, 0(FALSE) otherwise
|
159 | *
|
160 | * Side Effects: None
|
161 | *
|
162 | * Overview: The function checks that the RTCC has the clock enabled and counts the time.
|
163 | *
|
164 | * Note: None
|
165 | ********************************************************************/
|
166 | int CheckRtccRunning(int secWait)
|
167 | {
|
168 | #define WAIT_FOR_SEC_TMO 1100 // how many ms to wait for the RTCC seconds count to change
|
169 |
|
170 | rtccTime t0, t1;
|
171 | int fail;
|
172 | int secCnt;
|
173 | unsigned int tStart;
|
174 |
|
175 |
|
176 |
|
177 | for(secCnt=0, fail=0; secCnt<secWait; secCnt++)
|
178 | {
|
179 | tStart=ReadCoreTimer();
|
180 | t0.l=RtccGetTime();
|
181 | do
|
182 | {
|
183 | t1.l=RtccGetTime();
|
184 | }while((t1.sec == t0.sec) && (ReadCoreTimer()-tStart) < (SYS_FREQ/2000)*WAIT_FOR_SEC_TMO); // wait seconds change
|
185 |
|
186 | if(t1.sec==t0.sec)
|
187 | {
|
188 | fail=1;
|
189 | break; // failed
|
190 | }
|
191 | }
|
192 |
|
193 | return !fail;
|
194 | }
|