The Spanish flag is quite recent. The current flag was invented in 1785. The Royal Decree of 1785 May the 28th by king Carlos III created a new flag for the Spanish Navy. So next monday it will be its 222 aniversary.

Flags in past times were military banners and its use and signification was different than it is nowadays.

Before the current flag there wasn't a unique flag.
Every royal and noble familie had its own coats of arms. There were different flags depending of the military corps, division or military order.

History of the Spanish flags by the Spanish Ministry of Defense
History of the Spanish flag from the oficial web page of the Spanish Navy
History of the Spanish flag from the oficial web page of the Spanish Presidency
_________________________
Damián