Coughing provokes deep breathing, which makes us cough even more. You can stop this by exercising the Buteyko cough. Here is how to do it:
General rule: do not open your mouth; make coughing sounds through your nose.
Gently inhale through your nose and then cough about three times through the nose. When you are done, block your nose with your fingers for a small number of seconds and stop breathing. Then, open your nose and inhale; if you need to, repeat the Buteyko cough.
Try to cough only when you have to and suppress light urges. Don’t put any efforts into excreting mucus; your body will stop producing it excessively when your breathing is normalized. Mucus is your friend: it protects you from hyperventilation.