Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- *
- * COPYRIGHT(c) 2016 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f1xx_hal.h"
- /* USER CODE BEGIN Includes */
- #include <string.h>
- #include <stdlib.h>
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc1;
- SPI_HandleTypeDef hspi3;
- TIM_HandleTypeDef htim1;
- UART_HandleTypeDef huart2;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- int Time = 30;
- int Timeleft = 30;
- char StartMessage[] = "Start Game !!\r\n";
- char ADCBarMessage[] = "Your Password bar at below !!\r\n";
- char EndMessage[] = "End game.\r\n\n";
- int Password[4];
- int randomNumber;
- int countMs=0;
- int currentDigit;
- int Score=0, highScore=0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_ADC1_Init(void);
- static void MX_SPI3_Init(void);
- static void MX_TIM1_Init(void);
- static void MX_USART2_UART_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- void displayGate();
- void displayBar(uint16_t);
- int reverse_bits(int);
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- int reverse_bits (int num)
- {
- int result;
- result = (16 & (num<<4));
- result = (result | ((8 & num<<2)));
- result = (result | (4 & num));
- result = (result | ((2 & num>>2)));
- result = (result | ((1 & num>>4)));
- return result;
- }
- void displayGate(){
- char str [] = "\n\r0 1 2 3 4 5 6 7 8 9\n\r";
- while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC)==RESET){}
- HAL_UART_Transmit(&huart2, (uint8_t*) str, strlen(str),1000);
- }
- void displayBar(uint16_t level){
- char str[20];
- char bar = 178;
- int i = 0;
- switch(level){
- case 0 : i = 1; break;
- case 1 : i = 3; break;
- case 2 : i = 5; break;
- case 3 : i = 7; break;
- case 4 : i = 9; break;
- case 5 : i = 11; break;
- case 6 : i = 13; break;
- case 7 : i = 15; break;
- case 8 : i = 17; break;
- case 9 : i = 19; break;
- }
- int k;
- for(k=0;k<i;k++)
- str[k] = bar;
- for(k=i;k<20;k++)
- str[k] = ' ';
- str[20] = '\0';
- while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC)==RESET){}
- HAL_UART_Transmit(&huart2, (uint8_t*) "\r", 2,1000);
- while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC)==RESET){}
- HAL_UART_Transmit(&huart2, (uint8_t*) str, strlen(str),1000);
- }
- /* USER CODE END 0 */
- 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();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_ADC1_Init();
- MX_SPI3_Init();
- MX_TIM1_Init();
- MX_USART2_UART_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- volatile uint16_t adc_val;
- volatile uint16_t level;
- while (1)
- {
- // if press start button
- if(HAL_GPIO_ReadPin(TCS_GPIO_PORT,TCS_PEN_PIN)== GPIO_PIN_RESET){
- //Output to UART2
- HAL_UART_Transmit(&huart2, (uint8_t*) StartMessage, strlen(StartMessage), 500);
- HAL_GPIO_WritePin(GPIOE, GPIO_PIN_ALL, GPIO_PIN_RESET);
- //random Password
- srand(time(NULL));
- randomNumber = rand()%10;
- Password[0] = randomNumber;
- srand(time(NULL));
- randomNumber = rand()%10;
- Password[1] = randomNumber;
- srand(time(NULL));
- randomNumber = rand()%10;
- Password[2] = randomNumber;
- srand(time(NULL));
- randomNumber = rand()%10;
- Password[3] = randomNumber;
- // Create Screen to playing
- //Playing Screen
- // Settime
- Timeleft = Time;
- countMs = 0;
- currentDigit = 0;
- Score = 0;
- HAL_UART_Transmit(&huart2, (uint8_t*) ADCBarMessage, strlen(ADCBarMessage), 500);
- While(Timeleft>0) {
- displayGate();
- while(HAL_ADC_PollForConversion(&hadc1,100)!=HAL_OK){}
- adc_val = HAL_ADC_GetValue(&hadc1);
- int level = adc_val/409.6;
- displayBar(level);
- level = reverse_bits(level);
- GPIOE->BSRR = 0xFF000000;
- GPIOE->BSRR = level<<8;
- HAL_Delay(500);
- if (level == Password[currentDigit])
- {
- //next password
- currentDigit++;
- if (currentDigit == 4)
- {
- // Winner !!!!!!!!
- Score = Timeleft;
- break;
- }
- }
- }
- if (Score > highScore)
- {
- highScore = Score;
- }
- //End Screen
- HAL_GPIO_WritePin(GPIOE, GPIO_PIN_ALL, GPIO_PIN_SET);
- HAL_UART_Transmit(&huart2, (uint8_t*) EndMessage, strlen(EndMessage), 500);
- }
- //Create Home Screen
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV5;
- RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
- RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL8;
- RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV5;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
- PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
- HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- __HAL_RCC_PLLI2S_ENABLE();
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* ADC1 init function */
- void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Common config
- */
- hadc1.Instance = ADC1;
- hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 1;
- HAL_ADC_Init(&hadc1);
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_14;
- sConfig.Rank = 1;
- sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
- HAL_ADC_ConfigChannel(&hadc1, &sConfig);
- }
- /* SPI3 init function */
- void MX_SPI3_Init(void)
- {
- hspi3.Instance = SPI3;
- hspi3.Init.Mode = SPI_MODE_MASTER;
- hspi3.Init.Direction = SPI_DIRECTION_2LINES;
- hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH;
- hspi3.Init.CLKPhase = SPI_PHASE_2EDGE;
- hspi3.Init.NSS = SPI_NSS_SOFT;
- hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
- hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi3.Init.TIMode = SPI_TIMODE_DISABLED;
- hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
- hspi3.Init.CRCPolynomial = 10;
- HAL_SPI_Init(&hspi3);
- }
- /* TIM1 init function */
- void MX_TIM1_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig;
- TIM_MasterConfigTypeDef sMasterConfig;
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 72-1;
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim1.Init.Period = 1000-1;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- HAL_TIM_Base_Init(&htim1);
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
- }
- /* USART2 init function */
- void MX_USART2_UART_Init(void)
- {
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- HAL_UART_Init(&huart2);
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOE_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- /*Configure GPIO pins : PE3 PE4 */
- GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
- /*Configure GPIO pins : PE5 PE6 PE7 */
- GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
- /*Configure GPIO pins : PE8 PE9 PE10 PE11
- PE12 PE13 PE14 PE15 */
- GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
- |GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
- HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
- /*Configure GPIO pin : PC8 */
- GPIO_InitStruct.Pin = GPIO_PIN_8;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pin : PD7 */
- GPIO_InitStruct.Pin = GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- #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,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
- ---------------------------------------------------------------------------------------
- /**
- ******************************************************************************
- * @file stm32f1xx_it.c
- * @brief Interrupt Service Routines.
- ******************************************************************************
- *
- * COPYRIGHT(c) 2016 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f1xx_hal.h"
- #include "stm32f1xx.h"
- #include "stm32f1xx_it.h"
- /* USER CODE BEGIN 0 */
- #include "string.h"
- #include <stdlib.h>
- extern Time, Timeleft;
- extern UART_HandleTypeDef huart2;
- extern int Password[400;
- extern countMs;
- /* USER CODE END 0 */
- /* External variables --------------------------------------------------------*/
- extern TIM_HandleTypeDef htim1;
- /******************************************************************************/
- /* Cortex-M3 Processor Interruption and Exception Handlers */
- /******************************************************************************/
- /**
- * @brief This function handles System tick timer.
- */
- void SysTick_Handler(void)
- {
- /* USER CODE BEGIN SysTick_IRQn 0 */
- /* USER CODE END SysTick_IRQn 0 */
- HAL_IncTick();
- HAL_SYSTICK_IRQHandler();
- /* USER CODE BEGIN SysTick_IRQn 1 */
- /* USER CODE END SysTick_IRQn 1 */
- }
- /******************************************************************************/
- /* STM32F1xx Peripheral Interrupt Handlers */
- /* Add here the Interrupt Handlers for the used peripherals. */
- /* For the available peripheral interrupt handler names, */
- /* please refer to the startup file (startup_stm32f1xx.s). */
- /******************************************************************************/
- /**
- * @brief This function handles TIM1 update interrupt.
- */
- void TIM1_UP_IRQHandler(void)
- {
- /* USER CODE BEGIN TIM1_UP_IRQn 0 */
- if (countMs == 1000)
- {
- countMs = 0;
- Timeleft--;
- }
- /* USER CODE END TIM1_UP_IRQn 0 */
- HAL_TIM_IRQHandler(&htim1);
- /* USER CODE BEGIN TIM1_UP_IRQn 1 */
- countMs++;
- /* USER CODE END TIM1_UP_IRQn 1 */
- }
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement