largeimage_sensor.c


1
#include <avr/io.h>      // this contains all the IO port definitions
2
#include <avr/interrupt.h>
3
#include <avr/signal.h>
4
#include <avr/pgmspace.h>
5
#include <util/delay.h>
6
7
void delay_ms( uint16_t milliseconds)
8
{
9
   for( ; milliseconds > 0; milliseconds--)
10
   {
11
      _delay_ms( 1);
12
   }
13
} 
14
15
#define TIMER1_PRESCALE_1 1
16
#define TIMER1_PRESCALE_8 2
17
#define TIMER1_PRESCALE_64 3
18
#define TIMER1_PRESCALE_256 4
19
#define TIMER1_PRESCALE_1024 5
20
21
22
// We use these macros because binary constants arent always supported. ugh.
23
#define HEX__(n) 0x##n##UL
24
#define B8__(x) ((x&0x0000000FLU)?1:0)  \
25
               +((x&0x000000F0LU)?2:0)  \
26
               +((x&0x00000F00LU)?4:0)  \
27
               +((x&0x0000F000LU)?8:0)  \
28
               +((x&0x000F0000LU)?16:0) \
29
               +((x&0x00F00000LU)?32:0) \
30
               +((x&0x0F000000LU)?64:0) \
31
               +((x&0xF0000000LU)?128:0)
32
#define B8(d) ((unsigned char)B8__(HEX__(d)))
33
34
35
// store all the image data in program memory (ROM)
36
// instead of RAM (the default)
37
const uint8_t large_image[] PROGMEM = {
38
B8(00000000),
39
B8(00000000),
40
B8(00000000),
41
B8(00000000),
42
B8(00000000),
43
B8(00000000),
44
B8(00000000),
45
B8(00000000),
46
B8(00000000),
47
B8(00000000),
48
B8(00000000),
49
B8(00000000),
50
B8(00000000),
51
B8(00000000),
52
B8(00000000),
53
B8(00000000),
54
B8(00000000),
55
B8(00000000),
56
B8(00000000),
57
B8(00000000),
58
B8(11111111),
59
B8(11111111),
60
B8(11111111),
61
B8(11111111),
62
B8(11111111),
63
B8(11111111),
64
B8(11111111),
65
B8(11111111),
66
B8(11111111),
67
B8(11111111),
68
B8(00000000),
69
B8(00000000),
70
B8(00000000),
71
B8(00000000),
72
B8(00000000),
73
B8(00000000),
74
B8(00000000),
75
B8(00000000),
76
B8(00000000),
77
B8(00000000),
78
B8(00000000),
79
B8(00000000),
80
B8(00000000),
81
B8(00000000),
82
B8(00000000),
83
B8(00000000),
84
B8(00000000),
85
B8(00000000),
86
B8(00000000),
87
B8(00000000),
88
B8(00000000),
89
B8(00000000),
90
B8(00000000),
91
B8(00000000),
92
B8(00000000),
93
B8(00000000),
94
B8(00000000),
95
B8(00000000),
96
B8(00000000),
97
B8(00000000),
98
B8(00000000),
99
B8(00000000),
100
B8(00000000),
101
B8(00000000),
102
B8(00000000),
103
B8(00000000),
104
B8(00000000),
105
B8(00000000),
106
B8(00000000),
107
B8(00000000),
108
B8(00000000),
109
B8(00000000),
110
B8(00000000),
111
B8(00000000),
112
B8(00000000),
113
B8(00000000),
114
B8(00000000),
115
B8(00000000),
116
B8(00000000),
117
B8(00000000),
118
B8(00000000),
119
B8(00000000),
120
B8(00000000),
121
B8(00000000),
122
B8(00000000),
123
B8(00000000),
124
B8(00000000),
125
B8(00000000),
126
B8(00000000),
127
B8(00000000),
128
B8(11111111),
129
B8(11111111),
130
B8(11111111),
131
B8(11111111),
132
B8(11111111),
133
B8(11111111),
134
B8(11111111),
135
B8(11111111),
136
B8(11111111),
137
B8(11111111),
138
B8(00000000),
139
B8(00000000),
140
B8(00000000),
141
B8(00000000),
142
B8(00000000),
143
B8(00000000),
144
B8(00000000),
145
B8(00000000),
146
B8(00000000),
147
B8(00000000),
148
B8(00000000),
149
B8(00000000),
150
B8(00000000),
151
B8(00000000),
152
B8(00000000),
153
B8(00000000),
154
B8(00000000),
155
B8(00000000),
156
B8(00000000),
157
B8(00000000),
158
B8(00000000),
159
B8(00000000),
160
B8(00000000),
161
B8(00000000),
162
B8(00000000),
163
B8(00000000),
164
B8(00000000),
165
B8(00000000),
166
B8(00000000),
167
B8(00000000),
168
B8(00000000),
169
B8(00000000),
170
B8(00000000),
171
B8(00000000),
172
B8(00000000),
173
B8(00000000),
174
B8(00000000),
175
B8(00000000),
176
B8(00000000),
177
B8(00000000),
178
B8(00000000),
179
B8(00000000),
180
B8(00000000),
181
B8(00000000),
182
B8(00000000),
183
B8(00000000),
184
B8(00000000),
185
B8(00000000),
186
B8(00000000),
187
B8(00000000),
188
B8(00000000),
189
B8(00000000),
190
B8(00000000),
191
B8(00000000),
192
B8(00000000),
193
B8(00000000),
194
B8(00000000),
195
B8(00000000),
196
B8(00000000),
197
B8(00000000),
198
B8(11111111),
199
B8(11111111),
200
B8(11111111),
201
B8(11111111),
202
B8(11111111),
203
B8(11111111),
204
B8(11111111),
205
B8(11111111),
206
B8(11111111),
207
B8(11111111),
208
B8(00000000),
209
B8(00000000),
210
B8(00000000),
211
B8(00000000),
212
B8(00000000),
213
B8(00000000),
214
B8(00000000),
215
B8(00000000),
216
B8(00000000),
217
B8(00000000),
218
B8(00000000),
219
B8(00000000),
220
B8(00000000),
221
B8(00000000),
222
B8(00000000),
223
B8(00000000),
224
B8(00000000),
225
B8(00000000),
226
B8(00000000),
227
B8(00000000),
228
B8(00000000),
229
B8(00000000),
230
B8(00000000),
231
B8(00000000),
232
B8(00000000),
233
B8(00000000),
234
B8(00000000),
235
B8(00000000),
236
B8(00000000),
237
B8(00000000),
238
B8(00000000),
239
B8(00000000),
240
B8(00000000),
241
B8(00000000),
242
B8(00000000),
243
B8(00000000),
244
B8(00000000),
245
B8(00000000),
246
B8(00000000),
247
B8(00000000),
248
B8(00000000),
249
B8(00000000),
250
B8(00000000),
251
B8(00000000),
252
B8(00000000),
253
B8(00000000),
254
B8(00000000),
255
B8(00000000),
256
B8(00000000),
257
B8(00000000),
258
B8(00000000),
259
B8(00000000),
260
B8(00000000),
261
B8(00000000),
262
B8(00000000),
263
B8(00000000),
264
B8(00000000),
265
B8(00000000),
266
B8(00000000),
267
B8(00000000),
268
B8(11111111),
269
B8(11111111),
270
B8(11111111),
271
B8(11111111),
272
B8(11111111),
273
B8(11111111),
274
B8(11111111),
275
B8(11111111),
276
B8(11111111),
277
B8(11111111),
278
B8(00000000),
279
B8(00000000),
280
B8(00000000),
281
B8(00000000),
282
B8(00000000),
283
B8(00000000),
284
B8(00000000),
285
B8(00000000),
286
B8(00000000),
287
B8(00000000),
288
};
289
290
// special pointer for reading from ROM memory
291
PGM_P largeimage_p PROGMEM = large_image;
292
293
#define NUM_ELEM(x) (sizeof (x) / sizeof (*(x)))
294
int imagesize = NUM_ELEM(large_image);
295
296
297
298
// this function is called when timer1 compare matches OCR1A
299
uint8_t j = 0;
300
SIGNAL( SIG_TIMER1_COMPA ) { 
301
if (PIND & 0x4) { // if the sensor switch is off... 
302
303
if (j >= imagesize) 
304
j = 0; 
305
306
// read the image data from ROM 
307
PORTB = pgm_read_byte(largeimage_p + j); 
308
309
j++; 
310
311
} 
312
else { // this means the sensor is on 
313
j=0; // reset the image and set the display to blank 
314
PORTB = B8(00000000); 
315
} 
316
}
317
318
int main(void) {
319
320
  DDRB = 0xFF;       // set all 8 pins on port B to outputs
321
322
  DDRD = 0xFB;        // one input on pin D2
323
  PORTD = 0x04;       // turn on pullup on pin D2
324
325
  /*
326
    the frequency of the interrupt overflow is determined by the 
327
    prescaler and overflow value.
328
    freq = clock_frequency / ( 2 * prescaler * overflow_val)
329
    where prescaler can be 1, 8, 64, 256, or 1024
330
    clock_freq is 8MHz
331
    and overflow_val is 16bit
332
333
    the overflow value is placed in OCR1A, the prescale is set in TCCR1B
334
    so for example:
335
    A good POV frequency is around 400Hz
336
    desired freq = 400Hz
337
    clock freq = 8MHz
338
    8MHz / (400Hz * 2) = 10000
339
    since 10000 is less than 655536 (largest 16 bit number)
340
    OCR1A = 10000 and the prescale is 1
341
  */
342
343
  TCCR1B = (1 << WGM12) | TIMER1_PRESCALE_1;
344
  OCR1A = (uint16_t)3125;
345
346
  TIMSK |= 1 << OCIE1A;   // Output Compare Interrupt Enable (timer 1, OCR1A) 
347
348
  sei();                 // Set Enable Interrupts
349
350
  while (1);
351
}