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

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

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

Добавлен: 15.06.2025

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

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

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

Powering Up

You’re now ready to power up the target system. Turn on its power supply, and press the SPACE bar at the host’s keyboard. You should see this BASIC-52 sign-on message and prompt:

*MCS-51(tm) BASIC V1.1*

READY

Figure 3-6 shows the sign-on message and a simple program, using Windows’ Terminal accessory for communications.

Troubleshooting

If you don’t see the prompt, it’s time to troubleshoot. Getting the system to boot up the first time can be the most challenging part of a project, especially when serial communications are involved. Here are some things that may help you isolate the cause of the problem:

Try again by pressing and releasing S1 and pressing the space bar. If you are using a 32K RAM for U7, BASIC-52 requires about 1 second to perform its memory check after a reset, before it will respond to the space bar. With an 8K RAM, the delay is a few tenths of a second (proportionately longer with slower crystals).

Double-check the easy things. Are the communications parameters correct? Did you select the correct serial port? Are all ICs inserted?

Verify that pin 9 of U2 goes high, then low, when you press and release S1.

Check the power and ground pins of all ICs for proper voltages.

Connect a logic probe to pin 10 of U2. When you press the space bar, you should see the logic level toggle as U2 receives the ASCII code for a space (20h). If not, you probably have a problem in the setup of your communications software or in the serial cabling.

Verify that pin 30 of U2 is toggling (at 1/6 the crystal frequency, if you have an oscilloscope to measure). This indicates that the oscillator circuit is functioning.

Verify that pins 21-28 and 32-39 of U2 toggle as BASIC-52 performs its memory check immediately after powering up or rebooting.

If all else fails, recheck your wiring for missing or misrouted wires. Sometimes there’s no alternative but to go through the schematic connection by connection, checking each with an ohmmeter.

The Microcontroller Idea Book

37

Chapter 3

Basic tests

When your system boots, you’re ready for some basic tests. The BASIC-52 programming manual is a useful reference at this point.

In some ways, BASIC-52 is similar to BASIC compilers like Microsoft’s QuickBASIC. Many of the keywords and syntax rules are similar. But BASIC-52 is closer to older interpreted BASICs like GW-BASIC or BASICA. You can type a statement or command and execute it immediately when you press ENTER, or you can type a series of statements and run them later as a program. When a line begins with a line number, BASIC-52 treats it as a program line rather than as a command to execute immediately.

Here are some quick tests and experiments you can do:

Memory Check

Type

PRINT MTOP

to learn the amount of external data memory that BASIC-52 detected on boot-up. With an 8K RAM, MTOP should be 8191, and with 32K, it should be 32,767. If you prefer hexadecimal notation, type

PH0. MTOP

(In PH0., be sure to include the period and use a zero, not the letter “O”.)

Crystal Frequency

The special operator XTAL represents the value of the timing crystal that clocks the 8052-BASIC. The default value is 11059200, or 11.0592 Mhz. You can verify this by typing

PRINT XTAL

Most BASIC-52 statements don’t use the XTAL operator, so it doesn’t matter if the value isn’t accurate. Exceptions are the real-time clock, programming commands, PWM output, and LPT output. For these, XTAL should match your crystal’s frequency. To set XTAL for a 12Mhz crystal, type

XTAL=12000000

To verify, type

38

The Microcontroller Idea Book


Powering Up

PRINT XTAL

Line Editing

After typing a few commands, you may discover some of BASIC-52’s line-editing abilities. While typing a line, you can correct mistakes by deleting back to the mistake and retyping. In Procomm Plus, if you select VT100 terminal emulation (under Setup menu, Terminal Options), you can use either the DELETE or BACKSPACE key to delete. With the Windows terminal, you must use the DELETE key (not BACKSPACE). Many communications programs allow you remap the keyboard, so you can select whatever delete key you wish.

Once you press ENTER, you can’t edit a line you’ve typed, unless you retype it from the beginning.

BASIC-52 treats upper and lower-case characters the same. In most cases, spaces are ignored, so you can include them or not as you wish.

Running a Program

Here is a very simple program to try:

10 FOR I=1 to 10

20 PRINT I

30 NEXT I

40 END

Enter each of the lines, including the line numbers. BASIC-52 automatically stores the program in RAM. To run the program, type RUN. You should see this:

1

2

3

4

5

6

7

8

9

10

To view the program lines, type

LIST

The Microcontroller Idea Book

39


Chapter 3

To erase the current program, type

NEW

To verify that the program no longer exists, type

LIST

You can change individual program lines by typing the line number, followed by a new statement:

10 FOR I=1 to 20

To erase a line, type the line number and press ENTER:

20

Getting Out of Trouble

Occasionally, a programming error may cause a program to go into an endless loop or crash the system. If it’s an endless loop, you can exit it and return to the READY prompt by pressing CONTROL+C. If that doesn’t work, your only choice is to press S1 to reset the 8052-BASIC system. Resetting will erase the program in RAM, so you’ll have to re-enter it.

Simple Programs to Try

The following sections offer some short programs to try, to help you explore your system and become familiar with BASIC-52. Don’t worry if you don’t understand every line of the programs. Later chapters get into programming in more detail.

Reading Port 1

You can use BASIC-52 to read and write to Port 1 (pins 1-8) on the 8052-BASIC.

The command

PH0.PORT1

will display the hex value of the entire port. Listing 3-1 is a program that displays the value of each of the bits in the port.

Enter each line carefully. Be sure to include all of the punctuation shown. When you run the program, you should see a display like this:

40

The Microcontroller Idea Book

Powering Up

PORT 1 Bit Values:

Bit 0 = 1

Bit 1 = 1

Bit 2 = 1

Bit 3 = 1

Bit 4 = 1

Bit 5 = 1

Bit 6 = 1

Bit 7 = 1

If a port pin is open, or unconnected, its internal pull-up resistor will cause it to read as 1. If you connect a jumper wire from a port pin to ground, or bring the pin low by driving it with a logic low output, it should read 0. Line 10 in Listing 3-1 brings all of Port 1’s bits high, which enables them to be used as inputs.

Writing to Port 1

You can control the bits of Port 1 by writing to them. Listing 3-2 allows you to set or clear individual bits. Here’s an example of what happens when you run the program:

Enter a bit to set or clear (0-2, 4-7) :7

Enter 1 to set, 0 to clear :0

Enter a bit to set or clear (0-2, 4-7) :3

Do not change bit 3!

The program doesn’t allow you to change bit 3 (P1.3), because the 8052-BASIC circuit requires this bit to be high when accessing external memory (assuming that you’ve included U3B in your circuit). If you do clear bit 3 accidentally, you’ll crash the system and will have to reboot.

Listing 3-1. Displays the value of each bit in Port 1.

10 PORT1 = 0FFH

20 PRINT “PORT 1 Bit Values:”

30 PRINT “Bit 0 = ”,(PORT1.AND.1)

40 PRINT “Bit 1 = ”,(PORT1.AND.2)/2

50 PRINT “Bit 2 = ”,(PORT1.AND.4)/4

60 PRINT “Bit 3 = ”,(PORT1.AND.8)/8

70 PRINT “Bit 4 = ”,(PORT1.AND.10H)/10H

80 PRINT “Bit 5 = ”,(PORT1.AND.20H)/20H

90 PRINT “Bit 6 = ”,(PORT1.AND.40H)/40H

100 PRINT “Bit 7 = ”,(PORT1.AND.80H)/80H

110 END

The Microcontroller Idea Book

41


Chapter 3

Listing 3-2. Allows you to set or clear individual bits of Port 1.

10 INPUT “Enter a bit to set or clear (0-2, 4-7) :”,X 20 IF X=3 THEN PRINT “Do not change bit 3!” : GOTO 10 30 INPUT “Enter 1 to set, 0 to clear :”,Y

40 IF Y=1 THEN PORT1=PORT1.OR.2**X

50 IF Y=0 THEN PORT1=PORT1.AND.0FFH-2**X

60 END

Run the program and follow the on-screen instructions to set or clear a bit. To monitor a port bit as you set and clear it, you can use a logic probe, voltmeter, or oscilloscope. For example, to monitor bit 0, place a logic probe on pin 1 of U1, or connect the + lead of a voltmeter to pin 1 and the - lead to ground.

Accessing Memory

Listing 3-3 allows you to read and write to external RAM. Here is an example of what happens when you run this program:

Enter 0 (read), 1 (write), or 2 (quit): 1 Free memory ranges from 397H to 1FFFH Enter an address to write to : 1000H Enter data to be written : 55H

55H has been written to address 1000H Enter 0 (read), 1 (write), or 2 (quit): 0 External RAM ranges from 0 to 1FFFH

Enter an address to read : 1000H 55H is stored in address 1000H

If you write to an address outside the range specified as free memory, you will overwrite the RAM currently in use to store your program and run BASIC-52. If you do this accidentally, your system may crash and you’ll have to reset the system and re-enter the program.

