You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #include "adc.h"
  2. static adc_t s_adc[] = {
  3. {.port = "A", .pin = 2},
  4. {.port = "B", .pin = 8},
  5. {.port = "B", .pin = 9},
  6. };
  7. /***********************************************************************************************************************
  8. * =====================================================adc======================================================
  9. **
  10. ***********************************************************************************************************************/
  11. // uint16_t meanvalue;
  12. adc_t adc = {0};
  13. void ADCInit(uint8_t *port, uint8_t pin) {
  14. // GPIO_PIN(A, 28);
  15. ADC_InitStruType x;
  16. GPIO_InitSettingType y;
  17. y.Signal = GPIO_Pin_Signal_Analog; //�
  18. y.Dir = GPIO_Direction_Input; //����
  19. y.Func = GPIO_Reuse_Func0;
  20. // GPIO_Init(GPIO_Pin_B9, &y);
  21. if (pin == 2) {
  22. GPIO_Init(GPIO_Pin_A2, &y);
  23. x.CHS = ADC_CHS_AIN8;
  24. printf("A4\r\n");
  25. } else if (pin == 8) {
  26. GPIO_Init(GPIO_Pin_B8, &y);
  27. x.CHS = ADC_CHS_AIN3;
  28. printf("B8\r\n");
  29. } else if (pin == 9) {
  30. GPIO_Init(GPIO_Pin_B9, &y);
  31. x.CHS = ADC_CHS_AIN4;
  32. printf("B9\r\n");
  33. }
  34. //������Ƶ�Ժ�Tadclk=1/(PCLK/4)Լ����0.083us
  35. x.CLKS = ADC_CLKS_PCLK;
  36. x.CLKDIV = ADC_CLKDIV_1_4; /* ADCʱ��ԴԤ��Ƶ */
  37. //�ɼ�����ģ����*3.3/4096=���ڵĵ�ѹ
  38. x.VREF_SEL = ADC_VREF_SEL_0; /* �ڲ��ο���ѹ2.048v,�����ڲ��ο���ѹΪ���� */
  39. x.VREFP = ADC_VREFP_VDD; /* ѡ��оƬ�Ĺ�����ѹVDD��adc�IJο���ѹΪ���� */
  40. x.VREFN = ADC_VREFN_VSS; /* �����ο���ѹѡ�� */
  41. // x.CHS = ADC_CHS_AIN4;
  42. // x.CHS = ADC_CHS_AIN8;
  43. x.SMPS = ADC_SMPS_SOFT; /* AD����ģʽΪ�������� */
  44. //����ʱ��st*2+1(��Tadclk)=1.743us
  45. x.ST = 10; /* AD����ʱ��ѡ�� */
  46. x.BITSEL = ADC_BITSEL_12; /* AD�ֱ���12λ */
  47. ADC_Init(&x);
  48. }
  49. void get_adc_value(void) {
  50. uint16_t adc_value = 0;
  51. for (int i = 0; i < 3; i++) {
  52. ADCInit(s_adc[i].port, s_adc[i].pin);
  53. ADC_SoftStart();
  54. while (ADC_GetConvStatus() == SET)
  55. ; //����ת��
  56. s_adc[i].adc_just_now_gather_val = ADC_GetConvValue();
  57. ADC_SoftStop();
  58. record_adc_gather_value(s_adc[i].adc_just_now_gather_val);
  59. //printf("%d\r\n",s_adc[i].adc_just_now_gather_val);
  60. }
  61. }
  62. void adc_loop_gather(void) {
  63. static uint32_t adc_ticket = 0;
  64. if (port_haspassedms(adc_ticket) > 500) {
  65. adc_ticket = get_sys_ticket();
  66. get_adc_value();
  67. }
  68. }
  69. void record_adc_gather_value(uint16_t adc_value) {
  70. /**
  71. * @brief ¼adcͨɼ
  72. *
  73. */
  74. for (int i = 0; i < 3; i++) {
  75. s_adc[i].adc_value_buff[s_adc[i].adc_value_count++] = adc_value;
  76. if (s_adc[i].adc_value_count == 5) {
  77. s_adc[i].past_count_reached_five = true;
  78. s_adc[i].adc_value_count = 0;
  79. }
  80. if (s_adc[i].past_count_reached_five == true) {
  81. beg_average_value(s_adc[i].adc_value_buff);
  82. }
  83. }
  84. }
  85. void beg_average_value(uint16_t *adc_buff) {
  86. uint16_t adc_average_value = 0;
  87. uint32_t adc_sum_value = 0;
  88. for (int i = 0; i < 5; i++) {
  89. adc_sum_value += *adc_buff;
  90. adc_buff++;
  91. }
  92. adc_average_value = adc_sum_value / 5;
  93. if (adc_average_value <= 16 || adc.abnormal_state == true) {
  94. adc.abnormal_state = true;
  95. process_voltage_abnormal();
  96. // printf("short out%d\r\n", adc_average_value); //��·
  97. } else {
  98. //����adc_value=1700����ѹ=1700/4096=1.369
  99. printf("adc_value_t%d\r\n", adc_average_value);
  100. }
  101. }