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.

79 lines
2.5 KiB

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