We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
In this webpage :http://www.keil.com/support/man/docs/gsac/gsac_timer.htm The question is why two variables one use volatile but another not ?? The two variables is "TimeTick" and "clock_1s". Who can tell me why,i am very confused. thank you !
/*----------------------------------------------------------------------------- * Name: Timer.c * Purpose: Timer example. Prints a sting to UART2 * Notes: *----------------------------------------------------------------------------*/ #include <stdio.h> #include <stm32f10x_cl.h>
volatile unsigned long TimeTick; // Counts 10ms timeTicks unsigned char clock_1s; // Flag activated each second
extern void init_serial (void); // Function defined in Serial.c
/****************************************************************************** Initialise the TIM3 for 1ms @ 72MHz ******************************************************************************/ void TIM3_Init (void) {
RCC->APB1ENR |= (1<<1); // enable clock for TIM3
TIM3->PSC = 3; // set prescaler TIM3->ARR = (18000000UL / 1000UL) - 1UL; // set auto-reload TIM3->CR1 = 0; // reset command register 1 TIM3->CR2 = 0; // reset command register 2 TIM3->DIER = (1<<0); // Update interrupt enabled TIM3->CR1 |= (1<<0); // Enable Timer
NVIC_EnableIRQ (TIM3_IRQn); // Enable TIM3 interrupt }
/*----------------------------------------------------------------------------- Timer Counter 3 interrupt service function executes each 1ms @ 25 MHz Crystal Clock *----------------------------------------------------------------------------*/ void TIM3_IRQHandler (void) {
if (TIM3->SR & (1<<0)) { // UIF interrupt? if (TimeTick++ >= 999) { // Set clock_1s to 1 every 1 second TimeTick = 0; clock_1s = 1; } TIM3->SR &= ~(1<<0); // clear UIF flag } }
Thank you very much for you answer, i understand. thank you