lab1
This commit is contained in:
parent
d22cefce1a
commit
df05915624
59
lab1/main.c
59
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue