当前位置:   article > 正文

STM32 HX1838红外遥控程序_stmhx1838

stmhx1838
  1. #ifndef _REMOTE_H_
  2. #define _REMOTE_H_
  3. #define IR_1 0X45
  4. #define IR_2 0X46
  5. #define IR_3 0X47
  6. #define IR_4 0X44
  7. #define IR_5 0X40
  8. #define IR_6 0X43
  9. #define IR_7 0X07
  10. #define IR_8 0X15
  11. #define IR_9 0X09
  12. #define IR_star 0X16
  13. #define IR_0 0X19
  14. #define IR_signal 0X0D
  15. #define IR_up 0X18
  16. #define IR_left 0X08
  17. #define IR_ok 0X1C
  18. #define IR_right 0X5A
  19. #define IR_down 0X52
  20. extern uint8_t IR_Address,IR_Command;
  21. void Remote_Init(void);
  22. uint8_t IR_GetDataFlag(void);
  23. uint8_t IR_GetRepeatFlag(void);
  24. uint8_t IR_GetAddress(void);
  25. uint8_t IR_GetCommand(void);
  26. #endif
  1. #include "stm32f10x.h"
  2. void Remote_Init()
  3. {
  4. /*¿ªÆôʱÖÓ*/
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  7. /*GPIO³õʼ»¯*/
  8. GPIO_InitTypeDef GPIO_InitStructure;
  9. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPD;
  10. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  11. GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOA,&GPIO_InitStructure);
  13. GPIO_SetBits(GPIOA,GPIO_Pin_6);
  14. /*ʱ»ùµ¥Ôª³õʼ»¯*/
  15. TIM_InternalClockConfig(TIM3);
  16. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  17. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  18. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  19. TIM_TimeBaseStructure.TIM_Period = 30000 - 1;
  20. TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
  21. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  22. TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
  23. /*ÊäÈ벶»ñÅäÖÃ*/
  24. TIM_ICInitTypeDef TIM_ICInitStructure;
  25. TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
  26. TIM_ICInitStructure.TIM_ICFilter = 0xf;
  27. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  28. TIM_ICInitStructure.TIM_ICPrescaler = TIM_CKD_DIV1;
  29. TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  30. TIM_ICInit(TIM3,&TIM_ICInitStructure);
  31. /*NVICÖжϷÖ×é*/
  32. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  33. /*NVICÅäÖÃ*/
  34. NVIC_InitTypeDef NVIC_InitStructure;
  35. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  36. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  37. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
  38. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  39. NVIC_Init(&NVIC_InitStructure);
  40. /*ʱÖÓÉèÖÃ*/
  41. TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);
  42. /*ʱÖÓʹÄÜ*/
  43. TIM_Cmd(TIM3,ENABLE);
  44. }
  45. uint8_t IR_State;
  46. uint16_t IR_Time;
  47. uint8_t IR_RepeatFlag,IR_DataFlag;
  48. uint8_t IR_DATA[4];
  49. uint8_t IR_pData;
  50. uint8_t IR_Address,IR_Command;
  51. uint8_t IR_GetDataFlag()
  52. {
  53. if (IR_DataFlag)
  54. {
  55. IR_DataFlag = 0;
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. uint8_t IR_GetRepeatFlag()
  61. {
  62. if (IR_RepeatFlag)
  63. {
  64. IR_RepeatFlag = 0;
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. uint8_t IR_GetAddress()
  70. {
  71. return IR_Address;
  72. }
  73. uint8_t IR_GetCommand()
  74. {
  75. return IR_Command;
  76. }
  77. void TIM3_IRQHandler()
  78. {
  79. uint8_t Addr_Temp,Cmd_Temp;
  80. if (TIM_GetITStatus(TIM3,TIM_FLAG_CC1) != RESET)
  81. {
  82. if (IR_State == 0) //¿ÕÏÐ̬
  83. {
  84. TIM_SetCounter(TIM3,0);
  85. IR_State = 1;
  86. }
  87. else if (IR_State == 1) //×¼±¸Ì¬
  88. {
  89. IR_Time = TIM_GetCounter(TIM3);
  90. if (IR_Time > 13500 - 500 && IR_Time < 13500+500) //StartÐźÅ
  91. {
  92. IR_State = 2;
  93. }
  94. else if (IR_Time > 11250 - 500 && IR_Time < 11250+500) //RepeatÐźÅ
  95. {
  96. IR_State = 0;
  97. IR_RepeatFlag = 1;
  98. }
  99. else
  100. {
  101. }
  102. TIM_SetCounter(TIM3,0);
  103. }
  104. else if (IR_State == 2) //½ÓÊÕ̬
  105. {
  106. IR_Time = TIM_GetCounter(TIM3);
  107. if (IR_Time > 1120 -500 && IR_Time < 1120+500) //Âß¼­0
  108. {
  109. IR_DATA[IR_pData/8] &= ~(0x01 << (IR_pData %8));
  110. IR_pData++;
  111. }
  112. else if (IR_Time > 2250 -500 && IR_Time < 2250 + 500) //Âß¼­1
  113. {
  114. IR_DATA[IR_pData/8] |= (0x01 << (IR_pData %8));
  115. IR_pData++;
  116. }
  117. else
  118. {
  119. IR_pData = 0;
  120. IR_State = 1;
  121. }
  122. if (IR_pData>=32) //Èç¹û½ÓÊÕÍê32λÊý¾Ý
  123. {
  124. IR_pData = 0;
  125. Addr_Temp = ~IR_DATA[1];
  126. Cmd_Temp = ~IR_DATA[3];
  127. if (IR_DATA[0] == Addr_Temp && IR_DATA[2] == Cmd_Temp) //Êý¾ÝÑéÖ¤
  128. {
  129. IR_Address = IR_DATA[0];
  130. IR_Command = IR_DATA[2];
  131. IR_DataFlag = 1;
  132. }
  133. IR_State = 0;
  134. }
  135. TIM_SetCounter(TIM3,0);
  136. }
  137. else
  138. {
  139. }
  140. TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
  141. }
  142. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1020099
推荐阅读
相关标签
  

闽ICP备14008679号