How to interface HCSR04 ultrasonic sensor with ATxmega256A3?
It appears to me that you don't wait for the echo to return.

Reading from https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/ :


  "The HC-SR04 barely works to 10 feet giving a total path length of 20
  feet and a path time of about 20ms so set the timeout to something
  above that, say 25 or 30ms."


So the measurement could take up to 20 ms to arrive. I think you should reset pulseWidth to 0 before starting a sample, then wait until you got a value:

pulseWidth = 0;
PORTB.OUT = 0x01; // trig the sensor 
_delay_us((20));
PORTB.OUT = 0x00;
while (pulseWidth == 0) ;  //Waiting here.
count_a = pulseWidth / 58;


Or better yet, delay until a timeout, to catch if the echo goes missing.

By the way, the link I quoted only pings for 10 us, not 20 us.

Still, your code should catch the pulseWidth value registered on the previous loop, so something else could be wrong here. You might need to reset the input capture timer, as is done in https://www.avrfreaks.net/forum/xmega-input-capture

If those ideas don't help, if you can check the pins with an oscilloscope, that would help you narrow down if the signal arrives as it should.