Docsity

Teach YourSelf PIC Microcontroller Programming, Notas de estudo de Eletrônica

Toggle sidebar
bg1
Teach Yourself PIC Microcontrollers | www.electronicspk.com | 1
M. Amer Iqbal Qureshi
M
icrotronics Pakistan
PIC Microcontrollers
Teach Yourself
For Absolute Beginners
www.electronicspk.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Teach YourSelf PIC Microcontroller Programming e outras Notas de estudo em PDF para Eletrônica, somente na Docsity!

M. Amer Iqbal Qureshi M icrotronics Pakistan

PIC Microcontrollers

Teach Yourself

For Absolute Beginners

www.electronicspk.com

About This Book

This book, is an entry level text for those who want to explore the wonderful world of microcontrollers. Electronics has always fascinated me, ever since I was a child, making small crystal radio was the best pro- ject I still remember. I still enjoy the feel when I first heard my radio. Over the period of years and decades electronics has progressed, analogs changed into digital and digital into programmable.

A few years back it was a haunting task to design a project, solely with gates and relays etc, today its ex- tremely easy, just replace the components with your program, and that is it.

As an hobbyist I found it extremely difficult, to start microcontrollers, however thanks to internet, and ex- cellent cataloging by Google which made my task easier.

A large number of material in this text has its origins in someone else’s work, like I made extensive use of text available from Mikroelectronica and other sites.

This text is basically an accompanying tutorial for our PIC-Lab-II training board.

I wish my this attempt help someone, write another text.

Dr. Amer Iqbal

206 Sikandar Block

Allama Iqbal Town Lahore

Pakistan

[email protected]

To My Mother,

Prof. Razia Dr. Razia Iqbal

and my Father,

Late Prof. Dr. M. Iqbal Qureshi

who really did an excellent job, in my

training and inspiration.

May Allah bless them.

Table of Contents

  • Introduction to Microcontrollers
  • Understanding Hardware
  • Setting up the Programmer
  • Setting Up Proton Basic Compiler
  • Basic Programming Language … A Primer
  • I/O Ports
  • Writing Your First program
  • Reading Switches
  • Using Graphic LCD
  • Asynchronous Serial Communication
  • Sound and Digital Signals
  • Analog Module
  • On-Chip EEPROM
  • On-Chip CCP Capture | Compare | PWM
  • Pulse
  • Interrupts
  • Timers and Interrupts
  • I2C Communication
  • Basic Electronics
  • Expanding Microcontroller I/O Lines
  • H-Bridge and DC Motors
  • Stepper Motors
  • Real Time Clock
  • Making a frequency Counter
  • Working with Matrix LED Displays
  • MPLAB® and ICD-2
  • Using Boot Loader

do different tasks, but a change in specifications can easily be implemented by just changing the device configuration. This greatly facilitated the engineers and hobbyists to rapidly develop new electronic devices, and continuously improve previous ones. Not only the hardware requirements decreased, but also design time, and time to market were decreased.

Microcontrollers and microprocessors therefore took over the market. Large hardware designs were reduced, and most of the circuitry was replaced by the configuration scripts. Today we call this ability to configure a microprocessor or microcontroller, programming.

A program is nothing but a series of instructions, in a correct and logical manner to instruct the microprocessor respond to various inputs. By changing the program, the behavior of microcontroller will change. Think of it as a music system. The manufacturer has not designed it to produce any particular sounds out of its speakers. Yet it has all the necessary circuitry to do that. What music it will produce would depend upon the tape, or CD inserted. Thus you change the CD, and the same hardware is playing different thing. So we can say that the music system, is a programmable device, and the information stored on tape, or CD is the program, or instructions to help the music system, make sounds.

Similarly microprocessors and microcontrollers, are programmed to do a job. The job can be changing a TV channel to controlling complex movements of a robot. All these applications have a microcontroller doing its specific job. It can be astonishing to find the same microcontroller in the remote control, and the robot. In one place it is driving an infra-red LED and in other it is driving the motors.

