main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /********************** 头文件 *************************/
  2. #include "main.h"
  3. #include "User_Sleep.h"
  4. /********************** 变量区 *************************/
  5. /********************** 函数声明区 *************************/
  6. /********************** 系统函数区 *************************/
  7. void clocks_start( void )
  8. {
  9. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  10. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  11. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
  12. }
  13. void saadc_gpio_init(void)
  14. {
  15. nrf_gpio_cfg(
  16. 7,
  17. NRF_GPIO_PIN_DIR_OUTPUT,
  18. NRF_GPIO_PIN_INPUT_DISCONNECT,
  19. NRF_GPIO_PIN_NOPULL,
  20. NRF_GPIO_PIN_S0S1,
  21. NRF_GPIO_PIN_NOSENSE);
  22. }
  23. void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
  24. {
  25. }
  26. //saadc?????
  27. void saadc_init(void)
  28. {
  29. ret_code_t err_code;
  30. //??SAADC??????
  31. //????????????????????????
  32. //?????????????
  33. nrf_saadc_channel_config_t saadc_0 = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3);//????NRF_SAADC_INPUT_AIN2
  34. //???SAADC,????????,???????????
  35. //??????????,??nrf_drv_saadc_init()??????????
  36. //???????????,???????
  37. err_code=nrf_drv_saadc_init(NULL,saadc_callback);
  38. APP_ERROR_CHECK(err_code);
  39. //???SAADC??0
  40. err_code = nrf_drv_saadc_channel_init(0, &saadc_0);
  41. APP_ERROR_CHECK(err_code);
  42. saadc_gpio_init();
  43. }
  44. unsigned char adc_convert_to_power(short adcvalue)
  45. {
  46. const unsigned short list[3][12]={
  47. {100,90,80,70,60,50,40,30,20,10,5,0},
  48. {4200,4060,3980,3870,3820,3790,3770,3740,3680,3450,3000},
  49. {678,651,640,629,621,610,606,603,600,586,548,470}
  50. };
  51. if(adcvalue>list[2][0])return 100;
  52. if(adcvalue<=list[2][11])return 0;
  53. for(int i=0;i<11;i++)
  54. {
  55. if((adcvalue > list[2][i+1]) && (adcvalue <= list[2][i]))
  56. {
  57. return (list[0][i]-list[0][i+1])*(adcvalue-list[2][i+1])/(list[2][i]-list[2][i+1]) + list[0][i+1];
  58. }
  59. }
  60. return 100;
  61. }
  62. //?????????
  63. unsigned char Acquire_nrf_temp(void)
  64. {
  65. int32_t volatile temp;
  66. NRF_TEMP->TASKS_START = 1; /** Start the temperature measurement. */
  67. /* Busy wait while temperature measurement is not finished, you can skip waiting if you enable interrupt for DATARDY event and read the result in the interrupt. */
  68. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  69. while (NRF_TEMP->EVENTS_DATARDY == 0)
  70. {
  71. // Do nothing.
  72. }
  73. NRF_TEMP->EVENTS_DATARDY = 0;
  74. /**@note Workaround for PAN_028 rev2.0A anomaly 29 - TEMP: Stop task clears the TEMP register. */
  75. temp = (nrf_temp_read() / 4);
  76. /**@note Workaround for PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs. */
  77. NRF_TEMP->TASKS_STOP = 1; /** Stop the temperature measurement. */
  78. return temp;
  79. }
  80. extern uint16_t h_press;
  81. void AcquireTempPower(void)
  82. {
  83. nrf_saadc_value_t saadc_val;
  84. nrf_drv_saadc_sample_convert(0,&saadc_val); //4.2=678
  85. pm.power=adc_convert_to_power(saadc_val);
  86. pm.inner_temperature=Acquire_nrf_temp();
  87. pm.pressure[2]=(h_press&0xff00)>>8;
  88. pm.pressure[3]=h_press&0x00ff;
  89. }
  90. int main(void)
  91. {
  92. uint32_t err_code;
  93. gpio_init(); //初始化IO
  94. bsp_uart_init(); //初始化串口
  95. twi_master_init();
  96. nrf_delay_ms(100);
  97. MSE5611_Config();
  98. MPU6050_Init_reg();
  99. err_code = NRF_LOG_INIT(NULL);
  100. APP_ERROR_CHECK(err_code);
  101. NRF_LOG_DEFAULT_BACKENDS_INIT();
  102. clocks_start();
  103. err_code = bsp_esb_init(); //初始化esb
  104. APP_ERROR_CHECK(err_code);
  105. err_code = nrf_esb_start_rx();
  106. APP_ERROR_CHECK(err_code);
  107. time_init(); //?????
  108. saadc_init(); //???ADC????
  109. nrf_temp_init();//?????????
  110. //nrf_gpio_pin_write(MT_EN,0);
  111. //IntoSystemOffMode();
  112. while (true)
  113. {
  114. // nrf_gpio_pin_toggle(LED);
  115. AcquireTempPower();
  116. nrf_delay_ms(1000);
  117. }
  118. }
  119. /*lint -restore */