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.

110 lines
3.4 KiB

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