Take another example. Consider plain paper and pencil. Now you have a choice of 26 alphabets, 0- numbers and few others like space, full stop etc. that is it. Not much hardware, only paper and pencil, and not much choice of letters, just 26 + few more. What you can do with it. You can do miracles. Write a complete thesis, a poem, a novel, an essay or what not. It all depends how you organize those letters. Using the pencil and paper. So the same hardware serving thousands of different jobs. The choice of letters are the instructions you can give, and paper is your microcontroller, whereas pencil is a device through which you transfer the idea in your mind, to the paper. Once transferred you do not need the pencil, to use the book, or notebook.

This example fits exactly on the scenario of microcontrollers and microprocessors. Thus you have to learn the instructions your particular microcontroller understands, and what those instructions order it to do. Then its your mind, and ideas how you play with these instructions to get your job done. Literally there are hundreds of methods to get the same job done. Just like in English, there many ways you can arrange the alphabets, to convey the same message.

Difference between a Microprocessor and Microcontroller

Essentially these two devices are similar, but with a little bit of difference. A CPU which is the heart of these devices needs a host of external devices to make it communicate with real-world. A typical system would need a system to read the inputs from keyboard, and write outputs to a terminal, store intermediate processing data into some memory, and to keep permanent information into some safe place. These devices which are independent circuits, work in harmony with the CPU, to make one system. In a typical Personal Computer these devices are attached to the CPU, using hard-wired connections. This makes the system more flexible, that means you can add more memory, change capacity of hard drives, add or remove CD-ROMs, sound cards etc.

A microcontroller on the other hand is made up of most of these devices built exactly within the same package. Your microcontroller will therefore contain, the CPU, RAM, ROM, Timers, I/O etc. all packed within one integrated circuit. This facilitates the development process, as well as reduce the requirements of external components, however this also means

you can not change, the number and type of integrated devices. The applications where a microcontroller will be used, vary. They are usually quite simple, and do not require as much processing power as a PC does, so the microcontrollers with varying amounts of RAM, ROM, I/O lines and timers etc have been made available. Essentially all are almost same, and they only vary in the number of resources available on them. So for a particular application you chose a microcontroller, not the one which has maximum resources, but the one which has just enough to do the job.

Thus a microcontroller is a complete, small scale computer with all the necessary devices on-board. All you need is the external hardware, which you want to drive, like sensors and motors etc.

Why there are to many different Microcontrollers?

Well after the idea of having a programmable device, many electronics manufacturers took the idea to develop their own chip. The internal architecture therefore differs among the manufacturers but from our point they are almost similar. Like there are so many different car manufacturers, Toyota, Suzuki, Honda, Mercedes and so on. Each one manufactures the cars with their own internal technologies, their engines, aerodynamics, peripherals all are different in specifications, yet if you can drive one car, chances are you will not find it difficult to drive another, is that not so. Despite being different in power, cylinders, valves, type of fuel etc, yet they have the same basic architecture and same basic theme.

So learning one microcontroller facilitates learning the other. Moreover the same company manufactures many different microcontrollers, which are all almost compatible. This is again like an automobile company. They make cars for many different types of users. Some bigger while others smaller. In addition to cars, they also manufacture other locomotives, like vans, truck and buses etc. All these have similar idea, but the nature of job they are required to do is different. Similarly in electronics the requirements of the project vary. For example to make a security device, you need little memory, whereas to make a data logger you need lots of memory. A remote control will not need to display data on LCD, so needs lesser number of I/O lines, whereas an industrial control unit will need to display its data, and therefore needs more I/O lines. A calculator needs only digital input, whereas a temperature controller needs to acquire analog data. These differences in requirements, makes the manufacturers produce different microcontrollers with different memory size, number of I/O lines and number of integrated peripheral devices. Otherwise they are all similar to use. Again, if you have mastered one, its easy to migrate to another. So the type of microcontroller to be used in a given project will be determined by the exact requirements.

How did Microcontrollers evolve?

The situation we find ourselves today in the field of microcontrollers had its beginnings in the development of technology of integrated circuits. This development has enabled to store hundreds of thousands of transistors into one chip. That was a precondition for manufacture of microprocessor and the first computers were made by adding external peripherals such as memory, input/output lines, timers and others to it. Further increasing of package density resulted in creating an integrated circuit which contained both processor and peripherals. That is how the first chip containing a microcomputer later known as a microcontroller was developed.

In the year 1969, a team of Japanese engineers from BUSICOM company came to the USA with a request that a few integrated circuits for calculators were to be designed according to their projects. The request was set to INTEL company and Marcian Hoff was in charge of the project there. Since having been experienced in working with a computer PDP8, he came to an idea to suggest fundamentally different solution instead of suggested design. That solution presumed that the operation of integrated circuit was to be determined by the program stored in the circuit itself. It meant that configuration would be simpler, but it would require far more memory than the project proposed by Japanese engineers. After a while, even though the Japanese engineers were trying to find an easier solution, Marcian’s idea won and the first microprocessor was born. A major help with turning an idea into a ready-to-use product, Intel got from Federico Faggin. Nine months after his arrival to Intel he succeeded in developing such a product from its original concept. In 1971 Intel obtained the right to sell this integrated circuit. Before that Intel bought the license from BUSICOM company which had no idea what a treasure it had. During that year, a microprocessor called the 4004 appeared on the market. That was the first 4-bit microprocessor with the speed of 6000 operations per second. Not long after that, American company CTC requested from Intel and Texas Instruments to manufacture 8-bit microprocessor to be applied in terminals. Even though CTC gave up this project at last, Intel and Texas Instruments kept working on the microprocessor and in April 1972

now digital signal processing microcontrollers.

The beauty of these devices is their easy availability, low cost and easy programming and handling. This has made PIC microcontrollers as the apple of hobbyists and students eyes.

We shall be talking about mid-range PIC microcontrollers, and use PIC18F452 as a prototype in this manual to explore them. Knowledge gained by learning and exploring one microcontroller is almost 90% applicable on other microcontrollers of the same family. The only difference is in availability of resources on different chips.

General Organization of PIC
Microcontrollers

Although we shall talk in detail on various aspects of these chips in relevant sections, here I would like to give a brief introduction on the overall business involved. Fig-2 shows the pin out details of a very popular 40-pin PIC microcontroller, PIC16F877. as you can see that each pin has been assigned a number of functions. Sometimes two and sometimes three. This situation is very common in microcontrollers, as there is always more which your microcontroller can offer, yet the number of pins on a given package is limited.

In a given circuit/application a pin is usually tied to a specific job, and all functionality of a pin is usually not required, however you make opt to use the specific pin your own way.

The specific function of a pin is selected by configuring various bits of internal registers. The number and names of these special function registers (SFRs) vary from device to device as some devices have limited functionality while others have more. Nevertheless if we are talking about a function which is present in both devices, its SFR will be same. The selection and settings of these SFR’s is the key to successful programming. It is therefore mandatory to go through the data sheets of the device before starting a project.

Second important thing to know is that the devices with same number of pins (from microchip®), are all pin-compatible. Which means if you design a project for 40 pin PIC microcontroller, and later want to replace the chip with another 40 pin PIC the pins are all compatible. It is also good to know that a pin labeled as lets say RB0 is located on pin 33 of PIC 16F877, but the same pin is available on pin 6 in 18 pin PIC16F628. the pins are functionally same, as long as their names are same. So if you develop a project while experimenting on 18F452 using pin RB0, after successful testing you want to transport the project to an 18 pin device, which also has RB0 on it, apart from pin number on package, and recompiling the program, you don have to bother much about anything else.

Power Supply

PIC microcontrollers use TTL logic, and therefore expect a well regulated 5V power supply. The supply may however range from 3.5V to 5.5V. These microcontrollers require very small amount of current. Indeed these devices have been labeled as nano-watt technology devices. The logical levels are also same, a signal from 0 to about 2V is considered as logical ‘0’ and a signal from 3.5V to 4.5V is considered as logical ‘1’. In order to communicate with devices using higher logical voltages, consider level conversion.

MCLR , Master Clear

On every PIC microcontroller you will find a pin labeled as MCLR. This pin has two basic functions. It is

Fig-2 Showing Pin Outs of PIC-16F877 Microcontroller

Fig-3 wiring MCLR Pin

used to reset the microcontroller, like soft-boot. As well as to put the microcontroller into programming mode. The MCLR pin when connected to ground, will reset the microcontroller, and keep it in reset state, till the ground connection is released. After that the microcontroller will have all its RAM reset, and program execution will begin, just like the system has been just powered on. A 10K pull up resistor is usually connected with the pin, to keep it high when reset switch is released.

The same pin will also work as program mode pin. When a new software is to be downloaded into the microchip, about 12V are applied to the MCLR pin, by your programming device. This can be done right in your circuit, or by taking the IC out of circuit and putting it into the IC socket on your programmer. We shall talk more about this in section on programming. The 10K resistor is then useful to avoid 12V reaching VCC and therefore to other devices.

Analog and Digital Data

Our microprocessors use digital data to represent everything. Even music, videos and images all are represented as digital data, which is a series of logical ‘0’ and ‘1’. However our real world data is not digital. It is rather analog. It is rightly said, “We live in an analog world, but process the data in digital world”. Real world data like light, temperature, pressure, heat, height, distance, speed, force etc. all are analog data. In order to utilize these data we have to acquire them with specific sensors or transducers and then convert into digital format for use within microprocessor’s digital world. Many other microcontrollers require an external ADC chip to implement this, however this feature has been nicely built into PIC microcontrollers. The number of Analog channels will vary among devices and some devices will not have this feature on-board. Pins labeled as AN0, AN1 etc are for analog data if required, however they can also function as normal digital pins to work with digital data. As previously said this selection is made by configuring specific registers in microcontroller.

BASIC CONCEPTS

Did you know that all people can be classified into one of 10 groups- those who are familiar with binary number system and those who are not familiar with it. You don’t understand? That means that you still belong to the later group. If you want to change your status read the following text. Text describing briefly some of the basic concepts used further in this book (just to be sure that we discuss the same issues).

World of numbers

Mathematics is such a good science! Everything is so logical and is as simple as that. The whole universe can be described with ten digits only. But, does it really have to be like that? Do we need exactly ten digits? Of course not, it is only a matter of habit. Remember the lessons from the school. For example, what does the number 764 mean: four units, six tens and seven hundreds. Simple! Could it be described in a bit more complicated way? Of course it could: 4 + 60 + 700. Even more complicated? Naturally: 41 + 610 + 7100. Could this number look a bit more “scientific”? The answer is yes: 410^0 + 610^1 + 710^2. What does it actually mean? Why do we use exactly these numbers: 100, 101 and 102? Why is it always about the number 10? That is because we use ten different digits (0, 1, 2, ... 8, 9). In other words, because we use base-10 number system, i.e. decimal number system. It is easier to work with decimal numbers, however computers can not do so, they use only two digits, 0 and 1. these are represented within a computer by presence or absence of volts on a specific line.

Binary number system

What would happen if only two digits would be used- 0 and 1? Or if we would not know to determine whether something is 3 or 5 times greater than something else? Or if we would be restricted when comparing two sizes, i.e. if we could only state that something exists (1) or does not exist (0)? Nothing

Fig. 4 Different methods of representing a decimal number

Number system conversion

Binary number system is the most commonly used and decimal system is the most understandable while hexadecimal system is somewhere between them. Therefore, it is very important to learn how to convert numbers from one number system to another, i.e. how to turn series of zeros and units into values understandable for us.

Binary to decimal number conversion

Digits in a binary number have different values depending on their position in that number. Additionally, each position can contain either 1 or 0 and its value may be easily determined by its position from the right. To make the conversion of a binary number to decimal it is necessary to multiply values with the corresponding digits (0 or1) and add all the results. The magic of binary to decimal number conversion works...You doubt? Look at the example:

110 = 12^2 + 12^1 + 0*2^0 = 6

It should be further noticed that for decimal numbers from 0 to 3 it is enough to have two binary digits. For greater values, new binary digits must be added. Thus, for numbers from 0 to 7 it is enough to have three digits, for numbers from 0 to 15- four digits etc. Simply speaking, the largest binary number consisting of n digits is obtained when the base 2 is raised by n. The result should be afterwards subtracted by 1. For example, if n=4:

2^4 - 1 = 16 - 1 = 15

Accordingly, using 4 binary digits it is possible to represent decimal numbers from 0 to 15, including these two digits, which amounts to 16 different values in total.

Hexadecimal to decimal number conversion

In order to make conversion of a hexadecimal number to decimal, each hexadecimal digit should be multiplied with the number 16 raised by it’s position value. For example:

Hexadecimal to binary number conversion

It is not necessary to perform any calculation in order to convert hexadecimal number to binary number system. Hexadecimal digits are simply replaced by the appropriate four binary digits. Since the maximal hexadecimal digit is equivalent to decimal number 15, it is needed to use four binary digits to represent one hexadecimal digit. For example:

Marking numbers

Hexadecimal number system is along with binary and decimal number systems considered to be the most important for us. It is easy to make conversion of any hexadecimal number to binary and it is also easy to remember it. However, these conversions as well as common use of different number systems may cause confusion. For example, what does the statement “It is necessary to count up 110 products on assembly line” actually mean? Depending on whether it is about binary, decimal or hexadecimal system, the result could be 6, 110 or 272 products, respectively! Accordingly, in order to avoid misunderstandings, different prefixes and suffixes are directly added to the numbers. The prefix $ or 0x as well as the suffix h marks the numbers in hexadecimal system. For example, hexadecimal number 10AF may look as follows $10AF, 0x10AF or 10AFh. Similarly, binary numbers usually get the suffix % or 0b, whereas decimal numbers get

Fig. 7 Showing Hexadecimal to decimal conversion

the suffix D. Commonly if no suffix is used the number is assumed to be decimal.

Bit

Theory says a bit is the basic unit of information...Let us neglect such a dry explanation for a moment and take a look at what it is in practice. The answer is- nothing special- a bit is a binary digit. Similar to decimal number system in which digits in a number do not have the same value ( for example digits in the number 444 are the same, but have different values), the “significance” of some bit depends on the position it has in binary number. Therefore, there is no point to talk about units, tens etc. Instead, here it is about zero bit (rightmost bit), first bit (second from the right) etc. In addition, since the binary system uses two digits only (0 and 1), the value of one bit can be 0 or 1.

Do not let you be confused if you find some bit has value 4, 16 or 64. It means that bit’s values are represented in decimal system. Simply, we have got so much accustomed to the usage of decimal numbers that these expressions became common. It would be correct to say for example, “the value of the sixth bit in binary number is equivalent to decimal number 64”. But we all are just humans and a habit does its own...Besides, how would it sound “number: one-onezero- one-zero...”

Byte

A byte or a program word consists of eight bits placed next to each other. If a bit is a digit, it is logical that bytes represent numbers. All mathematical operations can be performed upon them, like upon common decimal numbers. As It is case with digits of any other number, byte digits do not have the same significance. The largest value has the left-most bit called most significant bit (MSB). The right-most bit has the least value and is therefore called least significant bit (LSB). Since eight zeros and units of one byte can be combined in 256 different ways, the largest decimal number which can be represented by one byte is

255 (one combination represents zero).

Concerning terminology used in computer science, a concept of nibble should be clarified. Somewhere and somehow, this term referred to as half a byte came up. Depending on which half of the byte we are talking about (left or right), there are “high” and “low” nibbles.

Logic circuits

Have you ever wondered what electronics within some digital integrated circuit, microcontroller or processor look like? What do the circuits performing complicated mathematical operations and making decisions look like? Do you know that their seemingly complicated schematics comprise only a few different elements called “logic circuits” or “logic gates”?

