This article was last updated on 2025-02-13, the content may be out of date.
Q1 Timer Calculations
For a Timer_A module operating with a system clock of 12 MHz and in “up mode”…
Q1.1
…what must be the timer’s period (in number of counts) be such that the overflow period is 8 ms? Assume that the timer’s clock divider is set to 32.
Timer clock=3212 MHz=375000 Hz
N=375000×0.008 s=3000 ticks
Q1.2
What is the smallest divider possible that could still allow the timer to produce the same overflow period? Assume, of course, that the timer’s period (in counts) can also change.
We have a 16-bit Timer_A (so its maximum count is 65536). We can use this inequality.
Convert the flow chart to an equivalent segment of code.
flowchart LR
A([Start]) --> B{Are a and b both zero?}
B -- Yes --> C([Done])
B -- No --> D[Divide a by 2 and save back into a]
D --> E[Multiply b by a and save back into b]
E --> B
flowchart LR
A([Start]) --> B{Are a and b both zero?}
B -- Yes --> C([Done])
B -- No --> D[Divide a by 2 and save back into a]
D --> E[Multiply b by a and save back into b]
E --> B
1
2
3
4
while(a==0&&b==0){a=a/2;b=b*a;}
Q4 Timer / Interrupt Code
Given the complete program below and knowing that SMCLK is 12 MHz, answer the following questions.
voidIncA(),IncB(),IncC();uint8_tA=0,B=0,C=0;Timer_A_UpModeConfigtim_config;uint32_ttimer=XXXXXXXXX;// Some valid value
voidmain(){SysInit();TimerInit();while(1){// To fill in
}}voidTimerInit(){tim_config.clockSource=TIMER_A_CLOCKSOURCE_SMCLK;tim_config.clockSourceDivider=TIMER_A_CLOCKSOURCE_DIVIDER_32;tim_config.timerPeriod=12345;tim_config.timerClear=TIMER_A_DO_CLEAR;tim_config.timerInterruptEnable_TAIE=TIMER_A_TAIE_INTERRUPT_ENABLE;Timer_A_configureUpMode(timer,&tim_config);Timer_A_registerInterrupt(timer,TIMER_A_CCRX_AND_OVERFLOW_INTERRUPT,IncC);Timer_A_startCounter(timer,TIMER_A_UP_MODE);}voidIncA(){Timer_A_clearInterruptFlag(TIMER_A1_BASE);A++;}voidIncB(){Timer_A_clearInterruptFlag(TIMER_A2_BASE);B++;}voidIncC(){Timer_A_clearInterruptFlag(TIMER_A3_BASE);C++;}
Q4.1
What function given in this code is and will be called as an Interrupt Service Routine? Give the function name.
1
IncC()
Because the line Timer_A_registerInterrupt(timer, TIMER_A_CCRX_AND_OVERFLOW_INTERRUPT, IncC); It sets up IncC() as the ISR for the timer interrupt.
Q4.2
How often is this function triggered by the hardware? Give your answer in milliseconds.
Timer Clock=3212MHz=375 kHz
Tick Period=375kHz1≈2.667,μs
Interrupt Period=12345×2.667μs≈32.92 ms
Q4.3
Write a segment of code to be placed in the while(1) loop that would implement a blocking delay of approximately 5 s without using __delay_cycles() such that the message “5 seconds” is printed after each delay.
Period≈12MHz/3212345≈32.92 ms
32.92 ms5000 ms≈152
1
2
3
C=0;while(C<152){}printf("5 seconds\n");
Q4.4
Write a segment of code to be placed in the while(1) loop that would implement a non-blocking delay of approximately 5 s without using __delay_cycles() such that the message “5 seconds” is printed after each delay and “not blocked” is printed continuously.