Speed Control of a Stepper Motor

Speed Control of a Stepper Motor

UNIT3

THE 8051-REAL WORLD INTERFACING

8031/51 INTERFACING TO

EXTERNAL MEMORY

The number of bits that asemiconductor memory chip can storeis called chip capacity

?It can be in units of Kbits (kilobits), Mbits(megabits), and so on

?This must be distinguished from thestorage capacity of computer systems

?While the memory capacity of a memoryIC chip is always given bits, the memorycapacity of a computer system is given inbytes

?16M memory chip – 16 megabits

?A computer comes with 16M memory – 16megabytes

Memory chips are organized into anumber of locations within the IC

?Each location can hold 1 bit, 4 bits, 8 bits,or even 16 bits, depending on how it isdesigned internally

?The number of locations within a memory ICdepends on the address pins

?The number of bits that each location can holdis always equal to the number of data pins

?To summarize

?A memory chip contain 2x location, where xis the number of address pins

?Each location contains y bits, where y isthe number of data pins on the chip

?The entire chip will contain 2x × y bits

One of the most importantcharacteristics of a memory chip is thespeed at which its data can beaccessed

?To access the data, the address ispresented to the address pins, the READpin is activated, and after a certain amountof time has elapsed, the data shows up atthe data pins

?The shorter this elapsed time, the better,and consequently, the more expensive thememory chip

?The speed of the memory chip iscommonly referred to as its access time

ROM is a type of memory that does notlose its contents when the power isturned off

?ROM is also called nonvolatile memory

?There are different types of read-onlymemory

?PROM

?EPROM

?EEPROM

?Flash EPROM

?Mask ROM

  • The CPU provides the address of thedata desired, but it is the job of the

decoding circuitry to locate the selectedmemory block

?Memory chips have one or more pins calledCS (chip select), which must be activatedfor the memory’s contents to be accessed

?Sometimes the chip select is also referredto as chip enable (CE)

In connecting a memory chip to theCPU, note the following points

?The data bus of the CPU is connecteddirectly to the data pins of the memory chip

?Control signals RD (read) and WR (memorywrite) from the CPU are connected to theOE (output enable) and WE (write enable)pins of the memory chip

?In the case of the address buses, while thelower bits of the address from the CPU godirectly to the memory chip address pins,the upper ones are used to activate the CSpin of the memory chip

Normally memories are divided intoblocks and the output of the decoderselects a given memory block

?Using simple logic gates

?Using the 74LS138

?Using programmable logics

The simplest way of decoding circuitryis the use of NAND or other gates

?The fact that the output of a NAND gate isactive low, and that the CS pin is also

active low makes them a perfect match

This is one of the most widely usedaddress decoders

?The 3 inputs A, B, and C generate 8 activelowoutputs Y0 – Y7

?Each Y output is connected to CS of a memorychip, allowing control of 8 memory blocks by asingle 74LS138

?In the 74LS138, where A, B, and C selectwhich output is activated, there are threeadditional inputs, G2A, G2B, and G1

?G2A and G2B are both active low, and G1 isactive high

?If any one of the inputs G1, G2A, or G2B is notconnected to an address signal, they must beactivated permanently either by Vcc or ground,depending on the activation level

Other widely used decoders areprogrammable logic chips such as PALand GAL chips

?One disadvantage of these chips is thatone must have access to a PAL/GAL software and burner, whereas the 74LS138needs neither of these

?The advantage of these chips is that theyare much more versatile since they can beprogrammed for any combination ofaddress ranges

The 8031 chip is a ROMless version ofthe 8051

?It is exactly like any member of the 8051family as far as executing the instructionsand features are concerned, but it has noon-chip ROM

?To make the 8031 execute 8051 code, itmust be connected to external ROMmemory containing the program code

?8031 is ideal for many systems wherethe on-chip ROM of 8051 is notsufficient, since it allows the programsize to be as large as 64K bytesFor 8751/89C51/DS5000-based system,we connected the EA pin to Vcc toindicate that the program code isstored in the microcontroller’s on-chipROM

?To indicate that the program code is storedin external ROM, this pin must beconnected to GND

Since the PC (program counter) of the8031/51 is 16-bit, it is capable ofaccessing up to 64K bytes of programcode

?In the 8031/51, port 0 and port 2 providethe 16-bit address to access external