The operation of these elements is based on the principles established by British mathematician George Boole in the middle of the 19th century- meaning before the first bulb was invented! In brief, the main idea was to express logical forms through algebraic functions. Such thinking was soon transformed into a practical product which far later evaluated in what today is known as AND, OR and NOT logic circuits. The principle of their operation is known as

Fig. 8 High and Low Nibbles of a Byte

Fig. 9 Logical AND gate with its Truth Table

inverter.

In a program, logic NOT operation is performed on one byte bits. The result is a byte with inverted bits. If byte bits are considered to be a number, inverted value is actually a complement of that number, i.e. The complement of a number is what is needed to add to it to make it reach the maximal 8 bit value (255).

EXCLUSIVE OR gate

This gate is a bit complicated comparing to other gates. It represents combination of all previously described gates. It is not simple to define mutual dependence of input and output, but we will anyway try to

do it. A logic one (1) appears on its output only in case the inputs have different logic states.

In a program, this operation is commonly used to compare two bytes. Subtraction may be used for the same purpose (if the result is 0, bytes are equal). The advantage of this logic operation is that there is no danger

to subtract larger number from smaller one.

Register

A register or a memory cell is an electronic circuit which can memorize the state of one byte. In other words, what is a byte theoretically, it is a register practically.

Special Function Registers (SFR registers)

In addition to the registers which do not have any special and predetermined function, every microcontroller has also a number of registers whose function is predetermined by the manufacturer. Their bits are connected (literally) to internal circuits such as timers, A/D converter, oscillators and others, which means that they are directly in command of the operation of the microcontroller. If you imagine that as eight switches which are in command of some smaller circuit within the microcontroller- you are right! SFRs do exactly that!

Input / Output ports

In order that the microcontroller is of any use, it has to be connected to additional electronics, i.e. peripherals. For that reason, each microcontroller has one or more registers (called “port” in this case) connected to the microcontroller pins. Why input/output? Because you can change the pin’s function as you wish. For example, suppose you want your device to turn on and off three signal LEDs and simultaneously monitor logic state of five sensors or push buttons. In accordance with that, some of ports should be configured so that there are three outputs (connected to LEDs) and five inputs (connected to sensors). It is simply performed by software, which means that pin’s function can be changed during operation.

One of more important feature of I/O pins is maximal current they can give/get. For the most microcontrollers, current obtained from one pin is sufficient to activate a LED or other similar low-current consumer (10-20 mA). If the microcontroller has many I/O pins, then maximal current of one pin is lower. Simply, you cannot expect all pins to give maximal current if there are more than 80 of them on one microcontroller.

Another important pin feature is to (or not to) have pull-up resistors. These resistors connect pin to positive power supply voltage and their effect is visible when the pin is configured as input connected to mechanical switch or push button. The later versions of the microcontrollers have pull-up resistors connected to and disconnected from the pins by software.

Usually, each I/O port is under control of another SFR, which means that each bit of that register determines state of the corresponding microcontroller pin. For example, by writing logic one (1) to one bit of that control register SFR, the appropriate port pin is automatically configured as input. It means that voltage brought to that pin can be read as logic 0 or 1. Otherwise, by writing zero to the SFR, the appropriate port pin is configured as output. Its voltage (0V or 5V) corresponds to the state of the appropriate bit of the port register.

Memory unit

Memory is part of the microcontroller used for data storage. The easiest way to explain it is to compare it with a big closet with many drawers. Suppose, the drawers are clearly marked so that it is easy to access any of them. It is enough to know the drawer’s mark

event takes place, the microcontroller does something... For example, when you push a button on remote controller, the microcontroller will register it and respond to the order by changing a channel, turn the volume up or down etc. The bottom line is that the microcontroller spends the most of its time in endlessly checking a few buttons- for hours, days... It’s not practical, is it?

Because of and similar situations, the microcontroller has learned during its evolution a trick. Instead of checking each pin or bit constantly, the microcontroller has left the “wait issue” to the “specialist” which will react only in case something worth attention happens.

