18
Chapter 23L Lab C5 SiLabs 5: Data Table; Serial Buses REV 1 1 ; November 20, 2011. Contents 23LLab C5 SiLabs 5: Data Table; Serial Buses 1 23L.1Port Pin Use in this Lab .................................................... 2 23L.2RAM Data Table ........................................................ 2 23L.2.1A Program too Long to Type: “Storage Scope” .................................... 3 23L.2.2Suggested Scope and Function-Generator Settings ................................... 5 23L.3Serial Bus 1: RS232 UART .................................................. 6 23L.3.1Hardware ........................................................ 6 23L.3.2Controller can talk to a PC ............................................... 7 23L.3.3Code: Controller Talks to PC .............................................. 8 23L.3.4Reversing the Transmission: Controller Can Listen to a PC .............................. 12 23L.3.5Code: Controller Listens to PC ............................................. 13 23L.4Serial Bus 2: SPI ........................................................ 14 23L.4.1Hardware: SPI Controls Digipot ............................................ 14 23L.4.2Apply the Digipot ................................................... 18 1 Revisions: corrected port assignments in diagram and in listing for RAM table program; add pinout use diagram, update serial-send program to aprill2011 (nov11); fix pin-use diagram on p. 1 and elsewhere (4/11); add UART micro-listens section; add figure showing playback faster than input; add Config Wizard images and Hyperterminal screen shots for UART section; speed system clock in store program; correct placement comment re INT1, RAM program (P0.7). 1

Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Chapter 23L

Lab C5 SiLabs 5: Data Table; SerialBuses

REV 11; November 20, 2011.

Contents

23LLab C5 SiLabs 5: Data Table; Serial Buses 123L.1Port Pin Use in this Lab . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . 223L.2RAM Data Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . 2

23L.2.1A Program too Long to Type: “Storage Scope” . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . 323L.2.2Suggested Scope and Function-Generator Settings .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

23L.3Serial Bus 1: RS232 UART . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . 623L.3.1Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . 623L.3.2Controller can talk to a PC . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . 723L.3.3Code: Controller Talks to PC . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 823L.3.4Reversing the Transmission: Controller Can Listento a PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1223L.3.5Code: Controller Listens to PC . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 13

23L.4Serial Bus 2: SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . 1423L.4.1Hardware: SPI Controls Digipot . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 1423L.4.2Apply the Digipot . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . 18

1Revisions: corrected port assignments in diagram and in listing for RAM table program; add pinout use diagram, update serial-sendprogram to aprill2011 (nov11); fix pin-use diagram on p. 1 andelsewhere (4/11); add UART micro-listens section; add figure showingplayback faster than input; add Config Wizard images and Hyperterminal screen shots for UART section; speed system clockin storeprogram; correct placement comment re INT1, RAM program (P0.7).

1

Page 2: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

2 Lab C5 SiLabs 5: Data Table; Serial Buses

23L.1 Port Pin Use in this Lab

We need to use seven of the eight pins of PORT0, this time. You will need to disconnect any conflicting usesfrom earlier labs. We have shown one signal, “MISO” (an SPI serial bus signal), though we do not use it inthis lab. We use it in the next lab, and it seems to make sense toinclude the full SPI bus wiring at this stage.

Figure 1: PORT0 Pin use in this lab

P0.5 is put to two different uses, in this lab. Be sure to disconnect the first use (INT0*) when you shift to thesecond (RXD).

Fig. 2 shows a map of pin use in all the SiLabs labs. You saw thisdiagram earlier, in lab C1—but this figurecorrects several errors in the figure of Lab C1.

Figure 2: ’410 pin use in the several controller labs

The small figures in parentheses indicate the labs in which the specified use occurs.

23L.2 RAM Data Table

The ’410 includes a modest amount of on-chip RAM: 2K. This is not enough to store much input from anADC—the task we will ask of it, today. 2K might be more appropriate as a buffer to store keyboard input,for example. But to demonstrate use of the on-chip “MOVX RAM,” we will use it today to store a couple ofthousand samples from the ADC, and then to play those back endlessly to the DAC. In this operation it willperform like a “storage scope” of small capacity.

This program uses GETSAMPLE from the ADC code you saw last time, and sends the ADC samples out tothe DAC, as it did last time. The novelty, today, is that for the first time we are using the 8051’s “data pointer”(DPTR), a 16-bit register that you have read about in class notes, but may well have ignored, since it has notappeared in any SiLabs lab program till now. DPTR defines the address used in anoff-chipdata transfer (with

Page 3: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 3

the Dallas computer on the other “branch” of these micro labs, this is the normal way to read and write data;that computer usesbusesand a relatively-large off-chip RAM [32K]).2

The routine STORE IT places one sample in RAM, at an address defined by the contents of DPTR. It thenadvances the pointer and checks to see if the table is full:

STORE_IT: MOV A, SAMPLE_HI ; if we haven’t hit end of RAM, store the sampleMOVX @DPTR, A ; store sampleINC DPTRMOV A, DPH ; beyond end of RAM?CJNE A, #08h, OKSETB TABLEFULL ; if RAM’s done, let the world know this

OK: RET

The way this code checks for “table full” is idiosyncratic: because the on-chip RAM resides at addresses 0 to7FFh, we can detect “. . . full” by watching thehighbyte of DPTR (an 8-bit register named DPH: data pointerhigh). When an increment of DPTR takes DPH to 8h, the pointer has moved past available RAM, and it istime to stop.3 The same scheme is used onplaybackfrom the table of stored data.

23L.2.1 A Program too Long to Type: “Storage Scope”

The program listed below would be painful to type in, so we hope you will download it, instead.

; adc_store_on_chip_nov11.a51 : try storing 8-bit adc samples in on-chip RAM; ports re-assigned, nov 1l

$NOSYMBOLS ; keeps listing short, lest...$INCLUDE (C:\MICRO\8051\RAISON\INC\c8051f410.inc) ; ...this line might produce huge list

; of symbol definitions (all ’51 registers)

$INCLUDE (C:\MICRO\8051\RAISON\INC\VECTORS320.INC) ; Tom’s vectors definition file

STACKBOT EQU 080h ; put stack at start of scratch indirectly-addressable block (80h and up)SOFTFLAG EQU 0 ; software flag at bit 0: used by ISR to say ’time to sample’TABLEFULL EQU 1 ; another soft flag. This one indicates pointer moved past end of RAM

TABLESTART EQU 0h ; start of data table: 0 is full-size; you could make the table tiny for debugging purposes,; Defining TABLESTART as 07FEh, for example, would let you step through

DISPLAY_HI EQU P1 ; display high 8 bits of 12

SAMPLE_HI EQU R7SAMPLE_LO EQU R6

; INT0 at P0.5, to signal time to take a sample; ADC at P0.0; DAC at PO.1

ORG 0hLJMP STARTUP

ORG 080h

STARTUP: mov SP, #STACKBOT-1

acall USUAL_SETUPacall PORT_SETUPacall ADC_SETUPacall DAC_SETUPacall INT_SETUPclr SOFTFLAGmov DPTR, #TABLESTART ; init table pointer, for storage

2We hope you can forgive our calling this MOVX RAM “off-chip” when in fact it nestles on the ’410 IC! Well, it is analogous tooff-chip RAM. It’s the RAM that shows up as “external” in the 8051 memory map that we gave you back in day Micro 2.

3In this event, the DPTR “wraps around” to value zero, and further data writes would overwrite good data. But that’s OK: theDPTRis tested after an increment and before its use, so the overwrite does not occur.

Page 4: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

4 Lab C5 SiLabs 5: Data Table; Serial Buses

clr TABLEFULL ; for first pass

; ---------------------------

CHECK_FILL: jnb SOFTFLAG, $ ; hang here till interrupt says ’time to transfer’clr EA ; disable interrupts till sample safely storedclr SOFTFLAG

acall GET_SAMPLEjb TABLEFULL, PLAYBACK ; if table is full, recall data for playback

acall STORE_IT ; ...but till full, keep storing samplessetb EA ; enable interrupts, now that sample is in placesjmp CHECK_FILL

PLAYBACK: mov dptr, #TABLESTART ; re-init store pointerCHECK_PLAY: setb EA ; re-enable interrupts

jnb SOFTFLAG, $ ; hang here till interrupt says ’time to transfer’clr EA ; disable interruptsclr SOFTFLAGmovx a, @dptr ; get stored sample

mov IDA1H, a ; ...and send to DACinc dptrmov a, dph ; beyond end of RAM?cjne a, #8h, CHECK_PLAY ; If not, go get another a bytesjmp PLAYBACK ; ...but if done, reinit and play table anew

STORE_IT:mov a, SAMPLE_HI ; if we haven’t hit end of RAM, store the sample

movx @dptr, a ; store sampleinc dptrmov a, dph ; beyond end of RAM?cjne a, #8h, OK ; this checks for address of 800h (2K)setb TABLEFULL ; if RAM’s done, let the world know this

OK: ret

;--- SUBROUTINES ----

; ISR0: This is response to INT ZERO set softflagORG INT0VECTOR ; this is defined in VECTORS3210.INC, included above.

; It is address 03h, the address to which micro hops; in response to interrupt ONE

ISR0: setb SOFTFLAG ; pseudo-polling: a flag that MAIN will checkRETI

GET_SAMPLE:mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)mov ADC0CN, #94h ; high on ADOBUSY starts conversionjnb ADC0CN.5, $ ; hang here till conversion-done flagmov SAMPLE_HI, ADC0H ; high bytemov SAMPLE_LO, ADC0L ; (we won’t use this low byte)ret

;------- INITIALIZATIONS

USUAL_SETUP:anl PCA0MD, #NOT(040h) ; Disable the WDT.

; Clear Watchdog Enable bit

; Configure the Oscillator; orl OSCICN, #04h ; sysclk = 24.5 Mhz / 8; next line if you want full-speed

mov OSCICN, #087h

; Enable the Port I/O Crossbarmov XBR1, #40h ; Enable Crossbarret

PORT_SETUP: setb P0.0 ; make sure latch is high (this for ADC)setb P0.5 ; ...this for INT0*

mov P0MDIN, #0FCh ; set DAC0 and ADC pins (d1 and d0) low, for analogmov P1MDIN, #0FBh ; ditto for voltage ref

Page 5: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 5

mov P0SKIP, #023h ; tell crossbar to skip DAC0 bit (bit1) and ADC bit (bit0) int INT0 (bit5)mov P1SKIP, #004h ; ...and skip this pin for voltage ref

ret

ADC_SETUP: mov REF0CN, #13h ; voltage ref value borrowed from SiLabs ADC example; DK what ’enable Vdd as Vref’ means; (but see sample file, ...DACs_SineCosine.c)

mov ADC0MX, #0h ; makes P0.0 the analog-mux path to ADC 0

mov ADC0CF, #0F8h ; this is the default, but OK: says do one conversion at a time

mov ADC0CN, #84h ; turns on ADC0; left justified; will start on write to AD0BUSY bit (#94h)

mov ADC0TK, #0FBh ; set tracking to default--"dual tracking"; # SAR cycles borrowed from SiLabs ADCret

DAC_SETUP: mov IDA1CN, #0F2h ; enable DAC1; update on write to high byte; left-justified; 1mA full-scaleret

; ----NOW ENABLE INTERRUPTS----INT_SETUP: setb IT0 ; make INT0 Edge-sensitive

setb EX0 ; ...and enable INT0clr IE0 ; clear int0 flag (not necessary, but permits watching this flag)mov IT01CF, #05h ; SiLabs special: makes interrupt active-low, places INT0 at P0.5setb EA ; Global int enable (pp.31-32)ret

END

One other detail of this program may call for explanation: inbothSTOREITandPLAYBACKroutines, wedisable interrupts. We do that so that you can single-step the program even when it is getting interrupt signalsat its usual high rate: you can watch the two routines in slow motion, untroubled by continual new responsesto interrupt.

23L.2.2 Suggested Scope and Function-Generator Settings

You can improvise, of course. We started with interrupt frequency (which defines fsample) 4kHz, analoginput frequency 1kHz. This gives 4 samples/period, so the steppiness of the DAC output will be obvious—andmay make you want to apply the MAX294 filter that we hope you built last time.

23L.2.2.1 A Curious Effect if You Pushfsample too High

We saw a strange effect that we had not anticipated. It appeared when the sampling rate was pushed beyondthe capability of the ’410’s ADC (about 210 kilosamples/second [ksps], when clock is set to full-speed). Wesaw what fig. 3 shows:

Figure 3: Startling result, when fsample outruns the ADC, in RAM-store program

What?, we asked ourselves. This looks like aliasing—exceptit’s backwards: the stored waveform comes at a

Page 6: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

6 Lab C5 SiLabs 5: Data Table; Serial Buses

frequencyhigherthan that of the signal that was sampled. What’s going on?

On the face of it, this result seemed impossible: bothstoreandplaybackroutines are timed by interrupt: eachwaits for thesoft flag bit set by interrupt. Therefore, it seemed, store and playback rates must match. Butthey don’t.

We did figure it out, eventually.4 This odd effect results from an asymmetry in the program: thestoragerate—which we had thought of as set by thefinterrupt—is set byfinterrupt only so long as that rate doesn’toutrun the ADC. The ADC needs about 5µs to complete a conversion.

Whenfinterrupt does go beyond what the ADC can handle, then not every interrupt causes a new sample toget stored. The storage rate hits a limit. But theplaybackrate hits no such limit: feeding the dac requires nosuch 5µs wait. Thus the playback rate can rise far above the storage rate, as shown in fig. 3 on the precedingpage.

Incidentally, the figure shows a detail that might at first puzzle a person: the kink in mid-screen. That’s thepoint where the end of the data table meets the start of a new playback cycle (showing the beginning of thedata table).

Serial Buses

Controllers are always wishing they had more pins. The smaller the controller, the more urgency there is tothe task of making efficient use of what pins are available. Wehave seen the C2 pin-sharing used to avoidcommitting even two lines to the debugging interface. Serial buses sacrifice speed in the interest of pin-saving. At the extreme, one line can carry all information (assuming, always, that ground is defined, as well).Dallas’ “1-wire” interface is such a scheme. So is the traditional UART bus, often referred to as “RS232.” Wewill start with this one. Later in this lab we will look at one that is less efficient, but also easier to implement,Motorola’s “SPI” bus (“Serial Peripheral Interface”).

23L.3 Serial Bus 1: RS232 UART

The old RS232 “serial port”—now removed from most personal computers—is included on nearly all micro-controllers, and provides a straightforward way for a controller to talk with a bigger computer. The controllercan report to a PC (perhaps to take advantage of the PC’s more sophisticated capacities for data manipulationand display); the PC also can talk to the controller (perhapsto direct the operation of its program: say, todirect the controller in its taking of data). In this sectionwe will try communicating onlytowardthe PC, fromthe micro.

23L.3.1 Hardware

A traditional RS232 link achieves good noise immunity by using very wide signal swings—as wide as± 12V.A simplified RS232 can get by with TTL levels. We will use the latter, simplified scheme, taking advantageof the USB⇔ RS232 translator that is included in the LCD display card. This converter uses the RS232protocol, but at TTL levels. This is convenient for us, allowing us to omit the level-translator sometimesrequired.5 to let PC and controller communicate

4Thanks to undergraduate Bill Rose for the hint that got our thinking un-stuck: the notion that the ADC might be slow.5Only “sometimes” required, because a simple clamp can be sufficient, in place of a true level-translator like those described in AoE

12.3.3.4.

Page 7: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 7

23L.3.2 Controller can talk to a PC

23L.3.2.1 Hardware: PC Listens Through LCD Card

The ’410’s UART uses line P0.4 and P0.5 (TX0 and RX0, respectively). This assignment is rigid. Connectthose lines—which are named from the point of view of thecontroller to the RXD and TXD terminals on theLCD card. Those terminals are labelled from the point of viewof thePC that is connected through the LCDcard. Sorry—but that means that the ’410’s TX0 connects to the LCD’s RXD, RX0 to the LCD board’s TXD.

Figure 4: LCD card provides USB-to-RS232 translation, allowing PC to talk with ’410

There is no rotation in the linking lines, in fig. 4—but don’t forget that TX goes to RX and vice versa.6

23L.3.2.2 Setting up the PC to Talk with the Controller

Hyperterminal is an old utility that comes with Windows, through XP.7 Here’s the way to set it up:

• On the PC, from START menu open RUN; typehypertrm.

• A window will open, suggesting that you want to use a telephone modem(!). You don’t, but give yourconnection a name anyway.

• The next window will offer a modem—but open the menu under “Connect using” and choose COM3(or other port; you may have to use Windows device manager to discover what port is assigned; youalso can use that manager to assign the link to COM3).8

6As you can see, we didn’t wire the ’410 in the figure, except to show the RS232 connection.7For later versions of Windows, a commercial version is available from Hilgraeve (www.hilgraeve.com).8How to control COM port assignments: from the START menu go toControl Panel, choose System, then Hardware, then Device

Manager. If a device is connected to the (virtual-) COM (or “serial”) port, you will see it under “Ports (COM and LPT).” If you clickon the COM port, you can then configure it. Particularly, you can assign it to a particular numbered port (Windows has a nasty habit ofmoving the COM ports about). You can also set the baud rate. Make sure it is at 9600, the rate used by the ’410 programs in thislab.

Page 8: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

8 Lab C5 SiLabs 5: Data Table; Serial Buses

Figure 5: Hyperterm setup screens: choose a COM port, not modem; turn off “Flow Control”

• Once you’ve chosen COM3 you’ll be offered options. Choose 9600 Bits per second (usually called“baud rate”). Accept the other defaults—with oneimportantexception:

• Turn off “Flow Control” By default, Hyperterminal uses handshaking lines that are not present in oursimpler RS232 link. In order to permit the PC to talk to the controller (rather than just listen) we mustturn off this handshaking. We do this by settingflow controlto none, as in the rightmost screen of fig. 5on the preceding page.

Running the Program To start the program you need to click the “Call” icon (a telephone) or the word“Call.” (Hyperterminal still dreams of the glory days of dial-up!) You stop by “hanging up” (hang-up icon).

Once you have started the program, it will display anything that comes in, and any character you type willbe sent on the PC’s TX line. If you find it disconcerting to see nothing when you type, you can turn onEchounder Setting/ASCII setup:

Figure 6: If you want the reassurance of a display when you send a character, choose ECHO

Incidentally, there seems to be no way to clear the screen, except to close and open again.

23L.3.3 Code: Controller Talks to PC

This program—listed below in§ 23L.3.3.4 on page 10— serves only to show that the controllercan, indeed,talk to the world of bigger computers.

Page 9: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 9

23L.3.3.1 Set Baud Rate. . .

The Configuration Wizard makes the setup manageable. The UART relies on one of the timers to set its baudrate; normally setting that up properly requires consulting a table showing timer reload values for a baud rateat a given system-clock frequency: pretty fussy work. The Wizard makes this easy:

Figure 7: Configuration helps to set up timer for proper baud rate

23L.3.3.2 . . .Route UART Signals, using Crossbar

Unlike most signals, the UART signals hold fixed pin assignments, if they are used at all (the DAC’s behavethis way, too; the ADC does not). The crossbar shows this.

Figure 8: UART signals routed on crossbar

On the crossbar we have kept the low 3 bits (LED, DAC and ADC) “skipped,” though that was not necessary

Page 10: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

10 Lab C5 SiLabs 5: Data Table; Serial Buses

in this case: the TX and RX signals would have been routed to the pins shown, in any case.

23L.3.3.3 Controller Talks: Hardware

Wire keypad to port P2 (as in Lab C2), and a byte-wide display to port P1. We won’t use the display until alittle later, in§ 23L.3.4 on page 12.

23L.3.3.4 Controller Talks: Code

This program sends to the PC some characters listed below in the “db” (define byte) line that followsthe routine “GETMESSAGE” How GETMESSAGE works is a detail wetry to explain just after this “se-rial message. . . ” listing.

; serial_message_silabs_apr11.a51 sends a message to pc using UART--and a value (P2, treated as ascii value); Nov 10: splices june 10 program with initializations done by Wizard Nov. 10

$NOSYMBOLS ; keeps listing short, lest...$INCLUDE (C:\MICRO\8051\RAISON\INC\c8051f410.inc) ; ...this line might produce huge list

; of symbol definitions (all ’51 registers)

$INCLUDE (C:\MICRO\8051\RAISON\INC\VECTORS320.INC) ; Tom’s vectors definition fileSTACKBOT EQU 80h ; put stack at start of scratch indirectly-addressable block (80h and up)

TIMER96K EQU 96h ; timer reload value for 9600 baud---assuming SiLabs, 24MHz div-by-12 (p.214); sys clock at full speed

SAY EQU 00h ; command says next characters (till ;) are speech codesEOM EQU ’;’ ; EOM for ascii modeLF EQU 0Ah ; linefeed

DISPLAY EQU P1KEYPAD EQU P2

START_OFFSET EQU 01h ; initial offset into speech table (may have to tinker, to get it right)

ORG 0hLJMP STARTUP

ORG 080h

STARTUP: MOV SP, #STACKBOT-1

acall USUAL_SETUPacall Init_Device

; next lines would use June 10 inits; acall IO_SETUP; acall TIMER_SETUP; acall SERIAL_SETUP

;* * * * * * * * * * * * * transmit code

REINIT: MOV R2, #START_OFFSET ; set up offset into table of characters

MORE: MOV A, R2 ; pick up offset into speech tableACALL GETMESSAGE ; Now start getting speech, a character at a timeCJNE A, #EOM, STILL_OK ; check for end of message (EOM is semicolon, in this program)

; now send data, after message: this is a single character, taken from controller keypad

mov A, P2 ; get keypad value, just to have something to sendacall SENDIT

; now a newlinemov A, #0Dh ; carriage returnacall SENDITmov A, #LF ; linefeed

Page 11: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 11

acall SENDIT

sjmp QUIT ; say it just once; SJMP REINIT ; ...or keep saying it, over and over

STILL_OK: ACALL SENDIT ; if not end, send to talkerMOV A, R2 ; pick up current offset valueINC A ; advance to next characterJZ OVERFLOW ; check for overflow of 8-bit offsetMOV R2, A ; ...if no overflow, update offset valueSJMP MORE

OVERFLOW: CLR P2.1 ; in case of overflow, light LED...QUIT: SJMP QUIT ; ...and hang up here, till reset

;* * * * sends a byte using UART0, when Talker isn’t busy

SENDIT:MOV SBUF0, A ; Put character in buffer (send it)--UART 1

LINGER: JNB TI0, LINGER ; wait here till told it’s been sentCLR TI0RET

; * * * THE SPEECH ITSELFORG 0480h

GETMESSAGE: MOVC A, @A+PCRET

db ’this is the 410 talking to a PC through its serial port. This is P2 value, as ascii:;’

;--- INITIALIZATIONS -----

USUAL_SETUP: anl PCA0MD, #NOT(040h) ; Disable the WDT.; Clear Watchdog Enable bit

; Configure the Oscillatormov OSCICN, #087h ; sysclk = 24.5 MHz

; Enable the Port I/O Crossbarmov XBR1, #40h ; Enable Crossbarret

;------------------------------------;- Generated Initialization File --;------------------------------------

; Peripheral specific initialization functions,; Called from the Init_Device labelTimer_Init:

mov TCON, #040hmov TMOD, #020hmov CKCON, #008hmov TH1, #0F6hret

UART_Init:mov SCON0, #010hret

; Initialization function for device,; Call Init_Device from your main programInit_Device:

lcall Timer_Initlcall UART_Initret

end

When you try this program, the last character displayed on the PC screen will be the character whose ASCIIcode you put on the microcontroller’s keypad input. 41h, forexample, will produce an upper-case “A.”

Page 12: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

12 Lab C5 SiLabs 5: Data Table; Serial Buses

23L.3.3.5 How GETMESSAGE Gets the Speech

The tiny subroutine GETMESSAGE uses a programming trick we have not used elsewhere in these labs.Here is the routine, reiterated:

; * * * THE SPEECH ITSELFORG 0480hGETMESSAGE: MOVC A, @A+PCRET

db ’this is the 410 talking to a PC through its serial port. This is P2 value, as ascii:;’

And the novel code is MOVC, in “GETMESSAGE: MOVC A, @A+PC.”

This line means ”move code,” and the source is an address formed by summing the PC (program counter)with A (accumulator). The destination is the accumulator, as you can see.

GETMESSAGE thus loads the accumulator, each time it is invoked, with the value stored atPC + A, wherePC is the value of the PC at this point. In programming jargon,A serves as anindex into a data table thatbegins at the valuePC.

The PC value used is the address of thenextinstruction (here, RET).9 So, to get the very first character in thespeech—the “t” in “this is the 410. . . ”—A must hold the value 1, so that PC + A forms the address where the“t” is stored. That is why A is loaded with the value “1” by the line

START OFFSET EQU 01h ; initial offset into speech table

This value, 1, is loaded into A in a two steps: first into R2, thence into A. And after use as the “index,” Athen serves as respository for the charaacter picked up fromthe table. This heavy use of the A register ischaracteristic of the 8051. Later processors, as we have said before, spread the labor among many registers;early processors including the 8051 made the Accumulator a central stage for a great many operations: theresults of all boolean and arithmetic operations, and the source and destination for off-chipmoves, called“MOVX.”

To make this program a littlemore fun—a little more like yourown—we suggest that you change the messagefrom that we have listed after “db. . . .” That may let you better appreciate the power that the serial port nowaffords you: to let the controller report its results to a full-scale computer. How you use this power we leaveto you, as usual. Some people may find it useful in a project, atthe end of this course.

23L.3.4 Reversing the Transmission: Controller Can Listento a PC

The serial port also allows a PC to tell a controller how to behave. The combination of this capacity withthe one demonstrated in the preceding section,§ 23L.3.3.3 on page 10, permits one to combine the familiarinterface of a full-scale computer with the nimbleness of a dedicated controller. Again, the program we offerin § 23L.3.4 provides only the simplest demonstration. It will be up to you to put this capacity to work, if youchoose to.

23L.3.4.1 Hardware: Display, again

We’ll now use a byte-wide display (using the LCD), wired to port P1, back in§ 23L.3.3.3 on page 10. Thisdisplay will let us see what the controller receives from thePC.

9This definition of the “present” PC value you may recall from the discussion of subroutine CALL, where it is this address that isstored on the stack, and from JUMP calculations, where the PCvalue used is the address twoafter the address of the JUMP instructionitself.

Page 13: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 13

23L.3.5 Code: Controller Listens to PC

This program uses the same initializations needed in§ 23L.3.3 on page 8, but the program loop is muchsimpler: the controller simply displays whatever character is sent to it by the PC—and “echoes” it back tothe PC (just to provide reassurance for the user). A useful version of this program would apply the receiveddata to a more interesting purpose, steering the controllerto this or that action.

The effective program loop,

SHOW_ONE: JNB RI0, $ ; await receipt of a byte from UARTCLR RI0 ; ...clear flag when it’s assertedMOV DISPLAY, SBUF0 ; get received dataSJMP SHOW_ONE ; go look for another character

. . . looks almost exactly like the loop of§ 23L.3.3 on page 8—except that it tests a flag that indicates acharacter has beenreceived(“RIO”) rather thantransmitted(“TIO”) as in the SENDIT loop of§ 23L.3.3 onpage 8, and in the Echo section of thisreceiveprogram.10

; serial_receive_silabs_hybrid_nov10.a51 receives a single byte from pc using UART; shows it on display (its ascii value); Also echoes character, sending it back to PC; Uses Wizard initializations

$NOSYMBOLS ; keeps listing short, lest...$INCLUDE (C:\MICRO\8051\RAISON\INC\c8051f410.inc) ; ...this line might produce huge list

; of symbol definitions (all ’51 registers)$INCLUDE (C:\MICRO\8051\RAISON\INC\VECTORS320.INC) ; Tom’s vectors definition fileSTACKBOT EQU 80h ; put stack at start of scratch indirectly-addressable block (80h and up)

DISPLAY EQU P1KEYPAD EQU P2

ORG 0hLJMP STARTUP

ORG 080h

STARTUP: MOV SP, #STACKBOT-1

acall USUAL_SETUPacall Init_Device

;----receive code

AWAIT: jnb RI0, $ ; await receipt of a byte from UARTclr RI0 ; ...clear flag when it’s assertedmov DISPLAY, SBUF0 ; get received data

;* * * * send code: echoes value received, sending it back to PC

SENDIT: MOV A, SBUF0 ; looks silly, but to produce a send, need to write to sbuf0; hence this silly-looking move from and to SBUF0

MOV SBUF0, A ; Put character in buffer (send it)--UART 0LOITER: JNB TI0, LOITER ; wait here till told it’s been sent

CLR TI0sjmp AWAIT ; show each character that comes in, forever

;--- INITIALIZATIONS -----

USUAL_SETUP: anl PCA0MD, #NOT(040h) ; Disable the WDT.; Clear Watchdog Enable bit

setb P0.5 ; set RX pin high, since it’s an input (just playing it safe)ret;------------------------------------;- Generated Initialization File --;------------------------------------

10Incidentally, in case you find yourself watching the register SBUF0, that register is the same register used for transmission. Thisfact can make its behavior puzzling to watch.

Page 14: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

14 Lab C5 SiLabs 5: Data Table; Serial Buses

; Peripheral specific initialization functions,; Called from the Init_Device labelTimer_Init:

mov TCON, #040h ; enable Timer1 (controls baud rate)mov TMOD, #020h ; Timer1 8-bit auto-reload

mov TH1, #096h ; reload value for 9600 baudret

UART_Init:mov SCON0, #010h ; set receive enable, 8-bitret

Port_IO_Init:; P0.0 - Unassigned, Open-Drain, Digital; P0.1 - Unassigned, Open-Drain, Digital; P0.2 - Unassigned, Open-Drain, Digital; P0.3 - Unassigned, Open-Drain, Digital; P0.4 - TX0 (UART0), Push-Pull, Digital; P0.5 - RX0 (UART0), Open-Drain, Digital; P0.6 - Unassigned, Open-Drain, Digital; P0.7 - Unassigned, Open-Drain, Digital

mov P0MDOUT, #010h ; make TX push-pullmov XBR0, #001h ; route UART signals to port pins (P0.4, P0.5)mov XBR1, #040h ; enable I/O crossbarret

Oscillator_Init:mov OSCICN, #087h ; full speedret

; Initialization function for device,; Call Init_Device from your main programInit_Device:

lcall Timer_Initlcall UART_Initlcall Port_IO_Initlcall Oscillator_Initret

end

In order to make this sort of program really useful, rather than just a demonstration, the controller programprobably should include aparserthat could use the transmitted text to carry out this or that act. For example,the parser could check whether the incoming character is “C”or “D.” If C, then store the next byte for lateruse as thecommandword for the Digipot. IfD, then store the byte that follows as thedata word to betransmitted to the Digipot. Then transmit both, and go back to checking the UART input. Thus one could,for example, use a PC to control the Digipot setting, throughthe microcontroller. Again, we have providedonly the most skeletal code. Flesh it out, if its use interests you.

23L.4 Serial Bus 2: SPI

SPI is not quite a true “bus” because it requires, along with the “bused” orsharedlines one line dedicated toeach particular peripheral: a privateenable. This is equivalent to providing an intercom buzzer line to eachuser in an office: the buzzer means “it’s for you.” The user whohears the buzzer picks up the phone on theshared line and uses it, listening, talking, or (most often)doing both. The bus is described in more detail inClassnotesµ 5.

23L.4.1 Hardware: SPI Controls Digipot

In the present example, SPI goes to a single peripheral, a digitally-controlled potentiometer (“digipot”). Thepot’s slider can be placed anywhere along the fixed resistance (which in this case has the value 100kΩ).

Page 15: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 15

23L.4.1.1 Test Setup

The pinout of the Digipot is shown in class notesµ5, and we repeat it here.

Figure 9: SPI digipot wiring

The SPI pin use on the ’410 is fixed in hardware, so you will needto disconnect anything that conflicts. Seethe history of pin use in fig. 2 on page 2.

First Test: Just Variable Resistance Initially, to test whether the digipot is working, let’s just use a DVMto measureresistancebetween the slider and either end of the digipot. The programof § 23L.4.1.4 on page 17should allow the keypad value to set a resistance anywhere between 100k and a value close to zero.11 Watchthe SPI signals on a scope: NSS (P0.3), the chip-enable; SCK (P0.0); MOSI (PO.2).12 See if you can makeout the values of the bytes sent. The fixedcommandbyte, 11h, may help you to get your time-bearings, asyou watch the effect of changing thedatabyte.

23L.4.1.2 Register Settings: Port I/O

The Configuration Wizard sets things up thus:

11The residual resistance of the analog switch that routes thesignals always remains; the minimum resistance is not zero ohms, butup to 100 ohms (100Ω, max.; 52Ω, typical).

12MOSI (“Master Out Slave In”) is labelled “SI” in fig. 9.

Page 16: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

16 Lab C5 SiLabs 5: Data Table; Serial Buses

Figure 10: SPI configuration, using Wizard

23L.4.1.3 Register Settings: SPI Choices

SPI obliges us to make several choices. Fortunately, the Configuration Wizard helps a lot. Here are thechoices we made:

Figure 11: SPI configuration choices

These selections produce the code that is appended to the program listed in§ 23L.4.1.4 on the facing page.

Page 17: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

Lab C5 SiLabs 5: Data Table; Serial Buses 17

23L.4.1.4 Digipot Code

. . . and the code. Keypad value (one byte) determines digipotposition.

; SPI_digipot_silabs_nov11.a51 try SiLabs SPI to control digipot (MCP411000); using Wizard initializations

$NOSYMBOLS ; keeps listing short, lest...$INCLUDE (C:\MICRO\8051\RAISON\INC\c8051f410.inc) ; ...this line might produce huge list; of symbol definitions (all ’51 registers)$INCLUDE (C:\MICRO\8051\RAISON\INC\VECTORS320.INC) ; Tom’s vectors definition fileSTACKBOT EQU 80h ; put stack at start of scratch indirectly-addressable block (80h and up)

; P0.2 - SPI SCK (digital output, push-pull); P0.3 - SPI MISO (digital input, open-drain); P0.6 - SPI MOSI (digital output, push-pull); P0.7 - SPI NSS (digital output, push-pull)

ORG 0hLJMP STARTUP

ORG 040h

STARTUP: MOV SP, #STACKBOT-1

acall USUAL_SETUPacall Init_Device

SEND_VALUE:clr NSSMD0 ; assert CS* of peripheral (NSSMD0 bit)mov SPI0DAT, #11h ; send command bytejnb SPIF, $ ; hang here till data-sent flag is asserted

clr SPIF ; clear data-sent flagmov A, P2 ; get key valuemov P1, P2 ; show keypad value

mov SPI0DAT, P2 ; send keypad value as SPI data bytejnb SPIF, $ ; hang here till data-sent flag is assertedclr SPIF ; clear data-sent flagsetb NSSMD0 ; disassert CS* of peripheralsjmp SEND_VALUE

;------- INITIALIZATIONS

USUAL_SETUP: anl PCA0MD, #NOT(040h) ; Disable the WDT.; Clear Watchdog Enable bit

; set MISO bit high, for later use as inputsetb P0.3ret

;------------------------------------;- Generated Initialization File --;------------------------------------

; Peripheral specific initialization functions,; Called from the Init_Device labelSPI_Init:

mov SPI0CFG, #040h ; 4 wire master mode, NSS serves as CS*mov SPI0CN, #009h ; fastest clock rate, for SPIret

Port_IO_Init:; P0.0 - Skipped, Open-Drain, Digital; P0.1 - Skipped, Open-Drain, Digital; P0.2 - SCK (SPI0), Push-Pull, Digital; P0.3 - MISO (SPI0), Open-Drain, Digital; P0.4 - Skipped, Open-Drain, Digital; P0.5 - Skipped, Open-Drain, Digital; P0.6 - MOSI (SPI0), Push-Pull, Digital; P0.7 - NSS (SPI0), Push-Pull, Digital

; P1.0 - Unassigned, Open-Drain, Digital; P1.1 - Unassigned, Open-Drain, Digital; P1.2 - Skipped, Open-Drain, Digital; P1.3 - Unassigned, Open-Drain, Digital; P1.4 - Unassigned, Open-Drain, Digital; P1.5 - Unassigned, Open-Drain, Digital; P1.6 - Unassigned, Open-Drain, Digital

Page 18: Lab C5 SiLabs 5: Data Table; Serial Busessites.fas.harvard.edu/~phys123/silabs_labs/lab_controller5_nov11.pdf · GET_SAMPLE: mov ADC0CN, #84h ; low on AD0BUSY (to permit rising edge)

18 Lab C5 SiLabs 5: Data Table; Serial Buses

; P1.7 - Unassigned, Open-Drain, Digital

; P2.0 - Unassigned, Open-Drain, Digital; P2.1 - Unassigned, Open-Drain, Digital; P2.2 - Unassigned, Open-Drain, Digital; P2.3 - Unassigned, Open-Drain, Digital; P2.4 - Unassigned, Open-Drain, Digital; P2.5 - Unassigned, Open-Drain, Digital; P2.6 - Unassigned, Open-Drain, Digital; P2.7 - Unassigned, Open-Drain, Digital

mov P0MDOUT, #0C4hmov P0SKIP, #033hmov P1SKIP, #004hmov XBR0, #002hmov XBR1, #040hret

Oscillator_Init:mov OSCICN, #87h ; full speed: 24.5MHzret

; Initialization function for device,; Call Init_Device from your main programInit_Device:

lcall SPI_Initlcall Port_IO_Initlcall Oscillator_Initret

end

23L.4.2 Apply the Digipot

Once you are satisfied that the digipot is working, try makingits behavior a bit more useful: use it to set thegain of an inverting amplifier. The circuit appears in class notesµ 5, and is repeated here:

Figure 12: Digipot applied to permit computer control of amplifier gain

As the classnotes suggest, adding an amplitude-averaging feature (just a diode and anRC) could allow oneto make an automatic gain control. The ’410 permits doing this also in digital form, by use of itswindowfunction: the ’410 can watch the ADC input and alert the program if the signal lies outside some permittedrange. But the analog averager seems to allow an implementation with simpler software. Do what you findappealing.

lab controller5nov11.tex; November 20, 2011