Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * @file main.c
  4. * @author Ac6
  5. * @version V1.0
  6. * @date 01-December-2013
  7. * @brief Default main function.
  8. ******************************************************************************
  9. */
  10.  
  11.  
  12. #include "stm32f4xx.h"
  13. #include "stm32f4_discovery.h"
  14.  
  15. double ADC_Result, ADC_Result2;
  16. double score, score2;
  17. int main(void)
  18. {
  19.  
  20. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  21. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  22. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
  23.  
  24.  
  25. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  26.  
  27. GPIO_InitTypeDef GPIO_InitStructure1;
  28. /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  29. GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14
  30. | GPIO_Pin_15;
  31. GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_OUT;
  32. GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
  33. GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_100MHz;
  34. GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_NOPULL;
  35. GPIO_Init(GPIOD, &GPIO_InitStructure1);
  36.  
  37. GPIO_InitTypeDef GPIO_InitStructure;
  38. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
  39. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  40. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  41. GPIO_Init(GPIOA, &GPIO_InitStructure);
  42.  
  43. ADC_InitTypeDef ADC_InitStructure;
  44. ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  45. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  46. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  47. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  48. ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  49. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  50. ADC_InitStructure.ADC_NbrOfConversion = 1;
  51. ADC_Init(ADC1, &ADC_InitStructure);
  52.  
  53. ADC_InitTypeDef ADC_InitStructure2;
  54. ADC_InitStructure2.ADC_Resolution = ADC_Resolution_12b;
  55. ADC_InitStructure2.ADC_ScanConvMode = DISABLE;
  56. ADC_InitStructure2.ADC_ContinuousConvMode = ENABLE;
  57. ADC_InitStructure2.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  58. ADC_InitStructure2.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  59. ADC_InitStructure2.ADC_DataAlign = ADC_DataAlign_Right;
  60. ADC_InitStructure2.ADC_NbrOfConversion = 1;
  61. ADC_Init(ADC2, &ADC_InitStructure2);
  62.  
  63.  
  64. ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_84Cycles);
  65. ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 1, ADC_SampleTime_84Cycles);
  66.  
  67. ADC_Cmd(ADC1, ENABLE);
  68. ADC_Cmd(ADC2, ENABLE);
  69.  
  70. for (;;) {
  71. ADC_SoftwareStartConv(ADC1);
  72. ADC_SoftwareStartConv(ADC2);
  73.  
  74. while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
  75. ;
  76.  
  77. ADC_Result = ADC_GetConversionValue(ADC1);
  78. score = (ADC_Result * 3) / 4095;
  79.  
  80. while (ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET)
  81. ;
  82. ADC_Result2 = ADC_GetConversionValue(ADC2);
  83. score2 = (ADC_Result2 * 3) / 4095;
  84.  
  85. if (score > 0 && score < 0.75 && score2 > 0 && score2 < 0.75) {
  86. GPIO_SetBits(GPIOD, GPIO_Pin_12);
  87. GPIO_ResetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
  88. }
  89. if (score > 0.75 && score < 1.5 && score2 > 0.75 && score2 < 1.5) {
  90. GPIO_SetBits(GPIOD, GPIO_Pin_13);
  91. GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15);
  92. }
  93. if (score > 1.5 && score < 2.25 && score2 > 1.5 && score2 < 2.25) {
  94. GPIO_SetBits(GPIOD, GPIO_Pin_14);
  95. GPIO_ResetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_12 | GPIO_Pin_15);
  96. }
  97. if (score > 2.25 && score <= 3 && score2 > 2.25 && score2 <= 3) {
  98. GPIO_SetBits(GPIOD, GPIO_Pin_15);
  99. GPIO_ResetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_12);
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement