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.

244 lines
7.7 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
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 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������Ҫ�ĸ���ѹ�����ο�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. //���Զ�ȡadc�ɼ��ĵ�ѹֵ
  51. void ADCPA2Init() {
  52. ADC_InitStruType x;
  53. GPIO_InitSettingType y;
  54. y.Signal = GPIO_Pin_Signal_Analog; //�
  55. y.Dir = GPIO_Direction_Input; //����
  56. y.Func = GPIO_Reuse_Func0;
  57. // x.CHS����adc�ɼ���ͨ��ÿ�����Ŷ�Ӧһ��ͨ��
  58. GPIO_Init(GPIO_Pin_A2, &y);
  59. //������Ƶ�Ժ�Tadclk=1/(PCLK/4)Լ����0.083us
  60. x.CLKS = ADC_CLKS_PCLK;
  61. x.CLKDIV = ADC_CLKDIV_1_32; /* ADCʱ��ԴԤ��Ƶ */
  62. //�ɼ�����ģ����*3.3/4096=���ڵĵ�ѹ
  63. x.VREF_SEL = ADC_VREF_SEL_0; /* �ڲ��ο���ѹ2.048v,�������ڲ��ο���ѹΪ���� */
  64. x.VREFP = ADC_VREFP_VDD; /* ѡ��оƬ�Ĺ�����ѹVDD������������adc������Ҫ�ĸ���ѹ�����ο�adc�IJο���ѹΪ���� */
  65. x.VREFN = ADC_VREFN_VSS; /* �����ο���ѹѡ�� */
  66. x.CHS = ADC_CHS_AIN8;
  67. x.SMPS = ADC_SMPS_SOFT; /* AD����ģʽΪ�������� */
  68. //����ʱ��st*2+1(��Tadclk)=1.743us
  69. x.ST = 10; /* ADӲ������ʱ��ѡ�� */
  70. x.BITSEL = ADC_BITSEL_12; /* AD�ֱ���12λ */
  71. ADC_Init(&x);
  72. }
  73. void get_pa2_adc_value(void) {
  74. volatile uint32_t pa2_adc_val = 0;
  75. ADCPA2Init();
  76. ADC_SoftStart();
  77. //����ת��
  78. while (ADC_GetConvStatus() != RESET)
  79. ;
  80. pa2_adc_val = ADC_GetConvValue();
  81. ADC_SoftStop();
  82. printf("%d\r\n", pa2_adc_val);
  83. printf("pa2_val:%lf\r\n", pa2_adc_val * 3.3 / 4096);
  84. }
  85. void ADCPB8Init() {
  86. ADC_InitStruType x;
  87. GPIO_InitSettingType y;
  88. y.Signal = GPIO_Pin_Signal_Analog; //�
  89. y.Dir = GPIO_Direction_Input; //����
  90. y.Func = GPIO_Reuse_Func0;
  91. // x.CHS����adc�ɼ���ͨ��ÿ�����Ŷ�Ӧһ��ͨ��
  92. GPIO_Init(GPIO_Pin_B8, &y);
  93. //������Ƶ�Ժ�Tadclk=1/(PCLK/4)Լ����0.083us
  94. x.CLKS = ADC_CLKS_PCLK;
  95. x.CLKDIV = ADC_CLKDIV_1_32; /* ADCʱ��ԴԤ��Ƶ */
  96. //�ɼ�����ģ����*3.3/4096=���ڵĵ�ѹ
  97. x.VREF_SEL = ADC_VREF_SEL_0; /* �ڲ��ο���ѹ2.048v,�������ڲ��ο���ѹΪ���� */
  98. x.VREFP = ADC_VREFP_VDD; /* ѡ��оƬ�Ĺ�����ѹVDD������������adc������Ҫ�ĸ���ѹ�����ο�adc�IJο���ѹΪ���� */
  99. x.VREFN = ADC_VREFN_VSS; /* �����ο���ѹѡ�� */
  100. x.CHS = ADC_CHS_AIN3;
  101. x.SMPS = ADC_SMPS_SOFT; /* AD����ģʽΪ�������� */
  102. //����ʱ��st*2+1(��Tadclk)=1.743us
  103. x.ST = 10; /* ADӲ������ʱ��ѡ�� */
  104. x.BITSEL = ADC_BITSEL_12; /* AD�ֱ���12λ */
  105. ADC_Init(&x);
  106. }
  107. void get_pb8_adc_value(void) {
  108. volatile uint32_t pb8_adc_val = 0;
  109. ADCPB8Init();
  110. ADC_SoftStart();
  111. //����ת��
  112. while (ADC_GetConvStatus() != RESET)
  113. ;
  114. pb8_adc_val = ADC_GetConvValue();
  115. ADC_SoftStop();
  116. printf("%d\r\n", pb8_adc_val);
  117. printf("pb8_val:%lf\r\n", pb8_adc_val * 3.3 / 4096);
  118. }
  119. void ADCPB9Init() {
  120. ADC_InitStruType x;
  121. GPIO_InitSettingType y;
  122. y.Signal = GPIO_Pin_Signal_Analog; //�
  123. y.Dir = GPIO_Direction_Input; //����
  124. y.Func = GPIO_Reuse_Func0;
  125. // x.CHS����adc�ɼ���ͨ��ÿ�����Ŷ�Ӧһ��ͨ��
  126. GPIO_Init(GPIO_Pin_B9, &y);
  127. //������Ƶ�Ժ�Tadclk=1/(PCLK/32)
  128. x.CLKS = ADC_CLKS_PCLK;
  129. x.CLKDIV = ADC_CLKDIV_1_32; /* ADCʱ��ԴԤ��Ƶ */
  130. //�ɼ�����ģ����*3.3/4096=���ڵĵ�ѹ
  131. x.VREF_SEL = ADC_VREF_SEL_0; /* �ڲ��ο���ѹ2.048v,�������ڲ��ο���ѹΪ���� */
  132. x.VREFP = ADC_VREFP_VDD; /* ѡ��оƬ�Ĺ�����ѹVDD������������adc������Ҫ�ĸ���ѹ�����ο�adc�IJο���ѹΪ���� */
  133. x.VREFN = ADC_VREFN_VSS; /* �����ο���ѹѡ�� */
  134. x.CHS = ADC_CHS_AIN4;
  135. // x.CHS = ADC_CHS_AIN8;
  136. x.SMPS = ADC_SMPS_SOFT; /* AD����ģʽΪ�������� */
  137. //����ʱ��st*2+1(��Tadclk)=1.743us
  138. x.ST = 10; /* ADӲ������ʱ��ѡ�� */
  139. x.BITSEL = ADC_BITSEL_12; /* AD�ֱ���12λ */
  140. ADC_Init(&x);
  141. }
  142. void get_pb9_adc_value(void) {
  143. volatile uint32_t pb9_adc_val = 0;
  144. ADCPB9Init();
  145. ADC_SoftStart();
  146. //����ת��
  147. while (ADC_GetConvStatus() != RESET)
  148. ;
  149. pb9_adc_val = ADC_GetConvValue();
  150. ADC_SoftStop();
  151. printf("%d\r\n", pb9_adc_val);
  152. printf("pb9_val:%lf\r\n", pb9_adc_val * 3.3 / 4096);
  153. }
  154. void try_adc_gather(void) {
  155. static uint32_t adc_ticket = 0;
  156. // static uint32_t adc_ticket_count = 0;
  157. if (port_haspassedms(adc_ticket) > 1000) {
  158. // adc_ticket_count++;
  159. adc_ticket = get_sys_ticket();
  160. // if (adc_ticket_count < 5) {
  161. get_pb8_adc_value();
  162. get_pa2_adc_value();
  163. get_pb9_adc_value();
  164. // }
  165. // get_adc_value();
  166. // if (adc_ticket_count >= 5) {
  167. // if (adc_ticket_count == 10) {
  168. // adc_ticket_count = 0;
  169. // }
  170. // ADC_Disable();
  171. // get_pa2_adc_value();
  172. // }
  173. }
  174. }
  175. void get_adc_value(void) {
  176. // uint16_t adc_value = 0;
  177. for (int i = 0; i < 3; i++) {
  178. ADCInit(s_adc[i].port, s_adc[i].pin);
  179. ADC_SoftStart();
  180. while (ADC_GetConvStatus() == SET)
  181. ; //����ת��
  182. s_adc[i].adc_just_now_gather_val = ADC_GetConvValue();
  183. ADC_SoftStop();
  184. // record_adc_gather_value(s_adc[i].adc_just_now_gather_val);
  185. printf("%d\r\n", s_adc[i].adc_just_now_gather_val);
  186. printf("voltage:%lf\r\n", (s_adc[i].adc_just_now_gather_val) * 3.3 / 4096);
  187. }
  188. }
  189. void record_adc_gather_value(uint16_t adc_value) {
  190. /**
  191. * @brief ¼adcͨɼ
  192. *
  193. */
  194. for (int i = 0; i < 3; i++) {
  195. s_adc[i].adc_value_buff[s_adc[i].adc_value_count++] = adc_value;
  196. if (s_adc[i].adc_value_count == 5) {
  197. s_adc[i].past_count_reached_five = true;
  198. s_adc[i].adc_value_count = 0;
  199. }
  200. if (s_adc[i].past_count_reached_five == true) {
  201. beg_average_value(s_adc[i].adc_value_buff);
  202. }
  203. }
  204. }
  205. void beg_average_value(uint16_t *adc_buff) {
  206. // uint16_t adc_average_value = 0;
  207. // uint32_t adc_sum_value = 0;
  208. // for (int i = 0; i < 5; i++) {
  209. // adc_sum_value += *adc_buff;
  210. // adc_buff++;
  211. // }
  212. // adc_average_value = adc_sum_value / 5;
  213. // if (adc_average_value <= 16 || adc.abnormal_state == true) {
  214. // adc.abnormal_state = true;
  215. // process_voltage_abnormal();
  216. // // printf("short out%d\r\n", adc_average_value); //��·
  217. // } else {
  218. // //����adc_value=1700����ѹ=1700/4096=1.369
  219. // printf("adc_value_t%d\r\n", adc_average_value);
  220. // }
  221. }