Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using code below, create a timer with the following specifications: /* Use switches to control an start/stop counter with */ /*up/down counting and display on

Using code below, create a timer with the following specifications:

image text in transcribed/* Use switches to control an start/stop counter with */

/*up/down counting and display on LED. Secondary inverse*/

/*decade counter (inverse of primary counter) */

/*======================================================*/

#include "STM32L1xx.h" /* Microcontroller information */

/* Define global variables */

uint8_t counter;

uint8_t pushedButton;

uint8_t columnWrite;

uint8_t rowRead;

/*------------------------------------------------------*/

/* Initialize GPIO pins used in the program */

/*------------------------------------------------------*/

void PinSetup()

{

/* Configure PA1-PA0 as input pins to read switch */

RCC->AHBENR |= 0x01; //Enable GPIOA clock (bit 0)

GPIOA->MODER &= ~(0x0000000C); //Enable PA1-PA0 as input

SYSCFG->EXTICR[0] &= 0xFF0F;

EXTI->RTSR |= 0x0002; //bit1=1 to make EXTI0 and EXTI1 rising-edge trig

EXTI->IMR |= 0x0002;

EXTI->PR |= 0x0002;

/* Configure PB3-PB0 inputs and PB7-PB4 as outputs*/

RCC->AHBENR |= 0x02;

GPIOB->MODER &= ~(0x0000FFFF);

GPIOB->MODER |= (0x00005500);

GPIOB->PUPDR &= ~(0x0000FFFF);

GPIOB->PUPDR |= (0x00000055);

/* Configure PC3-PC0 as output pins to write to the LEDs */

/*Configure PC7-PC4 as pull-up resistors */

RCC->AHBENR |= 0x04; //Enable GPIOC clock (bit 2)

GPIOC->MODER &= ~(0x000000FF); //Clear PC3-PC0 mode bits

GPIOC->MODER |= (0x00000055); //General purpose output mode

//GPIOC->PUPDR &= ~(0x0000FF00);

//GPIOC->PUPDR |= (0x00005500);

}

/*--------------------------------------------------------------*/

/* Delay function - do nothing for about 0.5 seconds */

/*--------------------------------------------------------------*/

void delay()

{

int i, j, n;

for (i = 0; i

{ //outer loop

for (j = 0; j

{

n = j; //dummy operation for single-step test

}

}

}

/*--------------------------------------------------------------*/

/* Count Up function - counts up */

/*--------------------------------------------------------------*/

uint8_t countUp(uint8_t counter)

{

uint8_t tempCounter;

tempCounter = counter;

if (counter == 9)

tempCounter = 0;

else

tempCounter++;

return tempCounter;

}

/*--------------------------------------------------------------*/

/*Counting function - counts up */

/*--------------------------------------------------------------*/

void counting()

{

counter = countUp(counter);

}

/*--------------------------------------------------------------*/

/*logBase2- due to no math, test cases are used */

/*--------------------------------------------------------------*/

int logBase2(uint8_t number)

{

number = ~number & (0xF);

if (number == 1)

return 1;

else if(number == 2)

return 2;

else if (number == 4)

return 3;

else if (number == 8)

return 4;

else

return 5; //error case, should never happen

}

/*--------------------------------------------------------------*/

/*findValue- Value from row and column */

/*--------------------------------------------------------------*/

uint8_t findValue()

{

uint8_t value;

if (columnWrite/16 == 7)

value = 9 + logBase2(rowRead);

else

value = 3 * (logBase2(rowRead) - 1) + logBase2(columnWrite/16);

if ((columnWrite/16 == 13)&&(rowRead == 7))

value = 0;

return value;

}

/*--------------------------------------------------------------*/

/*findPushedButton- finds the row/column */

/*--------------------------------------------------------------*/

uint8_t findPushedButton()

{

uint8_t value;

int k,i;

columnWrite = 7 * 32;

for (i = 0; i

{

GPIOB->ODR = columnWrite;

for(k=0;k

k=k;

rowRead = GPIOB->IDR & (0x000F);

if (rowRead != 15)

{

value = findValue();

i = 5;

}

columnWrite = columnWrite * 2 + 16;

}

return value;

}

/*--------------------------------------------------*/

/* Interupt PA01 */

/* determine button pressed. display for 5 delays */

/*increment counter without display */

/*--------------------------------------------------*/

void EXTI1_IRQHandler()

{

int i;

pushedButton = findPushedButton();

GPIOC->ODR = pushedButton;

for (i = 0; i

{

delay();

counting();

}

GPIOC->ODR = counter;

GPIOB->ODR = 0;

EXTI->PR |= 0x0002;

NVIC_ClearPendingIRQ(EXTI1_IRQn);

}

/*------------------------------------------------*/

/* Main program */

/*------------------------------------------------*/

int main(void)

{

PinSetup(); //Configure GPIO pins

NVIC_EnableIRQ(EXTI1_IRQn);

NVIC_ClearPendingIRQ(EXTI1_IRQn);

counter = 0; //Initial count state

GPIOB->ODR = 0;

__enable_irq();

GPIOC->ODR = counter; //Write count to the output data register of GPIO port C

/* Endless loop */

while (1)

{

counting();

GPIOC->ODR = counter;

delay();

}

}

Software Design Building on your previous projects, design a C program that initializes timer module TIM10, initializes the stopwatch to 0.0, and then enters a "do-nothing" loop to wait for a keypad interrupt. Stopwatch operation is to be as follows. 1. When the start/stop button is pressed for the first time, the timer and timer interrupts should be enabled and the stopwatch should begin running, updating the display every 0.1 seconds. The timing must be precise. You must measure it to verify the correct time base. 2. When the start/stop button is pressed again, the timer and/or timer interrupts should be disabled, effectively stopping the timer and freezing the display at the time corresponding to the instant at which the button was pressed display; the time should be reset to 0.0 only if the display has been cleared stopped. The clear button should be ignored if the stopwatch is running. 3. Pressing the start/stop button again should resume timing at the time shown on the 4. Pressing the clear button should reset the display to 0.0, but only if the stopwatch is Software Design Building on your previous projects, design a C program that initializes timer module TIM10, initializes the stopwatch to 0.0, and then enters a "do-nothing" loop to wait for a keypad interrupt. Stopwatch operation is to be as follows. 1. When the start/stop button is pressed for the first time, the timer and timer interrupts should be enabled and the stopwatch should begin running, updating the display every 0.1 seconds. The timing must be precise. You must measure it to verify the correct time base. 2. When the start/stop button is pressed again, the timer and/or timer interrupts should be disabled, effectively stopping the timer and freezing the display at the time corresponding to the instant at which the button was pressed display; the time should be reset to 0.0 only if the display has been cleared stopped. The clear button should be ignored if the stopwatch is running. 3. Pressing the start/stop button again should resume timing at the time shown on the 4. Pressing the clear button should reset the display to 0.0, but only if the stopwatch is

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions