Lab09-I/O Flash LED at 10Hz and record Data array

Purpose
In Lab09 you will learn about time delays, arrays and functional debugging. The data you collect will be physical proof that the system operates within specifications.

  • First, make the LED flash at 10 Hz. In other words, make it turn on for 0.05 seconds, and then turn off for 0.05 seconds.
  • Second, make the LED flash if either switch SW1 or SW2 is pressed (this means flash the LED if either PF4 or PF0 is 0).
  • Third, record PortF bits 4,1,0 every time the input changes or the output changes. For example, if your system detects a change in either PF4 or PF0 input, record PortF bits 4,1,0. If your system causes a change in PF1, record PortF bits 4,1,0.
  • If both PF4 and PF0 switch are not pressed, the PF1 output should be low.
  • If either PF4 or PF0 switches is pressed, the output toggles at 10 Hz (±10%).
  • Information collected in the Data array matches the I/O on PortF.
  • 50 data points are collected only on a change in input or a change in output.
  • This means no adjacent elements in the array should be equal.

Figure 9.1. Logic analyzer output showing PF1 toggles at 10 Hz whenever PF0 or PF4 is low. If both PF0 and PF4 are high, then the output PF1 should be low.



Solved by Tolulope Ademilua:
/*Copyright 2016 by Jonathan W. Valvano, valvano@mail.utexas.edu
You may use, edit, run or distribute this file
 as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,     INCIDENTAL,
 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.*/

// ***** 1. Pre-processor Directives Section *****
#include "TExaS.h"
#include "tm4c123gh6pm.h"
// ***** 2. Global Declarations Section *****
// FUNCTION PROTOTYPES: Each subroutine defined
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
void PortF_Init(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F
delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out
GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0
GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0
}
// Initialize SysTick with busy wait running at bus clock.
void SysTick_Init(void){
NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_RELOAD_R = 0x00FFFFFF; // maximum reload value
NVIC_ST_CURRENT_R = 0; // any write to current clears it
NVIC_ST_CTRL_R = 0x00000005; // enable SysTick with core clock
}
unsigned long Led; // output LED
unsigned long Sw1, Sw2; // input switches
void Delay(void){
unsigned long volatile time;
time = 80000; // changed clock from 0.1sec -> 0.05sec
while(time){
time--;
}
}
// first data point is wrong, the other 49 will be correct
unsigned long Time[50];
// you must leave the Data array defined exactly as it is
unsigned long Data[50];
int main(void){
unsigned long i,last,now;
unsigned long last_sw1, last_sw2, last_led; // helper variables to remember
// the previous state
unsigned short change = 0; // helper flag to determine the change of state
TExaS_Init(SW_PIN_PF40, LED_PIN_PF1); // activate grader and set system clock to 16 MHz
PortF_Init(); // initialize PF1 to output
SysTick_Init(); // initialize SysTick, runs at 16 MHz
i = 0; // array index
last = NVIC_ST_CURRENT_R;
last_sw1 = GPIO_PORTF_DATA_R&0x10; // fix last state of sw1
last_sw2 = GPIO_PORTF_DATA_R&0x01; // fix last state of sw2
last_led = GPIO_PORTF_DATA_R;
EnableInterrupts(); // enable interrupts for the grader
while(1) {
Sw1 = GPIO_PORTF_DATA_R&0x10;
Sw2 = GPIO_PORTF_DATA_R&0x01;
if (!Sw1 || !Sw2) { // check if either of the switches is pressed
Led = GPIO_PORTF_DATA_R; // read previous
Led = Led^0x02; // toggle red LED
GPIO_PORTF_DATA_R = Led; // output
if (last_led != GPIO_PORTF_DATA_R) { // if LED change was detected
change = 1; // raise a change flag
last_led = GPIO_PORTF_DATA_R; // and remember new state
}
} else {
GPIO_PORTF_DATA_R = 0x00; // if switches were released while led was lit, snuff it
if (last_led != GPIO_PORTF_DATA_R) { // and check the led state
change = 1; // if snuffing the led changed the previous value, trigger flag
last_led = GPIO_PORTF_DATA_R; // and remember the new state
}
}
if (last_sw1 != Sw1) { // same with switches, if switch state is different
change = 1; // trigger change flag
last_sw1 = Sw1; // and remember new state
}
if (last_sw2 != Sw2) { // same for the 2nd switch
change = 1;
last_sw2 = Sw2;
}
if (change == 1) { // if change was detected either from input or in output
if(i<50){ // check if we're not overlowing the array
now = NVIC_ST_CURRENT_R; // get current time
Time[i] = (last-now)&0x00FFFFFF; // 24-bit time difference
Data[i] = GPIO_PORTF_DATA_R&0x13; // record PF1, PF4, PF0
last = now; // zero time difference
i++;
}
change = 0; // reset change flag
}
Delay(); // respect the clock!
}
}


// Color LED(s) PortF
// dark --- 0
// red R-- 0x02
// blue --B 0x04
// green -G- 0x08
// yellow RG- 0x0A
// sky blue -GB 0x0C
// white RGB 0x0E
// pink R-B 0x06

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

New Post

New style string formatting in Python

In this section, you will learn the usage of the new style formatting. Learn more here . Python 3 introduced a new way to do string formatti...