Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf

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

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

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

Добавлен: 15.06.2025

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

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

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

302 The Quintessential PIC Microcontroller

Program 11.8 Displaying a 3-digit decimal number on a scanning readout.

; Task 1

DISPLAY

movf

BINARY,w

call

BIN_2_BCD

; Task 2(a)

LOOP

movf

HUNDREDS,w

call

SVN_SEG

movwf

PORTB

bcf

PORTA,2

call

DELAY_10MS

bsf

PORTA,2

; Task 2(b)

movf

TENS,w

call

SVN_SEG

movwf

PORTB

bcf

PORTA,1

call

DELAY_10MS

bsf

PORTA,1

; Task 2(c)

movf

UNITS,w

call

SVN_SEG

movwf

PORTB

bcf

PORTA,0

call

DELAY_10MS

bsf

PORTA,0

goto

LOOP

;Get binary byte

;Convert to 3-digit BCD

;Get Hundreds nybble

;Convert to 7-segment code

;Send out to PortB

;Enable Hundreds display

;for 10ms

;and turn off

;Get Tens nybble

;Convert to 7-segment code

;Send out to PortB

;Enable Tens display

;for 10ms

;and turn off

;Get Units nybble

;Convert to 7-segment code

;Send out to PortB

;Enable Units display

;for 10ms

;and turn off

;DO forever

ing on the software. Indeed, as illustrated here the entire existence of the PIC will be to service the display! However, in practice the situation can be redeemed somewhat by interrupting the PIC at 10 ms intervals to avoid the need for time-wasting delay routines. The listing on page 380 shows how this can be done, but of course the Timer cannot be used for anything else. Alternatively an external 100 Hz oscillator can be used in its place, but some of the hardware advantages are then lost.

Another issue that can occur with scanning, is noise introduced by switching on relatively large currents on a continual basis. This can be a particular problem where analog circuitry is adjacent. Good powersupply decoupling can reduce this problem to some extent.

Self-assessment questions

11.1 One problem with the intruder alarm configuration of Fig. 11.10 is the need to cable the Zone ports with eight conductors plus one per


11. One Byte at a Time 303

zone. An alternative approach would be to replace each zone’s 3-state bu er by a PIC. Each PIC would drive a 4-wire common bus back to the main base PIC. One wire can be used as a shared handshake line to signal the base that an intruder has been sensed at the zone indicated on the three data wires.

Show how a PIC16F84 could be configured as a Zone PIC paying particular attention to the usage of the single handshake line shared with all zones.

Would it be possible to reduce the number of wires to three? How could a local display be added to show which sensor has been set o ?

11.2 A certain PIC running at 20 MHz has its Port C connected to LEDs tied high through a 1 kΩ resistor and with a 300 pF capacitance to ground. All LEDs are o and the programmer attempts to turn on LED 7 and LED 0 as follows:

bcf

PORTC,7

;

Turn

on

LED7

bcf

PORTC,0

;

Turn

on

LED0

However, only LED 0 actually turns on. What is happening?

11.3A certain system needs to be able to both activate eight LEDs and to be able to read the state of up to eight normally-open (N.O.) push switches. It has been proposed that a single Port B might be able to combine these functions – the former when set to output, the latter when set to input. Can you devise a suitable circuit?

11.4A PIC-based reaction meter is to be designed to act as a crude bloodalcohol level indicator. The principle of the device is that a buzzer is sounded for 100 ms when the unseen tester closes his or her switch. The subject is to respond to the sound by immediately pressing his/her switch. An 8-LED barograph display is to indicate the passage of time by progressively illuminating an additional LED every 50 ms. The number of lit LEDs at the conclusion of the test is the reaction time in 50 ms steps.

Show how a PIC16F84 could be configured in hardware and software to read both switches and activate the eight LEDs and one buzzer.

11.5 The variation of logic 0 output voltage VOL against sink current IOL for the two extremes of the commercial temperature range is shown in Fig. 11.15. Using this graphical relationship determine the maximum value of series resistor to ensure a current of no less than 20 mA will


304 The Quintessential PIC Microcontroller

flow through an LED connected to +5 V, as shown in the diagram, for any temperature. With this value what will be the current be at −40◦C? You can assume that the conducting voltage across the LED is a constant 2 V.

+5 V

VOL

IOL

RB

5 V

4 V

3 V

2 V

Min @ +85 C

Max @ -40 C

1 V

0

20

40

60

80

100

IOL (mA)

Sink

Fig. 11.15 Low-level output voltage against sink current.

11.6Repeat the exercise of SAQ 11.4 but coding in C.

11.7Extend the digital comparator of Example 11.2 to compare two external digital bytes presented to a 28-pin footprint PIC, with byte P being input at Port B and Q at Port C.

11.8In a low-power wireless data logging system placing the PIC in its sleep mode will not a ect the current consumption of the radio transmitter. It is proposed to use a port pin to supply current to the transmitter and in way this auxillary circuitry can be switched on and o as necessary. Discuss.


CHAPTER 12

One Bit at a Time

Parallel data transmission is fast, with a minimum of software overhead. However, there are circumstances where its use is inappropriate; either because of the additional hardware cost (see Fig. 11.10 on page 287) or more commonly where the receivers are geographically distant, with the concomitant cost or non availability of multiple communication channels and their necessary interface hardware. In such situations data can be sent one bit at a time and assembled by the remote device into the original data bytes. In this manner a comparison can be made with the parallel port on a PC, commonly used for local peripherals, such as a printer, and the serial port frequently used with a modem to link into the internet via a single telephone line.

Smart bank

.com

VISA

+5V

RST

CLK

Serial I/O

Hologram

Embossing area

DEBIT

CARD

Fig. 12.1 The smart card.

As an example, consider the smart cards in your wallet. Each card will have an embedded microcontroller, typically 8-bit, giving it its intelligence. Cost constraints are severe to give a manufacturing price of under $1, and a large component of this is accounted by the non corrosive gold-plated contacts via which the microcontroller is powered and clocked when in contact with the card reader. In order to keep the mechanical precision of the reader low and hence reliability high, the number of contacts must be minimized and pad size maximized.

306 The Quintessential PIC Microcontroller

The standard arrangement shown in Fig. 12.1 uses contacts to provide the two power nodes, Reset, Clock and one line to allow data to be shifted in or out one bit at a time. Although this is relatively slow, in comparison to the human-mechanical constraints speed is not an issue. Furthermore, contact between the reader/automatic teller and the central computer, perhaps several thousands of miles/kilometers away, will typically be via a single channel telephone or ISDN line.

In this chapter we will examine a range of techniques used to serially transmit data, both using bespoke shift register circuits and industrial devices using standard communication protocols. After reading this chapter you will:

Understand the need for serial transmission.

Be able to design serial ports and associated software routines to communicate with standard parallel peripheral devices.

Be capable of interfacing serial peripheral devices using both the SPI and I2C protocols.

Appreciate the need for asynchronous serial communication and be able to write software drivers conforming to this protocol.

Be able to use the integral Universal Synchronous Asynchronous Receiver/Transmitter Port (USART) for asynchronous protocols.

Understand the necessity for bu ering long distance communication circuits.

Examine the parallel 3-digit 7-segment display interface of Fig. 11.13 on page 298 which uses both the parallel ports A and B. Although this is a working circuit, most of the parallel port budget of an 18-pin footprint device has been used up. Speed is certainly not a factor here, so a slower mode of data transmission is acceptable.

Consider the serial equivalent of Fig. 12.2. Here only two port pins are used. One labelled SDO (Serial Data Output) outputs the data bit by bit, most significant bit first. The other, labelled SCK (Serial ClocK) is used to clock the three shift registers at the one time, and hence shift the data right one bit at a time.

Each display has an associated 74HCT164 8-bit shift register1 – see Fig. 2.20 on page 36. The 74HCT164 has a positive-edge triggered shift input clock C1 and two serial data inputs ANDed together at 1D. One of these data inputs can be used to gate the other input, but in our example they are both connected together to give a single serial input. There is also an active-low Reset input to clear the register contents, which are held high in the diagram. If desired, another port line can be used to drive R.

To change the display, a total of 24 bits will have to be shifted into the register array. To see how this can be done we will repeat the 7-segment

1All data outputs are simultaneously available and thus the 74HCT164 is described as a Serial-In Parallel-Out (SIPO) register as well as a SISO shift register.