Problem with blink!

Hi i have Arduino mega 2560 +ch340
The problem is when i upload blinking sketch or writing the code like this:

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

the led doesn't blinking every second but like a wifi modem led( i mean it blinks completely irregularly not one second on and one second off just like a wifi router LED). If i write the code like blow it works perfectly:

void setup() {
  DDRB |= (1 << 7);  // Set pin 13 (PB7) as output
}

void loop() {
  PORTB |= (1 << 7);  // HIGH
  delay(1000);
  PORTB &= ~(1 << 7); // LOW
  delay(1000);
}

Welcome to the forum

What exactly do you mean by that ?
What do you see if you print a message each time you change the state of the LED ?

the latter is for pin 53 and the former for pin 13...

I have no idea what you mean by that.

How about trying:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

What's the point of this?

Sorry its my bad.
I mean normally it must be one second on and one second off. But t's blinking irregularly. Once its on for second then off for millisecond then rapidly blinking for some seconds then off for second and so on, just like a modem led

Does the LED blinking match the Serial monitor output if you add print statements as I recommended ?

Sorry. Corrected the code here

I mean it is blinking completely irregularly

Yes this the Arduino blinking code and have a problem.

I just meant the way of coding.

I will try this and tell you the results.

If that code results in irregular blinking, AND YOU HAVE NOTHING CONNECTED TO THE LED PIN CIRCUITRY, then the most likely culprit is a damaged LED, likely a cracked solder joint. There's not much else that can cause the issue you're describing, barring a varying CPU oscillator or something equally off-the-wall.

Tried and the led blinking normally just by writing Serial.begin(9600) without writing serial.println()

Led works well in certain conditions. As i mentioned above when i use direct register addressing (i mean using something like potb |= 1<<x) or just enabling serial it works well. So i don't think it is hardware

And forgot to say, i have this problem in all pins. I mean if i change the pin the other pins acts like this too.

There is only one pin connected to the onboard LED ("L" LED). Are you talking about using external LEDs? If so, please post a schematic showing your circuit.

Yes i used external LED with a resistor. The end of resistor connected to Gnd and led connected to the pin. Sorry can't send schematic right now but suppose something like this

Sorry, without a depiction of your actual wiring and components, it will be impossible to provide meaningful help.

I just loaded your first sketch up into a Mega here (after removing the errant text you added after posting), and the onboard L LED (which is hooked up to pin 13) is blinking one second on, one second off.

No problem seen.