Home | Forum | DAQ Fundamentals | DAQ Hardware | DAQ Software Input Devices | Data Loggers + Recorders | Books | Links + Resources |
AMAZON multi-meters discounts AMAZON oscilloscope discounts A detailed study shows how a typical support device may be programmed to demonstrate its facilities. The remainder of the SECTION comprises a collection of programming problems, based on the techniques described in earlier SECTIONs, but also introducing some new programming methods. Programming a support device When designing and building a microelectronic system, it may sometimes be necessary to incorporate one or more complex ICs into the system. The system may need a parallel input port, or a serial output port, or it may need an analogue-to-digital converter. Each of these devices has its own special features and it is unlikely to work unless it is set up in exactly the right way. There is not enough space in this guide to describe how to set up and use the many types of I/O ports and other microprocessor support devices. The details are available in the manufacturers' data sheets, which may be obtained from suppliers of components, or often downloaded from the World Wide Web. This case study describes the initial stages in the design and programming of a system based on a real time clock IC. There are many different real time clock ICs available. The type chosen is the Hitachi 146818. This is a 24-pin IC, intended to be part of a system based one of the 6800 family of microprocessors. It is also suitable for use with many other microprocessors and microcontrollers. Having chosen this IC, it is investigated by following the stages described below. Data sheet Data sheets usually begin with a list of the special features of the device. The data sheet of the 146818 explains that this is a time-of-day clock and calendar. It counts seconds, minutes, and hours. It registers days of the week, the date, the month and the year. It can deal with months of variable length and with leap years. It can also be used to generate interrupts at regular timed intervals, and can be programmed to produce a square wave of a number of different frequencies. The 146818 is a real time clock/calendar that is intended for use in a microprocessor system. It is driven by a crystal oscillator. The output from the oscillator goes to a 20-stage divider to produce a frequency suitable for driving the timing circuits. As might be expected, the logic of the IC is complicated and we shall not describe it here. The data sheet includes tables of Electrical Characteristics. The most important of these is the supply voltage, which in this case is 5V ± 0.5 V. It is essential to check on this before beginning a project because it may sometimes happen that the operating voltage is not compatible with that of other devices in the system. The Stamp system includes a 5 V regulator, so this can be used to power the clock. It is intended to operate it at 32.768 kHz, and the table in the data sheet shows that the current required is only 500 µA, which is well within the resources of the system. A data sheet provides a vast amount of detailed information. Coming direct from the manufacturer it is nearly always complete and accurate. However, it is not necessary to read every page of the data sheet. The information that is needed for a given project is often scattered in different parts of the sheet. Although the presentation of information is systematic, it is not necessarily in the order in which it is needed for developing a particular project. The experienced data sheet reader soon learns to skim quickly through the data sheet to ascertain what topics it covers. Then the reader moves rapidly from one part of the sheet to another, picking up facts here and there, and gradually summarizing the more relevant ones into two or three pages of notes. The following paragraphs outline the information which was gleaned from the data sheet and is needed for programming the 146818 as a clock. Pinout The pinout (FIG. 1) has been marked to show which are inputs and which are outputs. This is done because input pins must nearly always be connected to something. Output pins can usually be left unconnected, at least during the early stages of development. There are eight address input pins, which are also data output pins. These and four of the control pins are to be connected to a Stamp2, which will be used to command the IC. The data sheet explains that during read and write cycles, the address is placed on the bus by the controller. At a later stage in the cycle, data for writing is placed on the bus by the controller, or data for reading is placed on the bus by the clock. The data sheet has diagrams to show what other connections need to be made to the IC. The most important is the external clock circuitry, consisting of a crystal, and a few resistors and capacitors (FIG. 2). There is a choice of frequencies. In this project we use a 32.768 kHz crystal.
Address map There are 64 bytes of RAM on the chip. The first 14 bytes are reserved as registers and the remaining 50 bytes are available for any purpose that the user requires. The functions of the first 14 bytes are: 0 Seconds 1 Seconds alarm 2 Minutes 3 Minutes alarm 4 Hours 5 Hours alarm 6 Day of the week 7 Day of the month 8 Month 9 Year 10-13 Registers A to D The first 10 registers are loaded with the current times, day, and dates. When the clock runs, a series of divider/counters update the registers as time passes. At any future instant, the registers are read to provide data on time and date. Registers A to D hold flags to indicate various states of the clock. Some of these flags may be set to control the operation of the clock. For example, it may be set to run in the 12-hour mode or the 24-hour mode. Data sheets of computer chips usually include a number of timing diagrams. Exact timing is important if the clock is to operate at maximum speed. The diagrams show the minimum times taken for addresses and data to settle on the bus, and the minimum response time of the clock. They show how long data and addresses must be left on the bus to ensure that they are loaded by the clock chip. In general, an IC will work just as well when it is taken through its stages of operation at a slow pace. This project used a relatively slow controller, programmable in BASIC, so there was no need to aim for exact timings. The most important point is the order in which control, address and data signals are sent to the clock. FIG. 3 shows a write cycle. The levels on the DS, WR and AS pins have no effect until CE goes low, enabling the chip. The address must now be on the bus and is latched into the clock when AS goes low. Making WR low (= read) with DS high causes the clock to put data on the bus. At the end of the cycle the lines return to their original state with CE high and the rest low.
The next step is to write a program to put a sequence of logic levels on the four control pins: 'programming the rtc - writing loc var byte 'declare variables value var byte loc = 2 'their values value = 12 dirc = 15 'as outputs to control outc = 1 'disable chip 'write routine outc = 13 'DS and WR high outc = 15 'AS high outc = 14 'enable chip dirl = 255 'as outputs to bus outl = loc 'address on bus outc = 12 'AS low outc = 8 'WR low outl = value 'data on bus outc = 12 'WR high outc = 1 'disable chip Work through this program to see how the sequence of outc instructions corresponds to the levels shown in FIG. 2 (see box). The values of location and value are edited into the program before the it is run. It would be an improvement to write a proper routine for inputting this data. The program loads the selected location with the given value. The data can be read back by using this program, which is based on the read routine (FIG. 4): 'programming the rtc - reading loc var byte 'declare variables value var byte loc = 2 'their values dirc = 15 'as outputs to control outc = 1 'disable chip 'read routine outc = 13 'DS and WR high outc = 15 'AS high outc = 14 'enable chip =========== Stamp I/0 The Stamp has 16 I/O pins (P0 to P15), divided into a high byte and a low byte. The low byte (P0 to P7) is accessed by using the label l. To set the data direction registers of the low byte, we use dirl. To output a value we use outl and to input a value we use inl. The lower nybble of the upper byte (P8 to P11) is labeled c. To access and use these pins as a block we use dirc, outc (and inc, but not in this program). The pins of the control nybble are: The bottom row shows the value to allocate to the nybble when AS is low and the other pins are high. When programming in decimal, this is expressed as: outc = 13. ===========
dirl = 255 'as outputs to bus outl = loc 'address on bus outc = 12 'AS low outc = 4 'DS low dirl = 0 'as inputs to read data value = inl 'reading data from bus outc = 12 'WR high outc = 1 'disable chip debug dec value 'displays value in register. This is similar to the write program, but uses inl to load the data from the bus. The value read from the register is displayed on the computer screen. Configuring the clock Before the IC can be used as a clock, the values in registers A and B must be set appropriately. In Register A, UIP is a flag (update in progress), which is read-only: DV2 to DV1 set the length of the divider chain. A table in the data sheet shows that these bits should be set to 010 for a 32.768 kHz crystal. To start with, the periodic interrupt rate and square wave output are not being used, so the RS bits are set to 0000. The write program is run with location = 10 and value = 32 (working in decimal for convenience). Register B holds flags for functions which are of no concern at this stage, so the write program is run again with location = 11 and value = 0. Timing The clock is now ready for investigation, by writing values into the various locations and reading them back. To check that the clock is operating, read the seconds register (location = 0) at frequent intervals and check that it is incrementing. Similarly, the value in location 2 is found to increment once every minute. Other registers are tested very quickly by setting up the time and date as 11.59 pm on Sunday 31 December 2005, for example, and reading the data back again a few minutes later. Square wave output The IC has several other functions, all described in the data sheet, and we look at one of these as an example of how they can be investigated. The square wave output (pin 23) can be configured to provide output at one of a number of frequencies by setting the RS bits in Register A. For example, to obtain a 256 Hz wave, R3 is set to 1 and the other RS bits are 0. If we also need 1 in DV1 as explained above, the bits in Register A are 00101000. The values for the write program are location = 10 and value = 40. In addition, the SQWE bit (square wave enable) must be set in Register B, all other bits being 0. The SQWE bit is B3. Using the write program, make location = 11 and value = 8. When the settings are complete, a frequency meter applied to pin 23 shows a frequency of 256 Hz. There are several other functions in the 146818, which can all be investigated by using methods similar to those described above. It is left to the reader to work out how to implement and demonstrate these functions. ========= The hardware A test bed for the IC is constructed on stripboard. It is connected to the Stamp prototyping board by two ribbon cables, one of 8 lines for the address/data bus and one of four lines for the control. There are also two power lines, a ground (0 V) line and the regulated 5 V supply from the Stamp. A 100 µF electrolytic capacitor is connected across the supply lines where they enter the board, to decouple the supply. A clock and pull-down circuits on pins 18 and 22 are installed as shown in the diagrams in the data sheet (see also FIG. 2). Pin 20 is wired to the positive supply but, as the clock output pin (pin 21) is not being used, pin 20 could equally well be connected to ground. =========== =========== EXERCISE 1 -- Investigating a support IC Investigate the functions of an IC, following the same procedure as described above for the real time clock. You need: IC: This could be: Real time clock 74HC164 SIPO 74HC165 PISO 74LS244 octal tristate buffer 74LS373 octal tristate latch 74LS574 octal D-type flip-flop 6116 or similar SRAM RS232 output and input ICs Programmable parallel interface (8255 PPI, 6522 VIA, M68230 PIT or the Z80 PIO) Programmable serial interface (8250 UART, 16550 UART, M68661 DUART, M6850 ACIA, Z80 SIO) Analogue-to-digital converter (CA3304E, ADC0804), but see Topic XX. Digital to analogue converter Or any other processor support IC of interest. Data sheet. Power supply (regulated). This may be provided by an on-board regulator or taken from an external PSU. Microcontroller Test bed. Depending on the system, you could build this on a breadboard, or on stripboard. Your prototyping system may have one already. Study the data sheet and design and set up a suitable test bed. Investigate the functions of the IC by writing suitable programs. Suggestions for investigations include: Real time clock: Write a clock-calendar program to display date and time. Investigate the alarm function. Install a solid-state siren on the test bed and program the clock to sound it at a given time. Investigate the alarm interrupt function. Use it in a program that, say, flashes a red LED continuously but, when interrupted, flashes a green LED five times before going back to resume flashing the red LED. Set the interrupts to occur every minute. Investigate the periodic interrupt function. 74HC164, 74HC165, 74LS244, 74LS373, 74LS574 programmable interface ICs: Investigate the action of these ICs, using them as input or output ports for the processor. Connect them to input data from one or two sensors (these might be simple switches as in Fig. 2.6), and to output data to one or more actuators. The system could include address decoders to enable the port IC. Connections to the data bus must use three-state outputs. Program the system as a control system using an appropriate sensor(s) and actuator(s). Contrast the action of these ICs, suggesting to what kinds of application they are best suited. Investigate the use of handshaking and interrupt signals as detailed in the data sheet. ADCs and DACs: Program an ADC to sample the input from a light-sensitive circuit, convert it into digital form and display it. If your system lacks a display, program it to switch on an LED when the input voltage exceeds a given level. Program a DAC to accept a digital input. This is converted by the DAC into a varying voltage that can be used to control the speed of a small electric motor, or the brightness of a lamp. =========== Programming projects This SECTION concludes with a selection of programming topics. In each case, read the explanation and then write a program to demonstrate the topic. Debouncing Several of the programs have used the conventional routine of waiting for a keypress. An improvement is to debounce the key by using software. The routine should wait for the first contact to cause a change of input level. Then there should be a short delay, after which the input is sampled again to make sure the key is still pressed. The length of delay is important. If it is too short debouncing is not effective. If it is too long, the response is sluggish. A delay of 1 ms is reasonable as a starting point. Lookup tables Sometimes a value that is required may be calculated from another value by using a formula. For example, to convert a Fahrenheit temperature into a Celsius temperature we can use a formula. If it is needed several times in a program, it can be defined as a function: DEF FN FtoC = (f - 32)*5/9 Other pairs of values may not be related in a mathematical way. For example, the groups of Morse Code are not directly related to the letters and numerals they represent. As another example, the voltage produced by a circuit using a thermistor is not directly related to the temperature. In such cases as these we use a lookup table. If you are using assembler, this is a table in ROM, or it could be downloaded into RAM. When using a high-level language the data is stored in an array. In assembler, the table has a starting address incorporated in the program and we can access any particular value by offsetting the starting address by a given amount. For example, a table of the number of days in a month would be: In assembler, given the number of the month, the stored value of the number of days can be obtained by indexed addressing. In a high-level language it can be accessed by the commands used for manipulating arrays. It is a fairly easy project to program the month/days conversion. Slightly more difficult is to convert letters of the alphabet into the equivalent Morse Code. Addressing modes Microprocessors have several different ways or modes of specifying addresses, though they differ in which modes they can use. The four modes most often found are: Direct addressing: The mnemonic is followed by the actual memory address or register where the target data is stored. Indirect addressing: The mnemonic is followed by an address or register at which the address or register of the target data is stored. Indexed addressing: There is an index register in the microprocessor. The mnemonic is followed by a value that is added to the contents of the index register to obtain the address at which the data is stored. Relative addressing: The target data is stored at an address a given number of bytes further on (or further back). This is only applicable to machine code programming. A person using assembler would not be concerned with the actual addresses of the opcodes Investigate the addressing modes available on the CPU you are using and write short routines to try them out. Negative numbers We usually represent a negative decimal number by writing the negation sign (-) in front of it. This does not work in a computer because the registers contain 0's or 1's, and there is no '-' symbol. There are several ways of representing negative numbers in binary without using the negation sign. The most useful of these ways is the two's complement. We must first decide the number of digits in which we are working. This includes the sign digit on the left. We will work in four digits plus the sign digit. To form the twos complement of a number, write the positive number using 4 bits. Then write a 0 on the left to represent a positive sign. Next write the one's complement by writing 1 for every 0 and 0 for every 1. Form the two's complement by adding 1 to this number. Ignore any carry digits. This is the binary equivalent of the negative of the original number Examples (1) To find the 4-bit equivalent of -3. Write +3 in binary 0011 Write the sign digit for + 00011 Find the one's complement 11100 Add 1 1 Result is two's complement 11101 This is the equivalent of -3, and the 1 on the left indicates that it is negative. (2) Adding two negative numbers, for example, adding -2 to -3: Two's complement of -3 11101 Two's complement of -2 11110 Add [1]11011 Ignoring the carry digit in brackets, this is the two's complement of -5. (3) Adding a positive number to a negative number, to get a positive result, for example adding +4 to -3. Two's complement of -3 11101 Binary equivalent of +4 00100 Add [1]00001 The result is +1. (4) Adding positive to negative to get a negative result, for example, adding +2 to -6. Two's complement of -6 11010 Binary equivalent +2 00010 Add 11100 The result is -4. The technique also works with two positive numbers, for example, adding +3 to +2. Binary equivalent of +3 00011 Binary equivalent of +2 00010 Add 00101 The result is +5. Most assemblers have a mnemonic for forming two's complement. Write a routine for taking two values, negating one or the other or both and calculating their sum. PREV. | NEXT Related Articles -- Top of Page -- Home |
Updated: Thursday, May 18, 2017 9:58 PST