Hallo, ich bin grade dabei folgende Aufgabe zu loesen: Write a C Program that will read a two-bit value via Port 2.0, 2.1 when a push button is momentarily pressed. The push button is connected to Port 1.1 LEDs are connected to Port 1 bits 6 and 7. a. The routine should send a high output to Port 2 bit 6 if the value read in is greater than the last value read in. The value should remain high for approx 1 second. b. The routine should add one to a memory location SAME, when the value is equal to the previous value read in. c. The routine should send a low output to Port 1 bit 7 if it is less than the previous value read in. The light should extinguish for approximately 1 second. The routine should be initialised with the last value read in as 0, port 1.6 low and port 1.7 high. Ich habe etwas geschrieben und wuerde gerne wissen ob es einen einfacheren weg gibt. Es geht mir im wesentlichen um den kommentierten Teil. Wir benutzen microC und einen At89S8253. Der Compiler bringt keine Fehlermeldung. Leider habe ich erst wieder naechste Woche die moeglichkeit es am Board zu testen. Gibt es einen besseren weg z.B. ohne die Variable "buffer"?
1 | sbit lowbit at P2.B0; |
2 | sbit highbit at P2.B1; |
3 | sbit button_0 at P1.B1; |
4 | sbit led_1 at P1.B6; |
5 | sbit led_2 at P1.B7; |
6 | |
7 | void main() { |
8 | |
9 | unsigned char buffer = 0x00; |
10 | unsigned char val_new = 0x00; |
11 | unsigned char val_old = 0x00; |
12 | unsigned char same; |
13 | |
14 | lowbit = 1; //input |
15 | highbit = 1; //input |
16 | button_0 = 1; //input |
17 | led_1 = 0; //output |
18 | led_2 = 0; //output |
19 | |
20 | while(1){ |
21 | |
22 | if (button_0 == 0){ //is the button pressed? |
23 | while (button_0 == 0); //is the button still pressed? |
24 | |
25 | buffer = highbit; // if highbit 1 --> buffer = 00000001 |
26 | buffer <<= 1; // buffer = 00000010 |
27 | |
28 | val_new = buffer; // val_new = 00000010 |
29 | |
30 | buffer = lowbit; // if lowbit 1 --> buffer = 00000001 |
31 | |
32 | val_new |= buffer; // val_new = 00000011 |
33 | |
34 | if (val_new > val_old){ |
35 | led_1 = 1; |
36 | delay_ms (1000); |
37 | led_1 = 0; |
38 | }
|
39 | else if (val_new < val_old){ |
40 | led_2 = 0; |
41 | delay_ms (1000); |
42 | led_2 = 1; |
43 | }
|
44 | else { |
45 | same++; |
46 | }
|
47 | |
48 | val_old = val_new; |
49 | |
50 | }
|
51 | }
|
52 | |
53 | }
|