memory

?P0 provides the lower 8 bit address A0 – A7, andP2 provides the upper 8 bit address A8 – A15?P0 is also used to provide the 8-bit data busD0 – D7

?P0.0 – P0.7 are used for both the addressand data paths

?address/data multiplexing

ALE (address latch enable) pin is anoutput pin for 8031/51

?ALE = 0, P0 is used for data path

?ALE = 1, P0 is used for address path

74LS373 D Latch

?To extract theaddress from the P0pins we connect P0to a 74LS373 anduse the ALE pin tolatch the addressNormally ALE = 0, and P0 is used as adata bus, sending data out or bringingdata in

?Whenever the 8031/51 wants to use P0as an address bus, it puts theaddresses A0 – A7 on the P0 pins andactivates ALE = 1

PSEN (program store enable) signal isan output signal for the 8031/51microcontroller and must be connectedto the OE pin of a ROM containing theprogram code

?It is important to emphasize the role ofEA and PSEN when connecting the8031/51 to external ROM

?When the EA pin is connected to GND, the8031/51 fetches opcode from external ROMby using PSEN

The connection of the PSEN pin to theOE pin of ROM

?In systems based on the 8751/89C51/DS5000 where EA is connected to Vcc,these chips do not activate the PSEN pin

?This indicates that the on-chip ROM containsprogram codeConnection to External Program ROM

Show the design of an 8031-based system with 8K bytes of programROM and 8K bytes of data ROM.

speed control of a stepper motor

The requirement is to use a microcontroller to drive a stepper motor in bothforward and reverse directions of shaft rotation and to implement a two-speed(fast and slow) arrangement. Switches are to be used to produce the two speeds and effect a reversal of shaft rotation. A possible arrangement is shown in theblock diagram, which uses a P89C664 microcontroller.

Generally a stepper motor has four sets of coils. One end of each coil may beconnected together and then connected to DCsu pply. The remaining four endsmay be driven through transistors either separately or in integrated circuit form.A four-bit code sequence continuously applied to the drive circuit from the microcontroller port causes the motor shaft to rotate in angular steps. Cheap(e.g. £12) stepper motors have step angles of 7.5 degrees whereas more expensive(e.g. £45) motors have step angles of 1.8 degrees. Step resolution andturning force (i.e. torque) may be improved by using a step-down gearbox.The stepping code sequence may be obtained from the motor manufactureror distributor. The program in this example uses a common four-step sequenceof A 9 5 6 that, if sent continuously, would cause the motor shaft to rotate.Figure 7.8 shows the driving signals from the port pins.

Sending the code in reverse 6 5 9 A causes the motor shaft rotation to reverse.The rotation speed depends on the delay each step is held for. Details of asuitable drive circuit are shown in Figure 7.9.The transistors (TR) must be chosen to easily handle the coil current. If thevalue of coil current is not given by the motor supplier then it is possible tomeasure the coil resistance with a multimeter (a typical value would be15 ohms). Dividing Vcc by the coil resistance gives a good estimate of the coilcurrent; double this value and select a transistor that has this current as itsmaximum-rated value. In this way the transistors will not run hot. The valueof resistor R is chosen to control the input current of the transistor. Thetransistor current ratio is given in component catalogues as hFE, which is deviceforward current gain in common-emitter mode. In this circuit hFE is basicallythe coil current divided by the input current to the transistor. Thus the transistorinput current is:input current=(coil current)=hFE

The 74LS04 logic gate comprises eight inverter buffer circuits. Using two inseries will restore the voltage level at the input to resistor R to the same value as the output from the relevant port pin. The voltage from the 74LS04 logic gateto turn on the transistor is 5 V. The voltage input to the transistor on the otherside of the resistor is approximately 0.7 V; so the voltage difference across theresistor R is (5 _ 0:7)V ¼ 4:3V

The resistor value is given by:

R ¼ ðvoltage across the resistorÞ/input current

R ¼ ð4:3 _ hFEÞ=ðcoil currentÞ

A logic 1 (5 V) from the microcontroller port pin is applied through the twoinverter gates of the 74LS04 to the resistor R. This sets up 0.7V to the transistorbase that causes the transistor to behave as an electronic switch, turning thedevice on and allowing current to flow through the coil. The logic gates act as abuffer ensuring that the microcontroller port pin is not current loaded. Thediodes D reduce the large induced voltages caused when the current is suddenlyswitched on or off.

TIMER VALUES FOR ROTATION SPEED

Consider a 7.5 degree stepper motor having a step sequence of A 9 5 6. Assumeit is desired to make the shaft rotate at 60 revolutions per minute or onerevolution every second.360/7:5 ¼ 48 steps in a revolution and the program action will basically bestep-delay; so this means 48 delays¼one revolution.

48 delays ¼ 1 s

1 delay ¼ ð1=48Þ s ¼ 20:833 ms

8031/51 INTERFACINGWITH THE 8255

(a) Find the I/O port addresses assigned to ports A, B, C, and thecontrol register.

(b) Program the 8255 for ports A, B, and C to be output ports.

(c) Write a program to send 55H and AAH to all ports continuously.

Solution

(a) The base address for the 8255 is as follows:

(b) The control byte (word) for all ports as output is 80H as seen in

Example 15-1.

For Figure above.

(a) Find the I/O port addresses assigned to ports A, B, C, and thecontrol register.

(b) Find the control byte for PA = in, PB = out, PC = out.

(c) Write a program to get data from PA and send it to both B and C.

Solution:

(a) Assuming all the unused bits are 0s, the base port address for8255 is 1000H. Therefore we have:

1000H PA

1001H PB

1002H PC

1003H Control register

(b) The control word is 10010000, or 90H.

LCD AND KEYBOARDINTERFACING

LCD is finding widespread usereplacing LEDs

? The declining prices of LCD

? The ability to display numbers, characters,and graphics

? Incorporation of a refreshing controllerinto the LCD, thereby relieving the CPU ofthe task of refreshing the LCD

? Ease of programming for characters andgraphics

?Keyboards are organized in a matrix ofrows and columns

? The CPU accesses both rows and columnsthrough ports

? Therefore, with two 8-bit ports, an 8 x 8 matrixof keys can be connected to a microprocessor

? When a key is pressed, a row and acolumn make a contact

? Otherwise, there is no connection betweenrows and columns

? In IBM PC keyboards, a singlemicrocontroller takes care of hardwareand software interfacing

A 4x4 matrix connected to two ports

? The rows are connected to an output portand the columns are connected to aninput port

It is the function of the microcontrollerto scan the keyboard continuously todetect and identify the key pressed

? To detect a pressed key, themicrocontroller grounds all rows byproviding 0 to the output latch, then itreads the columns

? If the data read from columns is D3 – D0 =1111, no key has been pressed and theprocess continues till key press is detected

? If one of the column bits has a zero, thismeans that a key press has occurred

? For example, if D3 – D0 = 1101, this means thata key in the D1 column has been pressed

? After detecting a key press, microcontrollerStarting with the top row, themicrocontroller grounds it by providinga low to row D0 only

? It reads the columns, if the data read is all1s, no key in that row is activated and the process is moved to the next row

? It grounds the next row, reads thecolumns, and checks for any zero

? This process continues until the row isidentified

? After identification of the row in whichthe key has been pressed

? Find out which column the pressed keybelongs to

Program 12-4 for detection andidentification of key activation goes through the following stages:

1. To make sure that the preceding key hasbeen released, 0s are output to all rowsat once, and the columns are read andchecked repeatedly until all the columnsare high

? When all columns are found to be high, theprogram waits for a short amount of timebefore it goes to the next stage of waiting fora key to be pressedTo see if any key is pressed, the columnsare scanned over and over in an infiniteloop until one of them has a 0 on it

? Remember that the output latches connectedto rows still have their initial zeros (provided

in stage 1), making them grounded

?After the key press detection, it waits 20 msfor the bounce and then scans the columns

again

(a) it ensures that the first key pressdetection was not an erroneous one due aspike noise

(b) the key press. If after the 20-ms delay thekey is still pressed, it goes back into theloop to detect a real key press

To detect which row key press belongs to,it grounds one row at a time, reading thecolumns each time

? If it finds that all columns are high, this meansthat the key press cannot belong to that row

– Therefore, it grounds the next row andcontinues until it finds the row the keypress belongs to

? Upon finding the row that the key pressbelongs to, it sets up the starting address forthe look-up table holding the scan codes (orASCII) for that row

4. To identify the key press, it rotates thecolumn bits, one bit at a time, into thecarry flag and checks to see if it is low

? Upon finding the zero, it pulls out the ASCIIcode for that key from the look-up table

? otherwise, it increments the pointer to point tothe next

INTERFACING ADC

ADCs (analog-to-digital converters) are among the most widely used devices for data acquisition

?A physical quantity, like temperature,pressure, humidity, and velocity, etc., isconverted to electrical (voltage, current)signals using a device called a transducer,or sensor

?We need an analog-to-digital converterto translate the analog signals to digitalnumbers, so microcontroller can readthem

ADC804 IC is an analog-to-digitalconverter

?It works with +5 volts and has a resolutionof 8 bits Conversion time is another major factor injudging an ADC

?Conversion time is defined as the time it takesthe ADC to convert the analog input to a digital(binary) number

?In ADC804 conversion time varies depending onthe clocking signals applied to CLK R and CLK INpins, but it cannot be faster than 110 μs

CLK IN and CLK R CLK IN is an input pin connected to anexternal clock source

?To use the internal clock generator(also called self-clocking), CLK IN andCLK R pins are connected to a capacitorand a resistor, and the clock frequencyis determined by

?Typical values are R = 10K ohms and C =150 pF

?We get f = 606 kHz and the conversion timeis 110 μs

Vref/2

?It is used for the reference voltage

?If this pin is open (not connected), the analoginput voltage is in the range of 0 to 5 volts (thesame as the Vcc pin)

?If the analog input range needs to be 0 to 4volts, Vref/2 is connected to 2 volts

D0-D7

?The digital data output pins

?These are tri-state buffered

?The converted data is accessed only when CS =0 and RD is forced low

?To calculate the output voltage, use thefollowing formula

?Dout = digital data output (in decimal),

?Vin = analog voltage, and

?step size (resolution) is the smallest change

Analog ground and digital ground

?Analog ground is connected to the groundof the analog Vin

?Digital ground is connected to the groundof the Vcc pin

?To isolate the analog Vin signal fromtransient voltages caused by digitalswitching of the output D0 – D7

?This contributes to the accuracy of thedigital data output

The following steps must be followedfor data conversion by the ADC804 chip

?Make CS = 0 and send a low-to-high pulseto pin WR to start conversion

?Keep monitoring the INTR pin

?If INTR is low, the conversion is finished

?If the INTR is high, keep polling until it goes low

?After the INTR has become low, we makeCS = 0 and send a high-to-low pulse to theRD pin to get the data out of the ADC804

the following is a program for the ADCconnected to 8255 as show in fig 15-11

A unique feature 8255 of port C

?The bits can be controlled individually?BSR mode allows one to set to high or

low any of the PC0 to PC7, see figure.

INTERFACING ADC THROUGH 8255

INTERFACING “thermistor”

A thermistor responds to temperaturechange by changing resistance, but itsresponse is not linear

?The complexity associated with writingsoftware for such nonlinear devices hasled many manufacturers to market thelinear temperature sensor

The sensors of the LM34/LM35 seriesare precision integrated-circuittemperature sensors whose outputvoltage is linearly proportional to theFahrenheit/Celsius temperature

?The LM34/LM35 requires no externalcalibration since it is inherently calibrated?It outputs 10 mV for each degree ofFahrenheit/Celsius temperatureSignal conditioning is a widely usedterm in the world of data acquisition

?It is the conversion of the signals (voltage,current, charge, capacitance, andresistance) produced by transducers tovoltage, which is sent to the input of an Ato-

D converterSignal conditioning can be a current-tovoltageconversion or a signalamplification

?The thermistor changes resistance withtemperature, while the change ofresistance must be translated into voltagein order to be of any use to an ADC

Example:

Look at the case of connecting an LM35 to an ADC804. Since theADC804 has 8-bit resolution with a maximum of 256 steps and theLM35 (or LM34) produces 10 mV for every degree of temperaturechange, we can condition Vin of the ADC804 to produce a Vout of2560 mV full-scale output. Therefore, in order to produce the fullscaleVout of 2.56 V for the ADC804, We need to set Vref/2 = 1.28.This makes Vout of the ADC804 correspond directly to thetemperature as monitored by the LM35.