A new self-consistent model of circumstellar CO emission for deriving mass-loss rates in red giants. I, The carbon-rich star U Camelopardalis