Untitled

 avatar
unknown
plain_text
19 days ago
2.6 kB
5
Indexable
#include <stdint.h>

#define DELAY_1_SEC 4000000
#define GREEN 0x08  // Port B Pin 3
#define YELLOW 0x04 // Port B Pin 2
#define RED 0x02    // Port B Pin 1

#define BUTTON 0x01 // Port D Pin 0
#define PED_RED 0x02 // Port D Pin 1
#define PED_GREEN 0x04 // Port D Pin 2

volatile uint8_t button_pressed = 0;

void Delay(int time) {
    volatile uint32_t i;
    for (i = 0; i < time; i++);
}

void PortD_Handler(void) {
    if (GPIO_PORTD_RIS_R & BUTTON) { // Check if interrupt is from button
        button_pressed = 1;          // Set button pressed flag
        GPIO_PORTD_ICR_R = BUTTON;  // Clear interrupt flag
    }
}

void AutoTrafficLight(void) {
    *((volatile uint32_t *)0x400053FC) = GREEN; // Green light
    Delay(2 * DELAY_1_SEC);

    *((volatile uint32_t *)0x400053FC) = YELLOW; // Yellow light
    Delay(0.5 * DELAY_1_SEC);

    *((volatile uint32_t *)0x400053FC) = RED; // Red light
    Delay(1 * DELAY_1_SEC);
}

void PedestrianTrafficLight(void) {
    *((volatile uint32_t *)0x400073FC) = PED_RED; // Pedestrian red light by default

    if (button_pressed) {
        button_pressed = 0; // Reset button flag

        // Turn pedestrian light green when auto light is red
        *((volatile uint32_t *)0x400073FC) = PED_GREEN;
        Delay(1 * DELAY_1_SEC);

        *((volatile uint32_t *)0x400073FC) = PED_RED; // Turn back to red
    }
}

void setup(void) {
    *((volatile uint32_t *)0x400FE608) |= 0x0A; // Enable clock for Port B and D

    // Configure Port B pins for auto traffic lights
    *((volatile uint32_t *)0x40005400) |= RED | YELLOW | GREEN;
    *((volatile uint32_t *)0x4000551C) |= RED | YELLOW | GREEN;

    // Configure Port D pins for pedestrian traffic lights and button
    *((volatile uint32_t *)0x40007400) |= PED_RED | PED_GREEN;
    *((volatile uint32_t *)0x40007400) &= ~BUTTON;
    *((volatile uint32_t *)0x4000751C) |= PED_RED | PED_GREEN | BUTTON;
    *((volatile uint32_t *)0x40007510) |= BUTTON; // Enable pull-up resistor for button

    // Configure interrupt for button
    *((volatile uint32_t *)0x40007404) &= ~BUTTON; // Edge-sensitive
    *((volatile uint32_t *)0x40007408) &= ~BUTTON; // Single edge
    *((volatile uint32_t *)0x4000740C) |= BUTTON;  // Rising edge
    *((volatile uint32_t *)0x40007410) |= BUTTON;   // Enable interrupt
    *((volatile uint32_t *)0xE000E100) |= (1 << 3);      // Enable interrupt for Port D
}

int main(void) {
    setup();

    while (1) {
        AutoTrafficLight();
        PedestrianTrafficLight();
    }
}
Leave a Comment