Last active
March 30, 2022 13:45
-
-
Save Ananthusubramanian/8a68512a252d43fc598fafe6971ec53f to your computer and use it in GitHub Desktop.
Embedded Labsheet 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LaunchPad built-in hardware | |
// SW1 left switch is negative logic PF4 on the Launchpad | |
// SW2 right switch is negative logic PF0 on the Launchpad | |
// red LED connected to PF1 on the Launchpad | |
// blue LED connected to PF2 on the Launchpad | |
// green LED connected to PF3 on the Launchpad | |
// 1. Pre-processor Directives Section | |
// Constant declarations to access port registers using | |
// symbolic names instead of addresses | |
// TExaS.o is the object code for the real-board grader | |
#include "tm4c123gh6pm.h" | |
/*Actually below registers are referred in the header file | |
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC)) | |
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) | |
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420)) | |
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510)) | |
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C)) | |
#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520)) | |
#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524)) | |
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528)) | |
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C)) | |
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) | |
*/ | |
// 2. Declarations Section | |
// Global Variables | |
unsigned long SW1,SW2; // input from PF4,PF0 | |
unsigned long Out; // outputs to PF3,PF2,PF1 (multicolor LED) | |
// Function Prototypes | |
void PortF_Init(void); | |
void Delay(void); | |
// 3. Subroutines Section | |
// MAIN: Mandatory for a C Program to be executable | |
int main(void){ | |
PortF_Init(); // Call initialization of port PF4, PF3, PF2, PF1, PF0 | |
GPIO_PORTF_DATA_R=0x02; | |
while(1){ | |
GPIO_PORTF_DATA_R=~(GPIO_PORTF_DATA_R); | |
Delay(); | |
} | |
} | |
// Subroutine to initialize port F pins for input and output | |
// PF4 and PF0 are input SW1 and SW2 respectively | |
// PF3,PF2,PF1 are outputs to the LED | |
// Inputs: None | |
// Outputs: None | |
// Notes: These five pins are connected to hardware on the LaunchPad | |
void PortF_Init(void){ volatile unsigned long d; | |
SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock | |
d = SYSCTL_RCGC2_R; //Dummy instruction to wait for clock to stabilize | |
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock PortF PF0 | |
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0 | |
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog function | |
GPIO_PORTF_PCTL_R = 0x00000000; // 4) GPIO clear bit PCTL | |
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 input, PF3,PF2,PF1 output | |
GPIO_PORTF_AFSEL_R = 0x00; // 6) no alternate function | |
GPIO_PORTF_PUR_R = 0x11; // enable pullup resistors on PF4,PF0 | |
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital pins PF4-PF0 | |
} | |
void Delay(void){ | |
unsigned long volatile time; | |
time = 727240; // counter value for delay | |
while(time){ | |
time--; | |
} | |
} | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LaunchPad built-in hardware | |
// SW1 left switch is negative logic PF4 on the Launchpad | |
// SW2 right switch is negative logic PF0 on the Launchpad | |
// red LED connected to PF1 on the Launchpad | |
// blue LED connected to PF2 on the Launchpad | |
// green LED connected to PF3 on the Launchpad | |
// 1. Pre-processor Directives Section | |
// Constant declarations to access port registers using | |
// symbolic names instead of addresses | |
// TExaS.o is the object code for the real-board grader | |
#include "tm4c123gh6pm.h" | |
/*Actually below registers are referred in the header file | |
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC)) | |
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) | |
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420)) | |
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510)) | |
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C)) | |
#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520)) | |
#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524)) | |
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528)) | |
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C)) | |
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) | |
*/ | |
// 2. Declarations Section | |
// Global Variables | |
unsigned long SW1,SW2; // input from PF4,PF0 | |
unsigned long Out; // outputs to PF3,PF2,PF1 (multicolor LED) | |
// Function Prototypes | |
void PortF_Init(void); | |
void Delay(void); | |
// 3. Subroutines Section | |
// MAIN: Mandatory for a C Program to be executable | |
int main(void){ | |
PortF_Init(); // Call initialization of port PF4, PF3, PF2, PF1, PF0 | |
while(1){ | |
Delay(); | |
SW1 = GPIO_PORTF_DATA_R&0x10; // read PF4 into SW1 | |
Out= GPIO_PORTF_DATA_R|0x04; // write PF2 into Out | |
if(!(SW1)){ // button1 is pressed | |
GPIO_PORTF_DATA_R=~(GPIO_PORTF_DATA_R); | |
} | |
else{ | |
GPIO_PORTF_DATA_R = 0x02; // LED is red | |
} | |
} | |
} | |
// Subroutine to initialize port F pins for input and output | |
// PF4 and PF0 are input SW1 and SW2 respectively | |
// PF3,PF2,PF1 are outputs to the LED | |
// Inputs: None | |
// Outputs: None | |
// Notes: These five pins are connected to hardware on the LaunchPad | |
void PortF_Init(void){ volatile unsigned long d; | |
SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock | |
d = SYSCTL_RCGC2_R; //Dummy instruction to wait for clock to stabilize | |
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock PortF PF0 | |
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0 | |
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog function | |
GPIO_PORTF_PCTL_R = 0x00000000; // 4) GPIO clear bit PCTL | |
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 input, PF3,PF2,PF1 output | |
GPIO_PORTF_AFSEL_R = 0x00; // 6) no alternate function | |
GPIO_PORTF_PUR_R = 0x11; // enable pullup resistors on PF4,PF0 | |
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital pins PF4-PF0 | |
} | |
void Delay(void){ | |
unsigned long volatile time; | |
time = 100; // counter value for delay | |
while(time){ | |
time--; | |
} | |
} | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment