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

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

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

Добавлен: 15.06.2025

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

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

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

Programming

PRINT TAB(2) “hello”

hello

TAN(expression)

C,R

Returns the tangent of expression.

TCON

C,R

Retrieves or assigns a value to the 8052’s special function register TCON.

TIME

C,R

Retrieves or assigns a value, in seconds, to BASIC-52’s real-time clock.

TIMER0

C,R

Retrieves or assigns a value to the 8052’S special function registers TH0 and TL0.

TIMER1

C,R

Retrieves or assigns a value to the 8052’s special function registers TH1 and TL1.

TIMER2

C,R

Retrieves or assigns a value to the 8052’s special function registers TH2 and TL2.

TMOD

C,R

Retrieves or assigns a value to the 8052’s special function register TMOD.

U.

PRINT option. Same as USING.

UI0

C,R

Restores BASIC-52’s console input driver after using UI1.

UI1

C,R

Allows a user-provided assembly-language console (host computer) input routine

to replace BASIC-52’s console input driver. External program memory location

4033h must contain a jump to the user’s routine.

UO0

C,R

Restores BASIC-52’s console output driver after using UI1.

UO1

C,R

Allows a user-provided assembly-language console (host computer) output rou-

tine to replace BASIC-52’s console output driver. External program memory loca-

tion 4030h must contain a jump to the user’s routine.

The Microcontroller Idea Book

85


Chapter 5

USING (FN)

PRINT option. Causes BASIC-52 to output numbers in exponential format with N significant digits. BASIC-52 always outputs at least 3 significant digits. Maximum expression is 8.

Example:

PRINT USING(F3),3,4.1,100

3.00

E 0

4.10

E

0

1.00

E

2

USING(0)

PRINT option. Causes BASIC-52 to output numbers from ±.99999999 to ±0.1 as decimal fractions. Numbers outside this range display in USING(FN) format. USING(0) is the default format.

USING (#[...#][.]#[...#])

PRINT option. Causes BASIC-52 to output numbers using decimal fractions, with # representing the number of significant digits before and after the decimal point. Up to eight # characters are allowed.

Example:

PRINT USING(###.##),3,4.1,100 3.00 4.10

00.00

XBY(expression)

C,R

Retrieves or assigns a value in external data memory.

XFER

C

Copies the current program from the EPROM space (beginning at 8010h for pro-

gram 1) to RAM (beginning at 200h), and selects RAM mode.

expression .XOR. expression

C,R

Logical exclusive OR

XTAL

C,R

Assigns a value equal to the system’s crystal frequency, for use by BASIC-52 in

timing calculations.

86

The Microcontroller Idea Book

Inputs and Outputs

6

Inputs and Outputs

So far, our BASIC-52 circuit consists of the 8052-BASIC microcontroller, RAM, nonvolatile memory for permanent program storage, and a serial interface to a host computer. Now it’s time to add inputs and output interfaces that enable the system to monitor and control devices outside of these circuits. The options include low-cost buffers and latches, as well as programmable chips with features like individual bit control, automatic generation of control signals, and the ability to configure a port as input, output, or bidirectional.

The Memory Map

But before we start adding components, it’s time to draw a memory map for the system. The memory map is a diagram that shows the range of addresses a microcontroller or other computer can access, along with each component that the computer reads or writes to and the addresses where each component resides. The components may be memory chips like RAM or EPROM, or they may be other components that the computer accesses by specifying an address. Drawing a memory map helps to ensure that each component has a unique address or range of addresses.

Figure 6-1 shows the memory map for our design. Each 64K area of external memory consists of eight 8K blocks. Remember that 8 kilobytes equals 8192 in decimal, but 2000 in hexadecimal.

The Microcontroller Idea Book

87


Chapter 6

Internal code memory consists of the 8052-BASIC’s ROM, which uses the addresses from 0 to 1FFFh.

External data memory beginning at 0 is also required. BASIC-52 reserves the first 512 bytes for its own use, and it stores the current BASIC-52 user program immediately above this area. The memory map allows a choice of using an 8K RAM from 0 to 1FFFh, or a 32K RAM, which uses the entire area from 0 to 7FFFh. (The 8052 also has 256 bytes of internal data memory, which the memory map doesn’t show.)

Two 8K blocks of combined code/data memory are reserved beginning at 8000h. As Chapter 4 showed, BASIC-52’s programming commands store programs in nonvolatile memory beginning at this location.

For the input and output, or I/O, circuits described in this chapter, the memory map reserves the top block of data memory, from E000h to FFFFh. I/O interfaces include connections to buffers, latches, switches, displays, motors, or just about anything besides the system’s main memory. The I/O circuits don’t have to use this block, but BASIC-52 encourages it, since it clears external memory only up to E000h on bootup (unless you specify a lower value by saving MTOP with a PROG3 command.)

Figure

6-1. Memory map for the 8052-BASIC system’s internal and external

data and program memory.

88

The Microcontroller Idea Book

Inputs and Outputs

Unassigned space remains in the memory map, but there’s nothing wrong with this since it leaves room for additions. Also, this isn’t the only way to configure an 8052-BASIC system. For example, if you use a single 8K EPROM at 8000h, you can use the area from A000h to BFFFh for additional I/O.

Not shown are two areas that BASIC-52 reserves for optional enhancements. If you customize BASIC-52 by adding your own instructions, commands, or reset routines, BASIC-52 expects to find parameters relating to these in code memory from 2001h to 2090h. And, if you want to call assembly-language interrupt routines, BASIC-52 expects to find vectors for these in code memory from 4003h to 41FFh. For these, you can use EPROM, EEPROM, or NV RAM, as Chapter 13 shows.

Uses for I/O Ports

Just about all microcontroller circuits need to be able to do more than just read and write to memory. Other uses involve sensing and controlling of conditions, events, or devices external to the basic circuits. For example, a microcontroller-based drilling machine for printed-circuit boards might have these responsibilities:

Detect when a user presses a switch.

Move the pc board so that the hole to be drilled lies under the drill bit. Set the speed of rotation for the drill bit.

Lower the bit into the board, then raise it after drilling.

Detect problems, such as a bit that doesn’t lower or a drilling obstruction. Display messages to prompt the user for input or show progress.

These functions all involve reading and writing, but instead of reading and writing to memory, the microcontroller reads sensors and switches, and writes to motors and displays. Reading and writing to devices other than memory is often called input/output, or I/O for short.

Adding Ports

The 8052-BASIC has a few pins on Port 1 that you can use for I/O. Chapter 3 included programs for reading and writing to these. But many projects will require more I/O than these few pins can offer.

Figure 6-2 shows a circuit that allows you to add up to eight 8-bit ports to the main circuit. You can design the ports as inputs or outputs, in any combination.

The Microcontroller Idea Book

89


Chapter 6

90

The Microcontroller Idea Book

Inputs and Outputs

U11 is a 74HCT138 3-to-8-line decoder that generates individual chip-enable signals for eight 1K blocks in the memory area from E000h to FFFFh. It works the same way that U6 generates chip selects for 8K blocks in the main circuit.

U11 is enabled whenever pin 7 of U6 is low, which occurs when the 8052-BASIC reads or writes to addresses from E000h to FFFFh.

Address lines A10, A11, and A12 determine which of U11’s outputs goes low when the chip is enabled. As with U6, each output is low for a different memory area. For example, pin 15 is low only when addresses from E000h to E3FFh are being read or written to.

One possible point of confusion is that both pin 7 of U6 ( in Figure 3-1) and pin 15 of U11 are chip-selects with a starting address of E000h. One controls memory accesses in the entire 8K block from E000h to FFFFh, while the other controls only the 1K block from E000h to E3FFh. To distinguish the two, I’ve labeled U11’s pin 15 as E000H-1K.

U12 and U14 are examples of input and output ports that U11 can enable.

An Input Port

U12 is a 74LS244 octal buffer that adds eight inputs to the circuit. The buffer’s Y outputs connect to the system’s data bus (D0-D7). The chip has two groups of four buffers, with each having its own enable input (1G and 2G). For this application, the enables are tied together, and all eight buffers are accessed as a group.

I used an LSTTL device for the buffer rather than a CMOS 74HC244 for a couple of reasons. The LSTTL chip has Schmitt-trigger inputs, which are less sensitive to noise. Plus, unlike CMOS, the LSTTL buffer requires no pull-up resistors at unused inputs.

OR gate U9B ensures that U12 is enabled only when U2 (the 8052-BASIC chip) reads an address from E000h to E3FFh. When this occurs, output-enable pins 1 and 19 go low, the buffer’s outputs follow its inputs, and U2 reads the data that U12 has placed on D0-D7.

After a read operation, pins 1 and 19 go high and U12’s outputs are high-impedance. In other words, the outputs are electrically similar to an open circuit, which ensures that they won’t interfere with other operations on the data bus.

In the circuit, U12 is read-only. Write operations to E000h have no effect. To access U12 at a different address, wire pin 4 of U9B to a different output of U11.

Memory decoding options. Note that although U12 holds just one byte of information to read, you can access it at any address from E000h to E3FFh. This may seem like a waste of

The Microcontroller Idea Book

91

Chapter 6

1023 (3FEh) addresses. Indeed, if using every byte of memory is critical, you can use other methods in place of U11 to more completely decode, or divide, the memory space.

Programmable logic is one possibility, for those who have access to a logic compiler and device programmer. But for many circuits, complete decoding of memory isn’t necessary, and less-than-complete decoding using off-the-shelf parts is more practical.

An Output Port

To complement U12’s input byte, U14 provides eight bits of output. U14 is a 74LS374 octal flip-flop, which is very similar to the 74HCT373 octal latch at U4. On the ’373, 1Q-8Q follow 1D-8D until pin 11 goes high, after which 1D-8D no longer change. In contrast, on the ’374, 1Q-8Q change only on pin 11’s rising edge, when 1D-8D are latched to 1Q-8Q.

Here again, I chose LSTTL over CMOS, this time because the LSTTL device can sink 12ma at 0.25V, compared to 6ma at 0.2V for the 74HC374. However, the 74HC374 can also source 6ma at 4.2V, so it’s a better choice if you need to draw current from a logic-high output.

U14’s eight data inputs connect to the data bus (D0-D7). Its output control (pin 1) is tied low so that the outputs are always enabled. If you want to be able to disable the outputs, you can instead tie pin 1 to an unused bit on Port 1.

NOR gate U13A clocks U14 only when U2 writes to addresses from E400h to E7FFh. When this occurs, the data written is latched to the outputs of U14. The outputs do not change until the next time the chip is written to.

In the circuit, U14 is write-only. Reading address E400h will return the value 0FFh. To access U14 at a different address, wire pin 2 of U13A to a different output of U11.

Since U12 is read-only and U14 is write-only, you could use the same address for both, by having them share the same output of U11. For example, if you wire both pin 2 of U13A and pin 4 of U9B to pin 7 of U11, both will be accessed at FC00h. Write operations will access U12, and reads will access U14. In this way, you can add up to eight input ports and eight output ports, with eight chip-select addresses in all.

Wiring Tips

Add Figure 6-2’s circuits to Chapter 3’s circuits, including the connections shown to D0-D7, A10-A12, E000h, READ, and WRITE in the main circuit. The schematic continues the component numbering sequence from the earlier schematics. Use sockets for the ICs. Wire U13’s unused inputs (pins 5, 6, 8, 9, 11, and 12) to GND or +5V. Also add a couple of 0.1-microfarad ceramic decoupling capacitors from +5V to GND, near the added ICs.

92

The Microcontroller Idea Book