main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2016 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f1xx_hal.h"
  35. /* USER CODE BEGIN Includes */
  36. #include "string.h"
  37. /* USER CODE END Includes */
  38. /* Private variables ---------------------------------------------------------*/
  39. SPI_HandleTypeDef hspi3;
  40. /* USER CODE BEGIN PV */
  41. /* Private variables ---------------------------------------------------------*/
  42. uint32_t count=0,
  43. ADC_RCV[1024],
  44. CONF_STAT=0,
  45. MODE_STAT=0,
  46. CONT_CLK=0xFFFFFFFF;
  47. uint16_t ADC_COUNT=0, SKIP_COUNT=0;
  48. uint8_t ADC_RST[8]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
  49. LED_ON[2]={0x28,0x11},
  50. LED_OFF[2]={0x28,0x10},
  51. READ_DATA=0x58,
  52. READ_CONF=0b01010000,
  53. READ_MODE=0b01001000,
  54. WRITE_CONF[4]={0x10,0x00,0x41,0x10},
  55. WRITE_MODE[4]={0b00001000,0b00011000,0b00000000,0b01100000},
  56. ADC_VAL[5]={0,0,0,0,0},
  57. ADC_NUM[1024];
  58. /* USER CODE END PV */
  59. /* Private function prototypes -----------------------------------------------*/
  60. void SystemClock_Config(void);
  61. static void MX_GPIO_Init(void);
  62. static void MX_SPI3_Init(void);
  63. /* USER CODE BEGIN PFP */
  64. /* Private function prototypes -----------------------------------------------*/
  65. /* USER CODE END PFP */
  66. /* USER CODE BEGIN 0 */
  67. /* USER CODE END 0 */
  68. int main(void)
  69. {
  70. /* USER CODE BEGIN 1 */
  71. /* USER CODE END 1 */
  72. /* MCU Configuration----------------------------------------------------------*/
  73. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  74. HAL_Init();
  75. /* Configure the system clock */
  76. SystemClock_Config();
  77. /* Initialize all configured peripherals */
  78. MX_GPIO_Init();
  79. MX_SPI3_Init();
  80. /* USER CODE BEGIN 2 */
  81. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET);
  82. for (count=0;count<100000;count++);
  83. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
  84. HAL_SPI_Transmit(&hspi3,ADC_RST,5,1000);
  85. HAL_SPI_Transmit(&hspi3,WRITE_CONF,4,1000);
  86. while (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_9) == 1){SKIP_COUNT++;};
  87. HAL_SPI_Transmit(&hspi3,WRITE_MODE,4,1000);
  88. memset(ADC_RCV,0,sizeof(ADC_RCV));
  89. memset(ADC_NUM,0,sizeof(ADC_NUM));
  90. // HAL_SPI_Transmit(&hspi1,&READ_DATA,1,1000);
  91. /* USER CODE END 2 */
  92. /* Infinite loop */
  93. /* USER CODE BEGIN WHILE */
  94. while (1)
  95. {
  96. HAL_SPI_Transmit(&hspi3,LED_ON,2,1000);
  97. HAL_SPI_TransmitReceive(&hspi3,&READ_DATA,ADC_VAL,5,1000);
  98. HAL_SPI_Transmit(&hspi3,LED_OFF,2,1000);
  99. ADC_RCV[ADC_COUNT]=ADC_VAL[3] + (ADC_VAL[2]<<8) + (ADC_VAL[1]<<16);
  100. ADC_NUM[ADC_COUNT]=ADC_VAL[4];
  101. while (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_9) == 1){SKIP_COUNT++;};
  102. if (ADC_COUNT == 1024){ADC_COUNT = 0;};
  103. ADC_COUNT++;
  104. /* USER CODE END WHILE */
  105. /* USER CODE BEGIN 3 */
  106. }
  107. /* USER CODE END 3 */
  108. }
  109. /** System Clock Configuration
  110. */
  111. void SystemClock_Config(void)
  112. {
  113. RCC_OscInitTypeDef RCC_OscInitStruct;
  114. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  115. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  116. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  117. RCC_OscInitStruct.HSICalibrationValue = 16;
  118. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  119. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  120. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  121. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  122. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  123. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  124. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  125. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  126. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
  127. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  128. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  129. /* SysTick_IRQn interrupt configuration */
  130. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  131. }
  132. /* SPI3 init function */
  133. void MX_SPI3_Init(void)
  134. {
  135. hspi3.Instance = SPI3;
  136. hspi3.Init.Mode = SPI_MODE_MASTER;
  137. hspi3.Init.Direction = SPI_DIRECTION_2LINES;
  138. hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
  139. hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
  140. hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
  141. hspi3.Init.NSS = SPI_NSS_SOFT;
  142. hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  143. hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
  144. hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
  145. hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  146. hspi3.Init.CRCPolynomial = 10;
  147. HAL_SPI_Init(&hspi3);
  148. }
  149. /** Configure pins as
  150. * Analog
  151. * Input
  152. * Output
  153. * EVENT_OUT
  154. * EXTI
  155. */
  156. void MX_GPIO_Init(void)
  157. {
  158. GPIO_InitTypeDef GPIO_InitStruct;
  159. /* GPIO Ports Clock Enable */
  160. __HAL_RCC_GPIOA_CLK_ENABLE();
  161. __HAL_RCC_GPIOB_CLK_ENABLE();
  162. /*Configure GPIO pin Output Level */
  163. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
  164. /*Configure GPIO pin : PB8 */
  165. GPIO_InitStruct.Pin = GPIO_PIN_8;
  166. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  167. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  168. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  169. /*Configure GPIO pin : PB9 */
  170. GPIO_InitStruct.Pin = GPIO_PIN_9;
  171. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  172. GPIO_InitStruct.Pull = GPIO_NOPULL;
  173. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  174. }
  175. /* USER CODE BEGIN 4 */
  176. /* USER CODE END 4 */
  177. #ifdef USE_FULL_ASSERT
  178. /**
  179. * @brief Reports the name of the source file and the source line number
  180. * where the assert_param error has occurred.
  181. * @param file: pointer to the source file name
  182. * @param line: assert_param error line source number
  183. * @retval None
  184. */
  185. void assert_failed(uint8_t* file, uint32_t line)
  186. {
  187. /* USER CODE BEGIN 6 */
  188. /* User can add his own implementation to report the file name and line number,
  189. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  190. /* USER CODE END 6 */
  191. }
  192. #endif
  193. /**
  194. * @}
  195. */
  196. /**
  197. * @}
  198. */
  199. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/