ENGR 2350 - Quiz 2

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.

$$\text{Timer clock} = \frac{12\text{ MHz}}{32} = 375000 \text{ Hz}$$ $$N = 375000 \times 0.008 \text{ s} = \boxed{3000} \text{ 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.

$$ \begin{align*} N_{DIV} &\ge T_{period} \times \frac{f_{SMCLK}}{N_{period}} \\ N_{DIV} &\ge 0.008 \times \frac{12000000}{65536} \\ N_{DIV} &\approx 1.4648 \end{align*} $$

The divider must be an integer, so we need round up to $\boxed{2}$

Q2 Basic GPIO

Answer the following questions about GPIO functionality and usage considering the circuit as provided below.

/engr-2350-quiz-02/q2-gpio.avif
Q2 Basic GPIO Pin Out

Q2.1

Initialize the GPIO used in the diagram above using Registers only. Do not modify any other pins in the port.

1
2
P6DIR |= 0x10; // for pin 4
P6DIR &= ~0x42; // or 0xBD for 0x40 (pin 6) and 0x02 (pin 1) 

Q2.2

Initialize the GPIO pins in the circuit diagram above using the DriverLib. Do not modify any other pins in the port.

1
2
GPIO_setAsInputPin(GPIO_PORT_P6, GPIO_PIN1 | GPIO_PIN6);
GPIO_setAsOutputPin(GPIO_PORT_P6, GPIO_PIN4);

Q2.3

Using Registers or the DriverLib, turn on LED1 only if PB1 is pressed and PB2 is not. The LED should be set off otherwise.

1
2
3
4
5
6
7
8
PB1 = GPIO_getInputPinValue(GPIO_PORT_P6, GPIO_PIN1);
PB2 = GPIO_getInputPinValue(GPIO_PORT_P6, GPIO_PIN6);

if (!PB1 && !PB2){ 
    GPIO_setOutputHighOnPin(GPIO_PORT_P3, GPIO_PIN6);
} else { 
    GPIO_setOutputLowOnPIN(GPIO_PORT_P3, GPIO_PIN6);
}

Q3

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
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void IncA(),IncB(),IncC();
uint8_t A = 0,B = 0,C = 0;
Timer_A_UpModeConfig tim_config;
uint32_t timer = XXXXXXXXX; // Some valid value

void main(){
  SysInit();
  TimerInit();
  while(1){
        // To fill in
  }
}

void TimerInit(){
  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);
}

void IncA(){
  Timer_A_clearInterruptFlag(TIMER_A1_BASE);
  A++;
}

void IncB(){
  Timer_A_clearInterruptFlag(TIMER_A2_BASE);
  B++;
}

void IncC(){
  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.

$$\text{Timer Clock} = \frac{12,\text{MHz}}{32} = 375 \text{ kHz}$$ $$\text{Tick Period} = \frac{1}{375,\text{kHz}} \approx 2.667 \, \mu\text{s}$$ $$\text{Interrupt Period} = 12345 \times 2.667,\mu\text{s} \approx \boxed{32.92} \text{ 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.

$$\text{Period} \approx \frac{12345}{12,\text{MHz}/32} \approx 32.92\text{ ms}$$ $$\frac{5000\text{ ms}}{32.92\text{ ms}} \approx 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.

1
2
3
4
5
printf("not blocked");
if (C >= 152) {
    printf("5 seconds");
    C = 0;
}

Related Content

0%