Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26.  
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31.  
  32. /* USER CODE END PTD */
  33.  
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. #define ALL_SEG_OFF() LL_GPIO_SetOutputPin(SEG_PORT,SH|SG|SF|SE|SD|SC|SB|SA);
  37. #define I2C_REQUEST_WRITE 0x00
  38. #define I2C_REQUEST_READ 0x01
  39. #define SLAVE_OWN_ADDRESS 0xEE
  40. /* USER CODE END PD */
  41.  
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44.  
  45. /* USER CODE END PM */
  46.  
  47. /* Private variables ---------------------------------------------------------*/
  48.  
  49. /* USER CODE BEGIN PV */
  50. uint16_t AD,AD1,AD2;
  51. extern uint16_t num_gl;
  52. uint8_t rd_value[20] = {0};
  53. uint8_t wr_value[20] = {0x14,0x13,0x12,0x11,0x10,
  54. 0x0F,0x0E,0x0D,0x0C,0x0B,
  55. 0x0A,0x09,0x08,0x07,0x06,
  56. 0x05,0x04,0x03,0x02,0x01};
  57. uint16_t buf[2] = {0};
  58. /* USER CODE END PV */
  59.  
  60. /* Private function prototypes -----------------------------------------------*/
  61. void SystemClock_Config(void);
  62. static void MX_GPIO_Init(void);
  63. static void MX_I2C1_Init(void);
  64. /* USER CODE BEGIN PFP */
  65.  
  66. /* USER CODE END PFP */
  67.  
  68. /* Private user code ---------------------------------------------------------*/
  69. /* USER CODE BEGIN 0 */
  70. void BMP085_SendCmd (uint8_t Reg, uint8_t Cntr)
  71. {
  72. //Disable Pos
  73. LL_I2C_DisableBitPOS(I2C1);
  74. LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
  75. LL_I2C_GenerateStartCondition(I2C1);
  76. while(!LL_I2C_IsActiveFlag_SB(I2C1)){};
  77. (void) I2C1->SR1;
  78. LL_I2C_TransmitData8(I2C1, SLAVE_OWN_ADDRESS | I2C_REQUEST_WRITE);
  79. while(!LL_I2C_IsActiveFlag_ADDR(I2C1)){};
  80. LL_I2C_ClearFlag_ADDR(I2C1);
  81. // LL_I2C_TransmitData8(I2C1, (uint8_t) (addr>>8));
  82. // while(!LL_I2C_IsActiveFlag_TXE(I2C1)){};
  83. LL_I2C_TransmitData8(I2C1, Reg);
  84. while(!LL_I2C_IsActiveFlag_TXE(I2C1)){};
  85.  
  86. LL_I2C_TransmitData8(I2C1, Cntr);
  87. while(!LL_I2C_IsActiveFlag_TXE(I2C1)){};
  88.  
  89. LL_I2C_GenerateStopCondition(I2C1);
  90. }
  91.  
  92. void BMP085_ReadTemp(uint8_t Reg)
  93. {
  94. uint8_t i;
  95. LL_I2C_DisableBitPOS(I2C1);
  96. LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
  97. LL_I2C_GenerateStartCondition(I2C1);
  98. while(!LL_I2C_IsActiveFlag_SB(I2C1)){};
  99. (void) I2C1->SR1;
  100. LL_I2C_TransmitData8(I2C1, SLAVE_OWN_ADDRESS | I2C_REQUEST_WRITE);
  101. while(!LL_I2C_IsActiveFlag_ADDR(I2C1)){};
  102. LL_I2C_ClearFlag_ADDR(I2C1);
  103. LL_I2C_TransmitData8(I2C1, Reg);
  104. while(!LL_I2C_IsActiveFlag_TXE(I2C1)){};
  105.  
  106. LL_I2C_GenerateStartCondition(I2C1);
  107. while(!LL_I2C_IsActiveFlag_SB(I2C1)){};
  108. (void) I2C1->SR1;
  109. LL_I2C_TransmitData8(I2C1, SLAVE_OWN_ADDRESS | I2C_REQUEST_READ);
  110. while (!LL_I2C_IsActiveFlag_ADDR(I2C1)){};
  111. LL_I2C_ClearFlag_ADDR(I2C1);
  112.  
  113. for(i=0;i<2;i++)
  114. {
  115. if(i<(2-1))
  116. {
  117. while(!LL_I2C_IsActiveFlag_RXNE(I2C1)){};
  118. buf[i] = LL_I2C_ReceiveData8(I2C1);
  119. }
  120. else
  121. {
  122. LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
  123. LL_I2C_GenerateStopCondition(I2C1);
  124. while(!LL_I2C_IsActiveFlag_RXNE(I2C1)){};
  125. buf[i] = LL_I2C_ReceiveData8(I2C1);
  126. }
  127. }
  128. AD=buf[0];
  129. AD1=buf[1];
  130.  
  131. }
  132. //------------------------------------------------
  133. /* USER CODE END 0 */
  134.  
  135. /**
  136. * @brief The application entry point.
  137. * @retval int
  138. */
  139. int main(void)
  140. {
  141. /* USER CODE BEGIN 1 */
  142.  
  143. /* USER CODE END 1 */
  144.  
  145.  
  146. /* MCU Configuration--------------------------------------------------------*/
  147.  
  148. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  149.  
  150.  
  151. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
  152. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  153.  
  154. NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  155.  
  156. /* System interrupt init*/
  157.  
  158. /* USER CODE BEGIN Init */
  159.  
  160. /* USER CODE END Init */
  161.  
  162. /* Configure the system clock */
  163. SystemClock_Config();
  164.  
  165. /* USER CODE BEGIN SysInit */
  166.  
  167. /* USER CODE END SysInit */
  168.  
  169. /* Initialize all configured peripherals */
  170. MX_GPIO_Init();
  171. MX_I2C1_Init();
  172. /* USER CODE BEGIN 2 */
  173. BMP085_SendCmd (0xF4, 0x2E);
  174. BMP085_ReadTemp(0xF6);
  175. /* USER CODE END 2 */
  176.  
  177. /* Infinite loop */
  178. /* USER CODE BEGIN WHILE */
  179. while (1)
  180. {
  181. LL_mDelay(1000);
  182. LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
  183. LL_mDelay(1000);
  184. LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_13);
  185. /* USER CODE END WHILE */
  186.  
  187. /* USER CODE BEGIN 3 */
  188. }
  189. /* USER CODE END 3 */
  190. }
  191.  
  192. /**
  193. * @brief System Clock Configuration
  194. * @retval None
  195. */
  196. void SystemClock_Config(void)
  197. {
  198. LL_FLASH_SetLatency(LL_FLASH_LATENCY_0);
  199.  
  200. if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_0)
  201. {
  202. Error_Handler();
  203. }
  204. LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE2);
  205. LL_RCC_HSI_SetCalibTrimming(16);
  206. LL_RCC_HSI_Enable();
  207.  
  208. /* Wait till HSI is ready */
  209. while(LL_RCC_HSI_IsReady() != 1)
  210. {
  211.  
  212. }
  213. LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  214. LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  215. LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
  216. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI);
  217.  
  218. /* Wait till System clock is ready */
  219. while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSI)
  220. {
  221.  
  222. }
  223. LL_Init1msTick(16000000);
  224. LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
  225. LL_SetSystemCoreClock(16000000);
  226. LL_RCC_SetTIMPrescaler(LL_RCC_TIM_PRESCALER_TWICE);
  227. }
  228.  
  229. /**
  230. * @brief I2C1 Initialization Function
  231. * @param None
  232. * @retval None
  233. */
  234. static void MX_I2C1_Init(void)
  235. {
  236.  
  237. /* USER CODE BEGIN I2C1_Init 0 */
  238.  
  239. /* USER CODE END I2C1_Init 0 */
  240.  
  241. LL_I2C_InitTypeDef I2C_InitStruct = {0};
  242.  
  243. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  244.  
  245. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  246. /**I2C1 GPIO Configuration
  247. PB6 ------> I2C1_SCL
  248. PB7 ------> I2C1_SDA
  249. */
  250. GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
  251. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  252. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  253. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  254. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  255. GPIO_InitStruct.Alternate = LL_GPIO_AF_4;
  256. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  257.  
  258. /* Peripheral clock enable */
  259. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
  260.  
  261. /* USER CODE BEGIN I2C1_Init 1 */
  262.  
  263. /* USER CODE END I2C1_Init 1 */
  264. /** I2C Initialization
  265. */
  266. LL_I2C_DisableOwnAddress2(I2C1);
  267. LL_I2C_DisableGeneralCall(I2C1);
  268. LL_I2C_EnableClockStretching(I2C1);
  269. I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
  270. I2C_InitStruct.ClockSpeed = 100000;
  271. I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
  272. I2C_InitStruct.OwnAddress1 = 0;
  273. I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
  274. I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  275. LL_I2C_Init(I2C1, &I2C_InitStruct);
  276. LL_I2C_SetOwnAddress2(I2C1, 0);
  277. /* USER CODE BEGIN I2C1_Init 2 */
  278.  
  279. /* USER CODE END I2C1_Init 2 */
  280.  
  281. }
  282.  
  283. /**
  284. * @brief GPIO Initialization Function
  285. * @param None
  286. * @retval None
  287. */
  288. static void MX_GPIO_Init(void)
  289. {
  290. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  291.  
  292. /* GPIO Ports Clock Enable */
  293. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
  294. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOH);
  295. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
  296. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  297.  
  298. /**/
  299. LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_13);
  300.  
  301. /**/
  302. GPIO_InitStruct.Pin = LL_GPIO_PIN_13;
  303. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  304. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  305. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  306. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  307. LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  308.  
  309. }
  310.  
  311. /* USER CODE BEGIN 4 */
  312.  
  313. /* USER CODE END 4 */
  314.  
  315. /**
  316. * @brief This function is executed in case of error occurrence.
  317. * @retval None
  318. */
  319. void Error_Handler(void)
  320. {
  321. /* USER CODE BEGIN Error_Handler_Debug */
  322. /* User can add his own implementation to report the HAL error return state */
  323.  
  324. /* USER CODE END Error_Handler_Debug */
  325. }
  326.  
  327. #ifdef USE_FULL_ASSERT
  328. /**
  329. * @brief Reports the name of the source file and the source line number
  330. * where the assert_param error has occurred.
  331. * @param file: pointer to the source file name
  332. * @param line: assert_param error line source number
  333. * @retval None
  334. */
  335. void assert_failed(uint8_t *file, uint32_t line)
  336. {
  337. /* USER CODE BEGIN 6 */
  338. /* User can add his own implementation to report the file name and line number,
  339. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  340. /* USER CODE END 6 */
  341. }
  342. #endif /* USE_FULL_ASSERT */
  343.  
  344. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement