13 COUN1=TIM1-》CNT; //左轮在0.1秒里脉冲数

14 COUN2=TIM2-》CNT; //右轮在0.1秒里脉冲数

15 Dist_L=Dist_L+COUN1; //左轮行程脉冲数

16 Dist_R=Dist_R+COUN2; //右轮行程脉冲数

17 if( (COUN1-COUN2)》2)

18 {

19 PWM_L= TIM_GetCapture1(TIM4);

20 TIM_SetCompare1(TIM4, PWM_L - 4);

21 }

22 else if ( (COUN2-COUN1)》2)

23 {

24 PWM_L= TIM_GetCapture1(TIM4);

25 TIM_SetCompare1(TIM4, PWM_L + 4);

26 }

27

28 TIM_SetCounter(TIM1, 0);

29 TIM_SetCounter(TIM2, 0);

30

31 }

32

33 关于TIM_SetCompareX(;)这个函数,还是有很多用途的,其中另外一个用途,就是用于产生不同频率的PWM,具体程序如下:

34

35 u16 capture = 0;

36

37 extern vu16 CCR1_Val;

38 extern vu16 CCR2_Val;

39 extern vu16 CCR3_Val;

40 extern vu16 CCR4_Val;

41

42 void TIM2_IRQHandler(void)

43 {

44

45 /* TIM2_CH1 toggling with frequency = 183.1 Hz */

46 if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)

47 {

48 TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );

49 capture = TIM_GetCapture1(TIM2);

50 TIM_SetCompare1(TIM2, capture + CCR1_Val );

51 }

52

53 /* TIM2_CH2 toggling with frequency = 366.2 Hz */

54 if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)

55 {

56 TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

57 capture = TIM_GetCapture2(TIM2);

58 TIM_SetCompare2(TIM2, capture + CCR2_Val);

59 }

60

61 /* TIM2_CH3 toggling with frequency = 732.4 Hz */

62 if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)

63 {

64 TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

65 capture = TIM_GetCapture3(TIM2);

66 TIM_SetCompare3(TIM2, capture + CCR3_Val);

67 }

68

69 /* TIM2_CH4 toggling with frequency = 1464.8 Hz */

70 if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)

71 {

72 TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

73 capture = TIM_GetCapture4(TIM2);

74 TIM_SetCompare4(TIM2, capture + CCR4_Val);

75 }

76

77 }

来源;21ic

关于控制,MCU就介绍完了,您有什么想法可以联系小编。