bsp_time.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /********************** 头文件 *************************/
  2. #include "bsp_time.h"
  3. #include "mpu9250.h"
  4. /********************** 变量区 *************************/
  5. const nrf_drv_timer_t TIMER_INT = NRF_DRV_TIMER_INSTANCE(0);
  6. const uint32_t time_ms = 10; //Time(in miliseconds) between consecutive compare events.
  7. static uint32_t time_ticks;
  8. int8_t dex = 0;
  9. extern IMU_DAT_t m_IMU_DAT_t;
  10. extern void process_motion(IMU_DAT_t* p);
  11. extern motor_control_type m_motor_control;
  12. /********************** 函数声明区 *************************/
  13. /**********************************************************
  14. * 函数名字:HardwareTimer_handler
  15. * 函数作用:硬件定时器事件回调
  16. * 函数参数:event_type:事件类型
  17. * p_context: 上下文
  18. * 函数返回值:无
  19. ***********************************************************/
  20. volatile bool HardWareTime_10ms_Flag =false;
  21. void HardwareTimer_handler(nrf_timer_event_t event_type, void* p_context)
  22. {
  23. static uint8_t Led_Test =0;
  24. //static uint16_t press_temp=0;
  25. switch (event_type)
  26. {
  27. case NRF_TIMER_EVENT_COMPARE0:
  28. send_to_phone_process();
  29. gpio_mt_process();
  30. if(m_motor_control.flag)
  31. {
  32. if(m_motor_control.delay_ms >10){
  33. m_motor_control.delay_ms-=10;
  34. }
  35. else{
  36. m_motor_control.flag = false;
  37. nrf_gpio_pin_write(MT_EN,0);
  38. }
  39. }
  40. #if (MPU_SENSOR == MPU_SENSOR_6050)
  41. if(mpu6050_get_reg_data(m_IMU_DAT_t.h.gyr,m_IMU_DAT_t.h.acc)==0){ //读取 IMU 值
  42. #else
  43. if(MPU9250_ReadData(m_IMU_DAT_t.h.gyr,m_IMU_DAT_t.h.acc,m_IMU_DAT_t.h.mag,&(m_IMU_DAT_t.h.temperature))==0){
  44. #endif
  45. m_IMU_DAT_t.h.press = (uint16_t)(ReadPressure()>>8); //读取气压计值
  46. ReadPressure_Pre(); //准备下一次读取
  47. //m_IMU_DAT_t.h.press = press_temp;
  48. if(0 != m_IMU_DAT_t.h.press)
  49. {
  50. Led_Test++;
  51. if(Led_Test>10){
  52. nrf_gpio_pin_toggle(LED_G);
  53. Led_Test =0;
  54. }
  55. }
  56. else Led_Test =0;
  57. process_motion(&m_IMU_DAT_t);
  58. HardWareTime_10ms_Flag =true;
  59. }
  60. break;
  61. default:
  62. //Do nothing.
  63. break;
  64. }
  65. }
  66. /**********************************************************
  67. * 函数名字User_HardwareTime_Sete
  68. * 函数作用:硬件定时器的开关控制,默认为关闭状态
  69. * 函数参数:value: true 打开
  70. * false:关闭
  71. * 函数返回值:无
  72. ***********************************************************/
  73. void User_HardwareTime_Set(bool value)
  74. {
  75. static bool Hardflag =false;
  76. ret_code_t err_code;
  77. if(Hardflag == value)return;
  78. if(value){//打开硬件定时器
  79. nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
  80. err_code = nrf_drv_timer_init(&TIMER_INT, &timer_cfg, HardwareTimer_handler);
  81. APP_ERROR_CHECK(err_code);
  82. time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_INT, time_ms);
  83. nrf_drv_timer_extended_compare(
  84. &TIMER_INT, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
  85. nrf_drv_timer_enable(&TIMER_INT);
  86. }
  87. else{//打开软件定时器
  88. nrf_drv_timer_disable(&TIMER_INT);
  89. nrf_drv_timer_uninit(&TIMER_INT);
  90. }
  91. Hardflag =value;
  92. }