From 57c882325edcec6e1ab3bdadd59a6e4b55510fa5 Mon Sep 17 00:00:00 2001 From: Hladu357 Date: Tue, 25 Feb 2025 17:27:42 +0200 Subject: [PATCH] lab1 --- lab1/main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/lab1/main.c b/lab1/main.c index 3376c07..e462159 100644 --- a/lab1/main.c +++ b/lab1/main.c @@ -173,9 +173,66 @@ int main3(void) { } // Your solution here +void initRedLED(void) { + P1->SEL0 &= ~0x01; + P1->SEL1 &= ~0x01; // 1) configure P1.0 as GPIO + P1->DIR |= 0x01; // 2) make P1.0 output +} + +void initColorLED(void) { + P2->SEL0 &= ~0x07; + P2->SEL1 &= ~0x07; // 1) configure P2.2-P2.0 as GPIO + P2->DIR |= 0x07; // 2) make P2.2-P2.0 out + P2->DS |= 0x07; // 3) activate increased drive strength + P2->OUT &= ~0x07; // 4) all LEDs off +} + +void initInput(void) { + P1->SEL0 &= ~(SW1 | SW2); + P1->SEL1 &= ~(SW1 | SW2); // 1) configure P1.4 P1.1 P1.0 as GPIO + P1->DIR &= ~(SW1 | SW2); // 2) SW1 and SW2 input + P1->REN |= (SW1 | SW2); // 4) enable pull resistors on P1.4 and P1.1 +} + +// read P1.4, P1.1 inputs +uint8_t inP1(void) { + return P1->IN & (SW1 | SW2); +} + +// write one output bit to P1 +void outP1(uint8_t data) { + P1->OUT = (P1->OUT & ~0x01) | data; +} + +// write three outputs bits to P2 +void outP2(uint8_t data) { + P2->OUT = (P2->OUT & ~0x07) | data; +} + int main(void) { - + initRedLED(); + initColorLED(); + initInput(); + + outP2(0); while (true) { - + uint8_t switches = inP1() ^ (SW1 | SW2); + switch (switches) { + case SW1: + outP1(RED); + outP2(BLUE); + break; + case SW2: + outP1(RED); + outP2(GREEN); + break; + case SW1 | SW2: + outP1(0); + outP2(RED); + break; + default: + outP1(0); + outP2(BLUE | GREEN); + } } } \ No newline at end of file