Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2020 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stm32l152c_discovery.h"
- #include "stm32l152c_discovery_glass_lcd.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc;
- LCD_HandleTypeDef hlcd;
- /* USER CODE BEGIN PV */
- uint8_t estado = 0;
- uint8_t tiempo = 0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_ADC_Init(void);
- static void MX_LCD_Init(void);
- static void MX_TS_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- void EXTI0_IRQHandler(void) { //PA0
- if(EXTI->PR != 0){
- estado = 1;
- EXTI->PR = 0x01; //limpia el flag
- }
- }
- void TIM3_IRQHandler(void) { //TOC
- if ((TIM3->SR & 0x02) == 0x02){
- TIM3->CCR1 += 65306;
- TIM3->SR &= ~ 0x02;
- }
- }
- void TIM2_IRQHandler(void) { //TIC
- if((TIM2->SR & 0x8)!=0){
- tiempo = TIM2->CCR3; //leer el valor
- estado = 1;
- TIM2->SR &= ~ 0x8;
- }
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_ADC_Init();
- MX_LCD_Init();
- MX_TS_Init();
- /* USER CODE BEGIN 2 */
- BSP_LCD_GLASS_Init();
- BSP_LCD_GLASS_BarLevelConfig(0);
- BSP_LCD_GLASS_Clear();
- //Configuracion del GPIO
- //Boton de inicio (PA0): USER -> entrada digital(00)
- GPIOA->MODER &= ~(1 << (0*2 + 1));
- GPIOA->MODER &= ~(1 << (0*2));
- //LED verde (PB7) -> funcion alternativa(10) del TIM4 (0010)
- GPIOB->MODER |= (1 << ((7*2)+1));
- GPIOB->MODER &= ~(1 << (7*2));
- GPIOB->AFR[0] |= (1 << ((7*4) + 1));
- GPIOB->AFR[0] &= ~((1 << (7*4)) | (1 << ((7*4) + 2)) | (1 << ((7*4) + 3)));
- //LED externo -> PD2 -> funcion alternativa (10) del TIM3
- GPIOD->MODER |= (1 << ((2*2)+1));
- GPIOD->MODER &= ~(1 << (2*2));
- GPIOB->AFR[0] |= (0x02 <<(2*4));
- //Configuracion de la EXTI0 por flanco de bajada (PA0)
- SYSCFG->EXTICR[0] &= ~(0x000F);
- EXTI->IMR |= 0x01;
- EXTI->RTSR |= 0x01;
- EXTI->FTSR &= ~(0x01);
- NVIC->ISER[0] |= (1 << 6);
- //Configuracion del TIM4
- //CH2-> PWM (PB7)
- TIM4->CR1 = 0x80;
- TIM4->CR2 = 0;
- TIM4->SMCR = 0;
- TIM4->CCMR1 = 0x6800;
- TIM4->CCER = 0x0010;
- TIM4->CNT = 0;
- TIM4->PSC = 1;
- TIM4->ARR = 63999;
- TIM4->CCR2 = 0; //DC = 0 y se va a ir aumentando
- TIM4->EGR |= 0x01;
- TIM4->DIER = 0; //no hay interrupcion
- TIM4->SR = 0x00;
- TIM4->CR1 |= 0x01;
- //Configuracion del TIM3
- //CH1 -> TOC (PD2)
- TIM3->CR1 = 0x00;
- TIM3->CR2 = 0;
- TIM3->SMCR = 0;
- TIM3->CCMR1 = 0x0030;
- TIM3->CCER = 0x0001;
- TIM3->CNT = 0;
- TIM3->PSC = 244;
- TIM3->ARR = 0xFFFF;
- TIM3->CCR1 = 65305;
- TIM3->EGR |= 0x01;
- TIM3->DIER = 0x02;
- TIM3->SR = 0x00;
- NVIC->ISER[0] |= (1<<29);
- TIM3->CR1 |= 0x01;
- //Configuracion del TIM2
- //CH3->TIC
- TIM2->CR1 = 0;
- TIM2->CR2 = 0;
- TIM2->SMCR = 0;
- TIM2->CCMR2 = 0x01;
- TIM2->CCER = 0x0100;
- TIM2->CNT = 0;
- TIM2->PSC = 31999;
- TIM2->ARR = 0xFFFF;
- TIM2->CCR3 = 0;
- TIM2->EGR |= 0x01;
- TIM2->DIER = 0x8;
- TIM2->SR = 0x00;
- NVIC->ISER[0] |= (1<<28);
- TIM2->CR1 |= 0x01;
- short DC = 1;
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- if(estado){
- if((GPIOA->IDR & 0x00000001) != 0){ //compruebo que esta pulsado el boton PA0
- //medir el tiempo
- tiempo++;
- //mostrarlo por la pantalla
- BSP_LCD_GLASS_Clear();
- BSP_LCD_GLASS_DisplayString((uint8_t *) + tiempo);
- //ir cambiando el DC de pwm en el CCR2
- if(tiempo < 1){ //poner 32000 -> 1 segundo
- TIM4->CCR2 = 0;
- }else if(tiempo < 2 && tiempo >= 1){ //32000 -> 1 seg y 64000 -> 2 seg
- DC = 0.25;
- TIM4->CCR2 = TIM4->ARR * DC - 1; //15999 o tmb puede ser CCR2 = DC + 25;
- }else if(tiempo < 3 && tiempo >= 2){ //64000 -> 2 seg y 96000 -> 3 seg
- DC = 0.5;
- TIM4->CCR2 = TIM4->ARR * DC - 1; //31999
- }else if(tiempo < 4 && tiempo >= 3){ //96000 -> 3 seg y 128000 -> 4 seg
- DC = 0.75;
- TIM4->CCR2 = TIM4->ARR * DC - 1; //47999
- }else{
- DC = 1;
- TIM4->CCR2 = TIM4->ARR * DC - 1; //63999
- }
- estado = 0;
- }
- }
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
- RCC_OscInitStruct.LSEState = RCC_LSE_ON;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
- RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LCD;
- PeriphClkInit.LCDClockSelection = RCC_RTCCLKSOURCE_LSE;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief ADC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_ADC_Init(void)
- {
- /* USER CODE BEGIN ADC_Init 0 */
- /* USER CODE END ADC_Init 0 */
- ADC_ChannelConfTypeDef sConfig = {0};
- /* USER CODE BEGIN ADC_Init 1 */
- /* USER CODE END ADC_Init 1 */
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc.Instance = ADC1;
- hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
- hadc.Init.Resolution = ADC_RESOLUTION_12B;
- hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc.Init.ScanConvMode = ADC_SCAN_DISABLE;
- hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
- hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
- hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
- hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
- hadc.Init.ContinuousConvMode = DISABLE;
- hadc.Init.NbrOfConversion = 1;
- hadc.Init.DiscontinuousConvMode = DISABLE;
- hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_CC3;
- hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
- hadc.Init.DMAContinuousRequests = DISABLE;
- if (HAL_ADC_Init(&hadc) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_4;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN ADC_Init 2 */
- /* USER CODE END ADC_Init 2 */
- }
- /**
- * @brief LCD Initialization Function
- * @param None
- * @retval None
- */
- static void MX_LCD_Init(void)
- {
- /* USER CODE BEGIN LCD_Init 0 */
- /* USER CODE END LCD_Init 0 */
- /* USER CODE BEGIN LCD_Init 1 */
- /* USER CODE END LCD_Init 1 */
- hlcd.Instance = LCD;
- hlcd.Init.Prescaler = LCD_PRESCALER_1;
- hlcd.Init.Divider = LCD_DIVIDER_16;
- hlcd.Init.Duty = LCD_DUTY_1_4;
- hlcd.Init.Bias = LCD_BIAS_1_4;
- hlcd.Init.VoltageSource = LCD_VOLTAGESOURCE_INTERNAL;
- hlcd.Init.Contrast = LCD_CONTRASTLEVEL_0;
- hlcd.Init.DeadTime = LCD_DEADTIME_0;
- hlcd.Init.PulseOnDuration = LCD_PULSEONDURATION_0;
- hlcd.Init.MuxSegment = LCD_MUXSEGMENT_DISABLE;
- hlcd.Init.BlinkMode = LCD_BLINKMODE_OFF;
- hlcd.Init.BlinkFrequency = LCD_BLINKFREQUENCY_DIV8;
- if (HAL_LCD_Init(&hlcd) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN LCD_Init 2 */
- /* USER CODE END LCD_Init 2 */
- }
- /**
- * @brief TS Initialization Function
- * @param None
- * @retval None
- */
- static void MX_TS_Init(void)
- {
- /* USER CODE BEGIN TS_Init 0 */
- /* USER CODE END TS_Init 0 */
- /* USER CODE BEGIN TS_Init 1 */
- /* USER CODE END TS_Init 1 */
- /* USER CODE BEGIN TS_Init 2 */
- /* USER CODE END TS_Init 2 */
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, LD4_Pin|LD3_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : PC13 */
- GPIO_InitStruct.Pin = GPIO_PIN_13;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pins : LD4_Pin LD3_Pin */
- GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement