High-resolution CCD spectra of stars in globular clusters. V. Carbon, nitrogen, and oxygen abundances in stars in 47 Tuc, M4, and M22