Signal which inform the central processor about such event is called an INTERRUPT.

Central Processor Unit - CPU

As its name indicates, this is a unit which monitors and controls all processes inside the microcontroller. It consists of several smaller units, of which the most important are:

  • Instruction Decoder is a part of electronics which recognizes program instructions and runs other circuits on the basis of that. The “instruction set” which is different for each microcontroller family expresses the abilities of this circuit.
  • Arithmetical Logical Unit (ALU) performs all mathematical and logical operations upon data.
  • Accumulator is a SFR closely related to the operation of ALU. It is a kind of working desk used for storing all data upon which some operation should be performed (addition, shift/move etc.). It also stores results ready for use in further processing. One of SFRs, called Status Register (PSW), is closely related to the accumulator. It shows at any moment the “status” of a number stored in the accumulator (number is greater or less than zero etc.).

Bus

Physically, the bus consists of 8, 16 or more wires. There are two types of buses: address and data bus. The first one consists of as many lines as necessary for memory addressing. It is used to transmit address from CPU to memory. The later one is as wide as data, in our case it is 8 bits or wires wide. It is used to connect all circuits inside the microcontroller.

Serial communication

Connection between the microcontroller and peripherals via input/output ports is the ideal solution for shorter distances, up to several meters. However, in other cases - when it is necessary to establish communication between two devices on longer distances or when for some other reason it is not possible to use parallel connection - such a simple solution is out of question. In those and similar situations, serial communication is the solution imposing itself.

Today, most microcontrollers have built in several different systems for serial communication as a standard equipment. Which of these systems will be used in the very case depends on many factors of which the most important are:

  • How many devices the microcontroller has to exchange data with?
  • How fast the data exchange has to be?
  • What is the distance between devices?
  • Is it necessary to send and receive data simultaneously?

One of the most important thing concerning serial communication is the Protocol which should be strictly observed. It is a set of rules which must be applied in order the devices can correctly interpret data they mutually exchange. Fortunately, the microcontrollers automatically take care of that, so the work of the programmer/user is reduced to simple write (data to be sent) and read (received data).

Baud Rate

The term Baud rate is commonly used to denote the number of bits transferred per second [bps].

It should be noted that it refers to bits, not bytes! It is usually required by the protocol that each byte is transferred along with several control bits. It means that one byte in serial data stream may consist of 11 bits. For example, if the baud rate is 300 bps then maximum 37 and minimum 27 bytes may be transferred per second, which depends on type of connection and protocol in use.

The most commonly used serial communication systems are:

I2C Protocol (Two wire System)

I2C (Inter Integrated Circuit) is a system used when the distance between the microcontrollers is short and specialized integrated circuits of a new generation (receiver and transmitter are usually on the same printed circuit board). Connection is established via two conductors- one is used for data transfer whereas another is used for synchronization (clock signal). As seen in figure, in such connection, one device is always master. It performs addressing of one slave chip (subordinated) before communication starts. In this way one microcontroller can communicate with 112 different devices. Baud rate is usually 100 Kb.sec (standard mode) or 10 Kb/sec (slow baud rate mode). Systems with the baud rate of 3.4 Mb/sec have recently appeared. The distance between devices which communicate via an inter-integrated circuit bus is limited to several meters.

SPI (Three Wire Serial - Parallel Interface)

SPI (Serial Peripheral Interface Bus) is a system for serial communication which uses four conductors (usually three)- one for data receiving, one for data sending, one for synchronization and one (alternatively) for selecting device to communicate with. It is full duplex connection, which means that data are sent and received simultaneously. Maximal baud rate is higher than in I2C connection.

UART (Universal Asynchronous Receiver/Transmitter)

As seen from the name itself, this connection is asynchronous, which means that a special line for clock signal transmission is not used. In some situations this feature is crucial (for example, radio connection or infrared waves remote control). Since only one communication line is used, both receiver and transmitter operate at the same predefined rate in order to maintain necessary synchronization. This is a very simple