TODO:: #include "TM4C123GH6PM.h" // LED and Switch Definitions #define RED (0x02) // PF1 #define BLUE (0x04) // PF2 #define GREEN (0x08) // PF3 #define SW1 (1U << 4) // PF4 #define SW2 (1U << 0) // PF0 // Array of LED Colors const uint8_t colors[] = {RED, BLUE, GREEN, RED | BLUE, RED | GREEN, BLUE | GREEN, RED | BLUE | GREEN}; volatile int currentColorIndex = 0; // Index to track the current color // Function Prototypes void PortF_Init(void); void delay_ms(int delay); void GPIOF_Handler(void) { if (GPIOF->MIS & SW1) { // SW1 Interrupt (Next Color) currentColorIndex = (currentColorIndex + 1) % 7; // Increment and wrap around GPIOF->DATA = (GPIOF->DATA & ~0x0E) | colors[currentColorIndex]; // Update LED GPIOF->ICR |= SW1; // Clear SW1 interrupt } if (GPIOF->MIS & SW2) { // SW2 Interrupt (Previous Color) currentColorIndex = (currentColorIndex - 1 + 7) % 7; // Decrement and wrap around GPIOF->DATA = (GPIOF->DATA & ~0x0E) | colors[currentColorIndex]; // Update LED GPIOF->ICR |= SW2; // Clear SW2 interrupt } } int main(void) { PortF_Init(); // Initialize PortF while (1) { // Main loop does nothing; all functionality handled in ISR } } void PortF_Init(void) { SYSCTL->RCGCGPIO |= 0x20; // Enable clock to GPIOF GPIOF->LOCK = 0x4C4F434B; // Unlock GPIOF GPIOF->CR = 0x1F; // Allow changes to PF4-0 GPIOF->DIR |= 0x0E; // Set PF1, PF2, PF3 as output (LEDs) GPIOF->DIR &= ~(SW1 | SW2); // Set PF4, PF0 as input (Switches) GPIOF->DEN |= 0x1F; // Enable digital functionality GPIOF->PUR |= SW1 | SW2; // Enable pull-up resistors for switches GPIOF->IS &= ~(SW1 | SW2); // Edge-sensitive GPIOF->IBE &= ~(SW1 | SW2); // Interrupt on one edge GPIOF->IEV &= ~(SW1 | SW2); // Interrupt on falling edge GPIOF->ICR |= SW1 | SW2; // Clear any prior interrupts GPIOF->IM |= SW1 | SW2; // Enable interrupt for SW1 and SW2 NVIC->ISER[0] |= (1 << 30); // Enable IRQ30 (GPIOF) } void delay_ms(int delay) { int i, j; for (i = 0; i < delay; i++) { for (j = 0; j < 3180; j++) { __asm("NOP"); } } }///////////////////////// #include "TM4C123.h" #define RED 0x02 #define BLUE 0x04 #define GREEN 0x08 #define YELLOW RED + GREEN #define MAGENTA BLUE + RED #define CYAN GREEN + BLUE #define WHITE RED + GREEN + BLUE #define SW1 0x10 #define SW2 0x01 #define DELAY 900000 const int sequence[] = {RED, BLUE, GREEN, YELLOW, MAGENTA, CYAN, WHITE}; int index = 0; void delay( volatile unsigned long ulLoop ){ for (ulLoop = 0; ulLoop < DELAY; ulLoop++) { for (ulLoop = 0; ulLoop < DELAY; ulLoop++) { } } } int main(void) { SYSCTL->RCGCGPIO |= (1<<5); // Enable clock to GPIO Port F SYSCTL->RCGCTIMER |= (1<<1); // Enable clock to Timer 1 delay(0); GPIOF->DIR |= BLUE | GREEN; // Set BLUE and GREEN as output GPIOF->DEN |= BLUE | GREEN; // Enable digital functions for BLUE and GREEN LEDs // Timer Setup for 500ms blink (Green LED) TIMER1->CTL = 0; // Disable the timer TIMER1->CFG = 0x4; // Choose 16-bit mode TIMER1->TAMR = 0x02; // Periodic mode TIMER1->TAPR = 250 - 1; // Prescaler TIMER1->TAILR = 32000 - 1; // Set for 500ms delay (16-bit timer, 50MHz system clock -> 500ms = 32000 ticks) TIMER1->ICR = 0x1; // Clear any prior interrupts TIMER1->IMR |= (1<<0); // Enable timeout interrupt TIMER1->CTL |= 0x01; // Enable the timer NVIC->ISER[0] |= (1<<21); // Enable Timer 1 interrupt in NVIC while(1) { } } // Timer 1 interrupt handler void TIMER1A_Handler() { if (TIMER1->MIS & 0x1) { GPIOF->DATA ^= GREEN; // Toggle the GREEN LED } TIMER1->ICR = 0x1; // Clear the interrupt flag } //////////////////////////Nawrasss #define SYSCTL_RCGCGPIO_R (*((volatile unsigned long *)0x400FE608)) #define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC)) #define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) #define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C)) #define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510)) // Pull-up resistors for switches #define DELAY 200000 // Define an array of colors for the RGB LED unsigned long colors[] = {0x02, 0x04, 0x08, 0x01, 0x05, 0x06, 0x07}; // Colors array (Red, Green, Blue, etc.) unsigned long currentColorIndex = 0; // To keep track of the current color index // Define GPIO for Switches (SW1 and SW2) #define SW1_PIN (1 << 4) // PF4 #define SW2_PIN (1 << 0) // PF0 int main(void) { volatile unsigned long ulLoop; // Enable the GPIO port for the onboard LED and switches SYSCTL_RCGCGPIO_R = 0x20; // Enable GPIOF for LED (PF0, PF1, PF2, PF3) SYSCTL_RCGCGPIO_R |= 0x10; // Enable GPIOE for switches (PF4 and PF0) // Dummy read to insert a few cycles after enabling the peripherals ulLoop = SYSCTL_RCGCGPIO_R; // Enable the GPIO pins for the LED (PF3), Set the direction as output, Enable digital function for LED pins GPIO_PORTF_DIR_R = 0x0E; // Set PF3, PF2, PF1 as output (LEDs) GPIO_PORTF_DEN_R = 0x0E; // Enable digital function for PF3, PF2, PF1 (LEDs) // Enable switches SW1 (PF4) and SW2 (PF0), configure as input with pull-up resistors GPIO_PORTF_DIR_R &= ~(SW1_PIN | SW2_PIN); // Set PF4 and PF0 as input GPIO_PORTF_DEN_R |= (SW1_PIN | SW2_PIN); // Enable digital function for PF4 and PF0 GPIO_PORTF_PUR_R |= (SW1_PIN | SW2_PIN); // Enable pull-up resistors on PF4 and PF0 // Loop forever while (1) { // Check if SW1 is pressed (PF4) if ((GPIO_PORTF_DATA_R & SW1_PIN) == 0) { // Active low for switch press // Wait for SW1 to be released to avoid multiple changes on a single press while ((GPIO_PORTF_DATA_R & SW1_PIN) == 0) {} // Change color to the next in the array currentColorIndex++; if (currentColorIndex >= sizeof(colors) / sizeof(colors[0])) { currentColorIndex = 0; // Wrap around to the first color } // Update LED color GPIO_PORTF_DATA_R = colors[currentColorIndex]; // Debounce delay for (ulLoop = 0; ulLoop < DELAY; ulLoop++) {} } // Check if SW2 is pressed (PF0) if ((GPIO_PORTF_DATA_R & SW2_PIN) == 0) { // Active low for switch press // Wait for SW2 to be released to avoid multiple changes on a single press while ((GPIO_PORTF_DATA_R & SW2_PIN) == 0) {} // Change color to the previous in the array if (currentColorIndex == 0) { currentColorIndex = sizeof(colors) / sizeof(colors[0]) - 1; // Wrap around to the last color } else { currentColorIndex--; } // Update LED color GPIO_PORTF_DATA_R = colors[currentColorIndex]; // Debounce delay for (ulLoop = 0; ulLoop < DELAY; ulLoop++) {} } // Delay to prevent switch bouncing effects for (ulLoop = 0; ulLoop < DELAY; ulLoop++) {} } }