A method for controlling a rotation angle of a swinging head of a fan, the method comprising: driving, by a brushless motor in the fan, the swinging head to rotate, wherein an infrared photoelectric rotary encoder is sleeved on a rotation shaft of the swinging head and the infrared photoelectric rotary encoder comprises a code channel A and a code channel B; acquiring (S301) by a microcontroller , a pulse signal outputted by an encoder read head according to a change of a scale of the infrared photoelectric rotary encoder, wherein the encoder read head comprises a first port corresponding to the code channel A and a second port corresponding to the code channel B, determining a scale position where one pulse signal from the first port and one pulse signal from the second port are simultaneously received by the microcontroller as a central position of the code channel A and the code channel B, and calculating a number of pulse signals outputted by the first port from the central position to a maximum rotation angle position, wherein the maximum rotation angle position is a scale position where two or more pulse signals from the first port and two or more pulse signals from the second port are simultaneously received by the microcontroller; taking (S302) a product of the number of pulse signals outputted by the first port and a rotation angle of the swinging head corresponding to one pulse signal as a current rotation angle of the swinging head; and controlling (S303) the swinging head to rotate according to the current rotation angle of the swinging head and a preset target rotation angle by using the brushless motor. driving, by a brushless motor in the fan, the swinging head to rotate, wherein an infrared photoelectric rotary encoder is sleeved on a rotation shaft of the swinging head and the infrared photoelectric rotary encoder comprises a code channel A and a code channel B; acquiring (S301) by a microcontroller , a pulse signal outputted by an encoder read head according to a change of a scale of the infrared photoelectric rotary encoder, wherein the encoder read head comprises a first port corresponding to the code channel A and a second port corresponding to the code channel B, determining a scale position where one pulse signal from the first port and one pulse signal from the second port are simultaneously received by the microcontroller as a central position of the code channel A and the code channel B, and calculating a number of pulse signals outputted by the first port from the central position to a maximum rotation angle position, wherein the maximum rotation angle position is a scale position where two or more pulse signals from the first port and two or more pulse signals from the second port are simultaneously received by the microcontroller; taking (S302) a product of the number of pulse signals outputted by the first port and a rotation angle of the swinging head corresponding to one pulse signal as a current rotation angle of the swinging head; and controlling (S303) the swinging head to rotate according to the current rotation angle of the swinging head and a preset target rotation angle by using the brushless motor. <EOS>