Commit 605e8ed3 by vielex

Final version

parent 3ca637f2
Showing with 125 additions and 124 deletions
// ***** 0. Documentation Section ***** // ***** 0. Documentation Section *****
// main.c for Lab 9 // main.c for Lab 9
// Runs on LM4F120/TM4C123 // Runs on LM4F120/TM4C123
// In this lab we are learning functional debugging by dumping // In this lab we are learning functional debugging by dumping
// recorded I/O data into a buffer // recorded I/O data into a buffer
// January 15, 2016 // January 15, 2016
// Lab 9 // Lab 9
// Jon Valvano and Ramesh Yerraballi // Jon Valvano and Ramesh Yerraballi
// ***** 1. Pre-processor Directives Section ***** // ***** 1. Pre-processor Directives Section *****
#include "TExaS.h" #include "TExaS.h"
#include "tm4c123gh6pm.h" #include "tm4c123gh6pm.h"
#include <stdbool.h> #include <stdbool.h>
// ***** 2. Global Declarations Section ***** // ***** 2. Global Declarations Section *****
// FUNCTION PROTOTYPES: Each subroutine defined // FUNCTION PROTOTYPES: Each subroutine defined
void DisableInterrupts(void); // Disable interrupts void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts void EnableInterrupts(void); // Enable interrupts
// ***** 3. Subroutines Section ***** // ***** 3. Subroutines Section *****
/* /*
This Lab9 starter project is the same as C9_Debugging example but This Lab9 starter project is the same as C9_Debugging example but
includes the connections to the Lab9 grader. You will make three changes. includes the connections to the Lab9 grader. You will make three changes.
First, make the LED flash at 10 Hz. In other words, make it turn on for 0.05 seconds, 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. and then turn off for 0.05 seconds.
Second, make the LED flash if either switch SW1 or SW2 is pressed 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). (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. 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, 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. 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 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%). 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. 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. 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. This means no adjacent elements in the array should be equal.
*/ */
void PortF_Init(void){ volatile unsigned long delay; void PortF_Init(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F
delay = SYSCTL_RCGC2_R; // allow time for clock to start delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0 GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked // only PF0 needs to be unlocked, other bits can't be locked
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0 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_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out
GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0 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_PUR_R = 0x11; // enable pull-up on PF0 and PF4
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0 GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0
} }
// Initialize SysTick with busy wait running at bus clock. // Initialize SysTick with busy wait running at bus clock.
void SysTick_Init(void){ void SysTick_Init(void){
NVIC_ST_CTRL_R = 0; // disable SysTick during setup NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_RELOAD_R = 0x00FFFFFF; // maximum reload value NVIC_ST_RELOAD_R = 0x00FFFFFF; // maximum reload value
NVIC_ST_CURRENT_R = 0; // any write to current clears it NVIC_ST_CURRENT_R = 0; // any write to current clears it
NVIC_ST_CTRL_R = 0x00000005; // enable SysTick with core clock NVIC_ST_CTRL_R = 0x00000005; // enable SysTick with core clock
} }
unsigned long Led; unsigned long Led;
void Delay(void){unsigned long volatile time; void Delay(void){unsigned long volatile time;
time = 80000; // 0.05 sec time = 80000; // 0.05 sec
while(time){ while(time){
time--; time--;
} }
} }
// first data point is wrong, the other 49 will be correct // first data point is wrong, the other 49 will be correct
unsigned long Time[50]; unsigned long Time[50];
// you must leave the Data array defined exactly as it is // you must leave the Data array defined exactly as it is
unsigned long Data[50]; unsigned long Data[50];
int main(void){ unsigned long i,last,now; int main(void){ unsigned long i,last,now;
bool changed; bool changed;
TExaS_Init(SW_PIN_PF40, LED_PIN_PF1); // activate grader and set system clock to 16 MHz TExaS_Init(SW_PIN_PF40, LED_PIN_PF1); // activate grader and set system clock to 16 MHz
PortF_Init(); // initialize PF1 to output PortF_Init(); // initialize PF1 to output
SysTick_Init(); // initialize SysTick, runs at 16 MHz SysTick_Init(); // initialize SysTick, runs at 16 MHz
i = 0; // array index i = 0; // array index
changed = false; changed = false;
last = NVIC_ST_CURRENT_R; last = NVIC_ST_CURRENT_R;
EnableInterrupts(); // enable interrupts for the grader EnableInterrupts(); // enable interrupts for the grader
while(1){ while(1){
if(!(GPIO_PORTF_DATA_R&0x01) || !(GPIO_PORTF_DATA_R&0x10)){ // PF0 or PF4 //either PF4 or PF0 is 0
Led = GPIO_PORTF_DATA_R; // read previous if(!(GPIO_PORTF_DATA_R&0x01) || !(GPIO_PORTF_DATA_R&0x10)){
Led = Led^0x02; // toggle red LED Led = GPIO_PORTF_DATA_R; // read previous
GPIO_PORTF_DATA_R = Led; // output Led = Led^0x02; // toggle red LED
changed = true; GPIO_PORTF_DATA_R = Led; // output
} changed = true;
else { }
//disable LED if nothing is pressed else {
GPIO_PORTF_DATA_R &=~0x02; //disable LED if nothing is pressed
} GPIO_PORTF_DATA_R &=~0x02;
}
if(i<50){ if(i<50 && changed){
now = NVIC_ST_CURRENT_R; changed = false;
Time[i] = (last-now)&0x00FFFFFF; // 24-bit time difference now = NVIC_ST_CURRENT_R;
Data[i] = GPIO_PORTF_DATA_R&0x02; // record PF1 Time[i] = (last-now)&0x00FFFFFF; // 24-bit time difference
last = now; Data[i] = GPIO_PORTF_DATA_R&0x13; // record PF4,1,0
i++; last = now;
} i++;
Delay(); }
} Delay();
} }
}
// Color LED(s) PortF
// dark --- 0 // Color LED(s) PortF
// red R-- 0x02 // dark --- 0
// blue --B 0x04 // red R-- 0x02
// green -G- 0x08 // blue --B 0x04
// yellow RG- 0x0A // green -G- 0x08
// sky blue -GB 0x0C // yellow RG- 0x0A
// white RGB 0x0E // sky blue -GB 0x0C
// pink R-B 0x06 // white RGB 0x0E
// pink R-B 0x06
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment