Corning Community College
ENGR1050 C for Engineers
To adapt your bcf0 program to, instead of displaying values to STDOUT, instead lights up LEDs on the LEDbar, so the binary counter, in incrementing or decrementing fashion, is better visualized.
In “The C Book”, please read through Chapter 5.
To refresh your memory, here is a diagram of the circuit you can build to drive the LEDbar:
Be sure to use 220 Ohm resistors for this.
It is your task to adapt your bcf0 program so that, upon accepting various pieces of input from the user, acts as a binary counter, lighting up the resultant LEDs in binary (1 being on, 0 being off), inserting an adequate delay so we can watch as it proceeds from a starting to an ending value.
Your program should:
Some additional points of consideration:
pi@raspberrypi:~/src/c4eng/epf1$ ./epf1 Enter starting value (0-1023): 12 Enter ending value (0-1023): 28 pi@raspberrypi:~/src/c4eng/epf1$
The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.
pi@raspberrypi:~/src/c4eng/epf1$ ./epf1 Enter starting value (0-1023): 17 Enter ending value (0-1023): 0 pi@raspberrypi:~/src/c4eng/epf1$
lab46:~/src/c4eng/epf1$ ./epf1 Enter starting value (0-1023): 5543 ERROR: input value must be between 0-1023! lab46:~/src/c4eng/epf1$
lab46:~/src/c4eng/epf1$ ./epf1 Enter starting value (0-1023): 255 Enter ending value (0-1023): 7168 ERROR: input value must be between 0-1023! lab46:~/src/c4eng/epf1$
You might wonder how, when you are limited to non-binary input, how you can obtain the binary value so that you can work with it.
There are a couple ways to go about this:
Using the division method, you can convert a decimal value to binary, continually dividing the value (or its quotient) by the base, until the quotient is 0, then we use the remainders to give us the binary value:
The binary for 15 (decimal) is 1111 (binary)
Or:
The binary for 11 (decimal) is 1011 (binary).
NOTE that the order in which we get the remainders produces the number from right to left.
If one understands the weight values corresponding with the places of each bit in a binary number, we can simply do a bitwise AND and see if the result is greater than 0 or not:
2 to the 7 | 2 to the 6 | 2 to the 5 | 2 to the 4 | 2 to the 3 | 2 to the 2 | 2 to the 1 | 2 to the 0 |
---|---|---|---|---|---|---|---|
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
So, if we wanted to see if the 2 to the 7th bit is active, we can simply:
value = number & 128;
We can then check of value is greater than 0; if it is, we've got a 1 in that position, if it isn't, we have a 0.
We can then repeat the operation for 64, 32, 16, etc. down to 1, to get each bit of our number.
NOTE that I have only taken us out to 8-bits. You may need to extend this to incorporate all the allowed values for this project.
To successfully complete this project, the following criteria must be met:
To submit this program to me using the submit tool, run the following command at your lab46 prompt:
$ submit c4eng epf1 epf1.c Submitting c4eng project "epf1": -> epf1.c(OK) SUCCESSFULLY SUBMITTED
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.