Advertisement
Guest User

Untitled

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