If you prefer decimal numbers to hex notation, change each PH0 in the program to PRINT. (PH0. includes a period; PRINT does not.)

Real-time Clock

Listing 3-4 demonstrates BASIC-52’s real-time clock by displaying an on-screen 60-second timer.

42

The Microcontroller Idea Book

Powering Up

Listing 3-3. Allows user to read and write to external memory.

10

DO

20

INPUT “Enter 0 (read), 1 (write), or 2 (quit): ”,RW

30

IF RW=0 THEN GOSUB 70

40

IF RW=1 THEN GOSUB 120

50

WHILE RW<>2

60

END

70

PH0."External RAM ranges from 0 to “,MTOP

80

INPUT “Enter an address to read : ”,A

90

B=XBY(A)

100

PH0.B," is stored in address “,A

110

RETURN

120

PH0."Free memory ranges from “,LEN+512,” to “,MTOP

130

INPUT “Enter an address to write to :”,A

140

INPUT “Enter data to be written :”,B

150

XBY(A)=B

160

PH0.B," has been written to address “,A

170

RETURN

For the timer to be accurate, you must set XTAL to match the timing crystal your system uses.

Further Experiments

Feel free to continue experimenting with BASIC-52 programs, using the programming reference as a guide. You can do quite a bit with just these circuits.

Listing 3-4. Real-time clock.

10

CLOCK 1:TIME=0:SEC=0

20

DO

30

ONTIME 1,60

40

WHILE SEC<60

50

END

60

TIME=TIME-1

70

SEC=SEC+1

80

PRINT SEC

90

RETI

The Microcontroller Idea Book

43


Chapter 3

Listing 3-5. This program uses BASIC-52’s GET instruction to detect when the user has pressed a key.

10

CLOCK1:TIME=0:SEC=0

20

PRINT “Press any key to quit”

30

DO

40

ONTIME 1,100

50

G=GET

60

UNTIL G<>0

70

END

100

TIME=TIME-1

110

PH0. PORT1

120

RETI

Exiting Programs

Some programs, such as Listing 3-3’s, continue to run until the user requests to end it. In BASIC-52, there are several ways to detect that the user wants to stop a program.

Set a User Variable

In Listing 3-3, the program displays a menu of choices on the host computer’s screen. The program continues to run until the user selects QUIT by entering 2, which sets the variable RW to 2 and causes the DO...WHILE loop and the program to end.

Use GET

Sometimes, selecting a menu option isn’t convenient or appropriate. Listing 3-5 reads and displays the value of PORT1 once per second until the user presses any key at the host computer. The program uses BASIC-52’s GET operator to detect a keypress. GET stores the ASCII code of a keypress at the host computer. Setting a varialble equal to GET (line 50) causes GET to reset to 0. You can detect a keypress by reading GET periodically. If GET

Listing 3-6. This program will end only when the user presses CONTROL+C.

10 CLOCK 1:TIME=0:SEC=0

20 DO

30 ONTIME 1,100

40 WHILE 1=1

50 END

100 TIME=TIME-1

110 PH0. PORT1

120 RETI

44

The Microcontroller Idea Book

Powering Up

Listing 3-7. This program ends when INT1 (pin 13) is brought low and causes an interrupt routine to execute.

10

CLOCK 1:TIME=0:SEC=0

20

A=0

30

PRINT “Bring INT1 (pin 13) low to end program.”

40

DO

50

ONTIME 1,100

60

ONEX1 200

70

WHILE A=0

80

END

100

TIME=TIME-1

110

PHO. PORT1

120

RETI

200

A=1

210

RETI

doesn’t equal zero, it means that a key was pressed. In Listing 3-5, when GET no longer equals 0, the program ends.

Wait for CONTROL+C

You can always end a program by pressing CONTROL+C at the host’s keyboard. The only exceptions are runaway programs that have crashed the system and force you to reboot. Listing 3-6 is an expanded version of Listing 3-5. It continues to read and display PORT1 in an endless loop (DO...WHILE 1=1), until you press CONTROL+C.

Detect a Switch Press

A final method will end a program without any input from the host’s keyboard. You can use this in stand-alone projects that don’t connect to a host computer at all. Listing 3-7 ends when the 8052-BASIC’s pin 13 (INT1) goes low, which causes an interrupt routine to execute. Bring the pin low by jumpering it briefly to GND, or connect a pushbutton switch as described in Chapter 7.

The Microcontroller Idea Book

45