The 1640 Portuguese revolt was well-timed to coincide with the nadir of Spanish power during the 17th Century. The Empire's best troops were employed in the 30 Years War, if not fighting in the Netherlands, or putting down revolts in Arragon or Catalonia. If the Duchess of Mantua had discovered the conspiracy by Portuguese noblemen before they seized power in a coup, the revolt could have been snuffed out.
However, even barring the 1640 revolt, Spain is not out of the woods when it comes to maintaining its Iberian Union during the 17th Century, to say nothing of the 18th or 19th. During the incompetent reign of Carlos II's mother and ministers, Louis XIV engaged in several successful wars against the Spanish, and would have been open to collaborating with dissatisfied Portuguese noblemen against the mad king. Barring that, at the conclusion of the War of Spanish Succession, it is likely that the British would have attempted to restore the independence of their ancient ally as part of the Treaty of Utrecht in 1713.
Finally, by the 19th century you will probably see a more broad-based nationalist movement agitating for independence from Spain, not just a small group of noblemen. If Spain could hold onto Portugal after 1815 or so, it has a pretty good chance of keeping it indefinitely, but it would be just another hotbed of secession and rebellion that would strain the Spanish state during the 19th century, and contribute to political paralysis in the 20th.
Good points, but that's assuming the 18th and the 19th century still happen in a recogniceable way, which is dubious considering hundreds of years after the POD. Not to tell the Portuguese revolt happened many years before Charles II was born. What makes you think he would, ITTL? I can agree that, in case of Nationalism still emerging, Portugal would have lots of chances to become a hot spot of rebellion, but then again, as others have said, the Spanish empire was very decentralized and divided in inner "kingdoms" with their own fueros, that were progressively retired everytime regions chose the wrong side in a revolt or civil war, and that hasn't led to Peninsular breakup so far.
Also, look at the OTL examples, which are very illustrative of how little connection Nationalism has with historical political entities. Isn't that curious how the three main Nationalist movements (the Basque, Catalan, and, to some extent, the Galician one) are unrelated to any actual historical independent country?
Catalonia was a series of separated counties under French tuttelage, that eventually were directly annexed to Aragon throught marriage. The Basque Country was part of Navarre, and then part of Castille... and then, the Lordship of Vizcaya became a distinct political entity within Castille, but not the rest of what is now considered Euskadi. Galicia was reconquered by Asturias, which became Leon, wich was annexed by Castille centuries before Spain was formed.
On the other hand, The places that were not only independent countries, but in some cases, mighty empires (Navarre, Leon, Aragon), either lack a Separatist movement or it is testimonial enough for not being able to achieve the minimal political representation (Navarre has, admittedly, some Basque Nationalist presence in its north, but to which extent the Navarran kingdom historically corresponds to the Basque identity, that's another question). Not to tell there are bilingual places, like Valencia (which was, funnily enough, a kingdom within Aragon, unlike Catalonia, which was a principality), where Spanish Nationalism is almost hegemonic.
In order to get a succesful Nationalist movement, you need certain economic and social agents, not just a distinct culture, otherwise there would be very few countries in the world free from a breakup risk; and though I can see some bourgoisie emerging in Portugal and promoting a Portuguese Nationalist movement, it hardly means there is some kind of Portuguese Manifest Destiny. No contry has such a thing.