Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.94 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) 2020 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. #include "stm32l152c_discovery.h"
  28. #include "stm32l152c_discovery_glass_lcd.h"
  29.  
  30. /* USER CODE END Includes */
  31.  
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34.  
  35. /* USER CODE END PTD */
  36.  
  37. /* Private define ------------------------------------------------------------*/
  38. /* USER CODE BEGIN PD */
  39. /* USER CODE END PD */
  40.  
  41. /* Private macro -------------------------------------------------------------*/
  42. /* USER CODE BEGIN PM */
  43.  
  44. /* USER CODE END PM */
  45.  
  46. /* Private variables ---------------------------------------------------------*/
  47. ADC_HandleTypeDef hadc;
  48.  
  49. LCD_HandleTypeDef hlcd;
  50.  
  51. /* USER CODE BEGIN PV */
  52.  
  53. uint8_t estado = 0;
  54. uint8_t tiempo = 0;
  55.  
  56. /* USER CODE END PV */
  57.  
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. static void MX_GPIO_Init(void);
  61. static void MX_ADC_Init(void);
  62. static void MX_LCD_Init(void);
  63. static void MX_TS_Init(void);
  64. /* USER CODE BEGIN PFP */
  65.  
  66. /* USER CODE END PFP */
  67.  
  68. /* Private user code ---------------------------------------------------------*/
  69. /* USER CODE BEGIN 0 */
  70.  
  71. void EXTI0_IRQHandler(void) { //PA0
  72. if(EXTI->PR != 0){
  73. estado = 1;
  74. EXTI->PR = 0x01; //limpia el flag
  75. }
  76. }
  77.  
  78. void TIM3_IRQHandler(void) { //TOC
  79. if ((TIM3->SR & 0x02) == 0x02){
  80. TIM3->CCR1 += 65306;
  81. TIM3->SR &= ~ 0x02;
  82. }
  83. }
  84.  
  85. void TIM2_IRQHandler(void) { //TIC
  86. if((TIM2->SR & 0x8)!=0){
  87. tiempo = TIM2->CCR3; //leer el valor
  88. estado = 1;
  89. TIM2->SR &= ~ 0x8;
  90. }
  91. }
  92.  
  93.  
  94. /* USER CODE END 0 */
  95.  
  96. /**
  97. * @brief The application entry point.
  98. * @retval int
  99. */
  100. int main(void)
  101. {
  102. /* USER CODE BEGIN 1 */
  103.  
  104. /* USER CODE END 1 */
  105.  
  106.  
  107. /* MCU Configuration--------------------------------------------------------*/
  108.  
  109. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  110. HAL_Init();
  111.  
  112. /* USER CODE BEGIN Init */
  113.  
  114. /* USER CODE END Init */
  115.  
  116. /* Configure the system clock */
  117. SystemClock_Config();
  118.  
  119. /* USER CODE BEGIN SysInit */
  120.  
  121. /* USER CODE END SysInit */
  122.  
  123. /* Initialize all configured peripherals */
  124. MX_GPIO_Init();
  125. MX_ADC_Init();
  126. MX_LCD_Init();
  127. MX_TS_Init();
  128. /* USER CODE BEGIN 2 */
  129.  
  130. BSP_LCD_GLASS_Init();
  131. BSP_LCD_GLASS_BarLevelConfig(0);
  132. BSP_LCD_GLASS_Clear();
  133.  
  134. //Configuracion del GPIO
  135.  
  136. //Boton de inicio (PA0): USER -> entrada digital(00)
  137. GPIOA->MODER &= ~(1 << (0*2 + 1));
  138. GPIOA->MODER &= ~(1 << (0*2));
  139.  
  140. //LED verde (PB7) -> funcion alternativa(10) del TIM4 (0010)
  141. GPIOB->MODER |= (1 << ((7*2)+1));
  142. GPIOB->MODER &= ~(1 << (7*2));
  143. GPIOB->AFR[0] |= (1 << ((7*4) + 1));
  144. GPIOB->AFR[0] &= ~((1 << (7*4)) | (1 << ((7*4) + 2)) | (1 << ((7*4) + 3)));
  145.  
  146. //LED externo -> PD2 -> funcion alternativa (10) del TIM3
  147. GPIOD->MODER |= (1 << ((2*2)+1));
  148. GPIOD->MODER &= ~(1 << (2*2));
  149. GPIOB->AFR[0] |= (0x02 <<(2*4));
  150.  
  151. //Configuracion de la EXTI0 por flanco de bajada (PA0)
  152.  
  153. SYSCFG->EXTICR[0] &= ~(0x000F);
  154. EXTI->IMR |= 0x01;
  155. EXTI->RTSR |= 0x01;
  156. EXTI->FTSR &= ~(0x01);
  157. NVIC->ISER[0] |= (1 << 6);
  158.  
  159. //Configuracion del TIM4
  160.  
  161. //CH2-> PWM (PB7)
  162. TIM4->CR1 = 0x80;
  163. TIM4->CR2 = 0;
  164. TIM4->SMCR = 0;
  165. TIM4->CCMR1 = 0x6800;
  166. TIM4->CCER = 0x0010;
  167. TIM4->CNT = 0;
  168. TIM4->PSC = 1;
  169. TIM4->ARR = 63999;
  170. TIM4->CCR2 = 0; //DC = 0 y se va a ir aumentando
  171. TIM4->EGR |= 0x01;
  172. TIM4->DIER = 0; //no hay interrupcion
  173. TIM4->SR = 0x00;
  174. TIM4->CR1 |= 0x01;
  175.  
  176. //Configuracion del TIM3
  177.  
  178. //CH1 -> TOC (PD2)
  179. TIM3->CR1 = 0x00;
  180. TIM3->CR2 = 0;
  181. TIM3->SMCR = 0;
  182. TIM3->CCMR1 = 0x0030;
  183. TIM3->CCER = 0x0001;
  184. TIM3->CNT = 0;
  185. TIM3->PSC = 244;
  186. TIM3->ARR = 0xFFFF;
  187. TIM3->CCR1 = 65305;
  188. TIM3->EGR |= 0x01;
  189. TIM3->DIER = 0x02;
  190. TIM3->SR = 0x00;
  191. NVIC->ISER[0] |= (1<<29);
  192. TIM3->CR1 |= 0x01;
  193.  
  194. //Configuracion del TIM2
  195.  
  196. //CH3->TIC
  197. TIM2->CR1 = 0;
  198. TIM2->CR2 = 0;
  199. TIM2->SMCR = 0;
  200. TIM2->CCMR2 = 0x01;
  201. TIM2->CCER = 0x0100;
  202. TIM2->CNT = 0;
  203. TIM2->PSC = 31999;
  204. TIM2->ARR = 0xFFFF;
  205. TIM2->CCR3 = 0;
  206. TIM2->EGR |= 0x01;
  207. TIM2->DIER = 0x8;
  208. TIM2->SR = 0x00;
  209. NVIC->ISER[0] |= (1<<28);
  210. TIM2->CR1 |= 0x01;
  211.  
  212. short DC = 1;
  213.  
  214. /* USER CODE END 2 */
  215.  
  216.  
  217.  
  218. /* Infinite loop */
  219. /* USER CODE BEGIN WHILE */
  220. while (1)
  221. {
  222. if(estado){
  223. if((GPIOA->IDR & 0x00000001) != 0){ //compruebo que esta pulsado el boton PA0
  224. //medir el tiempo
  225.  
  226. tiempo++;
  227. //mostrarlo por la pantalla
  228. BSP_LCD_GLASS_Clear();
  229. BSP_LCD_GLASS_DisplayString((uint8_t *) + tiempo);
  230.  
  231. //ir cambiando el DC de pwm en el CCR2
  232. if(tiempo < 1){ //poner 32000 -> 1 segundo
  233. TIM4->CCR2 = 0;
  234. }else if(tiempo < 2 && tiempo >= 1){ //32000 -> 1 seg y 64000 -> 2 seg
  235. DC = 0.25;
  236. TIM4->CCR2 = TIM4->ARR * DC - 1; //15999 o tmb puede ser CCR2 = DC + 25;
  237. }else if(tiempo < 3 && tiempo >= 2){ //64000 -> 2 seg y 96000 -> 3 seg
  238. DC = 0.5;
  239. TIM4->CCR2 = TIM4->ARR * DC - 1; //31999
  240. }else if(tiempo < 4 && tiempo >= 3){ //96000 -> 3 seg y 128000 -> 4 seg
  241. DC = 0.75;
  242. TIM4->CCR2 = TIM4->ARR * DC - 1; //47999
  243. }else{
  244. DC = 1;
  245. TIM4->CCR2 = TIM4->ARR * DC - 1; //63999
  246. }
  247. estado = 0;
  248. }
  249. }
  250.  
  251. /* USER CODE END WHILE */
  252.  
  253. /* USER CODE BEGIN 3 */
  254. }
  255. /* USER CODE END 3 */
  256. }
  257.  
  258. /**
  259. * @brief System Clock Configuration
  260. * @retval None
  261. */
  262. void SystemClock_Config(void)
  263. {
  264. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  265. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  266. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  267.  
  268. /** Configure the main internal regulator output voltage
  269. */
  270. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  271. /** Initializes the CPU, AHB and APB busses clocks
  272. */
  273. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
  274. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  275. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  276. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  277. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  278. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  279. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  280. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  281. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  282. {
  283. Error_Handler();
  284. }
  285. /** Initializes the CPU, AHB and APB busses clocks
  286. */
  287. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  288. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  289. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  290. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  291. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  292. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  293.  
  294. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  295. {
  296. Error_Handler();
  297. }
  298. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LCD;
  299. PeriphClkInit.LCDClockSelection = RCC_RTCCLKSOURCE_LSE;
  300.  
  301. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  302. {
  303. Error_Handler();
  304. }
  305. }
  306.  
  307. /**
  308. * @brief ADC Initialization Function
  309. * @param None
  310. * @retval None
  311. */
  312. static void MX_ADC_Init(void)
  313. {
  314.  
  315. /* USER CODE BEGIN ADC_Init 0 */
  316.  
  317. /* USER CODE END ADC_Init 0 */
  318.  
  319. ADC_ChannelConfTypeDef sConfig = {0};
  320.  
  321. /* USER CODE BEGIN ADC_Init 1 */
  322.  
  323. /* USER CODE END ADC_Init 1 */
  324. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  325. */
  326. hadc.Instance = ADC1;
  327. hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  328. hadc.Init.Resolution = ADC_RESOLUTION_12B;
  329. hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  330. hadc.Init.ScanConvMode = ADC_SCAN_DISABLE;
  331. hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  332. hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
  333. hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
  334. hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
  335. hadc.Init.ContinuousConvMode = DISABLE;
  336. hadc.Init.NbrOfConversion = 1;
  337. hadc.Init.DiscontinuousConvMode = DISABLE;
  338. hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_CC3;
  339. hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  340. hadc.Init.DMAContinuousRequests = DISABLE;
  341. if (HAL_ADC_Init(&hadc) != HAL_OK)
  342. {
  343. Error_Handler();
  344. }
  345. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  346. */
  347. sConfig.Channel = ADC_CHANNEL_4;
  348. sConfig.Rank = ADC_REGULAR_RANK_1;
  349. sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
  350. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  351. {
  352. Error_Handler();
  353. }
  354. /* USER CODE BEGIN ADC_Init 2 */
  355.  
  356. /* USER CODE END ADC_Init 2 */
  357.  
  358. }
  359.  
  360. /**
  361. * @brief LCD Initialization Function
  362. * @param None
  363. * @retval None
  364. */
  365. static void MX_LCD_Init(void)
  366. {
  367.  
  368. /* USER CODE BEGIN LCD_Init 0 */
  369.  
  370. /* USER CODE END LCD_Init 0 */
  371.  
  372. /* USER CODE BEGIN LCD_Init 1 */
  373.  
  374. /* USER CODE END LCD_Init 1 */
  375. hlcd.Instance = LCD;
  376. hlcd.Init.Prescaler = LCD_PRESCALER_1;
  377. hlcd.Init.Divider = LCD_DIVIDER_16;
  378. hlcd.Init.Duty = LCD_DUTY_1_4;
  379. hlcd.Init.Bias = LCD_BIAS_1_4;
  380. hlcd.Init.VoltageSource = LCD_VOLTAGESOURCE_INTERNAL;
  381. hlcd.Init.Contrast = LCD_CONTRASTLEVEL_0;
  382. hlcd.Init.DeadTime = LCD_DEADTIME_0;
  383. hlcd.Init.PulseOnDuration = LCD_PULSEONDURATION_0;
  384. hlcd.Init.MuxSegment = LCD_MUXSEGMENT_DISABLE;
  385. hlcd.Init.BlinkMode = LCD_BLINKMODE_OFF;
  386. hlcd.Init.BlinkFrequency = LCD_BLINKFREQUENCY_DIV8;
  387. if (HAL_LCD_Init(&hlcd) != HAL_OK)
  388. {
  389. Error_Handler();
  390. }
  391. /* USER CODE BEGIN LCD_Init 2 */
  392.  
  393. /* USER CODE END LCD_Init 2 */
  394.  
  395. }
  396.  
  397. /**
  398. * @brief TS Initialization Function
  399. * @param None
  400. * @retval None
  401. */
  402. static void MX_TS_Init(void)
  403. {
  404.  
  405. /* USER CODE BEGIN TS_Init 0 */
  406.  
  407. /* USER CODE END TS_Init 0 */
  408.  
  409. /* USER CODE BEGIN TS_Init 1 */
  410.  
  411. /* USER CODE END TS_Init 1 */
  412. /* USER CODE BEGIN TS_Init 2 */
  413.  
  414. /* USER CODE END TS_Init 2 */
  415.  
  416. }
  417.  
  418. /**
  419. * @brief GPIO Initialization Function
  420. * @param None
  421. * @retval None
  422. */
  423. static void MX_GPIO_Init(void)
  424. {
  425. GPIO_InitTypeDef GPIO_InitStruct = {0};
  426.  
  427. /* GPIO Ports Clock Enable */
  428. __HAL_RCC_GPIOC_CLK_ENABLE();
  429. __HAL_RCC_GPIOA_CLK_ENABLE();
  430. __HAL_RCC_GPIOB_CLK_ENABLE();
  431.  
  432. /*Configure GPIO pin Output Level */
  433. HAL_GPIO_WritePin(GPIOB, LD4_Pin|LD3_Pin, GPIO_PIN_RESET);
  434.  
  435. /*Configure GPIO pin : PC13 */
  436. GPIO_InitStruct.Pin = GPIO_PIN_13;
  437. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  438. GPIO_InitStruct.Pull = GPIO_NOPULL;
  439. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  440.  
  441. /*Configure GPIO pin : B1_Pin */
  442. GPIO_InitStruct.Pin = B1_Pin;
  443. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  444. GPIO_InitStruct.Pull = GPIO_NOPULL;
  445. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  446.  
  447. /*Configure GPIO pins : LD4_Pin LD3_Pin */
  448. GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin;
  449. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  450. GPIO_InitStruct.Pull = GPIO_NOPULL;
  451. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  452. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  453.  
  454. }
  455.  
  456. /* USER CODE BEGIN 4 */
  457.  
  458. /* USER CODE END 4 */
  459.  
  460. /**
  461. * @brief This function is executed in case of error occurrence.
  462. * @retval None
  463. */
  464. void Error_Handler(void)
  465. {
  466. /* USER CODE BEGIN Error_Handler_Debug */
  467. /* User can add his own implementation to report the HAL error return state */
  468.  
  469. /* USER CODE END Error_Handler_Debug */
  470. }
  471.  
  472. #ifdef USE_FULL_ASSERT
  473. /**
  474. * @brief Reports the name of the source file and the source line number
  475. * where the assert_param error has occurred.
  476. * @param file: pointer to the source file name
  477. * @param line: assert_param error line source number
  478. * @retval None
  479. */
  480. void assert_failed(uint8_t *file, uint32_t line)
  481. {
  482. /* USER CODE BEGIN 6 */
  483. /* User can add his own implementation to report the file name and line number,
  484. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  485. /* USER CODE END 6 */
  486. }
  487. #endif /* USE_FULL_ASSERT */
  488.  
  489. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement