Summary of ST7

  • Page 1

    St7 8-bit mcu family user guide january 1999 1.

  • Page 2

    Use in life support devices or systems must be expressly authorized. Stmicroelectronics products are not authorized for use as critical components in life support devices or systems without the express written approval of stmicroelectronics. As used herein: 1. Life support devices or systems are tho...

  • Page 3: Table Of Contents

    3/317 table of contents 1 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.1 who is this book written for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.2 about the authors . . . . . . . . . . ....

  • Page 4: Table Of Contents

    4/317 table of contents 317 2.5.1.4 software interrupt instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 2.5.1.5 saving the state of the interrupted program . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 2.5.1.6 interrupt vectorization . . ....

  • Page 5: Table Of Contents

    5/317 table of contents 4 architecture of the st7 core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 4.1 position of the st7 within the st mcu family . . . . . . . . . . . . . . . . . . . . 62 4.2 st7 core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

  • Page 6: Table Of Contents

    6/317 table of contents 317 5.3.1 st72251 i/o ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94 5.3.2 st72311 i/o ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 5.4 watc...

  • Page 7: Table Of Contents

    7/317 table of contents 6.1.3 declaring variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.1.4 declaring constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 6.1.4.1 con...

  • Page 8: Table Of Contents

    8/317 table of contents 317 6.4.3.5 the caterpil.Bat file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179 6.4.4 using winedit to change and compile the files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180 7 debugger and prom programmer tutoria...

  • Page 9: Table Of Contents

    9/317 table of contents 7.8 summary remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 8 c language and the c compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 8.1 c language extensions for microcontrollers . . . ....

  • Page 10: Table Of Contents

    10/317 table of contents 317 8.9 conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 9 a carrier-current system for domestic remote control . . . . . . . . . 249 9.1 carrier current control and the x-10 standard . . . . . . . . . . ....

  • Page 11: Table Of Contents

    11/317 table of contents 10.5.3 polling the push-buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304 10.5.4 reading and filtering the wind direction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305 10.5.5 the periodic i...

  • Page 12

    12/317 1 - introduction 1 introduction 1.1 who is this book written for? This book is a technical guide for st7 users and may be approached in different ways: for students and anyone unfamiliar with microprocessors, but with some experience of logic circuits; they should start by reading chapters 1 ...

  • Page 13

    13/317 1 - introduction chapter 11: conclusion. Chapters 1, 2 and 3 are a refresher on the concept of a microcontroller. Chapter 1 introduces the concept, chapter 2 addresses the hardware and chapter 3 addresses the software as- pects. Chapters 4 through 7 describe the st7 and its programming tools,...

  • Page 14

    14/317 1 - introduction the following table shows a comparison of these solutions. Each one is discussed below. Wired logic uses commercially available logic functions and sometimes linear chips. Though it is simple, it is neither practical nor economical to consider this technology for building app...

  • Page 15

    15/317 1 - introduction achieve the required computational power. These external chips may simply be interface com- ponents, to adapt the electric signals to the input-output pins of the microcontroller, or addi- tional memory or peripheral components if the buses are available externally on the pin...

  • Page 16

    16/317 1 - introduction 01-anal such peripherals are typically available in many families. This example shows how two periph- erals properly selected can drastically reduce the component count and thus the printed circuit area. The solution shown may or may not fit the needs, but it is difficult to ...

  • Page 17

    17/317 1 - introduction 1.4.2 choice of microcontroller model the selected model of microcontroller must meet the requirements in terms of computational power. It must be able to handle the input-outputs, process the data in the required amount of time, and have enough memory to store both the progr...

  • Page 18

    18/317 1 - introduction memory of the application. The tools are able to test both the hardware and the software, and analyze any malfunctioning in order to allow corrections to be made. This can be done using only a personal computer, or external instruments connected to the computer, such as an em...

  • Page 19

    19/317 2 - how does a typical microcontroller work? 2 how does a typical microcontroller work? There is a wide range of microcontrollers available on the market. They differ in their compu- tational power, their internal organization, the number of their inputs and outputs, the type of peripherals t...

  • Page 20

    20/317 2 - how does a typical microcontroller work? 2.1 the central processing unit what is the central processing unit (cpu)? It is made up of the core, and auxiliary blocks like the clock generator, the reset circuitry, etc. The cpu of a microcontroller is the actual programmed logic circuitry tha...

  • Page 21

    21/317 2 - how does a typical microcontroller work? Table 1. Table of the main processor sizes 2.2 how the cpu and its peripherals make up a system the cpu cannot work alone. It is the central piece of a system that includes the following com- ponents: 2.2.1 cpu it computes and coordinates. It contr...

  • Page 22

    22/317 2 - how does a typical microcontroller work? The microcontroller has thus to handle two different kinds of things related to memory: the pro- gram, made of numbers that encodes the programming language instructions, and the data, that are what the calculations act on, and the result of these ...

  • Page 23

    23/317 2 - how does a typical microcontroller work? The second one, named «harvard», provides separate addressing spaces for the program and data. No instruction can thus write anything into the program space, protecting the pro- gram from accidental changes and doubling the total addressing range. ...

  • Page 24

    24/317 2 - how does a typical microcontroller work? 2.2.4 interrupt controller this is a piece of logic circuitry that manages the implementation of the interrupt concept de- scribed later in this chapter. Interrupts are the most common means of altering the normal course of the program, when an une...

  • Page 25

    25/317 2 - how does a typical microcontroller work? Features provided may include queueing of interrupt requests, handling requests according to their priorities, or even modification of priorities to increase the chance that low-priority re- quests will be eventually processed in a context where th...

  • Page 26

    26/317 2 - how does a typical microcontroller work? The alu is connected to a register that holds the state of the last calculation done, with bits in- dicating (among other things) whether the result was zero, negative, or overflowed the ca- pacity of the alu. It thus provides a means of testing th...

  • Page 27

    27/317 2 - how does a typical microcontroller work? 2.4 peripherals the peripherals are the places where the core, that executes computer code, is in contact with the real world that is represented by electrical signals. These signals may just be binary levels that change relatively infrequently, in...

  • Page 28

    28/317 2 - how does a typical microcontroller work? Push-pull or open drain. Some outputs also allow for a higher current to directly drive relays, leds or opto-isolators. In addition, these pins can also be used at the same time as the input-output pins of other pe- ripherals like timers, serial to...

  • Page 29

    29/317 2 - how does a typical microcontroller work? Quences in applications where the microcontroller must keep in control, like in automotive ap- plications or in security systems. Various solutions have been imagined to prevent such situations. The most popular is the watchdog timer. This is a tim...

  • Page 30

    30/317 2 - how does a typical microcontroller work? The effect of the interrupt is shown in the following diagram: 02-flow an interrupt is requested and authorized the current instruction is executed, the pc is incremented the pc and a small number of registers are saved, they are automatically push...

  • Page 31

    31/317 2 - how does a typical microcontroller work? 2.5.1.1 hardware mechanism the hardware mechanism is important to understand. It is different for each product, so what we shall describe here pertains specifically to the st7. An interrupt request is a binary signal (a flag) generated by several e...

  • Page 32

    32/317 2 - how does a typical microcontroller work? 2.5.1.3 global interrupt enable bit the various sources of interrupt may be inhibited as a whole using the i bit in the condition code register. When this bit is set, no interrupts are generated. However, the interrupt re- quests are not forgotten;...

  • Page 33

    33/317 2 - how does a typical microcontroller work? Depending on the source of the interrupt (i/o, timer, etc.), the core fetches, from a predefined location in memory, the address of the interrupt service routine especially written to process that event. The vectors are always located at the end of...

  • Page 34

    34/317 2 - how does a typical microcontroller work? 2.5.1.7 interrupt service routine when the processor has granted an interrupt request, and read the interrupt vector, it starts executing the interrupt service routine. This routine is merely a segment of program, written with exactly the same ease...

  • Page 35

    35/317 2 - how does a typical microcontroller work? Thus it is up to the programmer to save it, by pushing it to the stack at the beginning of the service routine, and popping it before executing the iret statement. 2.5.2.2 managing the stack this is just a reminder, since it applies anywhere in the...

  • Page 36

    36/317 2 - how does a typical microcontroller work? 2.5.2.5 data desynchronization and atomicity this paragraph addresses the precautions that must be taken, in the main program or any service routine that may be interrupted. In many cases, data is organized in blocks in memory, that is, several byt...

  • Page 37

    37/317 2 - how does a typical microcontroller work? The following example shows: what happens when the data are desynchronized. Let us assume the main program wants to increment the word variable reg (16-bit register in page zero), that currently contains 42ff hex. The following code will be used: ;...

  • Page 38

    38/317 2 - how does a typical microcontroller work? Reading the first bytes of the data, then the interrupt occurs; on return, the remainder of the data are read, but unfortunately there may not be coherence between the first byte that was read before the interrupt and those read after it. 2.5.3 con...

  • Page 39

    39/317 2 - how does a typical microcontroller work? 2.6 an application using interrupts: a multitasking kernel the conclusion in the previous paragraph states that interrupts are well-suited for a certain class of events to be processed. However, there are other cases outside this category. Some of ...

  • Page 40

    40/317 2 - how does a typical microcontroller work? Process of updating the data, and another task uses that data, there is a risk of desynchroni- zation. The same type of precautions must be taken to ensure atomicity of data updates. The same problem may also occur if more than one task handles con...

  • Page 41

    41/317 2 - how does a typical microcontroller work? 2.6.2 cooperative multitasking cooperative multitasking draws its name from the fact that task switching is not spontaneous. It only occurs when the task-switching function is called by the currently active task. This im- plies two facts: task swit...

  • Page 42

    42/317 2 - how does a typical microcontroller work? 2.6.3 multitasking kernels 2.6.3.1 advantages of programming with a multitasking kernel the multitasking kernel is the piece of code that controls the multitasking. The way it does it, and the flexibility it offers may vary greatly from one kernel ...

  • Page 43

    43/317 2 - how does a typical microcontroller work? Not simpler to perform the processing right in the interrupt service routine, instead of this ap- parent complexity. Actually, the processing of the keystroke can take a long time, too long to allow it to freeze the remainder of the application as ...

  • Page 44

    44/317 2 - how does a typical microcontroller work? 02-task common parts of the program code of the first task code of the second task code of the last task interrupt vector table program memory stack ram stack area for task #1 stack area for task #2 stack area for the last task 2 3 1 4 stack pointe...

  • Page 45

    45/317 3 - programming a microcontroller 3 programming a microcontroller a microcontroller is basically a programmable component. This means that it can do almost anything, when properly programmed. In fact, the design of the electrical schematic of a microcontroller-based application raises few que...

  • Page 46

    46/317 3 - programming a microcontroller there are, in almost all applications, parts of the code that still require assembly programming. These parts are, most of the time, small but have an important impact on the program. Here are a few such cases: the initialization part of the program. All high...

  • Page 47

    47/317 3 - programming a microcontroller quantity production, the microcontroller includes a masked rom programmed by the device manufacturer and that cannot be altered afterwards. 3.1.2.1 assembly language assembly language is merely a set of mnemonics that duplicates each instruction in a more leg...

  • Page 48

    48/317 3 - programming a microcontroller then, once he masters both, he may start to write his source file. Obviously, if later he has to do the same job with a different processor, the source file will be of no use in the future project. 3.1.2.2 assembler the word assembler has usually two meanings...

  • Page 49

    49/317 3 - programming a microcontroller here again, the format of all the above mentioned files is chosen by the assembler's manufac- turer, and differs from one to another. 03-asm 3.1.2.3 linker if the whole application program is small, it is easy to put the source text into a single file. The as...

  • Page 50

    50/317 3 - programming a microcontroller the tool that glues together all the object files, each the result of assembling the source files, is called the linker. The linker performs the following tasks: it takes all the object files, and merges them into a single object file by concatenating them on...

  • Page 51

    51/317 3 - programming a microcontroller 03-link 3.1.2.4 the project builder/make utility in the process introduced above, that relies on splitting-up the processing to save time, it is im- portant to keep track of which sources have been altered, only re-assemble these and not the others and then l...

  • Page 52

    52/317 3 - programming a microcontroller each time a file has a successor with a date older than its predecessor, the corresponding tool is run to update the successor. This is done in an orderly fashion, from the top of the hierarchy (the source files) to the bottom (the absolute object file). Then...

  • Page 53

    53/317 3 - programming a microcontroller 03-prom 3.1.2.6 simulators a simulator is a tool that pretends to run an absolute executable program. Like the other tools, it runs on the programmer's own pc. It interprets the machine code that is specific to the chosen processor or microcontroller, and out...

  • Page 54

    54/317 3 - programming a microcontroller the correct values, so as to avoid having to edit and make the program each time an error is found. In fact, simulation is limited to pieces of code that do not access the input-outputs, since this would require a complex program at simulator level to also si...

  • Page 55

    55/317 3 - programming a microcontroller this type of situation, its a difficult task for the programmer to complete the debugging of the application so that it can be validated. 3.2 c language 3.2.1 why use c? Many high-level language exist; some have been written specifically for a family of micro...

  • Page 56

    56/317 3 - programming a microcontroller take a long string of instructions, since the designer of the microcontroller has worked hard to provide efficient instructions to do the job. C is a structured language, meaning that code can easily be divided into blocks that can readily become functions, o...

  • Page 57

    57/317 3 - programming a microcontroller ability of programs to another machine. You will have to take account of this, especially when testing the code on your pc first as suggested above. This can in most cases solved by a dec- laration file that is included in the project and that exists in two v...

  • Page 58

    58/317 3 - programming a microcontroller 03-comp 3.2.3 debugging in c debugging in c is done the same way as explained for assembly language. However, the sim- ulator and debugger screens designed for high-level languages can also show the progress of the execution directly in the c-language source ...

  • Page 59

    59/317 3 - programming a microcontroller easy interrupt declaration and servicing in both assember and c language; efficient optimization that may be switched off at critical places; a simulator and a debugger that properly display the c source text, allowing you to easily change the values of varia...

  • Page 60

    60/317 3 - programming a microcontroller 3.3 development chain summary the development chain discussed above can be understood more clearly using a diagram. The diagram below shows the various files involved in the development process as cylinders; the translators used to change one file type into a...

  • Page 61

    61/317 3 - programming a microcontroller 3.4 application builders an application builder is a graphical language that allows you to define the function of the pro- gram by manipulating icons on a computer screen. Each icon represents a standard function, like addition, integration, comparison, etc. ...

  • Page 62

    62/317 4 - architecture of the st7 core 4 architecture of the st7 core 4.1 position of the st7 within the st mcu family the stmicroelectronics range of microcontrollers is very wide, from proprietary architectures like st6 to st10 to second-source products like microprocessors for pcs and digital si...

  • Page 63

    63/317 4 - architecture of the st7 core 4.2 st7 core the st7 core uses an extended version of an industry-standard instruction set. This set is both exhaustive (almost all instructions are available, with the exception of division) and fairly orthogonal (the instructions allow for most addressing mo...

  • Page 64

    64/317 4 - architecture of the st7 core 04-core accumulator x index register y index register stack pointer program counter condition code register arithmetic and logic unit cpu control core reset cpu clock interrupt requests from peripherals data memory from 0000h to 017fh: peripheral registers use...

  • Page 65

    65/317 4 - architecture of the st7 core 4.2.1 addressing space the st7, as said earlier, is based on a von neumann architecture. This means that there is only one addressing space in which the program, the data and the input-output peripherals are mapped. The advantages are: access to any byte (of p...

  • Page 66

    66/317 4 - architecture of the st7 core 04-ccreg the leftmost three bits, indicated as ones, are not used. When read, they yield ones. C bit the c bit is the carry that is generated by an addition or subtraction. When adding 16-bit num- bers, for example, we first add the two least-significant bytes...

  • Page 67

    67/317 4 - architecture of the st7 core h bit this bit is a similar to the c bit, since it is set when a carry occurs. This time, it is the carry be- tween the two nibbles of a byte. A byte is composed of two four-bit groups called nibbles. The h bit is set whenever an arithmetic instruction produce...

  • Page 68

    68/317 4 - architecture of the st7 core two addresses as is needed in a memory-to-memory data move, with or without alteration in between. However, these registers may also be used to store temporary data. 4.2.2.4 program counter (pc) the program counter is the register that controls the sequencing ...

  • Page 69

    69/317 4 - architecture of the st7 core 04-stack when a value is stored using the stack pointer, the stack is decremented, so that the next value stored will be placed at the address just below the previous one. This process of storing and decrementing the pointer is called pushing, and can be done ...

  • Page 70

    70/317 4 - architecture of the st7 core core has no y index register, so its instruction set does not take the y index register into ac- count. If needed, it must be pushed on entering the interrupt service routine using a push y in- struction, and restored by putting a pop y just before the iret . ...

  • Page 71

    71/317 4 - architecture of the st7 core from the categories above, numbers can be distinguished because they start with a decimal figure. For hexadecimal numbers that may start with a letter, there are two main conventions: in the so-called intel convention, a number will only be recognized as such ...

  • Page 72

    72/317 4 - architecture of the st7 core that is the translation of the statement above, supposing the variable voltage had been as- signed to the memory address 124h . In the examples of source lines above, the text that follows the semicolon (;) is a comment. It is ignored by the assembler, and its...

  • Page 73

    73/317 4 - architecture of the st7 core the last mode, called inherent, means that the data involved in the instruction does not need to be designated by an address, such as the instruction that increments the accumulator. The many indexed and indirect modes available are useful for translating prog...

  • Page 74

    74/317 4 - architecture of the st7 core 4.3.4 coding of the instructions and the address the instructions are coded using bytes stored in program memory. One instruction takes one to four bytes according to its type and the addressing mode. These bytes are, in order: the prefix byte (optional); the ...

  • Page 75

    75/317 4 - architecture of the st7 core 4.3.4.2 opcode byte the opcode uses a bit-level coding to specify the type of operation to perform (add, subtract, jump, etc.) and the addressing mode used (direct, indexed, indirect, etc.) in a single byte. The tables below summarize the available instruction...

  • Page 76

    76/317 4 - architecture of the st7 core as said about the prefix, this table changes to either y index, or indirect, or both according to the prefix byte. The letter m indicates «memory» in instructions such as ld m, x . Low digit high digit 8 9 a b c d e f 0 btjt m,4 btjf m,4 btjt m,5 btjf m,5 btjt...

  • Page 77

    77/317 4 - architecture of the st7 core 4.3.4.3 the addressing modes in detail immediate mode when a register has to be loaded with a constant value that has been fixed in the program source, that value is determined in the program source text and must be stored in the program memory to make it fixe...

  • Page 78

    78/317 4 - architecture of the st7 core in memory below address 100h . The assembler automatically takes care of this, by selecting the appropriate addressing mode when possible. Example: the instruction ld a, 1234h a is loaded with the value stored in an absolute extended memory address. Loads the ...

  • Page 79

    79/317 4 - architecture of the st7 core e6 23 this instruction takes two bytes of memory. The farthest address that can be reached is 1feh ( 0ffh + 0ffh ). Caution: there must be no space on either side of the comma within the parenthesis. Example: (23h, x) is incorrect. Indexed with long offset mod...

  • Page 80

    80/317 4 - architecture of the st7 core example: at address 1200h, the following instruction: jra 11f1h the absolute address supplied in the source code will be translated into a relative displacement by the assembler. Is coded as: 20 ef the two bytes of the instruction occupy addresses 1200h and 12...

  • Page 81

    81/317 4 - architecture of the st7 core where there and here are labels respectively designating the destination of the jump, and the address of the instruction that follows the jump instruction. Here again, if the difference yields a number that exceeds the range of a one-byte value, the assembler ...

  • Page 82

    82/317 4 - architecture of the st7 core indexed with indirect short offset mode this mode is similar to the indirect short mode, in that the address that follows the instruction is the address of a 8-bit pointer in memory that contains an address. The difference is that this address is not used righ...

  • Page 83

    83/317 4 - architecture of the st7 core a[i] = b[j] + c[k] where a , b and c are arrays of numbers, and i , j and k the indexes to these arrays. The high- level language compiler translates this so as to read the ith element of array a using the avail- able machine-language instruction. If these are...

  • Page 84

    84/317 5 - peripherals 5 peripherals 5.1 clock generator the core of the st7 is supplied with an internal clock that comes from the division of the os- cillator frequency. This division rate is programmable, allowing you to select the best compro- mise between speed and power consumption. This choic...

  • Page 85

    85/317 5 - peripherals 5.1.2 st72311 miscellaneous register the sms bit works the same way as in the st72251 (see previous paragraph), but in addition, the two bits psm1 and psm0 (prescaler for slow mode) select the supplementary division rate between 2 and 16. The bit mco (main clock out), when set...

  • Page 86

    86/317 5 - peripherals 5.2 interrupt processing interrupt requests may be generated by several external or internal sources. Most peripherals of the st7 can produce interrupt requests, such as the i/o ports, the timers or the spi. The in- terrupt request depends on the type of the peripheral: the i/...

  • Page 87

    87/317 5 - peripherals 5.2.1.1 interrupts sources for the st72251 source block description register vector address priority order reset reset n/a fffeh-ffffh highest trap software n/a fffch-fffdh ei0 pa0-pa7 n/a fffah-fffbh ei1 pb0-pb7 pc0-pc5 n/a fff8h-fff9h not used fff6h-fff7h spi transfer comple...

  • Page 88

    88/317 5 - peripherals 5.2.1.2 interrupt sources for the st72311 source block description register vector address priority order reset reset n/a fffeh-ffffh highest lowest trap software n/a fffch-fffdh not used fffah-fffbh not used fff8h-fff9h ei0 pa0-pa3 n/a fff6h-fff7h ei1 pf0-pf2 n/a fff4h-fff5h ...

  • Page 89

    89/317 5 - peripherals as shown in the rightmost column of these tables, these sources are prioritized. This means that if several interrupt requests are active at the same time, the interrupt controller will choose to service the highest priority request. When this interrupt is fully serviced, the ...

  • Page 90

    90/317 5 - peripherals 05-int 5.2.3 global interrupt enable bit reset and trap (explained in the next paragraph) are non-miscible interrupt; the other sources of interrupt may be inhibited as a whole thanks to the i bit of the condition code reg- an interrupt is requested its relative pending bit is...

  • Page 91

    91/317 5 - peripherals ister. When this bit is set, no interrupts are generated. However, the interrupt requests are not forgotten; they will be processed as soon as the i bit is reset. 5.2.4 trap instruction in addition to the hardware sources, a special instruction, trap, produces the same effect ...

  • Page 92

    92/317 5 - peripherals the interrupt service routine is supposed to take the appropriate actions that will vary ac- cording to the interrupt source. For example, if an input bit has changed its state, the service routine may change the state of an output bit; if the interrupt was generated by the ti...

  • Page 93

    93/317 5 - peripherals bled during the service of an interrupt. In this case, all interrupts are validated and the service routine may be itself interrupted by a request having a lower priority than its own. If the service routine must be interruptible only by certain requests and not others, the ro...

  • Page 94

    94/317 5 - peripherals 5.3 parallel input-output ports the purpose of the parallel input-outputs is basically to make binary signals either get into or out of the core. For the core and once initialized, they appear as a memory location that can be written or read. But in many cases, direct byte-wid...

  • Page 95

    95/317 5 - peripherals table 3. St72251 i/o configuration in addition, some pins also serve as inputs for other peripherals, or may be held from their normal output function and be taken as the output pins of some peripherals if those periph- erals are specially configured to do so by setting a bit ...

  • Page 96

    96/317 5 - peripherals 05-ei251 5.3.2 st72311 i/o ports bits 0 to 3 of port a and bits 0 to 2 of port f of are configurable as interrupt inputs. The option for the edge and level of these inputs is set by bits pei0 and pei1 (external interrupt polarity option) in the miscellaneous register. The inte...

  • Page 97

    97/317 5 - peripherals table 5. St72311n i/o configuration note: pa0-pa2, pb5-pb7, pd6-pd7 and pe4-pe7 are not available on the st72311j ver- sion. In addition, some pins also serve as inputs for other peripherals, or may be held from their normal output function and be taken as the output pins of s...

  • Page 98

    98/317 5 - peripherals table 6. St72311n i/o alternate functions note: none of the pins have two alternate functions, unlike the st72251. Since the pins can produce interrupt requests in some modes, you must take care, when pro- gramming the port configuration registers, to perform the transisitons ...

  • Page 99

    99/317 5 - peripherals 05-ei311 note: although the pins of the ports may be individually selected as interrupt inputs, the direc- tion of the edge or the active level is selected by groups in the miscellaneous register. This implies that the electrical schematic takes this into account, and that sig...

  • Page 100

    100/317 5 - peripherals foreseen event, the program may be fooled and the whole application may fail to work or even produce harmful actions. To prevent this, two actions may be taken. Write better code. Check what happens if a neglected condition arises. Lead the execution to a recovery routine in ...

  • Page 101

    101/317 5 - peripherals 05-wdg1 in some st7 family products, for example the st72311, two options are available for the watchdog: software-activated, as above, and hardware-activated, where the watchdog is al- ways active, regardless of the state of the msb of the wdgcr register. This option is sele...

  • Page 102

    102/317 5 - peripherals 05-wgd2 bit 6 must be set high at all times. Setting it low immediately resets the microcontroller. This may be used to reset the whole application if needed under program control (e.G. A message received is coded as a reset request). Bits 5 through 0 select the time-out valu...

  • Page 103

    103/317 5 - peripherals this bit is set whenever the last reset was triggered by the watchdog. This allows the program to check whether the current start is a fresh one, or results from the recovering from an error condition. 5.4.3 using the watchdog to protect an application the right value for the...

  • Page 104

    104/317 5 - peripherals 5.5.1 timer clock two clock sources are available for the timer: either the core clock, or an external clock sup- plied on a pin that takes up one port pin. When the internal clock is used, the timer is actually fed with the core clock after passing through a frequency divide...

  • Page 105

    105/317 5 - peripherals 5.5.2 free running counter the main component of the 16-bit timer is the free-running counter, called the chr (most significant byte) and clr (least significant byte) register. This is a binary counter that incre- ments by one at each clock cycle, hence its name. It is possib...

  • Page 106

    106/317 5 - peripherals if the low byte is read again, the actual value of the low byte of the counter can be read. To benefit from the latching feature, it is necessary to read the high byte first. Conversely, reading the high byte one more time will always yield the same value as before: a read of...

  • Page 107

    107/317 5 - peripherals 5.5.2.3 the tof flag the timer overflow flag is a bit in the timer status register. It is set each time the free-run- ning counter overflows from ffff to 0000. It can be cleared under program control by the following sequence: a read to the tsr register, followed by a read or...

  • Page 108

    108/317 5 - peripherals 05-intof the interrupt service routine must clear the tof bit before returning, using the sequence de- scribed above. No automatic clearing is performed by the interrupt service mechanism. 5.5.3 input capture operation the counter, as discussed above, can be read on-the-fly b...

  • Page 109

    109/317 5 - peripherals the clearing of icfi is done by a sequence similar to that described for the tof bit: read the tsr register, and either read or write the icilr register. Only the icie flag is common to both channels. Thus, if both channels are used simultane- ously, on interrupt, the tsr reg...

  • Page 110

    110/317 5 - peripherals 05-capt 5.5.4 output compare operation the output compare is a feature that produces an event when the current value of the free- running counter matches the value of a register called output compare register (ocnr, where n is 1 or 2 for the first of the second compare regist...

  • Page 111

    111/317 5 - peripherals the change of the state of an output pin; the reset of the free-running counter (only in pwm mode). The pwm mode will be studied later. The block diagram of one of the compare circuits is the following: 05-comp output compare and corresponding interrupt mechanism: diagram for...

  • Page 112

    112/317 5 - peripherals there are two such compare circuits for each free-running timer; the table below summarizes the register, bit, and pin names for both timers a and b and for compare 1 and compare 2 cir- cuits of each. Timer a: timer b: in the following text, we shall only consider the output ...

  • Page 113

    113/317 5 - peripherals the taoc1hr-taoc1lr pair is a 16 bit register whose value is continuously compared to the free-running counter. If the compare function is not used, it can be used for general purpose storage. When the match occurs, the ocf1 flag is set in the tasr register. This bit can only...

  • Page 114

    114/317 5 - peripherals 05-pulse the input capture event both toggles the output and resets the free-running counter to fffch, and a successful match with the value in the register taoc1r toggles the output back. This is why the compare register should be set to the calculated value minus 4. The lea...

  • Page 115

    115/317 5 - peripherals the input capture event, corresponding to the leading edge, sets the icf1 bit of tasr. This can trigger an interrupt if the icie mask bit is set. It must then be reset by software as explained above. The output compare 2 event, corresponding to the trailing edge, sets the ocf...

  • Page 116

    116/317 5 - peripherals 05-pwm the settings for this mode are performed as follows: set the taoc1hr-taoc1lr pair to the number of ticks corresponding to the duration of the output pulse (this number depends on the clock frequency), minus 4. Pwm mode and corresponding interrupt mechanism: diagram for...

  • Page 117

    117/317 5 - peripherals set the taoc2hr-taoc2lr register pair to the number of ticks corresponding to the duration of the whole cycle (this number depends on the clock frequency), minus 4. Set the olvl2 bit in the tacr1 register to the state required for the output pin for the duration of the pulse,...

  • Page 118

    118/317 5 - peripherals conversion time: 64 clock cycles of the adc. The adc being fed with the core clock frequency. These somewhat cryptic features actually mean the following simple facts: the input voltage must remain positive or null; below zero volt, the conversion yields zero. In addition, th...

  • Page 119

    119/317 5 - peripherals the coco bit is reset when the adccsr register is written. 05-adc0 5.6.3 the problem of the converter's accuracy linearity and accuracy are different ways of expressing the same reality: the successive voltage steps that correspond to each of the binary values are not absolut...

  • Page 120

    120/317 5 - peripherals criterion, that states the shape of the curve that relates the closed-loop gain with the phase shift. These parameters depend on the gain and phase shift of all of the components of the system, including the adc. The local gain of the converter is the slope of the conversion ...

  • Page 121

    121/317 5 - peripherals the second method uses an absolute rectifier circuit that produces a voltage that is the abso- lute value of the input voltage. This circuit also provides a bit that indicates the sign of the input voltage. Thus the conversion of the whole range is increased by one extra bit ...

  • Page 122

    122/317 5 - peripherals let us now add to that voltage v0, a small voltage, that is equal to half the smallest step, that is, if the voltage v0 is included within the converter will convert it as adc0. If the voltage v0 is included within the converter will convert it as adc0 + 1. We can now disting...

  • Page 123

    123/317 5 - peripherals this will yield two results, adc0 and adc1. If we take the sum of these values, whatever the value of v, it will range from 0 + 0 = 0 to 255 + 255 = 510. This new result now has a resolution of 511 points, which is almost double the original resolution. The figure below depic...

  • Page 124

    124/317 5 - peripherals actually, this method is not very convenient, because the typical error of the converter is 0.3 lsb which is too high compared to the small voltage that we intend to use as an extra step. Also, the noise would make this increased resolution meaningless. A practical way of inc...

  • Page 125

    125/317 5 - peripherals 5.7 serial peripheral interface the serial peripheral interface is a device that allows synchronous, bit serial communication between a microcontroller and a peripheral such as a serial-access memory, a liquid crystal display module, etc.; or between two or more microcontroll...

  • Page 126

    126/317 5 - peripherals when one master controls two or more slaves, a method must be used to distinguish between the two slaves. Several methods are available. The one shown below uses the ss pin of each slave to control which slave is active at any given time. These pins are each driven by a sep- ...

  • Page 127

    127/317 5 - peripherals an example of the use of the spi to send data to a lcd display module is given in the second application, chapter 10. The configuration used is shown in the figure below with the appro- priate values in the registers. 05-app10 8-bit shift register spidr internal data bus mosi...

  • Page 128

    128/317 5 - peripherals the cpol and cpha bits in the control register allow you to select which clock edge is used externally as the active one (leading or trailing) and its polarity (low or high level after trans- mission). 5.8 serial communication interface the serial communication interface is p...

  • Page 129

    129/317 5 - peripherals with another core clock, we would get different values. The values in bold are the most com- monly used. Please note that they are not exact; however, asynchronous serial transmission is by nature tolerant of bit rate errors of up to 4%. The error here is only 0.16%. The divi...

  • Page 130

    130/317 5 - peripherals 05-sci1 the receive mechanism is a bit more complicated. The receive clock must first be set to the same frequency as the transmit clock of the source of the serial data to be received. Since the start bit is used to resynchronize the clocks, the clock frequency has a fairly ...

  • Page 131

    131/317 5 - peripherals the resynchronization is the most difficult thing. It uses a clock a 16 times the bit rate, and the state of the receive data pin is checked at each period of that clock. When a falling edge is de- tected, a mechanism takes that time plus half a bit period as the reference ti...

  • Page 132

    132/317 5 - peripherals 5.8.3 status register the status register includes the following bits that show the current status of the sci: the tdre and tc bits, if set, indicate that a new character may be supplied to continue the transmission. The difference between these bits comes from the fact the s...

  • Page 133

    133/317 5 - peripherals rwu, when set, places the receiver in a mode where the rdrf bit is not set and a receive interrupt is not generated, even when characters are received. The sci exits this sleep state only when one of the following events occur: the ninth bit is a one, with the word length set...

  • Page 134

    134/317 5 - peripherals been received by a flag set by the interrupt service routine. Conversely, if the main program wants to send data, it prepares the character or string to send into a buffer in memory, then starts the transmission by enabling the transmit interrupt. This will make the interrupt...

  • Page 135

    135/317 6 - stmicroelectronics programming tools 6 stmicroelectronics programming tools this chapter describes the stmicroelectronics programming tools, their installation and their use, based on a simple example program. The standard st microelectronics st7 programming tools package includes the fo...

  • Page 136

    136/317 6 - stmicroelectronics programming tools 06-ast7 the source text is, like an ordinary text, made of words. There are two kinds of words (groups of characters in the text): the predefined words, that are the opcodes and the pseudo-ops, and user-defined words, called identifiers, that accordin...

  • Page 137

    137/317 6 - stmicroelectronics programming tools the label field must start on the first character of the line. The label is a name that identifies the address in memory of the objects defined in the current line, or, if none, on the next non-empty line. The label name may if desired and for sake of...

  • Page 138

    138/317 6 - stmicroelectronics programming tools the assembler automatically recognizes the addressing mode after the syntax of the operand, as in the following examples: ld a,#2; immediate ld a,value; direct short ld a,value2; direct long ld a,value,(x); indexed with short displacement ld a,value2,...

  • Page 139

    139/317 6 - stmicroelectronics programming tools the position of the data in the memory by hand, and this is susceptible to mistakes; in addition, inserting an extra variable in the middle of a list implies recalculating the addresses of all those that follow it. On the other hand, the absolute posi...

  • Page 140

    140/317 6 - stmicroelectronics programming tools thus the assembler relieves the programmer from any calculation. Any change in the program will be automatically accounted for when the source text is re-assembled. 6.1.4 declaring constants a constant, in assembler, may be of one of two kinds: the co...

  • Page 141

    141/317 6 - stmicroelectronics programming tools wish to build a table of jump addresses, you must use this order to make the jumps occur as expected. On the other hand, a call instruction pushes the return address most significant byte first, which means that in memory the most significant byte is ...

  • Page 142

    142/317 6 - stmicroelectronics programming tools if you use the symbol display_width several times in your program, and you place the above declaration at a convenient place in the source text (in general at the beginning), when the dis- play is later enlarged to 24 characters, this line is the only...

  • Page 143

    143/317 6 - stmicroelectronics programming tools the way that these functions communicate with each other has been defined, and that some common writing rules have been set out. Since the program is divided into several files, a problem arises because each programmer does not know at which address h...

  • Page 144

    144/317 6 - stmicroelectronics programming tools the read-write memory is divided into two areas: a short-addressable area and the remainder, which is accessible using extended addressing. The input-output area is organized as a collection of individual registers that each have their own function, a...

  • Page 145

    145/317 6 - stmicroelectronics programming tools combine option this option has the following effects: the effect of these options is detailed in the next paragraph. Class name a class is a group of segments. The notion of class does not have any other properties, and any number of classes may be cr...

  • Page 146

    146/317 6 - stmicroelectronics programming tools ld a, #cpudiv2 ld miscr, a ; fq 8mhz /2 = cpu clock = 4mhz etc. For a block of code, the segment directive may only occur after either an unconditional jump or a return instruction. This is because two segments are independent objects, that can be put...

  • Page 147

    147/317 6 - stmicroelectronics programming tools all segments found later in the program with the same class name will be allocated after this one. To make this work, the module that defines the classes has to be first in the object file list when you invoke the linker. If two seg ments of the sam e...

  • Page 148

    148/317 6 - stmicroelectronics programming tools .Longvalue ds.L 1 ; a long number data1 segment byte common 'data' .Byte1 ds.B 1 ; first byte of the long value .Byte2 ds.B 1 ; second one .Byte3 ds.B 1 ; third one .Byte4 ds.B 1 ; fourth one with this declaration, the four bytes byte1 to byte4 exactl...

  • Page 149

    149/317 6 - stmicroelectronics programming tools rectly during debugging; but when the product is put into production or, even worse, later at the customer, the byte could then have a value that makes the program behave wrongly. The consequences would then be very serious. To provide a more convenie...

  • Page 150

    150/317 6 - stmicroelectronics programming tools ld ({variables-1},x), a dec x; next byte jrne initvar1 this routine uses the addresses of both segments, and the length of one of the segments, as calculated by the expression size_ram: equ {* - variables} where the * character means “the current addr...

  • Page 151

    151/317 6 - stmicroelectronics programming tools drive the data copy loop in the piece of code above, you get your variables initialized in a con- venient and error-free way. 6.1.5.6 referencing symbols and labels between modules declaring external symbols if a program is split into several modules,...

  • Page 152

    152/317 6 - stmicroelectronics programming tools output. Besides this, he is free to organize his work as he likes – though some writing style rules might have been given to the team for sake of homogeneity of style and ease of mainte- nance afterwards. The consequence is that he may define as many ...

  • Page 153

    153/317 6 - stmicroelectronics programming tools dressing at all. Thus, the you are advised to pay special attention to these questions if you want to optimize the execution time of your program by using the data in page zero. Another way of declaring an identifier as public is to insert a dot befor...

  • Page 154

    154/317 6 - stmicroelectronics programming tools segment 'data_page0' bytes value1 ds.W 1 ; a word data in page zero segment 'data_extended' words value2 ds.B 1 ; a byte data in extended memory the lines containing the segment pseudo-op may appear either before or after the line with the bytes , wor...

  • Page 155

    155/317 6 - stmicroelectronics programming tools as an example, let us consider a product for which two different suppliers of displays are con- sidered. These two displays are almost the same, except for a few differences. The product is produced for some time with one type of display, then a bette...

  • Page 156

    156/317 6 - stmicroelectronics programming tools #define first_type must be removed, or changed, like this: #define second_type then, the identifier first_type is no longer defined; that the identifier second_type is defined instead does not prevent the first_type throughout the program being false,...

  • Page 157

    157/317 6 - stmicroelectronics programming tools the following code, in the same module, invokes the macro by inserting its name in the oper- ation field. Ld a, d1 ld d2, a mycode ld a, d3 the result is, when assembled: 35 0000 r c60000 ld a, d1 36 0003 r c70000 ld d2, a 37 38 mycode 38 0006 3c00 in...

  • Page 158

    158/317 6 - stmicroelectronics programming tools nition of the macro is: incbytwo macro thebyte inc thebyte inc thebyte mend the macro is used by adding the symbol of the byte to be incremented: incbytwo counterlo the macro is expanded by the assembler to the following code: 44 incbytwo counterlo 44...

  • Page 159

    159/317 6 - stmicroelectronics programming tools ments a word variable. Unlike the example above, the low byte is incremented, then we test if it is zero. If yes, we increment the high byte: incword macro lowbyte, hibyte inc lowbyte jrne noinchigh inc hibyte noinchigh: mend this macro expands correc...

  • Page 160

    160/317 6 - stmicroelectronics programming tools 67 67 0019 3c00 inc counterlo 67 001b r 2602 jrne loc1 67 001d 3c01 inc counterhi 67 loc1: we can see that the label noinchigh has been replaced in the first expansion by loc0 and in the second by loc1 . The multiple definition problem is now avoided....

  • Page 161

    161/317 6 - stmicroelectronics programming tools 95 001f b602 ld a, nbofapples 95 0021 bb03 add a, nbofpears 95 0023 #ifb nboffruit 95 0023 b704 ld nboffruit, a ; result in third argument 95 0025 #endif 96 0025 97 98 addition nbofapples, nboffruit, 98 0025 b602 ld a, nbofapples 98 0027 bb04 add a, n...

  • Page 162

    162/317 6 - stmicroelectronics programming tools 117 0033 #iflab 3 117 0033 a603 ld a, #3 ; 1st arg is constant 117 0035 #endif 117 0035 #iflab nbofapples 117 0035 bb02 add a, nbofapples 117 0037 #else 117 0037 b704 ld nboffruit, a 6.1.8 some miscellaneous features here are a few pseudo-ops or contr...

  • Page 163

    163/317 6 - stmicroelectronics programming tools above is encountered in the program, the assembler replaces it with the figure 3 ; when the identifier result is encountered, it is replaced with numberofapples . No other processing or property is involved. The #define statements are often put at the...

  • Page 164

    164/317 6 - stmicroelectronics programming tools 6.1.9.1 object files the result of the assembly of the source text is its translation into machine language. This is a binary file that contains the binary values of the instructions, addresses and constant data. This file is called the object file, a...

  • Page 165

    165/317 6 - stmicroelectronics programming tools 95 000c 96 000c r c60000 ld a, d3 97 98 initvariables: 99 000f r ae00 ld x, #{low size_ram} the first column is the number of the line of source text. Here, the line 94 appears four times because the source text contains the invocation of the macro my...

  • Page 166

    166/317 6 - stmicroelectronics programming tools 06-lyn caution: the linker is invoked with the list of object files ( .Obj ) to be linked, and if you wish, with the name of the resulting file ( .Cod ). The modules that define the absolute segment loca- tions must be put in the list before those tha...

  • Page 167

    167/317 6 - stmicroelectronics programming tools cations must be reused. This code might as well be included in the list of source files to as- semble; but with the following drawbacks: a source file may be modified accidentally, leading to a malfunctioning code. The problem is then difficult to pin...

  • Page 168

    168/317 6 - stmicroelectronics programming tools this choice allows you to download the executable progra m to virtually an y commercial eprom programmer. When using the wgdb7 debugger, the program is loaded into the de- bugger using either the intel or motorola format. 6.2.3 the back-annotation pas...

  • Page 169

    169/317 6 - stmicroelectronics programming tools to be made is to select the directory into which winedit will be stored. It is always advisable to follow the suggested directory. The second choice is that of the components to be installed. Unless there is a shortage of disk space, it is recommended...

  • Page 170

    170/317 6 - stmicroelectronics programming tools this means that you must edit your autoexec.Bat file and do the changes required. They are necessary for run ning the tools. This can be done using either w ine dit or notepad. The changes performed will only be taken into account after the computer h...

  • Page 171

    171/317 6 - stmicroelectronics programming tools the true source files of the project are actually main.Asm and timer500.Asm . All other files are auxiliary files. The project.Wpj file is the configuration file for the editor itself; it defines also the tools to be used. The reg72251.Asm file is a s...

  • Page 172

    172/317 6 - stmicroelectronics programming tools 06-proj.Bmp the project name is just free text. It is not the name of the project file that can be saved by pressing the save... Button. The working directory is that of the project. It is advisable to have one separate directory for each project. The...

  • Page 173

    173/317 6 - stmicroelectronics programming tools the compiler field that is currently set to clipper selects the rules for decoding the assem- bler or compiler report. It is used in conjunction with the capture output option that must then be enabled. When the assembler is launched, and the capture ...

  • Page 174

    174/317 6 - stmicroelectronics programming tools the main program is the following: st7/ ;============================================================= ;= main (caterpillar) = ;============================================================= #include "register.Inc" extern delay500 ; by default, externa...

  • Page 175

    175/317 6 - stmicroelectronics programming tools dc.W 0 dc.W 0 dc.W 0 dc.W 0 ; skip 7 vectors dc.W 0 dc.W 0 dc.W 0 dc.W 0 dc.W 0 dc.W 0 dc.W reset ; reset vector ($fffe) end the main program calls the following subroutine that has been written in a separate file on pur- pose, though it would be easi...

  • Page 176

    176/317 6 - stmicroelectronics programming tools delay500: ld a, #delay1 ; 2 cycles ld time, a ; 4 cycles loop1: ld a, #delay2 ; 2 cycles ld {time+1}, a ; 4 cycles loop2: ld a, #delay3 ; 2 cycles ld {time+2}, a ; 4 cycles loop3 ld a, #watch ; 2 cycles ld wdgr, a ; 4 cycles dec {time+2} ; 5 cycles jr...

  • Page 177

    177/317 6 - stmicroelectronics programming tools ;* declarations are in the include file reg72251.Inc that ;* must be included in the source files that use them. ;************************************************************* bytes ; the following addresses are 8-bit long ;***************************...

  • Page 178

    178/317 6 - stmicroelectronics programming tools extern padr ; port a data register extern paddr ; port a data direction register extern paor ; port a option register extern pbdr ; port b data register extern pbddr ; port b data direction register extern pbor ; port b option register extern pcdr ; p...

  • Page 179

    179/317 6 - stmicroelectronics programming tools ;* ;************************************************************* bytes ; following addresses are 8 bit long ;************************************************************* segment byte at 80-ff 'ram0' ; user ram in zero page ;*************************...

  • Page 180

    180/317 6 - stmicroelectronics programming tools the first four lines make the assembler translate the four source files of the project. The fifth line gives the object files to link, then the name of the result file, and the name of the libraries that may be needed. Here, no libraries are used, hen...

  • Page 181

    181/317 6 - stmicroelectronics programming tools the source text is shown in an editing window. As many files as you wish may be open at the same time. Try first to assemble it by clicking on the funnel tool, or select the project/compile option. A dos box appears for a short time, then the followin...

  • Page 182

    182/317 6 - stmicroelectronics programming tools to assemble all the source files, open all of them, and on each, perform the assembly in turn. When all the source files are assembled, it is time to build the result files. To do this, press the hammer button. This starts the batch file described abo...

  • Page 183

    183/317 7 - debugger and prom programmer tutorial for st72251 7 debugger and prom programmer tutorial for st72251 the tools supplied by st microelectronics at no cost are those described in the previous chapter, plus the following: the eprom programmer board software epromer, and w gdb7.Exe which is...

  • Page 184

    184/317 7 - debugger and prom programmer tutorial for st72251 7.1.2 starter kits the starter kits are exist in three flavors, each providing a different eprom programming board. They include the stmicroelectronics software tools, so that the board can be driven using the ep rome r software. They do ...

  • Page 185

    185/317 7 - debugger and prom programmer tutorial for st72251 a power supply, that connects to a wall outlet and to the programming board though a coaxial jack a cable that connects the programming board to a parallel port of the computer the software stored on disk 7.2.1 eprom programmer installati...

  • Page 186

    186/317 7 - debugger and prom programmer tutorial for st72251 select the option configure/configure epromer, then the following window pops up. It is made of two tabs, select hardware and port, and select chip. Let us start with the first one: 07-prog2.Bmp in the left-hand box, select the type of bo...

  • Page 187

    187/317 7 - debugger and prom programmer tutorial for st72251 in this window, select the device that will be used. Then press ok. The ep romer is now configured. Typically, to program an eeprom, we need to load the hexadecimal file into the programmer, then to program the device. The hexadecimal fil...

  • Page 188

    188/317 7 - debugger and prom programmer tutorial for st72251 this window is an edit window, that allows you to alter the values to be programmed directly by putting the cursor on one byte and typing in the new value. The tabs at the bottom allow you to select between the program memory (e prom) and...

  • Page 189

    189/317 7 - debugger and prom programmer tutorial for st72251 tents of the window. However, this test is automatically performed prior to programming, and thus, is optional. 7.3 emulator and debugger 7.3.1 introducing the emulator and the debugger the emulator and the debugger constitute together th...

  • Page 190

    190/317 7 - debugger and prom programmer tutorial for st72251 07-emu now that the hardware is connected, we can install the debugger. The software is made of two disks for the debugger program, plus one disk for the configura- tion of the debugger. To install the debugger, insert the first diskette ...

  • Page 191

    191/317 7 - debugger and prom programmer tutorial for st72251 07-emu1.Bmp if you have selected the hds1 emulator, you are requested to give the serial port number to which the emulator is connected: 07-emu2.Bmp if you have selected the hds2 emulator, you are requested to give the parallel port numbe...

  • Page 192

    192/317 7 - debugger and prom programmer tutorial for st72251 07-emu3.Bmp then, in both cases, you are requested the type of probe that is connected to the emulator; this is also the type of chip you want to use in your application: 07-emu4.Bmp.

  • Page 193

    193/317 7 - debugger and prom programmer tutorial for st72251 the installation process finishes a short time later. Once it is installed, we can start using it at once. If either of the settings in the boxes above has to be changed,you do not need to repeat the whole installation procedure. Just ins...

  • Page 194

    194/317 7 - debugger and prom programmer tutorial for st72251 press ok. The hexadecimal file, containing the machine-language program, is loaded. The debugger expects that the map file of the project also resides in the same directory and has the same name, but with the extension .Map. For this to h...

  • Page 195

    195/317 7 - debugger and prom programmer tutorial for st72251 since the processor has been automatically reset after the object file was opened, the first line of the program is underlined, showing where the execution will start from. Opening the sources pull-down menu, we get the list of the module...

  • Page 196

    196/317 7 - debugger and prom programmer tutorial for st72251 07-emu8.Bmp now, we want to see how the timer routine affects the variables in memory. Let us select the toolbox windows/dump option. A window opens, showing the contents of the whole memory. Using the scroll bar, let us watch the value o...

  • Page 197

    197/317 7 - debugger and prom programmer tutorial for st72251 07-emu10.Bmp now, if we step through the timing loops, we can see the values changing. 7.3.3.4 using inspect and watch the mem ory inspection as shown above gives access to the whole data and program in memory; however, it only gives the ...

  • Page 198

    198/317 7 - debugger and prom programmer tutorial for st72251 the address and the type of the variable, shown here, are only displayed if the misc/info op- tion of the box is selected. The misc/hot option also allows this window to be refreshed each time the execution of the program is suspended. Th...

  • Page 199

    199/317 7 - debugger and prom programmer tutorial for st72251 7.3.3.5 using breakpoints our timing loop is very long. It is impractical to step through it. There is an easier way to do it: put a breakpoint. For example, we want to watch what happens to the outer loop. Put the cursor on line 57, wher...

  • Page 200

    200/317 7 - debugger and prom programmer tutorial for st72251 07-emu12.Bmp when we press continue, the program runs at full speed until the next breakpoint, if any. During this time, we are blind to what happens in memory. To compensate for this, we have a tool called the trace recorder. Select the ...

  • Page 201

    201/317 7 - debugger and prom programmer tutorial for st72251 the top line is where we are now. The next line is where we came from. If we click on this second line, and select the v iew/source option, the window showing the main.Asm source- pops up and line 45 is highlighted in blue. This is very c...

  • Page 202

    202/317 7 - debugger and prom programmer tutorial for st72251 the first line of the above display, which is red, shows the current source line. The second line, which is blue, shows on the right the absolute code corresponding to the source line. For example, the identifier cpudiv2 in the source tex...

  • Page 203

    203/317 7 - debugger and prom programmer tutorial for st72251 verter. When at the highest position, it provides the full v cc voltage that is converted to a value of 255. The relationship provides for a frequency that changes from about 0.5 hz to 10 hz. 07-func since we use a timer to produce a freq...

  • Page 204

    204/317 7 - debugger and prom programmer tutorial for st72251 7.5 schematic drawing of the printed circuit board the schematic of the resulting board is the following: 07-vfx4 7.6 developing the program 7.6.1 peripherals used to implement the solution three peripherals will be used here: the 16-bit ...

  • Page 205

    205/317 7 - debugger and prom programmer tutorial for st72251 the 16-bit timer runs continuously and g enerates an in ter rupt every 10 m s. A software counter is used to count the half-period time of the blinking. The parallel input-output ports are used both to input the four voltages to the a dc ...

  • Page 206

    206/317 7 - debugger and prom programmer tutorial for st72251 7.6.3 a simple multitasking kernel for the st7 as mentioned in a previous chapter, to implement a multitasking kernel on the st7, you must sacrifice a lot of features. On the other hand, the st7 is not expected to handle programs re- quir...

  • Page 207

    207/317 7 - debugger and prom programmer tutorial for st72251 now, at the top of each of the stacks, the routine writes the address of the beginning of each task, and initializes the variable permut to zero. This variable keeps track of the number of the current task. 07-space an array of four varia...

  • Page 208

    208/317 7 - debugger and prom programmer tutorial for st72251 7.6.3.2 the yield routine a call to this routine must be inserted in the code of each task, at least once within the main loop. As many calls to yield as you wish may be inserted in each task. W hen this function is executed, the current ...

  • Page 209

    209/317 7 - debugger and prom programmer tutorial for st72251 this mechanism is illustrated in the diagram below: 07-yield pcl task2 pch task2 pcl task3 pch task3 pcl task4 pch task4 17f (sp) 16d 15d 14d task1 is awaken active area of the stack pcl task1 pch task1 17f 16f 15f 14f the active area of ...

  • Page 210

    210/317 7 - debugger and prom programmer tutorial for st72251 the next figure shows, in detail, the modifications to the stack when moving from task one to task two. 07-break task1 is running ; permut = 0 17f (sp) active area of the stack 17f "call yield" instruction is executed sp 17f pcl task1 pch...

  • Page 211

    211/317 7 - debugger and prom programmer tutorial for st72251 the yield functions are characteristic of cooperative multitasking. It is the programmer’s job to select the places to put them. The advantage is that they can easily be put at places where task-switching is safe, thus solving the problem...

  • Page 212

    212/317 7 - debugger and prom programmer tutorial for st72251 multitsk.Bat , that is the batch file that does the linking and the back-annotation. Here are details on each of these. 7.6.4.1 main file (multitsk.Asm) the main file contains the code for the initialization of the system, that is termina...

  • Page 213

    213/317 7 - debugger and prom programmer tutorial for st72251 the source code for the main program and for the multitasking kernel follow. First, the main program: st7/ ;================================================================== ;= demonstration of a simplified real-time cooperative kernel =...

  • Page 214

    214/317 7 - debugger and prom programmer tutorial for st72251 addrtasks: dc.W task1, task2, task3, task4 ; table of pulse duration. Returns the result of the voltage to period function. Timingtable: dc.B 100, 93, 87, 82, 77, 73, 69, 66 dc.B 63, 60, 57, 55, 53, 51, 49, 47 dc.B 46, 44, 43, 41, 40, 39,...

  • Page 215

    215/317 7 - debugger and prom programmer tutorial for st72251 ld a, #t_timer ; initialize taocr1 register ld taoc1hr, a ; (comparison register) clr taoc1lr inc a ; a counter reset will happen ld taoc2hr, a ; before it reaches the value ; in taocr2 rim ; enable maskable interrupts ; main ; ==== main:...

  • Page 216

    216/317 7 - debugger and prom programmer tutorial for st72251 dc.W inttim_a ; timer a: ($fff2) dc.W 0 dc.W 0 dc.W 0 dc.W 0 dc.W 0 res: dc.W reset ; vecteur de reset ($fffe) end 7.6.4.2 adc source file (acana.Asm) the analog to digital conversion handling is done in this short file: st7/ ;===========...

  • Page 217

    217/317 7 - debugger and prom programmer tutorial for st72251 7.6.4.3 kernel source file (littlk.Asm) the multitasking kernel is contained in the following file: st7/ ;============================================================== ;= simple real time kernel = ;=======================================...

  • Page 218

    218/317 7 - debugger and prom programmer tutorial for st72251 ld x, {addrtasks+4} ; msb address task push x ; written at top of stack ld a, s ld imstack3, a ; remember position of stack pointer ld a, #org2 ; top of stack for task 2 ld s, a ld x, {addrtasks+3} ; lsb address task push x ld x, {addrtas...

  • Page 219

    219/317 7 - debugger and prom programmer tutorial for st72251 7.7 running the application this application is assembled and built as already described in the previous chapter: open winedit select file/configure (if no previous configuration is loaded) or project/configure (if there is a current conf...

  • Page 220

    220/317 7 - debugger and prom programmer tutorial for st72251 lator. When you have played with it for a while, you will be ready to consider designing your own application. The following chapter will first introduce the c language and related software tools and dis- cuss its advantages. It will also...

  • Page 221

    221/317 8 - c language and the c compiler 8 c language and the c compiler the c language has been introduced in the chapter “p rogramming a microcontroller”. The present chapter relates specifically to the hicross c compiler for the st7. The software tools introduced in the previous chapters are sup...

  • Page 222

    222/317 8 - c language and the c compiler compilers to see which one matches your needs, and carefully read the user manual of that compiler. The details given below apply only to the hicross development chain. 8.2 description and installation of the hicross tool chain the hicross development tool c...

  • Page 223

    223/317 8 - c language and the c compiler to do this, press the left-hand button which is the configuration button. The following window opens: 08-hwto2.Bmp this box allows you to create a project configuration, open an existing one, or change the cur- rently open configuration. For the time being, ...

  • Page 224

    224/317 8 - c language and the c compiler click on the editor tab and select the winedit option in the combobox. In the executable box, write the path for winedit. You may use the browse... Button next to it. In our case, winedit is installed in the directory c:\program files\winedit\winedit.Exe : 0...

  • Page 225

    225/317 8 - c language and the c compiler now click on the additional tab, and complete the line with errorfile so that it reads: errorfile=\edout 08-hwto5.Bmp press ok twice. W e are done with the hiware tools. The second button of the tool bar now shows the winedit icon. The last line of the last ...

  • Page 226

    226/317 8 - c language and the c compiler 8.3 using the c compiler it is not the purpose of this book to teach the basics of c language. The reader is expected to have had some introduction to the c language, even if he is not an expert; but some experi- ence is required to appreciate the specific f...

  • Page 227

    227/317 8 - c language and the c compiler the availa ble m emory varies with the mode l of s t7 chose n. T o o ptim ize the access to memory, the compiler offers four memory models that set the rules for data allocation. These models are: these modes govern the default selections; however, it is sti...

  • Page 228

    228/317 8 - c language and the c compiler the effect of the const modifier is to generate an error message whenever a statement tries to assign a new value to that constant, as in the following example: size = 200 ;/* here the compiler will generate an error message */ this method, standard for c pr...

  • Page 229

    229/317 8 - c language and the c compiler float coefficient ;{ two non-volatile parameters } #pragma data_seg default int value[10] ;{ an array of ten integers, volatile } 8.3.1.3 page zero variables direct addressing, as mentioned in the chapter that discusses addressing modes, is much more efficie...

  • Page 230

    230/317 8 - c language and the c compiler 8.3.2 initialization of variables and constant variables on starting, a c program initializes all variables to zero, except those that are defined with an initial value. Example: int i ; int j = 3 ; here i is initialized to zero, and j is initialized to 3. T...

  • Page 231

    231/317 8 - c language and the c compiler 8.3.3.1 first method: using macros the method that the documentation of the compiler suggests relies on the definition of a macro that produces a pointer definition, as in the following example that defines padr that is located at address 8: #define padr * (...

  • Page 232

    232/317 8 - c language and the c compiler special care has to be taken at this stage: the linker normally optimizes the code by removing unused declarations. If we let it do so, it will remove some of the register declarations, and thus shift the addresses of the following registers. To prevent the ...

  • Page 233

    233/317 8 - c language and the c compiler rameters, and allocates local variables to ram just like global variables. This works the same way as in a typical implementation, but with the following restrictions: functions are neither reentrant nor can they be used recursively. The compiler uses tempor...

  • Page 234

    234/317 8 - c language and the c compiler #define enableinterrupts {asm rim;} #define disableinterrupts {asm sim;} 8.4.1.2 multiple-statement assembler block when a block of several lines must be included, the lines must be enclosed in an asm block, as follows: asm { etc. } an example of in-line ass...

  • Page 235

    235/317 8 - c language and the c compiler 8.4.2 using the hiware assembler the hiware assembler is comparable to the assembler described in chapter 6. The same principles apply, so they will not be repeated here. However, the syntax that hiware uses is specific and different from that of the stmicro...

  • Page 236

    236/317 8 - c language and the c compiler here is an example of a link parameter file taken from the x10xm it project described in chapter 9: link x10xmit.Abs names main.O interrup.O map72251.O+ start07.O ansi.Lib end sections aportc = read_write 0x00 to 0x02; /* for st72251 */ aportb = read_write 0...

  • Page 237

    237/317 8 - c language and the c compiler the name of the absolute object file is x10mit.Abs. In the list of the object files to be linked, start07.O is a predefined file supplied with the package; ansi.Lib is the library of c functions from which the linker will extract the required functions, and ...

  • Page 238

    238/317 8 - c language and the c compiler 08-burn.Bmp then use the epromer programmer software as described in chapter 7..

  • Page 239

    239/317 8 - c language and the c compiler 8.7 project directory structure needless to say, a well organized set of files on the computer's hard disk helps in designing and in maintaining a software project. Using the following recommended directory structure will make your project easier to support,...

  • Page 240

    240/317 8 - c language and the c compiler 08-hwto6.Bmp press ok. The path tab contains lists of paths grouped in classes. In the current example, the appro- priate settings for each group are: absolute: this is where the absolute file created by the linker is put: c:\st7\work\x10xmit\object general ...

  • Page 241

    241/317 8 - c language and the c compiler this is where the include files and the libraries are searched from: c:\hicross\lib\st7c c:\hicross\lib\st7c\include c:\st7\work\x10xmit\sources\map c:\st7\work\x10xmit\sources\lib object: this is where the object files created by the assembler and the compi...

  • Page 242

    242/317 8 - c language and the c compiler prefixed by a asterisk indicate that the files must not be searched only in that directory, but also in any of its subdirectories. Finally, the main path of the project is specified as the working path in the configuration panel of winedit. Here is an exampl...

  • Page 243

    243/317 8 - c language and the c compiler the effect of this + sign is to remove the optimization for this file, that would normally remove all the unused variable declarations, which would interfere with the allocation of the registers to the proper addresses. The header file looks like the excerpt...

  • Page 244

    244/317 8 - c language and the c compiler some registers hold numeric values, like taoc2hr or taoc2lr ; in such registers, reading or writing a value is done using an assignment statement: taoc2hr = timeraperiod >> 8 ; /* set new period as calculated in the capture */ taoc2lr = timeraperiod & 0xff ;...

  • Page 245

    245/317 8 - c language and the c compiler the register is and ed with the one's complement of 1 shifted by the appropriate number of places, which preserves all bits except the one that we want to reset; and the result is written back into the register. 8.8.2 setting configuration registers the synt...

  • Page 246

    246/317 8 - c language and the c compiler for example, in the project given in chapter 9, three push-buttons are called close, open and time. These push-buttons are wired so that pressing one grounds the corresponding pin, that is otherwise kept high by a pull-up resistor. The following macro yields...

  • Page 247

    247/317 8 - c language and the c compiler however, this does not mean that you should adopt a loose programming style, as it is often the case when programming for computers. There, a clear style, using techniques that can be quickly written, is recommended, but this may be at the expense of memory ...

  • Page 248

    248/317 8 - c language and the c compiler 8.8.4.3 limit the size of variables to the very minimum do not use long integers where integers are sufficient; and above all, avoid using floating arith- metic. Floating operations take a long time to process, and may impede the response time of a program. ...

  • Page 249

    249/317 9 - a carrier-current system for domestic remote control 9 a carrier-current system for domestic remote control the application described here is a basically a system that allows the remote control of electric appliances of any kind with no wiring other than the regular power wiring of a hou...

  • Page 250

    250/317 9 - a carrier-current system for domestic remote control we shall first explain what carrier-current control is, taking the x-10 standard as a reference; then, we shall describe the transmitter. The second part of this chapter will describe the re- ceiver. 9.1 carrier current control and the...

  • Page 251

    251/317 9 - a carrier-current system for domestic remote control tion. They are synchronized to the zero crossing point of the ac power line. They must be transmitted as close to the zero crossing point as possible, but certainly within 200 microsec- onds of the zero crossing point. A binary 1 is re...

  • Page 252

    252/317 9 - a carrier-current system for domestic remote control the next four cycles represent the house code and the last five cycles represent either the number code (1 thru 16) or a function code (on, off, etc.). This complete block, (start code, house code, key code) should always be transmitte...

  • Page 253

    253/317 9 - a carrier-current system for domestic remote control within each block of data, each four or five bit code should be transmitted in true complement form on alternate half cycles of the power line. So, if a 1 millisecond burst of signal is trans- mitted on one half cycle (binary 1) then n...

  • Page 254

    254/317 9 - a carrier-current system for domestic remote control the tables in the next figure show the binary codes to be transmitted for each house code and key code. The start code is always 1110 which is a unique code and is the only code which does not follow the true complementary relationship...

  • Page 255

    255/317 9 - a carrier-current system for domestic remote control note 1: hail request is transmitted to see if there are any x-10 transmitters within listening range. This allows the user to assign a different house code if a “hail acknowledge” is re- ceived. Note 2: in a pre-set dim instruction, th...

  • Page 256

    256/317 9 - a carrier-current system for domestic remote control the pb1 pin is connected to the gate of a transistor that switches the 120 khz oscillator on or off. The output of this oscillator is amplified, and injected to the second pole of the line through a capacitor. The same pole of the line...

  • Page 257

    257/317 9 - a carrier-current system for domestic remote control 09-xtx 4. 7 k 4. 7k pa0 (25) pa1 (24) pa2 (23) pa3 (22) pa4 (21) pa5 (20) pa6 (19) pa7 (18) 330 bs250 bs250 pc0 (17) pc1 (16) pc2 (15) pc3 (14) 10k 10 k 10 k pb5 (6) pb6 (5) pb7 (4) 220 / 6v + 470µf 16v 220nf 1 00k 100nf 400v 10 bdx 33...

  • Page 258

    258/317 9 - a carrier-current system for domestic remote control the features of the 72251 are used as follows: the first timer is set to pwm mode. It is configured to generate a pulse on the ocmp1_a output, that is the alternate function of the pb1 pin. The repetition period is set to exactly 1/6 o...

  • Page 259

    259/317 9 - a carrier-current system for domestic remote control so the repetition period will remain within the correct value. The variation of the timer period is constrained within narrow bounds, so that large frequency deviations are not possible. This prevents surges or stray pulses on the line...

  • Page 260

    260/317 9 - a carrier-current system for domestic remote control /* this function triggers the sending of one command */ void sendcommand ( byte command ) { keycode = codes[command] ; /* prepare the two variables that */ housecode = codes[house_code] ; /* contain the data to send. */ cyclenumber = 1...

  • Page 261

    261/317 9 - a carrier-current system for domestic remote control return 0 ; /* no button pressed or more than one button pressed. */ } } please note the use of the macro push_buttons in the text. This macro is defined as follows: #define push_buttons ( ( ~pbdr ) & 0xe0 ) /* upper three bits */ #defi...

  • Page 262

    262/317 9 - a carrier-current system for domestic remote control while ( cyclenumber != 0 ) ; /* wait for end of transmission */ while ( debounce () != 0 ) ; /* wait for all buttons released */ } } only those lines that are not self-explanatory have been commented. 9.2.3.2 timer a capture interrupt ...

  • Page 263

    263/317 9 - a carrier-current system for domestic remote control the icf1 flag is first tested to know whether the interrupt was caused by a capture 1 event. If yes, the first block is executed; if not, the second block is executed. In the first case, the two bytes of compare register 2 are set to t...

  • Page 264

    264/317 9 - a carrier-current system for domestic remote control the next five pairs of bits carry the key code, and work exactly the same way as for the house code. Since the whole process above must be repeated, all the case tags are duplicated with an offset of 22. When cyclenumber reaches 45, th...

  • Page 268

    268/317 9 - a carrier-current system for domestic remote control as described in the st72251 data sheet, a mechanism to avoid data desynchronization pre- vents any captures occuring once the high register has been read, until the low register has been also read. This implies that we must ensure that...

  • Page 269

    269/317 9 - a carrier-current system for domestic remote control the operation of the pll is illustrated by the diagram below: 09-pll 9.2.3.3 the timer b overflow interrupt service routine this routine has a dual role: it refreshes the display and counts down the time. Transmission windows t : 20ms ...

  • Page 270

    270/317 9 - a carrier-current system for domestic remote control the interrupts are triggered by an overflow of the free-running counter. At each interrupt, the pa7 bit is toggled, so that each digit is lit every other interrupt, that is, there are 32 refresh cy- cles per second. Since the values to...

  • Page 271

    271/317 9 - a carrier-current system for domestic remote control if ( second > 50 ) /* the display blinks at 1 hz with a short duty cycle. */ padr = display_off ; /* switch led off. */ else padr = sevensegment[delay][second & 1] ; /* light one of both digits at a time. */ since second ranges from 63...

  • Page 272

    272/317 9 - a carrier-current system for domestic remote control 9.3 receiver 9.3.1 instructions for use whereas there is only one transmitter in a house, there are several receivers, each located near the blind to be controlled. All receivers are identical, and, provided they are set to the same ho...

  • Page 273

    273/317 9 - a carrier-current system for domestic remote control same time, this produces a voltage drop of about 1 v across the capacitor. The st72251 al- lows a comfortable range of power supply voltages, so that the drop is acceptable. The st7 will also be used in an economical manner. For this r...

  • Page 274

    274/317 9 - a carrier-current system for domestic remote control the following figures show the schematics of the receiver: 09-xrx-1 n ph 10nf-400v 180 pb2 (9) 1m 22pf 47pf 47pf 22k 22k 20k 47pf 47pf pc1 (16) 100nf 400v + 6.2v power supply 100k 100k 100k 100k va 220µf pa7 (18) pa6 (19) pa5 (20) pa4 ...

  • Page 275

    275/317 9 - a carrier-current system for domestic remote control the four switches at the top select the house code of the receiver. The receiver only obeys the commands that are sent to its own house code. The power supply circuit looks like a classical scheinkel voltage doubler. A ctually, the inp...

  • Page 276

    276/317 9 - a carrier-current system for domestic remote control the relay coils are driven by mos transistors, since the current specification of the parallel port does not allow for direct connection of the coil to the port. The pull-down resistors guar- antee that at power on, no coil is powered,...

  • Page 277

    277/317 9 - a carrier-current system for domestic remote control case 24 : case 25 : /* first 3 half cycles must have a pulse (start condition) */ if ( ! Carrierdetected ) cyclenumber = 1 ; /* reset the whole process if incorrect header. */ break ; case 4 : /* 4th half cycle must have no pulse ; end...

  • Page 280

    280/317 9 - a carrier-current system for domestic remote control the interrupts, for whatever the initial value of tboc2r, there will necessarily be a compare 2 event before the free-running counter overflows. This would trigger an interrupt request as soon as the interrupt mask bit is set. To avoid...

  • Page 281

    281/317 9 - a carrier-current system for domestic remote control switches. To allow this, the voltage at both phases of the motor is reduced, then rectified and the resulting voltage is the higher of the two. When the motor is steady, the voltage at its ter- minals is the line voltage (e.G. 230 vac)...

  • Page 282

    282/317 9 - a carrier-current system for domestic remote control the control of the relay coils the relays are arranged so that one relay switches the motor on or off, while the other relay selects the open or close direction. As said above, the relays are of the locking- or bistable-type. This mean...

  • Page 283

    283/317 9 - a carrier-current system for domestic remote control case stop_close: stopmotor () ; break ; case start_open: coils = open ; waitdelay ( coil_pulse ) ; coils = start ; waitdelay ( coil_pulse ) ; waitdelay ( 5 * tenth_second ) ; break ; case start_close: coils = close ; waitdelay ( coil_p...

  • Page 284

    284/317 9 - a carrier-current system for domestic remote control initports () ; inittimera() ; inittimerb() ; miscr = 0x0 ; /* normal mode (clock/2). */ enableinterrupts ; lastdirection = stop_open ; /* to properly start manual cycle */ stopmotor () ; /* to guarantee motor off at power on */ while (...

  • Page 285

    285/317 9 - a carrier-current system for domestic remote control switchmotor ( stop_open ) ; break ; case start_close : if ( readadc ( motor_adc_channel ) switchmotor ( stop_close ) ; break ; } } } 9.4 conclusion this first application has been designed to emphasize the following features: using c l...

  • Page 286

    286/317 10 - second application: a sailing computer 10 second application: a sailing computer the aim of this application is to build a calculator for optimizing the steering of a yacht when going against the wind. In this situation, sailors have to make tacks, that is, sail close to the wind altern...

  • Page 287

    287/317 10 - second application: a sailing computer 10-vmg loch-speedometer weather vane / anemometer visible wind real wind visible angle real angle vmg boat speed printed board circuit with st72311 mcu sailing computer.

  • Page 288

    288/317 10 - second application: a sailing computer 10.1 theory of the computation the following diagrams apply against the wind, starboard side and before the wind, port side, respectively. 10-again against the wind alphav alphar bs vmg bs vmg rw vw.

  • Page 289

    289/317 10 - second application: a sailing computer 10-befor bs rw vw vmg alphar alphav before the wind.

  • Page 290

    290/317 10 - second application: a sailing computer the meaning of the symbols used in these diagrams is the following: the mathematical relationships involved in the calculation of are given from the equations: and these relationships are rewritten under the following shape: and: which give: symbol...

  • Page 291

    291/317 10 - second application: a sailing computer they apply for alpha ranging from zero included to 360° excluded. The angles are counted clockwise, according to nautical usage. 10.2 interfacing the measurement devices there are three measurement devices to interface: the speedometer, the wind ga...

  • Page 292

    292/317 10 - second application: a sailing computer to achieve the required accuracy, a clocking frequency of at least 2.048 khz is required, so as to produce a count of 20 points or more at the highest speed, meeting the required resolution of 5%. 10.2.1.3 using a common timer for both speed measur...

  • Page 293

    293/317 10 - second application: a sailing computer unchanged when the trimmer is at the center; the values are decreased or increased by 20% when the trimmer is at one end or the other, respectively. The trimmers are only read once at power on. 10.2.2 interfacing the weather vane the weather vane c...

  • Page 294

    294/317 10 - second application: a sailing computer 10.3 interfacing the display the display used is made of three, three-digits-and-a-half, liquid-crystal display. A pair of leds indicates the value currently being displayed in each lcd display. Toggling be- tween the values is done by pressing a p...

  • Page 295

    295/317 10 - second application: a sailing computer 10.3.1 display circuit 10-schem mosi (pc5) sck (pc6) st 72311 4094 4094 4094 4094 3 lcd modules, each driven by 4 shift registers 5v pd0 (ain) 1k 330 330 330 push buttons: (2.49v) (0x7f) (1.99v) (0x65) (1.24v) (0x3f) 5v 390 5v 390 5v 390 pa4 pa5 pa...

  • Page 296

    296/317 10 - second application: a sailing computer the schematic above shows how the three lcd components are driven. Each segment and the backplane of each component is connected to an output of a eight-bit, serial-in, parallel- out, shift register. All shift registers are cascaded. Thus the whole...

  • Page 297

    297/317 10 - second application: a sailing computer 10-code "-" 0x08 "+" 0x 0a "-1" 0x18 "+1" 0x1a " b ack plane on output 0 of the first shift register "0" 0xf6 ".0" 0xfe "1" 0x90 ".1" 0x98 "2" 0x75 ".2" 0x7d "3" 0xf1 ".3" 0xf9 "4" 0x93 ".4" 0x9b "5" 0xe3 ".5" 0xeb "6" 0xe7 ".6" 0xef "7" 0xb0 ".7" ...

  • Page 298

    298/317 10 - second application: a sailing computer thus, displaying a number, say -1.234, consists of finding the corresponding pattern in the table above and sending all four patterns in a row, starting with the right-most digit. In this ex- ample, we would send the bytes (in hexadecimal): the liq...

  • Page 299

    299/317 10 - second application: a sailing computer able pins is not sufficient. This is not the case here, as far as parallel ports are concerned, however a restriction lies in the fact that the display board is connected to the main board using a 9-pin connector. To overcome this problem, a differ...

  • Page 300

    300/317 10 - second application: a sailing computer 10.5 program architecture the program performs the following tasks: read the values of wind speed and direction and boat speed compute the absolute values and the vmg display these values on request from the pc, send the appropriate data the values...

  • Page 301

    301/317 10 - second application: a sailing computer ld a, tbic1hr /* get new time */ ld capture1.W.Low, a ld a, tbic1lr ld capture1.W.Low:1, a } boatperiod = capture1.Long - lastcapture1 ; /* calculate time difference */ lastcapture1 = capture1.Long ; /* remember this time for next capture */ } if (...

  • Page 302

    302/317 10 - second application: a sailing computer high-order word of both capture variables. To do so, the capture variable is declared as fol- lows: typedef union ucapture { lword long ; struct scapture { word high ; word low ; } w ; } tcapture ; it consists of the union of an unsigned long and o...

  • Page 303

    303/317 10 - second application: a sailing computer the contents of this array are copied to the spi, one byte at a time, by the following function. The last three bytes of the array contain the values to be displayed. The function uses the sevensegmentcode lookup table to find the pattern to be sen...

  • Page 304

    304/317 10 - second application: a sailing computer the polarity variable is also toggled here, so that the drive voltage will have a frequency of 50 hz since the function is called 100 times per second. 10.5.3 polling the push-buttons the push-buttons are wired to a resistor arrangement that produc...

  • Page 305

    305/317 10 - second application: a sailing computer oldstate = newstate ; /* remember for next time */ } the button number is determined by checking that the voltage is within a certain range. The debouncing consists of checking that the voltage remains in the same range for a certain number (here 3...

  • Page 306

    306/317 10 - second application: a sailing computer adcsr = ( 1 while ( ( adcsr & ( 1 /* wait for end of conversion */ /* correct mounting and take advantage of overflow to roll over. */ a = adcdr + vaneadjust ; /* configure a to d back to channel 0 (push-buttons) */ adcsr = ( 1 wiper[wiperindex++] ...

  • Page 307

    307/317 10 - second application: a sailing computer if ( turn ) { getpushbuttons() ; readdirection () ; } turn = ! Turn ; refreshalldisplays () ; } 10.5.6 computation of the results the main program takes the raw values and applies to them the formulae given at the begin- ning of this chapter. For e...

  • Page 308

    308/317 10 - second application: a sailing computer the results of the computation are word values that must be converted into a string of charac- ters. This is done by the following function which is a very classical number-to-string conver- sion. The leftmost digit is processed separately: if the ...

  • Page 309

    309/317 10 - second application: a sailing computer 10.5.7 handling of the serial interface the serial interface is configured to both transmit and receive under interrupt control. The con- figuration function is the following: void initsci ( void ) { scicr1 = 0 ; /* 8 bits, one stop */ scibrr= ( 3 ...

  • Page 310

    310/317 10 - second application: a sailing computer if the interrupt cause is the reception of a character, the interrupt request is cleared by reading the status register (which is done when it is tested), then reading the data register. The character received is copied to the global variable remot...

  • Page 311

    311/317 10 - second application: a sailing computer the three calibration parameters are read from the position of the three trimmers. The three voltages are read and converted into the proper numeric values to work appropriately when used in the calculations. These parameters are produced by the fo...

  • Page 312

    312/317 10 - second application: a sailing computer 10.6 memory allocation and compile and link options the whole program make extensive use of floating point arithmetic. It is thus a memory eater. The rom section does not represent a problem, provided a device with a sufficient rom size is chosen. ...

  • Page 313

    313/317 10 - second application: a sailing computer aram2 = read_write 0x200 to 0x27f; arom = read_only 0xc000 to 0xffe0; placement default_rom, rom_var, strings into arom; default_ram into aram; _zeropage into azram; _overlap into aram2; sstack into astack; ports into aports; misc into amisc; spi i...

  • Page 314

    314/317 10 - second application: a sailing computer which both forces the linker to put any variables declared after this line into the _zeropage segment. It also specifies that all accesses to the corresponding variables must be done using direct short addressing. The fact that a variable is in the...

  • Page 315

    315/317 11 - some last remarks 11 some last remarks the st7 has been shown at work in two different applications, where two variants have been used. These can be considered as the most common variants. Actually, the st7 range is very rich, with a wide choice of memory sizes and peripherals. This all...

  • Page 316

    316/317 11 - some last remarks the analog loop is closed by means of the 16-bit timer which can produce accurate pwm sig- nals, that can easily be converted to voltage using a low-pass filter. In addition to being powerful for its size, the st7 is also easy to use, using the available pro- gramming ...

  • Page 317

    317/317 11 - some last remarks in short, the range of tools offered for the st7 spans all different needs from those of a small lab to a large engineering department. The mid-range solution is also particularly valuable for education purposes. Information furnished is believed to be accurate and rel...