/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ //initiate user button //PB_Config(); STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); //initiate LEDs and turn them on LED_Config(); /* ----------------------------------------------------------------------- TIM3 Configuration: Output Compare Timing Mode: In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM3CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK = HCLK / 2 = SystemCoreClock /2 To get TIM3 counter clock at 50 MHz, the prescaler is computed as follows: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /0.5 MHz) - 1 CC1 update rate = TIM3 counter clock / CCR1_Val = 10.0 Hz ==> Toggling frequency = 5 Hz Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() function to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. ----------------------------------------------------------------------- */ //=======================Configure and init Timer====================== /* Compute the prescaler value */ PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 500000) - 1; //configures clock speed at 500 KHz. Both Tim2 and Tim3 use the same prescsaler and therefore run at the same speed. /* TIM Configuration */ TIM3_Config(); TIM2_Config(); // configure the output compare TIM3_OCConfig(); TIM2_OCConfig(); /* TIM Interrupts enable */ TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM2, ENABLE); //======================================configure and init LCD ====================== /* LCD initiatization */ LCD_Init(); /* LCD Layer initiatization */ LCD_LayerInit(); /* Enable the LTDC */ LTDC_Cmd(ENABLE); /* Set LCD foreground layer */ LCD_SetLayer(LCD_FOREGROUND_LAYER); //================EEPROM init==================================== /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); /* EEPROM Init */ EE_Init(); //============ Set up for random number generation============== RNG_Config(); Ext_PushButton_Interrupt(); //configures external push button //with the default font, LCD can display 12 lines of chars, they are LINE(0), LINE(1)...LINE(11) //with the default font, LCD can display 15 columns, they are COLUMN(0)....COLUMN(14) LCD_Clear(LCD_COLOR_WHITE); //change the background colour of LCD //Display a string in one line, on the first line (line=0) LCD_DisplayString(0, 2, (uint8_t *) "Best: "); //the line will not wrap while (1){ if (UBPressed==1) { //press user button if (pause==1){ //pause mode randnum = ((RNG_GetRandomNumber()%2000)+1000); //generates a random number between 1000 and 3000 Pause_Random(randnum); //see below function to see how the pause is implemented } else { //measure time mode TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE); //turns off timer 2 TIM_Cmd(TIM2, DISABLE); time = timer; //gets user's time if (initial == 1) { //sets initial best_time to first time best_time = time; initial = 0; LCD_DisplayInt((uint16_t) 0, (uint16_t) 7, best_time); } LCD_DisplayString(2, 1, (uint8_t *) "Time: "); //print time LCD_DisplayString(2, 7, (uint8_t *) " "); //clears line LCD_DisplayInt((uint16_t) 2, (uint16_t) 7, time); //displays user's time LCD_DisplayString(2, 11, (uint8_t *) "ms"); //print ms if (time > 10 && time < best_time) { //set new best time best_time = time; LCD_DisplayString(0, 7, (uint8_t *) " "); //clears line LCD_DisplayInt((uint16_t) 0, (uint16_t) 7, best_time); } TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); //turns on timer 2 TIM_Cmd(TIM2, ENABLE); pause = 1; //this makes it so that you can use the user button to repeat the cycle in case you don't have an external push button } UBPressed=0; } } }
int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ //initiate user button PB_Config(); //initiate LEDs and turn them on LED_Config(); /* ----------------------------------------------------------------------- TIM3 Configuration: Output Compare Timing Mode: In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM3CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK = HCLK / 2 = SystemCoreClock /2 To get TIM3 counter clock at 50 MHz, the prescaler is computed as follows: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /50 MHz) - 1 CC1 update rate = TIM3 counter clock / CCR1_Val = 9.154 Hz ==> Toggling frequency = 4.57 Hz Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() function to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. ----------------------------------------------------------------------- */ //=======================Configure and init Timer====================== /* Compute the prescaler value */ PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 200000) - 1; /* TIM Configuration */ TIM2_Config(); // configure the output compare TIM2_OCConfig(); /* TIM Interrupts enable */ TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM2, ENABLE); while (1){ if (UBPressed==1) { STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); hardwareBlinking(); UBPressed=0; } } }