-
Notifications
You must be signed in to change notification settings - Fork 2
/
puncher.c
191 lines (157 loc) · 4.32 KB
/
puncher.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Simple Real time clock example by Garth Klee.
* If run on PICDEM2+, time will be displayed
* on LCD and will incorporate LED pendulum.
* Has been written to run on PIC16F87x/A
*/
#include <htc.h>
//#include<pic16f1827.h>
#define ON 1
#define OFF 0
#define AM 0
#define PM 1
// Set start time here
#define START_C 0
#define START_H 0
#define START_M 0
#define START_S 55
//unsigned char* memcpyee(unsigned char to, const void *from, unsigned char size);
//unsigned char* eecpymem(unsigned char *to, unsigned char from, unsigned char size);
//unsigned char eeprom_read(unsigned char addr);
//void eeprom_write(unsigned char addr, unsigned char value);
void init(void);
//call the following twice (with different args each time) and the compiler automatically sets each config register
__CONFIG(FOSC_LP & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_ON & FCMEN_OFF);
__CONFIG(PLLEN_OFF & WRT_OFF & STVREN_ON & BORV_LO & LVP_OFF );
volatile near unsigned char tickCounter;
volatile near unsigned char newSecond;
near unsigned char centons; //hee hee 100 hours!
near unsigned char hours;
near unsigned char minutes;
near unsigned char seconds;
const unsigned char led[] = {1,2,4,8,16,32,64,128};
const unsigned char common[] ={1,2,4};
void main(void){
init();
newSecond = tickCounter = 0;
seconds=eeprom_read(0);
minutes=eeprom_read(1);
// Initialise the current time
// Measure time
while(1){
// if(RA4==0){
// LATA=0b00000001;
// LATB=0xFD;
// while(RA4==0){;}
// }else{LATA=0b00000010;}
// if(RA3==0){
// centons=START_C;
// hours = START_H;
// minutes = START_M;
// seconds = START_S;
// }
if(newSecond){
LATB = ~led[seconds%8];
LATA = common[minutes%3];
eeprom_write(0, seconds);
eeprom_write(1, minutes);
// A second has accumulated, count it
newSecond=0;
if(++seconds > 59){
seconds=0;
minutes++;
// EEPROM_WRITE(0, seconds);
// EEPROM_WRITE(1, minutes);
// LATA = common[minutes%3];
if(++minutes > 59){
minutes = 0;
hours++;
// EEPROM_WRITE(2, hours);
if(hours>99){
centons++;
// EEPROM_WRITE(3, centons);
hours=0;
}
}
}
}
}
}
void interrupt isr(void){
/***** Timer 2 Code *****/
if((TMR2IE)&&(TMR2IF)){
// Interrupt period is 40 mSec, 25 interrupts = 1 Sec
newSecond++; // Notify a second has accumulated
TMR2IF=0; // clear event flag
}
}
void init(void){
/***** Common Code ****
* Timer 2 interrupt enabled.
*/
PIE1 = 0b00000010;
/*
* Peripheral interrupts enabled
* Global interrupt disabled during initialization
*/
INTCON = 0b11000000;
/***** Timer 2 Code ****
* Prescale ratio set at 1:64
* Timer2 module activated
* Postscale ratio set at 1:16
*/
T2CON = 0b01111101;
/*
* Period register set to 0xF9
*/
PR2 = 127; //TODO check is this precisely right?
TRISB=0;
TRISA=0b11111000;
LATA=0b00000110;
LATB=0;
}
//void eeprom_write(unsigned char addr, unsigned char value){
// EEPROM_WRITE(addr, value);
//}
//unsigned char eeprom_read(unsigned char addr){
// while (WR) continue;
// return EEPROM_READ(addr);
//}
/*
unsigned char* eecpymem(unsigned char *to, unsigned char from, unsigned char size){
unsigned char *cp = to;
while (WR) continue;
EEADRL = (unsigned char)from;
while(size--) {
while (WR) continue;
#ifdef __FLASHTYPE
EECON1 &= 0x7F;
#endif
RD = 1;
*cp++ = EEDATA;
++EEADRL;
}
return to;
}
unsigned char* memcpyee(unsigned char to, const void *from, unsigned char size){
const unsigned char *ptr =from;
while (WR) continue;
EEADRL = (unsigned char)to - 1;
#ifdef __FLASHTYPE
EECON1 &= 0x7F;
#endif
while(size--) {
while (WR) continue;
EEDATA = *ptr++;
++EEADRL;
CARRY = 0; if (GIE) CARRY = 1; GIE = 0;
WREN = 1;
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1;
WREN = 0;
if (CARRY) GIE = 1; //an opportunity for interrupts to happen
}
return (unsigned char *)to;
}
*/