Advertisement
Guest User

VGA_Arduino_color

a guest
Apr 4th, 2012
7,702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #define NOP asm("nop")
  2. #define BLACK    PORTB = B00000000;
  3. #define BLUE     PORTB = B00000001;
  4. #define GREEN    PORTB = B00000010;
  5. #define CYAN     PORTB = B00000011;
  6. #define RED  PORTB = B00000100;
  7. #define MAGENTA  PORTB = B00000101;
  8. #define YELLOW   PORTB = B00000110;
  9. #define WHITE    PORTB = B00000111;
  10.  
  11. unsigned int linecount = 1;
  12.  
  13. void setup()
  14. {
  15.   //Set pins 5 to 10 as outputs
  16.   // 7 - HSYNC
  17.   // 6 - VSYNC
  18.   // 10, 9 e 8 - RGB  
  19.   DDRD |= B11100000;
  20.   DDRB |= B11100111;
  21.   PORTD |= B11000000;
  22.  
  23.   //set timer  
  24.   TCCR2A = 0x02;                        // WGM22=0 + WGM21=1 + WGM20=0 = Mode2 (CTC)
  25.   TCCR2B |= (1 << CS20);                //
  26.   TCCR2B |= (1 << CS21);                // Set prescaler
  27.   TCCR2B &= ~(1 << CS22);               //
  28.  
  29.   TCNT2 = 0;                            // clean counter
  30.  
  31.   TIMSK2 &= ~(1<<OCIE2A);                // set comparison interrupt  
  32.   TIMSK2 |= (1<<TOIE2);                // set overflow interrupt  
  33. }
  34.  
  35. void loop()
  36. {
  37.   noInterrupts();
  38.   do{
  39.     BLACK;
  40.     if (TCNT2 > 0x0f){
  41.  
  42.       delayMicroseconds(1);
  43.       NOP;NOP;NOP;NOP;
  44.      
  45.       TCNT2 = 0x00;
  46.  
  47.    
  48.                        
  49.       // #### HSYNC ###
  50.       PORTD &= ~(1 << 7);      
  51.       if (++linecount >= 525){ //525 lines
  52.         linecount = 1;
  53.       }      
  54.       PORTD |= (1 << 7);
  55.  
  56.  
  57.  
  58.       // ### VSYNC ###
  59.       if ((linecount == 1)||(linecount == 2)){
  60.         PORTD &= ~(1 << 6);      
  61.       } else {
  62.         PORTD |= (1 << 6);
  63.        
  64.  
  65.         NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  66.         NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  67.         NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  68.         NOP;NOP;NOP;NOP;NOP;
  69.        
  70.         if ((linecount >= 9) && (linecount <= 489)){
  71.          
  72.                 WHITE;
  73.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  74.                 BLACK;
  75.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  76.                 BLUE;
  77.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  78.                 GREEN;
  79.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  80.                 CYAN;
  81.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  82.                 RED;
  83.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  84.                 MAGENTA;
  85.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  86.                 YELLOW;
  87.                 delayMicroseconds(3);NOP;NOP;NOP;NOP;NOP;NOP;NOP;NOP;
  88.                 BLACK;
  89.                 NOP;NOP;NOP;NOP;
  90.         }
  91.            
  92.       }
  93.  
  94.  
  95. }
  96.  
  97.  
  98. }while(1);
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement