this arduino sketch displays a 32x32 pixel image. two potentiometers can been hooked up to analog inputs 0 and 1 for adjusting/corrupting the hsync/vsync. a potentiometer on analog input 2 scrolls the image vertically and adjacent memory. images can be uploaded via serial.
// copy-it-right
#define VSYNC 1 << 0
#define HSYNC 1 << 1
// high voltage (bright) RGB
#define HR 1 << 2
#define HG 1 << 3
#define HB 1 << 4
// low voltage (dim) RGB
#define LR 1 << 5
#define LG 1 << 6
#define LB 1 << 7
#define NOM1 asm("nop");
#define NOM10 asm("nop\t\nnop\t\nnop\t\nnop\t\nnop\t\nnop\t\nnop\t\nnop\t\nnop\t\nnop");
int lnc = 0;
int lna = 0;
int lnt = 479;
int imgb, imgc = 0, imgf = 0;
unsigned short int lni = 0;
unsigned char img[1792];
void hsync()
{
NOM10;
PORTB &= ~HSYNC;
NOM10; NOM10; NOM10; NOM10; NOM10; NOM10;
PORTB |= HSYNC;
NOM10; NOM10; NOM10;
}
inline void pxl(int x)
{
PORTD = img[lni + x];
NOM1; NOM1; NOM1; NOM1; NOM1; NOM1;
}
ISR(TIMER1_COMPA_vect)
{
// vsync
if (lnc == -35) PORTB &= ~VSYNC;
if (lnc == -37) PORTB |= VSYNC;
hsync();
if (lnc > -1) {
// drawing a 30x30 image for now
lni = (lnc + lna) >> 4 << 5;
// 382 cycles
pxl( 0); pxl( 1); pxl( 2); pxl( 3); pxl( 4); pxl( 5); pxl( 6); pxl( 7);
pxl( 8); pxl( 9); pxl(10); pxl(11); pxl(12); pxl(13); pxl(14); pxl(15);
pxl(16); pxl(17); pxl(18); pxl(19); pxl(20); pxl(21); pxl(22); pxl(23);
pxl(24); pxl(25); pxl(26); pxl(27); pxl(28); pxl(29); pxl(30); pxl(31);
// 2 cycles
PORTD = 0;
}
lnc++;
if (lnc > lnt) {
lnc = -45;
}
}
void setup()
{
// pins
DDRD = HR | HG | HB | LR | LG | LB;
DDRB = VSYNC | HSYNC;
PORTB = VSYNC | HSYNC;
// timer
TIMSK0 &= !(1 << TOIE0);
cli();
TCCR1A = 0;
TCCR1B = 1 << WGM22 | 1 << CS10;
OCR1A = 508;
TIMSK1 = 1 << OCIE1A;
sei();
Serial.begin(9600);
}
void read_img_data()
{
if (Serial.available() > 0) {
if (Serial.read() == B11111100) {
noInterrupts();
lnt = 479;
while (imgc < 1792) {
imgb = Serial.read();
if (imgb > -1) {
img[imgc] = (unsigned char) imgb;
imgf = 0;
imgc++;
} else {
imgf++;
if (imgf > 4096) {
interrupts();
return;
}
}
}
interrupts();
}
}
}
void loop()
{
read_img_data();
OCR1A = analogRead(0); // 5K potentiometer
lnt = analogRead(1) * 2; // 5K potentiometer
lna = analogRead(2); // 1K or 5K potentiometer
}
Powered by Tumblr; designed by Adam Lloyd.