VGA sync timing issue

I'm trying to set up a VGA sync signal for 640x480, using a Pi Pico 2, and using arduino-pico core, but all i get on my display is "Invalid Format". And i'm sure the correct Pico pins are connected to the correct VGA connector pins, so it has to be the code. Here is the code:

int VSYNC = 17;
int HSYNC = 16;

void setup() 
{
  
  pinMode(VSYNC, OUTPUT);
  pinMode(HSYNC, OUTPUT);
  pinMode(19, OUTPUT);

  // Initialize sync signals
  gpio_put(VSYNC, HIGH);
  gpio_put(HSYNC, HIGH);
  gpio_put(19, HIGH);
  // put your setup code here, to run once:

}

int line = 0;

void generate_sync() 
{
  
  while(true)
  {
    // Horizontal Sync Timing
    // Front Porch
    busy_wait_us(0.635);

    // Sync Pulse
    gpio_put(HSYNC, LOW); 
    busy_wait_us(3.81); 
    gpio_put(HSYNC, HIGH); 

    // Back Porch + Visible Area
    busy_wait_us(1.906); 
    
    line++;

    // Vertical Sync Timing
    if (line == 525) 
    {
      line = 0;
    }
    
    if (line == 0)  // Start VSYNC pulse
    {
      gpio_put(VSYNC, LOW);
    }else if (line == 2)  // End VSYNC pulse after 2 lines
    {
      gpio_put(VSYNC, HIGH);
    }
  }
  
}

void loop() 

{
  generate_sync();
}

Any ideas why the sync signal is not working properly and not being detected properly by the VGA display?

This function works with integer argument, so the line above will produce about a 3u delay rather than 3.81.
And this will be no delay at all:

In general, generate a sync signal with a delays in a code is a bad idea - the result will be a signal of approximate frequency and low stability.
It must be done by hardware timers.
On Pico using a PIO machines is a most reliable approach.

1 Like

A very in-depth tutorial on Uno working VGA monochrome text output.

You might get some tips.

1 Like