What is a Microcontrollers?






The Wikipedia defines a Microcontroller as follows :

A microcontroller (or MCU for microcontroller unit) is a small computer on a single integrated circuit.

 

The IoTAgenda defines a Microcontroller as follows :

A microcontroller is a compact microcomputer designed to govern the operation of embedded systems in motor vehicles, robots, office machines, complex medical devices, mobile radio transceivers, vending machines, home appliances, and various other devices. A typical microcontroller includes a processor, memory, and peripherals.

Following are some of the examples of Microcontrollers. See if you can draw any intuitive image in your brain about Microcontroller. (Try search more examples with web search on your own).


 


 


 



Embedded system

Before we talk about 'Embedded System', let's think of what the word 'embedded' mean. 'Embedded' mean 'Attached to something' or more accurately 'Attached deep inside to something'.

Now you may easily guess what is Embedded System. According to the meaning of the word 'Embedded', you can define 'Embedded System' as 'a system that is attached deep inside to something'.

Now you may ask what kind of system are you talking about ? what is the 'something' ?

The system in this definition usually mean a kind of small computer. We usually call this small computer as 'Microcontroller'. The 'something' usually mean many different things like Washing machine, Copy Machine, ATM, Elevator, Drone even a car etc. Actually almost every thing that is driven by electricity has a small computer (Microcontroller) in it. So you can say almost every electronic appliances you see these days has one or more embedded system in it.

Just to give you an intuitive feeling about an Embedded System, let me give you a comparative image as shown below. The biggest difference would be.

  • Usually a Microcontroller is used in Embedded System, whereas general purpose CPU is used in General Purpose PC
  • In most cases, a lot of sensors and actuators (e.g, Motors) are connected and controlled by the processor
  • You would notice some other differences in the devices attached to the embedded system and general purpose CPU.

Followings are a couple of examples of electronic appliances (machines) that has embedded system(s) in it. I hope you can get some intuitive understanding from these examples.



 

 



Then you may ask 'Can we take a PC / Laptop as an embedded system ?' We don't call a PC/Laptop as an embedded system, mainly because a PC/Laptop is a computer as a whole system. It is not something attached (embedded) to some other things. Also, the controller (CPU) in the PC/Laptop is too big/high performance to be called as Microcontroller.

 

 

 



 

 

 

 



Mobile phone used to be the most typical example of Embedded System, but as it evolves into Smartphone.. it would become more like general purpose PC. However, mobile phone is one of the biggest application of Embedded System.

 

 

Probably one of the biggest area of the Embedded System in near future will be Internet of Things (IoT) as illustrated below. Basic idea of IoT is to connect every objects (Things) to each other. It means all of these objects should have an embedded system with wired or wireless communication module.


Microcontroller architecture

What is Architecture ? You might have heard often when people are talking about building. But, Of course, I am not anything about designing a building here. Then what does it mean here ?

The literal meaning of Architecture is 'Structure of Anything'. That's it, the term Architecuture is a fancy expression for 'Structure'. So if you heard of 'Architecture of an Operating System', it simply mean 'Structure of an Operating System'. If you say 'Architecture of a Microcontroller', it means 'Structure of a Microcontroller'.

Even though the definition sound simple, understanding the real meaning of an Architecture of Anything is not that simple because the description of Architecture of something sound (or looks) very abstract (not tangible). If you just see the documentation of something (e.g, documents of a microcontroller), you would just see a bunch of diagram with a lot of boring / dry description. So those document would not give you much concrete understanding. However, if you revisit the document/diagrams over and over while you are doing some hands on with those microcontroller, you would start making more and more sense out of those abstract documents.

Whatever Micontroller you get, you would find a bunch of Architecture diagram of the chip almost at the beginning of the technical document of the chip. For some people, those diagrams would look like 'a bunch of retangles connected each other by messy arrows' but for some people it would give you almost full details of the microcontroller'. The more you have practical experience with the chipset, more meaning would show up from the diagram. So, your goal is to try to reach such a status where you can get the most of the chipset just by looking at a couple of architecture diagram.

Examples of Microcontroller Architecture
  • PIC Architecture
  • AVR Architecture
  • STM32 (Texas Instrument)
  • 8051 Architecture
  • Quark (Intel) Architecture
  • LPC 1768 (NXP)
  • Z8 Architecture
Before I buy any Embedded board or chipset, I usually search for datasheet or user manual for the microcontroller being used on the board and check the architecture diagram first. I would try to find answers to following questions from the architecture diagram and one/two pages of feature summary in the datasheet.
  • How many GPIO ports are available ?
  • How many Digital I/O pins are available ?
  • How many Analog I/O pins are available ?
  • What kind of communication interface are available ? (I2C, SPI, UART, CAN, Ethernet etc)
  • What is I/O voltage for GPIO pins ? (5V ? or 3.3 V?) // You should see the datasheet for this kind of information
  • What is max current for GPIO pins ? // you would need to search through a little further in the document
  • What is driving voltage for the communication interface ?

 

Examples of Microcontroller Architecture


 

I will put down example architecture of some of the well known microcontrollers. As I mentioned above, if you haven't played with any of microcontroller on your own, these diagram would not make much sense to you. However, you will gradually get more intuition from these as you play with real hardware. You can find this kind of Architecture diagram for almost any chipset from the datasheet (or user manual) of the chipset (Microcontroller). The original document from which these diagram came from are linked under Reference section.

As you see here, different manufacturers provide a little bit different level of details in the architecture diagram. So in many cases, you would not be able to get full details from the architecture diagram only. However, it is always good idea to take a quick look at this high level diagram and look into the manufacturer datasheet when you need to get more detailed information.

PIC Architecture

 

 

AVR Architecture

 

 

STM 32 (STM32 F20x) Architecture

 




 Quark Architecture 

 


 

8051 Architecture

 

 

LPC 1768 Architecture

 


 

Z8 Architecture

 



Harvard vs Von Neuman



CISC vs RISC

CISC stands for Complex Instruction Set Computer and RISC stands for Reduced Instruction Set Computer. This is what all the text book says. Sounds simple ? yes.. but understanding the real meaning would not be simple as it sound.

If you see the two words 'ISC (Instruction Set Computer)' is common to both CISC and RISC. If I assume that you know (or vaguely know) what Instruciton Set is, the real difference between CISC and RISC lies in the words 'Complex' and 'Reduced'. What does it by 'Comlex' ? and what does it mean by 'Reduced' ? This is not what you can find easily in text book. Even though they describe on 'Complex' and 'Reduced' in text book, it sounded too dry and abstract, it didn't make much sense to me.

The purpose of this page is to help you understand the meaning of 'Complex' and 'Reduced' in my words and I hope this would make more sense to you than textbook.

Before we look into CISC and RISC separately, let's think about the general logic of computations common to any architecture.

Step 1 : Read Operands (Value) from memory and move it to specified registers (If the operation needs
multiple values, this step can split into multiple sub steps)

Step 2 : Apply the specified operation to the values in the registers

Step 3 : store the result of the operation to a specified memory location

Purely logical point of view, this basic logic applies to both CISC and RISC. However, when we convert this into program (Assembly code, Instruction Set), the resulting instruciton gets different between CISC and RISC.

Let's take an example of the simple addition operation 'a = a + b'. The instruction for this operation would be implemented as follows.




Both codes shown above should produce the same result that performes Step 1, 2, 3 described above. However as you see here in CISC only one instruction do the job whereas it requires 4 instructions to do the same job.

Now you may feel a little bit strange. 'CISC' implies 'Complex' and 'RISC' sound like 'Simple' because the term RISC is used in contrast with the term CISC. But the program code in CISC obviously look simpler than the code in RISC. This would be confusing (at least it was very confusing to me). This is because the meaning of 'Complex' is not clearly defined. When we say 'Complex', it doesn't mean that CISC is using complex programming sequence to perform a operation. It mean 'One Instruction' in CISC can perform 'Complex functions'. As in this example, in CISC only one instruction 'ADD' perform all the procedures (Step 1, 2, 3) required to achieve a = a + b whereas in RISC four instractuion is required to do the same job. Because of this, the program in CISC system generally requires less amount of program memory. This used to be a great advantage of CISC when the memory was expensive resources in old days. However, memory has not become such a expesive resources any more now. So motivation for CISC has gone much weaker.

Why weaker motivation ? Even though the size of programming memory is not a limitting factor, isn't it still good to use CISC because programming seems to be much simpler ?

If you are programming in Assembly, I think CISC programming can be easier (less tedius) than in RISC, but if you are programming in high level language like C / C++, the simplicity in assembly code would not be such an important attraction because it is the compiler's job (not your job) to generate the assembly code.

There is another drawback in CISC. Have you thought of how CISC can perform such a complex job with single instruction ?

I am not a CPU designer who can explain this with full experties.. but what I can say is that 'the complex functionality with single instruction' is implemented by hardware (basically more transistors at the bottom). It implies that the logic circuit to execute instructions in CISC tend to be more complicated than the one in RISC.

Another fundamental difference between CISC and RISC is depicted below. As you see here, the machine code instruction is relatively long and the length varies depending on instructions in CISC, but in RISC the machine code is relatively short and the length of the instruction is same for all instruction. Longer instruction implies that it may take multiple clock cycle to execute single instruction in CISC. The shorter/simpler instruction implies that it would take only one cycle to execute an instruction in RISC. Actually, RISC is designed in such a way to guarantee to execute single instruction in single cycle. Also, same instruction length for all instruction in RISC implies that every instruction can be executed within the same length of cycle.

 

There are several other differences between CISC and RISC, but I think most of those additional differences are based on the fundamental differences described above. These differences can be summarized as follows.


ARM

This page for now is mainly for gathering various materials for ARM core history, architecture. I will try to write something on my own once I have digested all of these materials. Until then, I would suggest you to go through the videos and documents that I will put under Reference section.

 

Memory Map


It is a kind of Map for a memory (mainly for the memory that is used for programmng). From this map, you can easily get the information like what is the maximum memory space and which part (which address) is associated with what kind of MCU functionality etc.


Putting it in very short, Embedded programming is mainly a sequence of writing specific values to a specific memory location or specific retisters and read values from the memory or registers. Actually this can applies not only to embedded programming but also for programming large scale CPU like the CPU in the PC. However, in large scale application like PC that is operated by high level operating system like Windows / Linux. This kind of memory manipulation is not visible to general programmer. Most of those operation is done by the high level programming tool like Visuall C, C++ or VB etc and all the detailed memory operation is done by underlying operating system. In other words, you as a programmer don't have to care of those detailed process. However in embedded system you need to controll all of those memory operation on your own. Of course, it would be tedius and confusing / frustrating, however you can have full control of those operation. As you get into the embedded system, understanding the memory map and utilizing the map for your needs would be the first obstacle to overcome.

Do we have any easy guide to understand the memory map as we use GPS Navigation system ? Unfortunately No. You have to find where to go in a memory map just by looking at a couple of block diagram and a lot of boring description.

Let's first see how the memory map is given to us. Following is an example of memory map for a microcontroller called 80C196KC. Don't be scared if this does not make any clear sense to you. Just have some general feeling about how a memory map look like.

 

< Memory map of 80C196KC >

 

In some case, the memory map is given in a tabular form as below.


 

Register (General Purpose Register, Special Function Register)


Simply put, a Register is a kind of memory. Then what is it so special to have different name for it. It is because they are special memory which is different from what we normally call a Memory (e.g, Flash RAM, Program Memory etc). Usually Register is located within a CPU or MCU (Microcontroller) very near to Arithematic unit or directly connected to various I/O ports. Due to this location or special design, Register is much faster in terms of Read / Write speed comparing to other types of memory. Among the register there are roughly two groups of Registers. One is mostly used for storing (or assigning) a specific values that are used by ALU (Arithmatic Logic Unit). These registers are called General Purpose Registers. The other type of the registers is mostly used to control the various I/O pins or to read / write to those I/O pins. The one related to I/O pins are generally called I/O registers or Special Function Registers(SFR). Every CPUs and MCUs have various Registers in it but we don't usually care much of these registers when we are working on high level application programming. However, if you are an embedded system engineer working with Microcontrollers, you should be very familiar with the details of those Registers for the specific MCU (Microcontroller) that is used on your embedded board.

Then you may ask 'where/how I can get the register information about the MCU / CPU that is on my embedded Board ?'. The first document you need to see is the data sheet of the chipset. For example, if you are using Arduino board, the board is using the MCU called ATmega family. If you open up the ATmeaga datasheet you would be able to find a diagrams as shown below (I combined three different diagrams from the document to create this picture). The parts high lighted blue and red are all a kind of memory. The parts highlighted red are registers. Whenever you want to programm on a certain microcontroller, this kind of diagram would be the first one you have to visualize in your brain. When you really write a program, just this kind of big picture will not be enough. You have to read the details of each and every of these registers in the datasheet.

 

< Block Diagram of the AVR Architecture >



What does Register mean to Embedded System ?
Whenever I get a new Embedded Board or try to select a microcontroller, I always try to look up two things first. One is Memory Map and the other one is Register information. I also check how well they are documented in manufacturers datasheet or user manual. If these two items are poorly documented, I would rather not pick up those board or chipset. Why I care so much about these, especially about Registers. Personally to me, Register means
  • Registers are almost everything to Embedded System
  • Registers are windows (channel) to directly talk to hardware
  • Registers are kind of global variables for hardware to check / read / write for its operation
  • Understanding a Microcontroller is understanding all the registers in it

I/O Register

The GPIO on ATmega chipset on Arduno board is controlled by a couple of Registers called I/O register (if you don't know what the register is, read Register page in Embedded System section first). It is one of the most important things that you need to know because almost every Arduino programming is to operate one or more of these I/O pins. It means regardless of whether you recognize or not, almost all of your Arduino program is manipulating one or more of these registers.

If you are intersted in writing Arduino program in C or Assembly,  you need to very clearly understand the relationship between these registers and the I/O pins connected to these registers.

If you want to know how much you already know of these registers and its operations, take a look at the section Atmega 328 GPIO Memory Map/ Register Description and ask your self 'can I write a C or Assembly program to set a specific I/O port just based on the table ?' If the answer is Yes, you don't have to ready this page any more.. if the answer is 'No', this page would be helpful for you to get some practical understanding on Arduino I/O registers.

If you look at the AVR Architecture (ATmega processor) diagram, you would find three I/O ports. Each of these I/O ports has 8 GPIO pins. How to you manipulate these GPIO. To figure out how to program these GPIO, you need to figure out what kind of I/O registers are controlling each of these I/O pins. In AVR microprocessor, each of these ports are connected to three different I/O registers called DDRx, PORTx, PINx. And then, each I/O pins within a port is mapped to each individual bit of the I/O registers. These mappings can be illustrated as shown below (It took me while to draw all of these wires in power point :).

 

 



 

Now let me explain the functionality of each I/O Registers. The functionality of each I/O registers is as follows :

    DDRx : DDR stands for Data Direction Register. 'x' is just a place holder for a chacter like B, C, D port. So the register name of each Port become DDRB, DDRC, DDRD. This register specifies the direction of each I/O pins connected to it. For example, if you set a specific bit in the register to be '1', the pin connected to the bit become a Output pin and if you set a specific bit in the register to be '0', the pin connected to the bit become a Input pin.

    PORTx : Each bit in this register specifies the output voltage of the pin connected to the bit.  'x' is just a place holder for a chacter like B, C, D port. So the register name of each Port become PORTB, PORTC, PORTD.  If you set a specific bit in the register to be '1', the output voltage of the pin connected to the bit become +5 V (i.e, logical value '1') and If you set a specific bit in the register to be '1', the output voltage of the pin connected to the bit become 0 V (i.e, logical value '0'). Since this defines the output votage (value) of a pin, this value become meaningful only when DDR bit for the pin is set to be '1'.

    PINx : This register is for reading the value for a specific pin connected to the register.

 

Now I think (hope) you get some general idea on how Arduino I/O Register works. For more concrete understaing on how this principle is applied in programming, let me give you a short example program written in Assembly. Don't worry if you don't know anything about Assembly, this is very short and I will put the comments for each line.

 

.equ    PORTB, 0x05 // this is like #define PORTB 0x05.This is the address of the register PORTB

.equ    DDRB, 0x04  // this is like #define DDRB  0x04.This is the address of the register DDRB

 

.org 0x0000

    jmp main

 

main:

    ldi r16,0b00100000  // this is like an assignment int r16 = 0b00100000 ('0b' mean 'binary')

    out DDRB, r16       // this mean write the value r16(0b00100000) into DDRB register

    out PORTB, r16      // this mean write the value r16(0b00100000) into PORTB register

 

// The processor keep running these two lines. It become as if the program stand still at this point

Start:    

    rjmp Start

 

 

Can you guess what would happen when you run the code above ? The result is as follows. Pin 5 in Port B is outputting 5 V( logic '1') as illustrated below.

 

Interrupt

If you first get into the world of computer (or microcontroller) as programmer, one of the words that you hear most often but very confusing would the word 'interrupt'. It would be even more frustrating because everybody knows what 'interrupt' means in our daily talk, but can't make any clear sense when they are talking in computer things.

Let's first think of what 'interrupt' means in our daily talk. Followings are two definition from dictionary.

Stop something from happening for a short period (http://dictionary.cambridge.org/dictionary/english/interrupt)

Stop the continuous progress of (an activity or process) (https://en.oxforddictionaries.com/definition/interrupt)

Actually the term 'Interrupt' in computer (Microcontroller) is not so much different from those definitions listed above.

Just adding and tweaking a couple of words from the above definitions would give pretty good definition on 'Interrupt' in computer / microcontroller.

Interrupt is a singnal (usually hardware signal) or event to stop 'CPU (microcontroller)' from doing current process for a short time.

Do you realized what is the biggest difference between the Interrupt in computer and the interrupt in our daily talk ?

Interrupt in our daily talk usually used as a 'verb', but Interrupt in computer is used as a 'noun'.

Let me add a couple of words to make the definition more practical in terms of computer / microcontroller programming.

Interrupt is a singnal (usually hardware signal) or event to stop 'CPU (microcontroller)' from doing current process for a short time and execute a short/predefined procedure.

Now what you have to figure out is 'What is the singnal (usually hardware signal) or event ?'. In case of microcontroller, there are usually one or more pins specially assinged to handle this kind of signal. Any signal coming into this pin will function as 'Interrupt'. If you connect a small button to the special pin, an interrupt will be generated whenever you hit the button. If you connect a keypad to the pin, an interrupt will be raised whenever you press a key on the pad. In some microcontroller, some event (e.g, the expiration of a specified Timer) can generate interrupt. As you see here, interrupt generation mechnically is mostly determined by chipset design. 'How many interrupt are supported ? or what kind of interrupts are supported ? ' are one of the most common / important factors for many embedded application. So you may find these information at the first page of the CPU / Microcontroller datasheet.

Now let's think of 'execute a short/predefined procedure'. In microcontroller (CPU), it is designed to execute a specific procedure when the interrupt happens. This specific procedure is called 'Interrupt Vector' or 'Intterrupt Handler' and most of the interrupt vector is supposed to written by user (the programmer).

GPIO

In Embedded World, you would hear a lot about GPIO. What is a GPIO ?

You may easily guess 'IO' stands for 'Input / Output', but not sure of what 'GP' mean. GP stands for General Purpose. So, GPIO stands for General Purpose Input / Output.

You would see many pins on a Microcontroller that can be used as Input pin or Output pin. If you are a chipset designer, it would be simpler to design each of the pins to be used only for single purpose. For example, pin 0 is only for input, pin 1 is only for output, pin 3,4 are only for I2C. However, in this case you would need huge number of pins on the microcontroller to provide such a diverse in / out functionalities that is required in most of the application.

I think the concept of GPIO is invented to handle this kind of situation. It is to provide more I/O functionality with a small number of pins.

How ?

The fundamental Idea behind GPIO is to provide a kind of 'programming' capability for the pin. In reality, they put one or more registers to a GPIO pin and let a pin to perform different functionality depending on the value stored to the register. In this way, a single GPIO pin can be Input or Output or Serial Communication pin depending on the configuration.

Following is the control (configuration) circuit for the digital GPIO pin on ATmega328 (Microcontroller on Arduino). As you see here, one GPIO pin can be configured for various functions via the pins shaded in blue. These control pins are connected to a few special registers that can be set or read by program.

 

Let me give you a couple of examples of GPIO. Following example shows GPIOs on ATmega Microcontroller (Left) and pin connection on Arduino board. You don't have to know the details of each of these GPIOs unless you are working on Arduino board right now.  Just note that most of the pin on chipset has multiple functions as underlined in Red, Blue, Orange. When you using these pins, you need to decide how (what purpose) you want to use it and configure in your program to make the pin work as you intended.

 

Microcontroller ICs usually include GPIOs. Depending on the application, a microcontroller's GPIOs may comprise its primary interface to external circuitry or they may be just one type of I/O used among several, such as analog signal I/O, counter/timer, and serial communication.


GPIO interfaces vary widely. In some cases, they are simple, a group of pins that can switch as a group to either input or output. In others, each pin can be set up to accept or source different logic voltages, with configurable drive strengths and pull ups/downs. Input and output voltages are usually, but not always, limited to the supply voltage of the device with the GPIOs, and may be damaged by greater voltages.

A GPIO pin's state may be exposed to the software developer through one of a number of different interfaces, such as a memory-mapped I/O peripheral, or through dedicated IO port instructions. Some GPIOs have 5 V tolerant inputs: even when the device has a low supply voltage (such as 2 V), the device can accept 5 V without damage.

A GPIO port is a group of GPIO pins (often 8 pins, but it may be less) arranged in a group and controlled as a group.

GPIO abilities may include:
  • GPIO pins can be configured to be input or output
  • GPIO pins can be enabled/disabled
  • Input values are readable (usually high or low)
  • Output values are writable/readable
  • Input values can often be used as IRQs (usually for wakeup events)

I²C

I2C or I2C came from the term Inter Integrated Circuit. You see two 'I's and one 'C' here. That's how 'I2' or 'I2' came from. Then, what does it mean ? What does it do ?

Since I2C is implemented by only Two Wires (SDA, SCL), it is also called TWI (Two Wire Interface).

  • Connections in I2C network
  • How a Master can send data to a specific Slave ?
  • Simplest form of communication between Master and Slave
  • What is the data rate of I2C ?
  • How far they can communicate over I2C ?
  • Which one to choose : I2C or SPI ?

 

Connections in I2C network

 

It is a kind of serial communication technology which is originally designed for exchanging data between multiple IC chips. This is very simple communication mechanism and I think very smartly designed. It requires only two lines as shown below. One of the line is used for sending Clock signal and the other line is to send/receive data. Also, these two lines can be shared by multiple devices. This communication works as Master-Slave mode. One Master can control / communicate multiple Slaves.

 


Another good (may be confusing sometimes) there can be multiple Master on the line as shown below.

 



 

Even though I said you need only two lines in I2C, in real application you would need a couple of more lines because we need to supply Power to each of the connected chipset and common ground as shown below. Because of this, you would see 4 wires attached to a device (small I2C modules) that use I2C.

 


 

How a Master can send data to a specific Slave ?

 

Then your question is how a Master can send to /control data to a specific Slave. I think you can easily guess how. Each of the Slave device should have a unique address and Master specifies a unique slave address when it sends data or command as shown below.

 

<I2C Specification - Rev .6 - Fig 11. A master-transmitter addressing a slave receiver with a 7-bit address >


Simplest form of communication between Master and Slave


What is the data rate of I2C ?

 

You may easily guess that I2C data rate may vary since it has its own clock line. Theoretically, you can achieve different data rate by changing clock rate. However, I2C is also a kind of hardware that is limited by various physical factors. So the data rate of I2C would be within a certain range.

Typical clock rate of I2C is a few hundred KHz. Since I2C data line works only two level (0 or 1), the data rate would also be a few hundred Kbps (should be a little bit lower than Clock cycle).

 

 

How far they can communicate over I2C ?

 

I haven't found any specific answer from I2C specification, but I have read through many pages on the web stating various numbers based on experience.

As it's name Inter Integrated Circuit, it was originally designed for communication between multiple ICs being placed not far away from each other. So I think it would be safe to say 'less than 1 m' as communication distance, but you would see many people states that they made it work over several meters or several tens of meters.

 

 

Which one to choose : I2C or SPI ?

 

In most of the Embedded Board, you would find both I2C and SPI pins and then you would ask yourself which one to use.

The answer would be given partly by personal preference and partly by the nature of the application you want to implement. I think most important criteria you may think of would be as follows

  • Data Rate : If you need a communication system supporting in Mhz range clock (similar data rate), you would go with SPI
  • Complexity : If you don't need very high data rate (well under a Mhz) and want to get very simple technique (or less wiring), you would go with I2C.
  • Number of Slaves : Even though it is doable to control multiple Slaves with SPI, it would not be doable as easy as I2C

SPI

SPI stands for Serial Peripheral Interface. As the name implies, this is mainly for communicating with other device (chipset or module) using a kind of serial communication.  Most common use case for this technology is connecting one master to one slave module as illustrated below. Like I2C, it uses a common clock called SCLK, but unlike I2C it uses two separate data lines for input and output. And it has another pin called SS which does not exist at all in I2C.

 


 

Even though SPI is designed mainly for Single Master and Single Server, it is doable to use it in single master and multiple slave as in BUS as illustrated below. However, the wiring gets much more complicated in this case.

 



Common Applications of SPI

 

Most common applications of SPI that I have found through surfing many pages on the web is to interface a board or Microprocessor to external memory. Since this kind of application would need relatively high data rate, SPI would be more suitable than I2C.

 

 

How far they can communicate over SPI ?

 

I haven't found any explicit answer from any formal document, but I have read through many pages on the web stating various numbers based on experience. Most common number seems to be around a couple of meters. It seems to me that the number seems to be smaller than I2C max distance.

 

Which one to choose : I2C or SPI ?

 

In most of the Embedded Board, you would find both I2C and SPI pins and then you would ask yourself which one to use.

The answer would be given partly by personal preference and partly by the nature of the application you want to implement. I think most important criteria you may think of would be as follows

  • Data Rate : If you need a communication system supporting in Mhz range clock (similar data rate), you would go with SPI
  • Complexity : If you don't need very high data rate (well under a Mhz) and want to get very simple technique (or less wiring), you would go with I2C.
  • Number of Slaves : Even though it is doable to control multiple Slaves with SPI, it would not be doable as easy as I2C

CAN

CAN stands for Control Area Network. Many people call it as 'CAN BUS'. It is originally developed by BOSCH.

 

Just from these three statements you may get pretty good insight without reading anything further. From the word 'Network' or 'BUS', you would guess overall architecture of CAN. It implies that CAN is a kind of common communication channel that can connect multiple devices simultaneously. From the fact that it is first developed by BOSCH, you may easily guess that the major application of CAN would be Automotives.

 

Overall CAN architecture looks as follows. As you see here a lot of devices (Nodes) shares a common communication lines (BUS) and it forms a kind of network. Like I2C, the common data line is made up of only two lines (Of course, in real CAN system you will see many more lines since it need power line, ground line etc. This is also same as in I2C). Unlike I2C, both lines of the bus is for data (it is for the same data represented by two different signal level) whereas in I2C only one line is for data and the other line is for clock signal).  

You may notice another difference from I2C from this picture. There is no Master and no Slave in CAN. It means every Node can transmit the data at the same time.

I know what you think now.. what if multiple nodes transmit the data exactly at the same time and those signal colide each other ?

Good Question. There should be some mechanism that can prevent this kind of collision happening. You would learn about this later.

 


 

Basic Features

 

Follwoings are the list of basic features of CAN (To me, the most important characteristics comparing to other types of communication method is that it provides very robust Error checking method, meaning high reliability. Also comparing to I2C, SPI, Serial, it can cover very long distance)

  • Two different versions of CAN. One is v2.0 Original specification by BOSCH im 1980 and the other one is CAN FD (CAN Flexible Data) which is specified in ISO 11898-2
  • Uses a single terminated twisted pair cable
  • Maximum Signal frequency used is 1 Mbit/sec (CAN 2.0) , 15 Mbits/sec (CANFD)
  • Length depends on the bit rate.
    • Typical values encountered in the field for CAN 2.0 are
      • 1 Mbit/s 40 m
      • 500 kbit/s 110 m
      • 250 kbit/s 240 m
      • 125 kbit/s 500 m
      • 50 kbit/s 1.3 km
      • 20 kbit/s 3.3 km
      • 10 kbit/s 6.6 km
      • 5 kbit/s 130 km
    • Typical max data rate for CAN FD
      • 3.7 Mbits/sec
  • Has high reliability with extensive error checking
  • Typical maximum data rate achievable is 320 KBites/sec for CAN 2.0 and 3.7

 

 

Frame Structure

 

Frame Structure of CAN is as follows. One thing that I noticed was there is no 'address' field. What does this mean ? It implies that in CAN the transmitted frame is no specific target. It is for every node(device) connected to the network (BUS). In other words, CAN is basically for broadcasting data. Every device in the network recieves the same data and it is up to the reciever whether use the data or just discard it.

 



Why CAN ?

The biggest motivation for CAN is to remove all those spaghetti-like (messy) and bulky wires connecting multiple devices within a car. Simply put, it can be illustrated as below.

 



PWM

PWM stands for Pulse Width Modulation. 'Modulation' means 'Converting one thing to another'. So PWM would mean 'Converting Pulse Width to another thing'. What is the 'another thing' here ? In PWM case, the 'another thing' means 'Amplitude'. Therefore, PWM mean 'Converting the pulse width to the amplitude'.

 

Take a look at some of the examples as illustrated below.

In (A), you see a sequence of 'ON'/'OFF' repetition but ON Width is wider than OFF width. However, every ON has the same width and every OFF has the same width. If this is converted to amplitude by PWM, you would get the relatively high amplitude as shown in the dotted line at the bottom.

In (B), you see a sequence of 'ON'/'OFF' repetition but ON Width is almost the same as OFF width. And, every ON has the same width and every OFF has the same width. If this is converted to amplitude by PWM, you would get the relatively low amplitude comparing to (A) as shown in the dotted line at the bottom.

In (C), you see a sequence of 'ON'/'OFF' repetition but ON Width and OFF width varies at every pulse.  If this is converted to amplitude by PWM, you would get gradually changing amplitude as shown at the bottom plot.

 

 

I hope you got the big picture of PWM. Then you may ask "Why we use this technique ?" or "When do we use this kind of technique ?".

 

In short, the answer is "Using PWM, you can convert a digital pulse with has only two states (ON or OFF) into any arbitrary level of amplitude'. So, in real application, the input is almost always digital pulse (DC voltage or DC current) and output becomes various other forms depending on the application.

 

Following is one of the simplest example of using PWM and showing the concept of PWM. Using the train of digital pulse with various different pulse width and produce the effect of different brightness of a light (analog concept).

 




Universal asynchronous receiver-transmitter (UART)

In UART communication, two UARTs communicate directly with each other. The transmitting UART converts parallel data from a controlling device like a CPU into serial form, transmits it in serial to the receiving UART, which then converts the serial data back into parallel data for the receiving device. Only two wires are needed to transmit data between two UARTs. Data flows from the Tx pin of the transmitting UART to the Rx pin of the receiving UART:

UARTs transmit data asynchronously, which means there is no clock signal to synchronize the output of bits from the transmitting UART to the sampling of bits by the receiving UART. Instead of a clock signal, the transmitting UART adds start and stop bits to the data packet being transferred. These bits define the beginning and end of the data packet so the receiving UART knows when to start reading the bits.

When the receiving UART detects a start bit, it starts to read the incoming bits at a specific frequency known as the baud rate. Baud rate is a measure of the speed of data transfer, expressed in bits per second (bps). Both UARTs must operate at about the same baud rate. The baud rate between the transmitting and receiving UARTs can only differ by about 10% before the timing of bits gets too far off.

Both UARTs must also must be configured to transmit and receive the same data packet structure.

How UART Works

The UART that is going to transmit data receives the data from a data bus. The data bus is used to send data to the UART by another device like a CPU, memory, or microcontroller. Data is transferred from the data bus to the transmitting UART in parallel form. After the transmitting UART gets the parallel data from the data bus, it adds a start bit, a parity bit, and a stop bit, creating the data packet. Next, the data packet is output serially, bit by bit at the Tx pin. The receiving UART reads the data packet bit by bit at its Rx pin. The receiving UART then converts the data back into parallel form and removes the start bit, parity bit, and stop bits. Finally, the receiving UART transfers the data packet in parallel to the data bus on the receiving end:

UART transmitted data is organized into packets. Each packet contains 1 start bit, 5 to 9 data bits (depending on the UART), an optional parity bit, and 1 or 2 stop bits:

Start Bit
The UART data transmission line is normally held at a high voltage level when it’s not transmitting data. To start the transfer of data, the transmitting UART pulls the transmission line from high to low for one clock cycle. When the receiving UART detects the high to low voltage transition, it begins reading the bits in the data frame at the frequency of the baud rate.

Data Frame
The data frame contains the actual data being transferred. It can be 5 bits up to 8 bits long if a parity bit is used. If no parity bit is used, the data frame can be 9 bits long. In most cases, the data is sent with the least significant bit first.

Parity

Parity describes the evenness or oddness of a number. The parity bit is a way for the receiving UART to tell if any data has changed during transmission. Bits can be changed by electromagnetic radiation, mismatched baud rates, or long distance data transfers. After the receiving UART reads the data frame, it counts the number of bits with a value of 1 and checks if the total is an even or odd number. If the parity bit is a 0 (even parity), the 1 bits in the data frame should total to an even number. If the parity bit is a 1 (odd parity), the 1 bits in the data frame should total to an odd number. When the parity bit matches the data, the UART knows that the transmission was free of errors. But if the parity bit is a 0, and the total is odd; or the parity bit is a 1, and the total is even, the UART knows that bits in the data frame have changed.

Stop Bits

To signal the end of the data packet, the sending UART drives the data transmission line from a low voltage to a high voltage for at least two bit durations.

Step of UART Transmission

1. The transmitting UART receives data in parallel from the data bus:

2. The transmitting UART adds the start bit, parity bit, and the stop bit(s) to the data frame:

3. The entire packet is sent serially from the transmitting UART to the receiving UART. The receiving UART samples the data line at the pre-configured baud rate:


4.  The receiving UART discards the start bit, parity bit, and stop bit from the data frame:

5. The receiving UART converts the serial data back into parallel and transfers it to the data bus on the receiving end:


Advantages and Disadvantages of UART

No communication protocol is perfect, but UARTs are pretty good at what they do. Here are some pros and cons to help you decide whether or not they fit the needs of your project:

Advantages
  • Only uses two wires
  • No clock signal is necessary
  • Has a parity bit to allow for error checking
  • The structure of the data packet can be changed as long as both sides are set up for it
  • Well documented and widely used method

Disadvantages

  • The size of the data frame is limited to a maximum of 9 bits
  • Doesn’t support multiple slave or multiple master systems
  • The baud rates of each UART must be within 10% of each other

Continue on to part three of this series, Basics of the I2C Communication Protocol to learn about another way electronic devices communicate. Or if you haven’t already, check out part one, Basics of the SPI Communication Protocol

Analog-to-digital converter (ADC)



Usually, transducers can also convert the input analog variables into currents or voltages. Basically, the digital numbers it uses are binary, i.e.,  ‘0’ and ‘1’. The ‘0’ indicates the ‘off’ state, and ‘1’ represents the ‘on’ state. Hence, an ADC converts all the analog values into digital binary values. For example, if we have to install an alarm in our house or at some facility whose function is to set off in case of fire or overheating, our whole alarm system will be electronic, but the temperature sensor will give analog values at the output after sensing the temperature. Therefore, to convert the varying values of temperature into digital or discrete values, we have to use an analog to digital converter.


A/D Conversion Process and How ADC works ?

Mainly, there are two steps for the analog to digital conversion:


  1. S/H: Sampling and holding
  2. Q/E: Quantizing and Encoding


We can see the ADC process in the figure below:



1. Sampling and Holding


An analog signal continuously changes with time. In order to measure the signal, we have to keep it steady for a short duration so that we can sample it. We could measure the signal repeatedly and very fast and then find out the right time scale, or we could measure the signal at different times and then average it. The last preferable option is that we can hold the signal for a specific duration and then digitize the signal and sample the value. We can do this using a sample and hold circuit. For at least the time required for digitization, it keeps the value stable. The below figure shows the circuit for sampling and holding a signal.


Sampling and holding circuit


We keep the switch normally open, and when we want to find a measurement, we close the switch momentarily.


2. Quantizing and Encoding


On the output of (S/H), a certain voltage level is present. It assigns a numerical value to it. Then search for the nearest value in correspondence with the amplitude of the sampling and holding signals. And this value cannot be just any value; it should be from a limited set of possible values. It depends on the range of the quantizer, and the range is in a power of 2, i.e.,e 2n (28 = 256, 210=1024 etc.).


After identifying the closest value, it assigns a numerical value to it, which is encoded in the form of a binary number. The binary-encoded numbers generated by quantizers are represented by ‘n’ bits. The resolution of an ADC can also be denoted by an ‘n’ bit. The figure shows the whole conversion process:



Sampling, Holding and Quantizing


The values we get after the quantization and encoding process cannot be said to be thoroughly accurate. These are only approximations of real-world values. The accuracy of the quantizer highly depends on the resolution of the quantizer; the greater the resolution, the more accurate the values will be. The ADC resolution has limits due to a number of constraints, out of which time is a major issue. If it needs to search for the closest value from a set of possible values and it is greater, then it will surely take more time. But to accelerate this process, more techniques have been developed.


The following table shows the performance of different ‘n’ bit ADCs. If the number of bits is greater, then the frequency is less and the time consumption is also greater. On the other hand, the error minimizes as the number of bits increases. The maximum sampling rates have also been indicated in the table.


ADC steps

ADC Types

The most common types of analog to digital converters available are:
  1. Flash Analog to Digital converter.
  2. Dual slope Analog to Digital converter.
  3. Successive Approximation Analog to Digital Converter.

1. Flash ADC

Flash ADC is one of the simplest ADCs. It is also known as the parallel ADC converter. It consists of a number of comparators. An encoder circuit connects to the output of the comparators, which gives us binary output. The below figure shows a flash ADC circuit of 3-bits:


flash ADC

Vref is the reference voltage; if the analog value at the input becomes greater than the reference voltage, then the comparator output will be high. The flash converter is the most efficient of all the converters in terms of speed. But the number of comparators increases as the number of bits increases. We would require 7 comparators for 3-bits and 15 comparators for 4-bits. This is the disadvantage of using a flash ADC.


But a flash converter can produce a non-linear output, which is an additional advantage. The voltage divider network consists of equal-value resistors, which provide a proportional response. But for special applications, the value of the resistors is changeable, which will give a non-linear response.


2. Dual Slope ADC

A dual-slope integrator first integrates and then disintegrates a voltage signal. It integrates an unknown voltage for a fixed time and disintegrates for a variable time using a reference voltage. We can see the graph of dual slope integration in the figure below.

Dual-slope integration

The main advantage is that the error occurring in a component during integration cancels out during the phase of de-integration. The figure below shows a dual slope converter block diagram:

. Dual-slope converter

For example, if we want to obtain a resolution of 10 bits, we would integrate for 210= 1024 cycles and then de-integrate for 1024 cycles. By increasing the number of clock cycles, we can obtain more resolution.

ADC Applications

If we look around, we can notice hundreds to thousands of ADCs and DACs in our daily lives. Some of the popular applications are:
  • Audio applications: For example, when listening to music on our mobile phone, the mobile phone stores music in memory in digital form, and a speaker accepts an electrical signal, which is an analog signal. Therefore, we need an ADC to convert a digital bitstream of music into an analog signal to play the music so that we can hear it through a mobile speaker. Hence, our mobile phone contains many ADCs for audio and many other applications.
  • Call receiver and transmitter on mobile phone.
  • Video Streaming
  • Data Acquistion

Digital-to-analog converter (DAC)



The Digital-to-Analog Converter (DAC) is a system that converts a digital signal into an analog signal. An analog-to-digital converter (ADC) performs the reverse function.

There are several DAC architectures; the suitability of a DAC for a particular application is determined by figures of merit including: resolution, maximum sampling frequency and others. Digital-to-analog conversion can degrade a signal, so a DAC should be specified that has insignificant errors in terms of the application.

DACs are commonly used in music players to convert digital data streams into analog audio signals. They are also used in televisions and mobile phones to convert digital video data into analog video signals. These two applications use DACs at opposite ends of the frequency/resolution trade-off. The audio DAC is a low-frequency, high-resolution type while the video DAC is a high-frequency low- to medium-resolution type.

Introduction

A DAC converts an abstract finite-precision number (usually a fixed-point binary number) into a physical quantity (e.g., a voltage or a pressure). In particular, DACs are often used to convert finite-precision time series data to a continually varying physical signal.

Sampled signal.

Provided that a signal's bandwidth meets the requirements of the Nyquist–Shannon sampling theorem (i.e., a baseband signal with bandwidth less than the Nyquist frequency) and was sampled with infinite resolution, the original signal can theoretically be reconstructed from the sampled data. However, an ADC's filtering can't entirely eliminate all frequencies above the Nyquist frequency, which will alias into the baseband frequency range. And the ADC's digital sampling process introduces some quantization error (rounding error), which manifests as low-level noise. These errors can be kept within the requirements of the targeted application (e.g. under the limited dynamic range of human hearing for audio applications).

Applications
A simplified functional diagram of an 8-bit DAC

DACs and ADCs are part of an enabling technology that has contributed greatly to the digital revolution. To illustrate, consider a typical long-distance telephone call. The caller's voice is converted into an analog electrical signal by a microphone, then the analog signal is converted to a digital stream by an ADC. The digital stream is then divided into network packets where it may be sent along with other digital data, not necessarily audio. The packets are then received at the destination, but each packet may take a completely different route and may not even arrive at the destination in the correct time order. The digital voice data is then extracted from the packets and assembled into a digital data stream. A DAC converts this back into an analog electrical signal, which drives an audio amplifier, which in turn drives a speaker, which finally produces sound.

USB
Universal Serial Bus (USB) is an industry standard that allows data exchange and delivery of power between many various types of electronics. It specifies its architecture, in particular its physical interface, and communication protocols for data transfer and power delivery to and from hosts, such as personal computers, to and from peripheral devices, e.g. displays, keyboards, and mass storage devices, and to and from intermediate hubs, which multiply the number of a host's ports.

A USB device may consist of several logical sub-devices that are referred to as device functions. A composite device may provide several functions, for example, a webcam (video device function) with a built-in microphone (audio device function). An alternative to this is a compound device, in which the host assigns each logical device a distinct address and all logical devices connect to a built-in hub that connects to the physical USB cable.

USB device communication is based on pipes (logical channels). A pipe is a connection from the host controller to a logical entity within a device, called an endpoint. Because pipes correspond to endpoints, the terms are sometimes used interchangeably. Each USB device can have up to 32 endpoints (16 in and 16 out), though it is rare to have so many. Endpoints are defined and numbered by the device during initialization (the period after physical connection called "enumeration") and so are relatively permanent, whereas pipes may be opened and closed.

There are two types of pipe: stream and message.

  • A message pipe is bi-directional and is used for control transfers. Message pipes are typically used for short, simple commands to the device, and for status responses from the device, used, for example, by the bus control pipe number 0.
  • A stream pipe is a uni-directional pipe connected to a uni-directional endpoint that transfers data using an isochronous, interrupt, or bulk transfer:.

Isochronous transfers
At some guaranteed data rate (for fixed-bandwidth streaming data) but with possible data loss (e.g., realtime audio or video)


Interrupt transfers
Devices that need guaranteed quick responses (bounded latency) such as pointing devices, mice, and keyboards


Bulk transfers
Large sporadic transfers using all remaining available bandwidth, but with no guarantees on bandwidth or latency (e.g., file transfers)


Timer


A timer or countdown timer is a type of clock that starts from a specified time duration and stops when reaching zero. 




Post a Comment

Previous Post Next Post