main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "stm32f10x.h"
  5. #include "adc.h"
  6. #include "stdio.h"
  7. #include "key.h"
  8. #include "usart.h"
  9. #include "stdio.h"
  10. #include "string.h"
  11. #define EN_INTERRUPT __enable_irq();
  12. #define DI_INTERRUPT __disable_irq();
  13. unsigned char LRC_Check(unsigned char* data, int data_len)
  14. {
  15. int i = 0;
  16. unsigned char lrc = 0;
  17. unsigned int sum=0;
  18. for (i = 0; i < data_len; i++)
  19. {
  20. sum+= data[i];
  21. }
  22. lrc = (sum%256);
  23. lrc = ~lrc+1;
  24. return lrc;
  25. }
  26. #define KEY_GPIO_PROT GPIOC
  27. #define KEY1_GPIO_PIN GPIO_Pin_1
  28. #define KEY2_GPIO_PIN GPIO_Pin_13
  29. #define KEY3_GPIO_PIN GPIO_Pin_2
  30. #define KEY1_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY1_GPIO_PIN)
  31. #define KEY2_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY2_GPIO_PIN)
  32. #define KEY3_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY3_GPIO_PIN)
  33. //#define KEY1_IN PCin(1)
  34. //#define KEY2_IN PCin(13)
  35. //#define KEY3_IN PCin(2)
  36. void key_gpio_init(void)
  37. {
  38. GPIO_InitTypeDef GPIO_InitStructure;
  39. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
  40. GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN|KEY2_GPIO_PIN|KEY3_GPIO_PIN;
  41. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  42. GPIO_Init(KEY_GPIO_PROT, &GPIO_InitStructure);
  43. }
  44. uint32_t get_sys_time=0;
  45. uint16_t NO_val=0;
  46. uint16_t CO2_val=0;
  47. uint8_t no_changeflag=0;
  48. uint8_t co2_changeflag=0;
  49. uint16_t no_changeval=0;
  50. uint16_t co2_changeval=0;
  51. uint16_t no_tempval=0;
  52. uint16_t co2_tempval=0;
  53. long cnt_temp=0;
  54. //取数据指令 05 30 05 02 01 C3
  55. //NO减 15%
  56. //CO2 加 15%
  57. int key_Task(void)
  58. {
  59. static char key_flag=0;
  60. if(KEY1_IN==0||KEY2_IN==0||KEY3_IN==0)
  61. {
  62. if(key_flag==0)
  63. {
  64. key_flag=1;
  65. if(KEY1_IN==0)
  66. {
  67. //printf("k1\r\n");
  68. no_changeflag=1;
  69. no_changeval+=15;
  70. }else if(KEY2_IN==0)
  71. {
  72. // printf("k2\r\n");
  73. co2_changeflag=1;
  74. co2_changeval+=15;
  75. }
  76. else if(KEY3_IN==0)
  77. {
  78. printf("k3\r\n");
  79. no_changeflag=0;
  80. no_changeval=0;
  81. co2_changeflag=0;
  82. co2_changeval=0;
  83. __set_FAULTMASK(1);//关闭总中断
  84. NVIC_SystemReset();//请求单片机重启
  85. }
  86. }
  87. }else
  88. {
  89. key_flag=0;
  90. }
  91. return 20;
  92. }
  93. /*
  94. 06H-30H-LB-02H-烟度计适配位-气路压力高位-气路压力低位-油温高位
  95. -油温低位-转速高位-转速低位-NO高位-NO低位-CO2高位- CO2低位
  96. -N 值高位-N 值低位-Ns 值高位-Ns 值低位-K 值高位
  97. -K 值低位-烟气温度-校验码
  98. */
  99. void user_data_check(void)
  100. {
  101. if(user_uart_handle.uart2_sta)
  102. {
  103. if(user_uart_handle.uart2_len>0&&user_uart_handle.uart2_len<Usart2ReadLen)
  104. {
  105. if(user_uart_handle.uart2_len==3&&user_uart_handle.uart2_buff[0]==0x88&&user_uart_handle.uart2_buff[1]==0x55&&user_uart_handle.uart2_buff[2]==0x99)
  106. {
  107. user_uart_handle.uart2_buff[0]=no_changeval<<8;
  108. user_uart_handle.uart2_buff[1]=no_changeval;
  109. user_uart_handle.uart2_buff[2]=co2_changeval<<8;
  110. user_uart_handle.uart2_buff[3]=co2_changeval;
  111. USART2_String(user_uart_handle.uart2_buff,4);
  112. }else
  113. {
  114. USART1_String(user_uart_handle.uart2_buff,user_uart_handle.uart2_len);
  115. }
  116. user_uart_handle.uart2_sta=0;
  117. }
  118. }
  119. if(user_uart_handle.uart1_sta)
  120. {
  121. user_uart_handle.uart1_sta=0;
  122. if(user_uart_handle.uart1_len>0&&user_uart_handle.uart1_len<Usart1ReadLen)
  123. {
  124. if(user_uart_handle.uart1_buff[0]==0x06
  125. &&user_uart_handle.uart1_buff[1]==0x30
  126. &&user_uart_handle.uart1_buff[2]==0x16
  127. &&user_uart_handle.uart1_buff[3]==0x02
  128. &&user_uart_handle.uart1_buff[4]==0x01
  129. )
  130. {
  131. if(LRC_Check(user_uart_handle.uart1_buff,user_uart_handle.uart1_len-1)==user_uart_handle.uart1_buff[user_uart_handle.uart1_len-1])
  132. {
  133. NO_val = user_uart_handle.uart1_buff[11]<<8|user_uart_handle.uart1_buff[12];
  134. CO2_val = user_uart_handle.uart1_buff[13]<<8|user_uart_handle.uart1_buff[14];
  135. if(no_changeflag)
  136. {
  137. cnt_temp = NO_val - no_changeval;
  138. if(cnt_temp<1)cnt_temp=1;
  139. no_tempval = cnt_temp;
  140. user_uart_handle.uart1_buff[11]=no_tempval>>8;
  141. user_uart_handle.uart1_buff[12]=no_tempval;
  142. }
  143. if(co2_changeflag)
  144. {
  145. cnt_temp = CO2_val + co2_changeval;
  146. if(cnt_temp>1222)cnt_temp=1222;
  147. co2_tempval = cnt_temp;
  148. user_uart_handle.uart1_buff[13]=co2_tempval>>8;
  149. user_uart_handle.uart1_buff[14]=co2_tempval;
  150. }
  151. user_uart_handle.uart1_buff[user_uart_handle.uart1_len-1]=LRC_Check(user_uart_handle.uart1_buff,user_uart_handle.uart1_len-1);
  152. USART2_String(user_uart_handle.uart1_buff,user_uart_handle.uart1_len);
  153. }
  154. }else
  155. {
  156. USART2_String(user_uart_handle.uart1_buff,user_uart_handle.uart1_len);
  157. }
  158. }
  159. }
  160. }
  161. void TIM4_IRQHandler() //20ms 1次
  162. {
  163. u8 st;
  164. st=TIM_GetFlagStatus(TIM4, TIM_IT_Update);
  165. if(st!=0)
  166. {
  167. TIM_ClearFlag(TIM4, TIM_IT_Update);
  168. get_sys_time++;
  169. key_Task();
  170. }
  171. }
  172. void Timer4_init() //T4 20ms时钟
  173. {
  174. //
  175. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  176. NVIC_InitTypeDef NVIC_InitStructure;
  177. RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4,ENABLE);//M
  178. TIM_TimeBaseStructure.TIM_Period = 20000-1; //计数个数 //20ms
  179. TIM_TimeBaseStructure.TIM_Prescaler =72-1;//分频值
  180. TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //分割时钟
  181. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
  182. TIM_DeInit(TIM4);
  183. TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
  184. TIM_Cmd(TIM4, ENABLE); //使能定时器2
  185. /*以下定时器4中断初始化*/
  186. TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); //向上计数溢出产生中断
  187. NVIC_InitStructure.NVIC_IRQChannel =TIM4_IRQn;// TIM4_IRQChannel;
  188. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  189. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  190. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  191. NVIC_Init (&NVIC_InitStructure);
  192. }
  193. void IWDG_Init(u8 prer,u16 rlr)
  194. {
  195. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  196. IWDG_SetPrescaler(prer);
  197. IWDG_SetReload(rlr);
  198. IWDG_ReloadCounter();
  199. IWDG_Enable();
  200. }
  201. void IWDG_Feed(void)
  202. {
  203. IWDG_ReloadCounter();//reload
  204. }
  205. //母头uart2接电脑 公头uart1接下位机
  206. int main(void)
  207. {
  208. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  209. delay_init(); //延时函数初始化
  210. LED_Init(); //初始化与LED连接的硬件接口
  211. uart_init(9600);//串口初始化
  212. uart2_init(9600);
  213. key_gpio_init();
  214. Timer4_init();//初始化定时器
  215. printf("sys_init\r\n");
  216. IWDG_Init(4,625); //为1s
  217. while(1)
  218. {
  219. if(get_sys_time>50)
  220. {
  221. get_sys_time=0;
  222. //USART2_String((u8*)"sys_init\r\n",10);
  223. // printf("sys_init\r\n");
  224. }
  225. user_data_check();
  226. IWDG_Feed();
  227. }
  228. }