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.

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