1. (15) Write a small C program for the Tiva Launchpad that: (a) Configures Port
ID: 3854014 • Letter: 1
Question
1. (15) Write a small C program for the Tiva Launchpad that:
(a) Configures Port D as all digital inputs
(b) Should generate a flag when any kind of change appears on the pins (rising or falling edges)
(c) Counts the number of pins on Port D that currently have a ‘1’ as the input
(d) Saves that value to a variable called numOnes
(e) Performs these operations forever (except the initializations; these should only be performed once)
You may use either the DRA method or the TivaWare functions, whichever makes more sense to you
Explanation / Answer
#include<tm4c123gh6pm.h>
#include<stdbool.h>
#include<stdint.h>
#include<interrupt.h>
#include<sysctl.h>
#include<gpio.h>
#include<hw_memmap.h>
#include<timer.h>
void PortDIntHandler(void);
void PortDInit(void);
void TimerInit(void);
volatile unsigned long ui32Loop,Count,f;
float Frequency;
int main(void){
PortDInit();
while(1)
{}
}
void PortDInit(void){
IntDisable(INT_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE,GPIO_PIN_4);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,GPIO_PIN_3);
GPIO_PORTD_PUR_R |= 0x10;
IntEnable(INT_GPIOD);
GPIOIntEnable(GPIO_PORTD_BASE | GPIO_PIN_4, GPIO_FALLING_EDGE);
}
void PortDIntHandler(void){
if(f!=1)
TimerInit();
else
{
GPIO_PORTD_ICR_R = 0x10;
Count=TimerValueGet(TIMER0_BASE, TIMER_A);
Frequency=80000000/count
TimerInit();
f=1;
}}
void TimerInit(void)
{
TimerDisable(TIMER0_BASE,TIMER_A);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
TimerLoadSet(TIMER0_BASE, TIMER_A, 0xFFFFFFFF);
TimerEnable(TIMER0_BASE, TIMER_A);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.