forked from cnlohr/avr_vhf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
420 lines (341 loc) · 9.96 KB
/
test.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
Copyright (C) 2015 <>< Charles Lohr
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "ntscfont.h"
void delay_ms(uint32_t time) {
uint32_t i;
for (i = 0; i < time; i++) {
_delay_ms(1);
}
}
#define NOOP asm volatile("nop" ::)
void NumToText( char * c, uint8_t a )
{
c[0] = (a/100)+'0';
c[1] = ((a/10)%10)+'0';
c[2] = (a%10)+'0';
c[3] = 0;
}
void NumToText4( char * c, uint16_t a )
{
c[0] = (a/1000)+'0';
c[1] = ((a/100)%10)+'0';
c[2] = ((a/10)%10)+'0';
c[3] = (a%10)+'0';
c[4] = 0;
}
EMPTY_INTERRUPT(TIM0_OVF_vect );
#define NOP() do { __asm__ __volatile__ ("nop"); } while (0)
int main( )
{
cli();
// Setup PLL for TIM1
CLKPR = 0x80; /*Setup CLKPCE to be receptive*/
CLKPR = 0x00; /*No pll prescale*/
PLLCSR = _BV(PLLE) | _BV( PCKE );
// Setup PWM on TIM1
//DDRB = _BV(1);
DDRB = 0;
DDRB |= _BV(3);
DDRB |= _BV(4);
PORTB |= _BV(3);
PORTB |= _BV(4);
TCCR1 = _BV(CS10);// | _BV(CTC1); //Clear on trigger.
GTCCR |= _BV(PWM1B) | _BV(COM1B0);// | _BV(COM1B1);
OCR1B = 1;
OCR1C = 3;
DTPS1 = 0;
DT1B = _BV(0) | _BV(4);
DT1B = 0;
// Setup TIM0 for line interval
TCCR0A = 0;
TCCR0B = _BV(CS01); // 30MHz/8 = 3.75MHz timer = 266.67ns
TIMSK |= _BV(TOIE0);
//CH3 on my AVR is OSCCAL=237 to 241
OSCCAL = 251;
#define LINETIME 15 //Linetime of 7..20 is barely valid.
// Linetime = H = 64usec = 240 timer ticks
#define LINETIME_HALF (LINETIME+120)
#define POWERSET3
#ifdef POWERSET1
#define NTSC_VH { DDRB=0;}
#define NTSC_HI { DDRB=_BV(4)|_BV(3); OCR1B = 1; TCNT1 = 0;}
#define NTSC_LOW { DDRB=_BV(4)|_BV(3); OCR1B = 2; TCNT1 = 0;}
#elif defined( POWERSET2 )
// normal;
#define NTSC_VH { DDRB=0;}
#define NTSC_HI { DDRB=_BV(3); }
#define NTSC_LOW { DDRB=_BV(4)|_BV(3); }
#elif defined( POWERSET3 )
// test duty cycle
#define NTSC_VH { DDRB=0; }
#define NTSC_HI { DDRB=_BV(3); OCR1B = 1;}
#define NTSC_LOW { DDRB=_BV(4)|_BV(3); OCR1B = 2;}
#elif defined( POWERSET4 )
// swap polarity;
#define NTSC_VH { DDRB=_BV(4)|_BV(3); }
#define NTSC_HI { DDRB=_BV(3); }
#define NTSC_LOW { DDRB=0;}
#else
//#define NTSC_VH { DDRB=0; }
//#define NTSC_HI { DDRB=_BV(3); }
//#define NTSC_LOW { DDRB=_BV(4)|_BV(3); }
#endif
uint8_t line, i;
#define TIMEOFFSET 0
#define CLKOFS 0
uint8_t frame = 0, k, ctll;
char stdsr[8*13];
sprintf( stdsr, "Fr: " );
sprintf( stdsr+8, "Hello<><" );
sprintf( stdsr+16, "World<><" );
sprintf( stdsr+24, " " );
sprintf( stdsr+32, " " );
sprintf( stdsr+40, " " );
sprintf( stdsr+48, " " );
sprintf( stdsr+56, " " );
sprintf( stdsr+64, " " );
sprintf( stdsr+72, " " );
sprintf( stdsr+80, " " );
sprintf( stdsr+88, " " );
//ADMUX =/* _BV(REFS1) | _BV(ADLAR) | */ 1; //1 = PB2
//ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADPS2) | _BV(ADPS1);
#define RESETCNT {TCNT0 = LINETIME; TIFR|=_BV(TOV0); GTCCR|=PSR0;sei();}
#define RESETCNT_HALF {TCNT0 = LINETIME_HALF; TIFR|=_BV(TOV0); GTCCR|=PSR0;sei();}
#define WAITTCNT sleep_cpu();
//#define LONGSYNC { NTSC_LOW; _delay_us(27.3-TIMEOFFSET); NTSC_HI; _delay_us(4.7-TIMEOFFSET-CLKOFS);}
//#define SHORTSYNC { NTSC_LOW; _delay_us(2.35-TIMEOFFSET); NTSC_HI; _delay_us(29.65-TIMEOFFSET-CLKOFS); }
#define LONGSYNC { NTSC_LOW; _delay_us(27-TIMEOFFSET); NTSC_HI; _delay_us(5-TIMEOFFSET-CLKOFS);}
#define SHORTSYNC { NTSC_LOW; _delay_us(3-TIMEOFFSET); NTSC_HI; _delay_us(28-TIMEOFFSET-CLKOFS); }
//#define LONGSYNC { NTSC_LOW; RESETCNT_HALF; _delay_us(27.3-TIMEOFFSET); NTSC_HI; WAITTCNT;}
//#define SHORTSYNC { NTSC_LOW; RESETCNT_HALF; _delay_us(2.35-TIMEOFFSET); NTSC_HI; WAITTCNT; }
sleep_enable();
sei();
uint8_t hh = 0;
#define HHS 128
uint8_t history[HHS];
uint16_t ovax = 0; //0..1024 = 0...5v
uint8_t ovax8 = 0;
while(1)
{
frame++;
//H = 1./15625 = 64
// H / 2 = 32
// Long sync: H*2.5
for( line = 0; line < 5; line++ ) { LONGSYNC; }
// Short sync: H*2.5
for( line = 0; line < 4; line++ ) { SHORTSYNC; }
// CNT == H == 64us
RESETCNT;
// FIRST
#define MAX_LINES (200)
for( line = 0; line < MAX_LINES; line++ ) {
_delay_us(1.65-TIMEOFFSET); // front porch
NTSC_LOW;
_delay_us(4.7-TIMEOFFSET); // horizontal sync
NTSC_HI;
_delay_us(1-TIMEOFFSET); // back porch (arbituary value)
if (line < 20) {
// Field-blanking interval of 17.5H
} else if (line < 80) {
if (40 < line && line < 60) {
_delay_us(5);
NTSC_VH;
_delay_us(5);
NTSC_HI;
_delay_us(5);
NTSC_VH;
_delay_us(5);
NTSC_HI;
}
/*NTSC_VH;
for (uint8_t x = 0; x < line; x+=4) {
NOP();
}
NTSC_HI;*/
/*
if (line < 22) {
} else if (line < 30) {
} else if (line < 50) {
NTSC_VH;
_delay_us(10-TIMEOFFSET-CLKOFS);
NTSC_HI;
} else if (line < 70) {
NTSC_VH;
_delay_us(15-TIMEOFFSET-CLKOFS);
NTSC_HI;
} else if (line < 100) {
NTSC_VH;
_delay_us(20-TIMEOFFSET-CLKOFS);
NTSC_HI;
} else {
NTSC_VH;
_delay_us(23-TIMEOFFSET-CLKOFS);
NTSC_HI;
}*/
} else if (line < 125) {
_delay_us(5);
#define V_DELAY { NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP(); }
#define V1 {NTSC_VH; V_DELAY; NTSC_HI; }
#define V0 {NTSC_HI; V_DELAY; NTSC_HI; }
// Hello World where 4x4 dots is 1 pixel
if (line>>2 == 20) { V1;V0;V0;V1;V0;V1;V1;V1;V1;V0;V1;V0;V0;V0;V0;V1;V0;V0;V0;V0;V1;V1;V1;V1; NTSC_HI; }
if (line>>2 == 21) { V1;V0;V0;V1;V0;V1;V0;V0;V0;V0;V1;V0;V0;V0;V0;V1;V0;V0;V0;V0;V1;V0;V0;V1; NTSC_HI; }
if (line>>2 == 22) { V1;V1;V1;V1;V0;V1;V1;V1;V1;V0;V1;V0;V0;V0;V0;V1;V0;V0;V0;V0;V1;V0;V0;V1; NTSC_HI; }
if (line>>2 == 23) { V1;V0;V0;V1;V0;V1;V0;V0;V0;V0;V1;V0;V0;V0;V0;V1;V0;V0;V0;V0;V1;V0;V0;V1; NTSC_HI; }
if (line>>2 == 24) { V1;V0;V0;V1;V0;V1;V1;V1;V1;V0;V1;V1;V1;V1;V0;V1;V1;V1;V1;V0;V1;V1;V1;V1; NTSC_HI; }
if (line>>2 == 26) { V1;V0;V0;V0;V1;V0;V1;V1;V1;V1;V0;V1;V1;V1;V1;V0;V1;V0;V0;V0;V0;V1;V1;V1; NTSC_HI; }
if (line>>2 == 27) { V1;V0;V0;V0;V1;V0;V1;V0;V0;V1;V0;V1;V0;V0;V1;V0;V1;V0;V0;V0;V0;V1;V0;V0;V1; NTSC_HI; }
if (line>>2 == 28) { V1;V0;V1;V0;V1;V0;V1;V0;V0;V1;V0;V1;V1;V1;V1;V0;V1;V0;V0;V0;V0;V1;V0;V0;V1; NTSC_HI; }
if (line>>2 == 29) { V1;V0;V1;V0;V1;V0;V1;V0;V0;V1;V0;V1;V0;V1;V0;V0;V1;V0;V0;V0;V0;V1;V0;V0;V1; NTSC_HI; }
if (line>>2 == 30) { V0;V1;V0;V1;V0;V0;V1;V1;V1;V1;V0;V1;V0;V0;V1;V0;V1;V1;V1;V1;V0;V1;V1;V1; NTSC_HI; }
// up to 30*4 = 124
} else {
}
// Use sleep interrupt to reset
WAITTCNT;
NTSC_HI;
RESETCNT;
}
/*
// Short sync: H*2.5
for( line = 0; line < 5; line++ ) { SHORTSYNC; }
// Long sync: H*2.5
for( line = 0; line < 5; line++ ) { LONGSYNC; }
// Short sync: H*2
for( line = 0; line < 4; line++ ) { SHORTSYNC; }
// SECOND
for( line = 0; line < 305; line++ ) {
_delay_us(1.65-TIMEOFFSET); // front porch
NTSC_LOW;
_delay_us(4.7-TIMEOFFSET); // horizontal sync/Synchronizing pulse
NTSC_HI;
_delay_us(5.7-TIMEOFFSET-CLKOFS); // back porch
if (line < 20) {
// Field-blanking interval of 17.5H
} else if (line < 200) {
NTSC_VH;
_delay_us(10-TIMEOFFSET-CLKOFS);
}
NTSC_HI; //_delay_us(44.5);
WAITTCNT;
RESETCNT;
}
*/
// Short sync: H*3
for( line = 0; line < 6; line++ ) { SHORTSYNC; }
/*
for( line = 0; line < 39; line++ )
{
NTSC_LOW;
_delay_us(4.7-TIMEOFFSET);
NTSC_HI;
//Do whatever you want.
switch (line)
{
case 0:
NumToText( stdsr+4, frame );
break;
case 1:
ovax = ADC;
ovax8 = ovax >> 2;
history[hh++] = ovax8;
hh&=sizeof(history)-1;
ovax = ovax * 49 + (ovax>>1);
ovax/=10;
break;
case 2:
NumToText( stdsr+24, ovax/1000 );
stdsr[27] = '.';
break;
case 3:
break;
case 5:
NumToText4( stdsr+27, ovax );
stdsr[27] = '.';
break;
}
WAITTCNT;
RESETCNT;
}
for( line = 0; line < 2; line++ )
{
NTSC_LOW;
_delay_us(4.7-TIMEOFFSET);
NTSC_HI;
WAITTCNT;
RESETCNT;
}
for( line = 0; line < 128; line++ )
{
NTSC_LOW; _delay_us(4.7-TIMEOFFSET);
NTSC_HI; _delay_us(8-TIMEOFFSET-CLKOFS);
//#define LINETEST
#ifdef LINETEST
NTSC_VH; _delay_us(8-TIMEOFFSET-CLKOFS);
NTSC_HI; _delay_us(44.5);
#else
ctll = line>>2;
for( k = 0; k < 8; k++ )
{
uint8_t ch = pgm_read_byte( &font_8x8_data[(stdsr[k+((ctll>>3)<<3)]<<3)] + (ctll&0x07) );
for( i = 0; i < 8; i++ )
{
if( (ch&1) )
{
NTSC_VH;
}
else
{
NTSC_HI;
NOOP;
}
ch>>=1;
NOOP; NOOP; NOOP; NOOP;
}
NTSC_HI;
}
NTSC_HI;// _delay_us(46-TIMEOFFSET-CLKOFS);
WAITTCNT;
RESETCNT;
#endif
}
for( line = 0; line < (92+47); line++ )
{
NTSC_LOW;
_delay_us(4.7-TIMEOFFSET);
NTSC_HI;
_delay_us(10);
uint8_t v = history[(hh-line+HHS-1)&(HHS-1)];
while(v--) NOOP;
NTSC_VH;
_delay_us(1);
NTSC_HI;
WAITTCNT;
RESETCNT;
}
*/
}
return 0;
}