main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "main.h"
  2. #include "sys.h"
  3. #include "delay.h"
  4. #include "QC12864B.h"
  5. #include "Uart1.h"
  6. unsigned int SystemClockFreq;
  7. cc Status0 , Status1 , Status2, Status3;
  8. /****************************配置引脚**********************************/
  9. /*
  10. //APB1(低速外设):电源接口、备份接口、CAN、USB、I2C1、I2C2、UART2、UART3、SPI2、窗口看门狗、Timer2、Timer3、Timer4。
  11. //APB2(高速外设):UART1、SPI1、Timer1、ADC1、ADC2、所有普通IO口(PA~PE)、第二功能IO口。
  12. 版本1.5
  13. AHB :IO口(PA~PF)、TS、CRC、FLITF、SRAM、DMA1、DMA2
  14. APB2:SYSCFG、USART6~8、ADC1、TIM1、SPI1、USART1、TIM15~17、DBGMCU
  15. APB1:TIM2/3/6/7/14、WWDG、SPI2、USART2~5、I2C1~2、USB、CAN、CRS、PWR、DAC、CEC
  16. */
  17. #define HAL_LED0 PAout->b4
  18. #define HAL_TEST PAout->b5
  19. void GpioConfig_SYSLed ( void )
  20. {
  21. #if SYSLed_def
  22. GPIO_InitTypeDef GPIO_InitStructure;
  23. RCC_AHBPeriphClockCmd ( LED_RCC_CONFIG , ENABLE );
  24. /**
  25. * LED -> PA4
  26. */
  27. GPIO_InitStructure.GPIO_Pin = LED_PIN_CONFIG;
  28. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  29. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  30. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  31. GPIO_Init ( LED_PORT_CONFIG, &GPIO_InitStructure );
  32. #endif
  33. }
  34. void GpioConfig_SR04 ( void )
  35. {
  36. #if HC_SR04_def
  37. GPIO_InitTypeDef GPIO_InitStructure;
  38. RCC_AHBPeriphClockCmd ( SR04_RCC_CONFIG , ENABLE );
  39. /**
  40. * out -> PA5 in -> PA6
  41. */
  42. GPIO_InitStructure.GPIO_Pin = SR04_OUTPIN_CONFIG;
  43. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  45. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  46. GPIO_Init ( SR04_PORT_CONFIG, &GPIO_InitStructure );
  47. //外部按键GPIO初始华,PA6
  48. GPIO_InitStructure.GPIO_Pin = SR04_INPIN_CONFIG;
  49. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  50. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  51. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //无上下拉
  52. GPIO_Init ( SR04_PORT_CONFIG, &GPIO_InitStructure );
  53. //将EXTI0指向PA0
  54. SYSCFG_EXTILineConfig ( EXTI_PortSourceGPIOA, EXTI_PinSource6 );
  55. //EXTI0中断线配置
  56. EXTI_InitTypeDef EXTI_InitStructure;
  57. EXTI_InitStructure.EXTI_Line = EXTI_Line6;
  58. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  59. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //上下沿触发 EXTI_Trigger_Rising_Falling
  60. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  61. EXTI_Init ( &EXTI_InitStructure );
  62. //EXTI0中断向量配置
  63. NVIC_InitTypeDef NVIC_InitStructure;
  64. NVIC_InitStructure.NVIC_IRQChannel = EXTI4_15_IRQn;
  65. NVIC_InitStructure.NVIC_IRQChannelPriority = SR04_INPIN_Pre;
  66. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  67. NVIC_Init ( &NVIC_InitStructure );
  68. #endif
  69. }
  70. void GpioConfig_LCD ( void )
  71. {
  72. #if LCD_def
  73. GPIO_InitTypeDef GPIO_InitStructure;
  74. /* Enable the GPIO_LED Clock */
  75. RCC_AHBPeriphClockCmd ( LCD_QC12864B_RCC, ENABLE );
  76. /* Configure the GPIO_LED pin */
  77. GPIO_InitStructure.GPIO_Pin = LCD_QC12864B_PIN;
  78. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  79. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  80. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
  81. GPIO_Init ( LCD_QC12864B_PORT, &GPIO_InitStructure );
  82. #endif
  83. }
  84. //F030 只有高速TIM1,普通TIM3\14\16\17
  85. void TIM3_Init ( void )
  86. {
  87. TIM_TimeBaseInitTypeDef TimeBase;
  88. //TIM2 RCC
  89. RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM3 , ENABLE );
  90. //Tim5.TIM_Prescaler=72-1; //1MHz ,计数1次为us
  91. //Tim5.TIM_Prescaler=720-1; //100KHz ,定时器计数100次为1ms
  92. TIM_DeInit ( TIM3 ); //使用缺省值初始化TIM外设寄存器
  93. TimeBase.TIM_Period = 1; //自动重装载寄存器值为1
  94. TimeBase.TIM_Prescaler = ( 42 - 1 ) * 100; //42MHz/42/10=0.01Mhz=0.1ms
  95. TimeBase.TIM_ClockDivision = TIM_CKD_DIV1; //采样分频倍数1,未明该语句作用。
  96. TimeBase.TIM_CounterMode = TIM_CounterMode_Up; //上升模式
  97. TIM_TimeBaseInit ( TIM3, &TimeBase );
  98. //TIM_ClearFlag(TIM3,TIM_FLAG_Update);//清除更新标志位
  99. //TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能中断
  100. //TIM_Cmd(TIM3,ENABLE);//使能TIM3定时器
  101. }
  102. u16 TIM3_CALC()
  103. {
  104. u16 counter = 0;
  105. TIM_Cmd ( TIM3, ENABLE );
  106. TIM_SetCounter ( TIM3, counter ); //设置计数值
  107. while ( PAin->b6 ) //低电平停止
  108. {
  109. counter = TIM_GetCounter ( TIM3 );
  110. }
  111. TIM_Cmd ( TIM3, DISABLE );
  112. return counter;
  113. }
  114. void SysTick_Init ( int SYSTICK_TIME )
  115. {
  116. RCC_ClocksTypeDef RCC_Clocks;
  117. RCC_GetClocksFreq ( &RCC_Clocks );
  118. SystemClockFreq = RCC_Clocks.SYSCLK_Frequency;
  119. if ( SysTick_Config ( SystemClockFreq / ( 1000000 / SYSTICK_TIME ) ) )
  120. {
  121. while ( 1 );
  122. }
  123. }
  124. /****************************配置结束**********************************/
  125. #ifdef USE_FULL_ASSERT
  126. /**
  127. * @brief Reports the name of the source file and the source line number
  128. * where the assert_param error has occurred.
  129. * @param file: pointer to the source file name
  130. * @param line: assert_param error line source number
  131. * @retval None
  132. */
  133. void assert_failed ( uint8_t* file, uint32_t line )
  134. {
  135. /* User can add his own implementation to report the file name and line number,
  136. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  137. /* Infinite loop */
  138. while ( 1 )
  139. {}
  140. }
  141. #endif
  142. void sysLed_main ( void )
  143. {
  144. #if SYSLed_def
  145. static bool sysLedOn = false;
  146. //200ms
  147. sysLedOn = !sysLedOn;
  148. HAL_LED0 = sysLedOn;
  149. #endif
  150. }
  151. u32 sys_100us = 0;
  152. u32 sys_rtc_time = 0;
  153. u32 TaskTime = 0;
  154. #define SR04_dat_size 6
  155. bool StartCalSR04 = false;
  156. u32 SR04_100us = 0;
  157. u32 SR04_dat[SR04_dat_size + 2];
  158. u8 SR04_i = 0;
  159. u32 adcGetVal()
  160. {
  161. int i, j;
  162. SR04_dat[SR04_dat_size] = 0;
  163. SR04_dat[SR04_dat_size + 1] = 0xffffffff;
  164. for ( i = 0; i < SR04_dat_size; i++ )
  165. {
  166. for ( j = i; j < SR04_dat_size; j++ )
  167. {
  168. if ( SR04_dat[i] > SR04_dat[j] )
  169. {
  170. SR04_dat[SR04_dat_size] = SR04_dat[j];
  171. SR04_dat[j] = SR04_dat[i];
  172. SR04_dat[i] = SR04_dat[SR04_dat_size];
  173. }
  174. }
  175. }
  176. return SR04_dat[SR04_dat_size / 2];
  177. }
  178. void EXTI4_15_IRQHandler ( void )
  179. {
  180. if ( EXTI_GetITStatus ( EXTI_Line6 ) != RESET ) //上升沿触发
  181. {
  182. if ( PAin->b6 ) //上升沿
  183. {
  184. SR04_100us = 0;
  185. PBout->b1 = 1;
  186. }
  187. else //下降沿
  188. {
  189. PBout->b1 = 0;
  190. if ( SR04_i < SR04_dat_size )
  191. {
  192. SR04_dat[SR04_i] = SR04_100us;
  193. SR04_i++;
  194. }
  195. else
  196. {
  197. SR04_100us = adcGetVal();
  198. sprintf ( QC12864B_String[2], "时间:%.2f ms", SR04_100us * 1.0 / 10 );
  199. sprintf ( QC12864B_String[3], "距离:%.2f m", SR04_100us * 1.0 / 10 * 0.34 / 2 );
  200. SR04_i = 0;
  201. }
  202. }
  203. EXTI_ClearFlag ( EXTI_Line6 ); //退出中断时注意清除标志位
  204. }
  205. }
  206. void SysTick_Handler ( void ) //0.1ms
  207. {
  208. SR04_100us++;
  209. sys_100us++;
  210. if ( 0 == ( sys_100us % 10000 ) )
  211. {
  212. sys_rtc_time++; //1s
  213. }
  214. TaskTime++;
  215. if ( TaskTime % 20 == 0 ) System_Flag_2Ms = 1;
  216. if ( TaskTime % 100 == 0 ) System_Flag_10Ms = 1;
  217. if ( TaskTime % 1000 == 0 ) System_Flag_100Ms = 1;
  218. if ( TaskTime % 2000 == 0 ) System_Flag_200Ms = 1;
  219. if ( TaskTime % 5000 == 0 ) System_Flag_500Ms = 1;
  220. if ( TaskTime % 10000 == 0 ) System_Flag_1s = 1;
  221. if ( TaskTime % 80000 == 0 ) System_Flag_8s = 1;
  222. if ( TaskTime % 200000 == 0 ) System_Flag_20s = 1;
  223. }
  224. int main ( void )
  225. {
  226. SystemInit();
  227. SysTick_Init ( 100 ); //us, 最小10us,推荐1000us
  228. TIM3_Init();
  229. GpioConfig_SYSLed();
  230. GpioConfig_SR04();
  231. GpioConfig_LCD();
  232. GpioConfig_Usart1();
  233. LcdInit();
  234. sprintf ( QC12864B_String[0], "欢迎0824" );
  235. //LcdPrint(1, "2017-05-26");
  236. //LcdPrint(1, "09:56");
  237. //LcdPrint(1, "hjq");
  238. while ( 1 )
  239. {
  240. if ( System_Flag_2Ms )
  241. {
  242. System_Flag_2Ms = 0;
  243. }
  244. if ( System_Flag_10Ms )
  245. {
  246. System_Flag_10Ms = 0;
  247. //按键扫描
  248. //KeyHander();
  249. }
  250. if ( System_Flag_100Ms )
  251. {
  252. System_Flag_100Ms = 0;
  253. //系统信号灯
  254. sysLed_main();
  255. //LCD刷新
  256. LcdRefresh();
  257. }
  258. if ( System_Flag_200Ms )
  259. {
  260. System_Flag_200Ms = 0;
  261. if ( IsUart1Rcv )
  262. {
  263. IsUart1Rcv = false;
  264. UartSend ( "i recv your msg:\n", 17 );
  265. UartSend ( ( char * ) RxBufferUart, RxCounterUart );
  266. RxCounterUart = 0;
  267. }
  268. }
  269. if ( System_Flag_500Ms )
  270. {
  271. System_Flag_500Ms = 0;
  272. //0.2~40m, 即波形1.176~235.294ms,0.5秒最大可测2次
  273. PAout->b5 = 1;
  274. PAout->b7 = 1;
  275. delay_us ( 20 ); //10us以上的周期
  276. PAout->b5 = 0;
  277. PAout->b7 = 0;
  278. }
  279. if ( System_Flag_1s )
  280. {
  281. System_Flag_1s = 0;
  282. sprintf ( QC12864B_String[1], "时钟:%04d s", sys_rtc_time % 10000 );
  283. }
  284. if ( System_Flag_8s )
  285. {
  286. System_Flag_8s = 0;
  287. UartSend ( "i send one msg", 14 );
  288. }
  289. if ( System_Flag_20s )
  290. {
  291. System_Flag_20s = 0;
  292. }
  293. }
  294. }