ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1665

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter 2: Introduction to Bit Crunching · Page 27

Six DEBUG commands are used to display all the measured states and the calculated binary values in the Debug Terminal. The first DEBUG command below displays four different items. Remember, each item in a single DEBUG command must be separated by a comma.

The DEBUG HOME command sends the cursor to the top-left "home" position in the Debug Terminal. Note that it is followed by a comma to separate it from the next item. The next item is a message in quotes: "State of pin P1 is ".

Whenever you want to print a text message to the Debug Terminal, use quotes. The third item is BIN a, which tells the Debug Terminal to print the binary value of the variable a. The fourth item is CR, which makes the Debug Terminal print a carriage return.

DEBUG HOME, "State of pin P0 is ", BIN a, CR

A similar message is printed for the variable b, without the HOME command. The HOME command works well when it's used once per loop. Remember that DEBUG HOME sends the cursor to the top-left corner of the Debug Terminal. If we used HOME more than once in the loop, the information displayed after the first HOME command would be over written by the information following the second HOME command.

DEBUG "State of pin P1 is ", BIN B, CR, CR

Next, two DEBUG commands are used. Each prints a message in quotes followed by two carriage returns.

DEBUG "2-bit binary number: ", CR

Next, another message in quotes is printed followed by a single carriage return.

DEBUG "P1

P0", CR

In this next command, the quotes contain spaces. The first pair of quotes just contains one space (the space bar on the keyboard was pressed once). Then the binary value of b is printed, followed by another two spaces in quotes, followed by the binary value of a, then two more carriage returns.

DEBUG " ", BIN b, " ", BIN a, CR, CR

Page 28 · Basic Analog and Digital

Here's something new. The modifier DEC was used to print the decimal value of the variable d. Because this is the last instruction we want to repeat, it’s followed by the LOOP command.

DEBUG "Decimal equivalent: ", DEC1 d, CR

LOOP

Counting in Binary

Table 2-1 shows how to count from 0 to 3 using 2-bit binary numbers and how to count from 0 to 7 using 3-bit binary numbers.

Note that four numbers (decimal 0 through 3) can be represented with a 2-bit binary number. Eight numbers (0 through 7) can be represented with a 3-bit binary number. 4- bits can describe 16 different numbers, 5-bits can describe 32 different numbers and so on.

Table 2-1:Measured voltages during charge cycle

Decimal number

2-bit binary

3-bit binary

representation

representation

0

00

000

1

01

001

2

10

010

3

11

011

4

100

5

101

6

110

7

111

You can always determine how many counting numbers (combinations of 0s and 1s) can come from a given number of bits by using this formula:

combinations = 2bits

This means the number of combinations equals two raised to the power of the number of bits. For 2-bits, the number of combinations is 22 = 4. For 3-bits, the number of combinations is 23 = 8, and so on.


Chapter 2: Introduction to Bit Crunching · Page 29

Converting from binary to decimal takes two steps. The first step is to multiply each bit by its power of two. Table 2-2 shows the powers of two for up to 8-bits. When you multiply each bit by its value from Table 2-2, you end up with a series of decimal values. The second step is to add up all the decimal values.

Table 2-2: Bit Multipliers for an 8-bit Binary Number

Bit

7

6

5

4

3

2

1

0

Multiplier

128

64

32

16

8

4

2

1

Bit multipliers and Powers of Two: Bit-0 is the least significant bit (LSB) and bit-7 is the most significant bit (MSB). That’s because bit-0 makes the smallest contribution to the number and bit-7 makes the largest contribution. Thinking about a binary number as starting on the left with bit-7 and ending on the right with bit-0 is useful because these numbers indicate the power of 2 for each digit.

Examples:

The multiplier for bit-0 is 1, which equals 20.

The multiplier for bit-1 is 2, which equals 21.

The multiplier for bit-7 is 128, which equals 27.

Note: You can use powers of two to extend Table 2-2 to any number of bits!

As an example, let’s convert binary-1011 to decimal. First, multiply each bit by its power of two from Table 2-2.

8 × 1 = 8

4 × 0 = 0

2 × 1 = 2

1 × 1 = 1

Second, add all 4 of the decimal values:

8 + 0 + 2 + 1 = 11

Now we know the binary number 1011 is equal to the decimal number 11.

Page 30 · Basic Analog and Digital

Parallel and Serial Transmission

Program Listing 2.1 repeats the entire check and report on the pushbutton states routine over and over again. Because the BASIC Stamp checks for input over and over again without waiting for some kind of signal that the data is ready, we are sending the binary numbers to the BASIC Stamp asynchronously.

Asynchronous means not synchronized. In the case of our binary keypad, it means that we change the binary values whenever we want to without waiting for permission from the BASIC Stamp to do so. Likewise, the BASIC Stamp checks the signals at P0 and P1 as fast as it can without waiting for a signal from us that says the data is ready to be checked.

We are also sending the binary bits across two separate data lines at the same time. This means we are sending our data bits to the BASIC Stamp in parallel.

The BASIC Stamp has a 16 I/O pins. We could actually send a word-size binary number to the BASIC Stamp in parallel. The problem is that we wouldn't have any pins left for outgoing signals or other input data. When dealing with larger binary numbers, sending serial data instead of parallel data can be useful because it reduces the number of BASIC Stamp I/O pins used to receive data.

When sending serial data, there has to be some way of letting the BASIC Stamp know when each new bit is ready. The BASIC Stamp has built-in functions for sending asynchronous as well as synchronous serial data.

In this next example, the same two pushbuttons are used to send the BASIC Stamp a nibble (4-bits) of serial, synchronous data. The result is displayed in the Debug Terminal.

Parallel means the data bits are sent across more than one data line at the same time. We just finished using the pushbuttons to send two parallel bits.

Serial: Instead of sending data in parallel along multiple data lines, a single data line can be used and the data bits can be sent one after another.

Synchronous: Sending data synchronously means we are sending the data in time coordinated manner (in sync). Technically, it means that the sender and receiver of the data bits do so according to signals from the same clock


Chapter 2: Introduction to Bit Crunching · Page 31

Reprogramming to Receive Serial Data

Enter Program Listing 2.2 into the BASIC Stamp Editor, and save it under the name PL2_2R0.bs2.

'Basic Analog and Digital - PL2_2R0.bs2

'Program Listing 2.2 Revision 0.

'{$STAMP BS2}

'{$PBASIC 2.5}

n

VAR

Nib

d

VAR

Nib

INPUT 0

INPUT 1

FOR n = 1 TO 4

DO

'Wait for high LOOP UNTIL IN1=1

DO

'Wait for low LOOP UNTIL IN1=0

d = d << 1 d = d + IN0

DEBUG HOME, "Shifting in bits: ", BIN4 d NEXT

DEBUG CR, CR, "Done shifting.", CR, CR

DEBUG "Decimal value: ", DEC2 d, CR, CR

The Output

The Debug Terminal will initially show a blank screen when you run the program. Follow these instructions carefully to send the synchronous, serial data. First, press and hold the right button. Then press and release the left button. The output should look like Figure 2-5 below.

Page 32 · Basic Analog and Digital

Figure 2-5

Debug Terminal

Output for Program

Listing 2.2.

Next, release the right button, and press and release the left button again. The output should change so that it looks like Figure 2-6.

Figure 2-6

Debug Terminal

Output for Program

Listing 2.2.

Chapter 2: Introduction to Bit Crunching · Page 33

Press and hold the right button, then press and release the left button twice. Then the output should look like Figure 2-7.

Figure 2-7

Debug Terminal

Output for Program

Listing 2.2.

If your program worked as shown, you just synchronously shifted 4 serial bits into the BASIC Stamp's RAM. In addition, it was verified that the decimal value of binary-1011 really is 11 in the decimal number system.

Figure 2-8 shows how these events occurred (from left to right) in a timing diagram. The left pushbutton was the clock signal. The clock signal consisted of a series of clock pulses. Each clock pulse was the press and release of the left pushbutton. This sent a low- high-low signal to P1. Each time the left pushbutton was released, the BASIC Stamp checked the state of the data line, which was the state of the right pushbutton (P0). The BASIC Stamp is programmed to read the input data after the clock signal's transition from high to low. This transition is referred to as the negative edge of the clock pulse.

Page 34 · Basic Analog and Digital

Read Input

Read Input

Read Input

Read Input

Clock signal from left pushbutton to P1

Data signal from right pushbutton to P0

1

0

1

1

Figure 2-8: Timing Diagram.

About the Code

Like Program Listing 2.1, we use a comment to include some information about the program at the beginning. Remember, as soon as an apostrophe appears in a line of PBASIC code, everything to the right of the apostrophe is ignored by the BASIC Stamp. Next the two nibble variables are defined, and I/O pins P0 and P1 are set to function as inputs. One of those nibble variables, n, is used by the FOR…NEXT loop.

The main portion of the code is in a FOR…NEXT loop, which has the following syntax:

FOR Counter = StartValue TO EndValue

In the example code snippet below, n is the Counter, 1 is the StartValue argument and 4 is the EndValue argument of the FOR…NEXT command.

FOR n = 1 TO 4

DEBUG DEC n, CR

NEXT

DEBUG “Done!”


Chapter 2: Introduction to Bit Crunching · Page 35

This FOR…NEXT loop executes the PBASIC command between the FOR command and the NEXT command four times. The first time through the loop the value of n is 1, the second time through the value of n increments to 2, and so on until n gets to 4. After completing the 4th pass, the program skips out of the loop and continues on to the line of code after the NEXT command.

Program Listing 2.2 has a more complicated set of commands that gets repeated within its FOR…NEXT loop, and it is different from what we've seen before. It starts with a DO…LOOP conditional loop. This loop is used to check the value at pin P0. If the value of P0 is low, LOOP UNTIL IN1=1 sends the program back to DO. If the value at pin P1 is high, then the program executes the next line, after the LOOP instruction.

FOR n = 1 TO 4 DO

'Wait for high LOOP UNTIL IN1=1

The same technique is applied with the next two lines of code (and one comment), which repeat themselves until a low signal is received.

DO

'Wait for low LOOP UNTIL IN1=0

The command d=d<<1 shifts all the bits in d left by 1. When values are shifted, the vacant space in the rightmost bit (the LSB) is automatically filled with a zero. Then the bit value at pin P0 is loaded into the LSB position in the nibble. This is done using the d=d+IN0 command, which adds the single bit measured at P0 to the value of the nibble variable d. The second time through the loop, the value that was placed in the LSB gets shifted left by one, so it ends up in bit-1. Meanwhile the next value sampled at P0 is placed in the LSB slot. The shift and add process is repeated four times as each bit is shifted into the byte.

d = d << 1 d = d + IN0

Each time a new bit is shifted in, we use the DEBUG command to display the new value. Each time the program gets to the NEXT command it returns to the FOR n = 1 TO 4 command, and the value of n is incremented, until it gets to 4. Finally the program breaks out of the loop.

Page 36 · Basic Analog and Digital

DEBUG HOME, "Shifting in bits: ", BIN4 d

NEXT

Once the FOR…NEXT loop is finished and all the bits are shifted into the d variable, two messages are printed. The second of the two DEBUG commands has a new modifier, DEC2. This modifier is used to make the Debug Terminal display the value of d as two decimal digits.

DEBUG CR, CR, "Done shifting.", CR, CR

DEBUG "Decimal value: ", DEC2 d, CR, CR

While Chapter 1 introduced analog voltage, this chapter introduced the basics of sending and receiving binary numbers. In the next experiment, we'll combine these topics to build a digital DC voltmeter, which is a device that measures analog voltage and displays the measurement as a digital value.

Chapter 2: Introduction to Bit Crunching · Page 37

What have I learned?

On the lines below, insert the appropriate words from the list on the left.

The _____________ can be used to process information about analog

largest

measurements using _____________ . Before working with analog to

digital interfaces, it's important to understand how the BASIC Stamp

word

sends, receives, and stores binary data. It's also important to be able to

use _____________ to program the BASIC Stamp to send, receive, and

decimal

store binary data.

When converting a binary number to its _____________ equivalent, each

BASIC Stamp

bit should be multiplied by a particular

power of two.

The

synchronously

_____________ is the rightmost bit in a binary number, and it makes the

smallest contribution to the value of that number. It is also referenced as

LSB

bit-0. The MSB is the leftmost bit. It makes the _____________

contribution to the value of a binary number.

The bit just to the left of

PBASIC

the LSB is bit-1, the bit 2-bits to the left of LSB is bit-2 and so on.

The BASIC Stamp can store a single bit, a nibble, which is 4-bits, a

byte

_____________, which is 8-bits, or a _____________, which is 16-bits.

binary

To save I/O pins, the BASIC Stamp can send and receive serial data as

opposed to parallel data, which uses multiple data lines. Binary data can

numbers

also be sent _____________ or asynchronously.