Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,211 members, 7,818,712 topics. Date: Sunday, 05 May 2024 at 10:34 PM

Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? - Science/Technology - Nairaland

Nairaland Forum / Science/Technology / Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? (2391 Views)

EMBEDDED SYSTEMS Tutorial For Beginners:#include <module4.h> Experiments 1 & 2 / EMBEDDED SYSTEMS Tutorial:#include <module3.h>software And Hardware Tool Needed / Embedded System Tutorial For Beginners #include<module1.h> (2) (3) (4)

(1) (Reply) (Go Down)

Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by guassian: 12:38am On Mar 06, 2015
#MODULE TWO: MICROCONTROLLERS AND MODERN DAY ELECTRONICS:

Welcome again to my Embedded systems tutorial, last time we learnt about embedded systems in general (https://www.nairaland.com/2175820/embedded-system-tutorial-beginners-include ) today we shall be learning in summary what a microcontroller is, memory types, oscillator, ports, programming the microcontroller etc. Remember that the aim of this tutorial is to give you a summary, basic things you need to know to arouse your interest, you still need to pick up an ebook and read in details if you want to have full knowledge. And don’t hesitate to ask questions if you don’t understand anything discussed in this topic and embedded systems in general. This tutorial is quit long but I bet you will gain a lot. Please Just Exercise patience and read through till the end.

INTRODUCTION
Looking back into the history of microcomputers, one would at first come across the development of microprocessor, but the stand alone microprocessor is not self-sufficient. It requires other components like memory to store data and I/O devices that serves as a gateway to receive and transmit data to and from the microprocessor, to form a workable system configuration. The device which contains a microprocessor and the above mentioned components in small scale on a single chip is a microcontroller. The introduction of microcontrollers drastically changed the microprocessor based system design. There are various type of micro controllers such as: AMR, Atmel AVR, Intel 8051, Infineon, ZILOG Z80, Motorola M68HC11 series, Microchip PIC etc.

Microcontrollers are integrated circuits, but they differ fundamentally from other ICs. They are a class in themselves, and the designers have not made them to do a particular job. As such when you buy them from the market, you can not specify what function it will do. In order to get some useful function, these ICs have to be configured or programmed. Thus a microcontroller can be configured to check the status of a button, and then turn a motor ON or OFF and lit an LED etc. While the same IC can be configured later, to read the status of an infra-red sensor, decode the signal and turn another device ON or OFF or send a number to a LCD screen etc. If these two types of circuitries mentioned above were to be made using conventional digital ICs, it would have required a large number of components. Moreover any change in the specification, like change of Infra-Red codes would result in total change in design, but using a configurable IC, no design has to be changed but just the program has to be edited.
Not only that the same IC can be configured to do different tasks, but a change in specifications can easily be implemented by just changing the device configuration some lines of programming codes. This greatly facilitates 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 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 microcontroller “Programming”.
Someone might be thinking why are there so many types of microcontroller and which is best to use. No microcontroller is better than the other, each has both advantage and disadvantage, some are costly and others are cheap, some are of big sizes and others are small, some has many I/O ports and peripherals for easy interfacing while others does not have enough and so on. There are many microcontrollers available for developers to choose from. The microcontroller used mostly in Nigeria here are the PIC Microcontroller, ATMega, AVR but PIC and is what I will most likely be using for tutorials on this Nairaland because it is the one I am most familiar with, it is easy to use and has lots of inbuilt peripherals e.g internal oscillator, ADC, timers etc and also so many compilers supports it. Eg mikro C, CCS C compiler etc. Forgive me if you have already been learning other types of microcontroller, but I can tell you that they all do the same thing and the principles of operation are all the same, they just differs by architecture, inbuilt peripherals etc.

THE PIC MICROCONTROLLER
Although microcontrollers were being developed since early 1970’s real boom came in mid 1990’s. A company named Microchip® made its first simple microcontroller, which they called PIC. Originally this was developed as a supporting device for computers to control its peripheral devices, and therefore named as PIC, Peripheral Interface Controller. Thus all the chips developed by Microchip have been named as a class by themselves and called PIC. Microchip itself does not use this term anymore to describe their microcontrollers; however use PIC as part of product name. They call their products MCU’s.
A large number of microcontroller designs are available from microchip. Depending upon the architecture, memory layout, number of input and output ports and processing power. They have been classified as low range, mid range, high range and 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 using PIC12f683(8 pin), PIC16f88(18pins), PIC16f877A,PIC18f452 as in various experiment for this tutorial.
[pic pinout diagrams]

BASIC THINGS YOU NEED TO KNOW ABOUT PIC MICROCONTROLLERS

Power Supply to the Microcontroller

All electronics components or circuits needs power supply to work either high voltage AC or low voltage DC. Fortunately for us, the PIC microcontroller uses low voltage so we are safe and not at risk of electric shock except we are using the MCU(microcontroller) to switch high voltage. PIC microcontrollers use TTL logic, and therefore expect a well regulated 5V power supply. You can generate 5v by using a 7805 voltage regulator IC or better still use a USB cable to tap 5v from your laptop or desktop or power bank or from your USB charger.
The supply may however range from 3.5V to 5.5V but it’s typically 5V for most PIC MCU’s. MCU requires very small amount of current in order of mA for normal operation and uA in sleep mode. 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 5.5V is considered as logical ‘1’. In order to communicate with devices using higher logical voltages, you will need a level converter such as a max232 IC or use a voltage divider circuit or a transistor or an opto-coupler. MCU’s can supply about 25mA to output peripherals or transducers but if higher current is needed, a transistor is used for amplification.

Oscillator of the microcontroller
Every microcontroller needs a kind of timing to work with. An oscillator is used to provide clock for the microcontroller and the clock frequency determines the speed of the microcontroller. There are four oscillator modes a PIC microcontroller can work with. These are the internal RC, external RC, external crystal oscillator/resonator and external oscillator source. You might be wondering which one is best, all of them has their advantages and disadvantages. We shall compare and contrast these various oscillator sources.

Using internal RC oscillator makes the design cheaper, lighter, more compact and less complex but the maximum oscillator speed can be about 8Mhz which is slow for some applications and the accuracy is above average.

Using an external RC however makes the circuit more complex and more costly and inaccurate (because of high resistor capacitor tolerance).

Using external oscillator source makes the circuit much more complex, bogus and expensive but in a case whereby so many MCU is used in a circuit, they all can tap from one oscillator source and the accuracy of these MCU will be determined by the accuracy of the oscillator source.

Using a crystal oscillator makes the circuit much more accurate and used when great precision is needed. Crystal oscillator can make the MCU run with maximum speed which is typically 20MHz, but the crystal oscillator is expensive and can add extra cost to production of cheap gadgets.
The faster is the oscillator, the faster is the processing speed. However fast processing also requires more current. The highest frequency that most PIC MCU can respond to is 20MHz. However PIC18F series have an internal mechanism to multiply the clock frequency by 4 and generate an internal frequency 4 times that of the crystal being used. The highest frequency for 18F452 and many others is 40MHz to 48MHz.

1 Like

Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by guassian: 12:39am On Mar 06, 2015
Memories of the microcontroller.
Memory is a very important component of any computer. When we go to market to buy phones and PC the first thing we ask is the memory size. Memory in Embedded systems is very important just as human beings needs memory to store tasks and information. As we have discussed ealier, memory size in microcontrollers is very low compared to one in smartphones and personal computers because what a MCU needs to control or memorize is very small. A 256Mb of RAM in a PC world is very low and obsolete but in a MCU world, 512Kb of RAM has never existed (i stand to be corrected).
There are three major types of memory in embedded system. RAM(Random Access Memory), ROM(Read Only Memory) and EEPROM(Electrically Erasable Programmable Read Only Memory). All these have its major functions which cannot be used interchangeably.

RAM is used to store and read temporary data with high speed and does not have limit of the number of times it can be accessed. Its content is lost when power is lost and can never be retrieved again. Its content can be accessed at “Runtime” i.e time of use of the MCU in applications.
ROM is used to store data permanently and can only be changed at “program-time” i.e time of burning program into the MCU and its content can never be changed at runtime. The instructions ececuted by MCU and some temporary data are stored in the ROM. Ordinary ROM is OTP(one time programmable) i.e it can only be programmed once, just like your CD-ROM which information can only be written to it once. A special kind of ROM called Flash ROM is what a MCU use, this technology allows ROM to be reprogrammed up to 100,000 times. This flash technology allows us to re-write and read information from a ROM at program-time, and this makes MCU attractive for students and Hobbyist. However, the disadvantage of flash ROM is that it is costly, so companies go for OTP ROM MCU because they must have been sure of the content of what they want to program into the chip and do not have any need to reprogram.
EEPROM is a type of ROM that is used to store data permanently also but just that it can be electrically erased and reprogrammed i.e. its content can be modified at runtime and it will retain its data for more than a century. It is what is synonymous to floppy disk or Hard disk of a PC. The number of read and write times of EEPROM is limited; about 1,000,000 times and the write and read speed is quite low compared to a RAM. So an EEPROM cannot probably be used as a RAM in real practical situations.
I/O (Input/Output) PORTS is a gateway for the MCU to communicate with the outside world i.e. it is a physical connection port for the microcontroller to get data from transducers or other input device into the MCU and also gives processed data from the MCU to output transducers or peripheral. General purpose ports are used to get logical data(1 and 0, high and low ) from input device and also send logical data to output device. Some PIC microcontroller in addition to the general purpose function, has many special function ports ranging from USB port, UART port, SPI, I2C, ADC ports, PSP, PWM etc.

Resetting the Microcontroller

On every PIC microcontroller you will find a pin labeled as MCLR. This pin has two basic functions. Used to reset the microcontroller like a soft-boot and as well as to put the microcontroller into programming mode. A MCU can “hang” just like your smartphone and your PC used to hang and you probably need to restart it by switching it off or removing the battery. A MCU can hang when there is a power brownout (low voltage or fluctuation) or when the programmer writes the instruction such that he tells the microcontroller to access a memory location that does not exist and thereby makes the MCU practically lost in the program flow and could not find its way back to execute the next instruction, the MCU needs a reset to bring the program flow to the initial state.
The MCLR pin when connected to ground(0 volts or logic 0), will reset the microcontroller, and keep it in a reset state or OFF state, till the mclr pin is connected to 5v before the MCU restarts. After a reset, 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”(used to give an input pin a default logic 1) is usually connected with the mclr pin, to keep it high when reset switch is released.
The mclr pin will also work as program mode pin i.e it is used to tell the MCU to go into programming mode when a programming device wants to modify its ROM content. When new software is to be downloaded into a PIC MCU, about 13.5V is applied to the MCLR pin, by your programming device. This can be done right in your circuit called ICSP(In-Circuit Serial Programming), or by taking the IC out of circuit and putting it into the IC socket on your programmer. More about programming Device will be discussed in another module of this tutorial, probably when we do the first experiment.

Analog and Digital Data
Microcontrollers 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 microcontroller’s digital world.
Many other microcontrollers require an external ADC(Analog to Digital Converter) chip to implement this, however this feature has been nicely built into PIC microcontrollers. The number of Analog channels varies among PIC MCU but typically they have about 5, and some PICs do 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.

PROGRAM OR FIRMWARE OF MICROCONTROLLER
A Program is nothing but a series of instructions, in a correct and logical manner to instruct the microcontroller to respond to various inputs and give specific output. By changing the program, the behavior of microcontroller will change. The MCU doesn’t come with a preloaded function from the manufacturer. Using an analogy of 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 microcontrollers are programmed to do a job. The job can be changing a TV channel to controlling complex movements of a robot, control all home lighting and electrical appliance etc. 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. So the same hardware serving thousands of different jobs. You have to learn the instructions your particular microcontroller understands, and what those instructions order it to do. Then it’s 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.
Programming the microcontroller
Programming is a mechanism to transfer the compiled program into the microcontroller’s program memory. Like earlier said when talking about memories PIC microcontrollers have a separate area of memory called program memory or ROM. The size of this memory differs in various chips.
The source code is written which is called the program with either low-level language or high-level language. This program is assembled or compiled with an assembler or compiler respectively to generate an object code usually with a file extension of “.HEX”. It is otherwise known as “hex file”, analogous to a computer’s “.EXE”. This hex file is loaded or burnt into the PIC with a device called “Programmer”. As people from PC world are used to talking about megabytes and gigabytes, most of programs written, will not exceed few hundred bytes. This is because unlike the PC the microcontroller has less to do with videos, pictures, and music. These devices are not meant to run windows, but to control a specific device based upon certain input and logic.
In order to put your microcontroller into programming mode, the MCLR pin has to be driven up to 12-13.5V. This is referred as VPP. The VPP is generated by programmer. Once VPP is applied to MCLR pin, the processor stops functioning and accepts data from programmer on PGD and PGC pins, which are mostly RB7 and RB6 pins on microcontroller. The programmer first erases the old program memory and then writes new program and EEPROM data if required. After the program is transferred it is verified. After successful programming, the VPP must be taken down, so that the program may be started.
There various programming device in market. There is a universal programming device which can program all types of microcontroller but they are quite expensive. I recommend you use a PIC programmer which is very cheap and you can also build one for yourself. Someone has made the work easy by posting this link on Nairaland ( https://www.nairaland.com/2157202/simplest-reliable-pic-microcontroller-programmer ) I bet this circuit works like magic. The disadvantages it has is that it uses 15v power supply which make you look for mains electricity when u can power the rest of circuit with a 5v power bank, it uses serial port which most modern laptop doesn't have, old Dell D630 has serial port and some old HP laptop too. So you will need a USB programmer if you intend to use a modern laptop. If you need a cheap and reliable USB programmer, contact www.hub360.com.ng and make your request.

Conclusion
Forgive me if I am boring you with theories; these are summaries of some of important things you need to know and you will surely need them when I start showing you practical examples. Please read more online about the little you learnt in today's topic as this will give you broad knowledge and make widen your scope.
In the next tutorial, I will be exposing you to the various software and hardware tools you will need to start performing experiments and doing projects. I will be giving you links to where you will download very useful software free. I bet you will gain a lot. Remember, I will be posting the next tutorial with a new post. This will help to give room for continuous comments, contributions and questions on the topic of today and will avoid unnecessary long thread.

More information about today’s tutorial can be gotten from these links.
http://www.personal.rdg.ac.uk/~stsgrimb/teaching/programming_pic_microcontrollers.pdf
http://www4.hcmut.edu.vn/~bmthanh/ESD/Programming%208-bit%20PIC%20Microcontrollers%20in%20C%202008%20Martin%20PBates.pdf

2 Likes

Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by GrAnDwEeZ(m): 7:12am On Mar 06, 2015
I'm with u bro. following. Currently studying elect elect with no practical experience @ all. This is d kinda platform I ll need 2 kick me started. Got a laptop. No knowledge on programming @ all. It ll b nyc if u can com down 2 a beginners level as much as possible. Thanks
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by guassian: 10:30am On Mar 06, 2015
GrAnDwEeZ:
I'm with u bro. following. Currently studying elect elect with no practical experience @ all. This is d kinda platform I ll need 2 kick me started. Got a laptop. No knowledge on programming @ all. It ll b nyc if u can com down 2 a beginners level as much as possible. Thanks

Kindly follow this tutorial from the beginning including comments and contribution. Links to some useful books is there where you can learn basics and i recommend this book for you "practical electronics for inventors". Just type the name on google search or copy and paste this link on your browser https://www.google.com/url?q=http://www1.appstate.edu/~curtincm/sculpture/suppliers/gizmos/Practical_Electronics_for_Inventors.pdf&sa=U&ei=PHP5VOytCMXgaqbxgcAH&ved=0CA8QFjAC&sig2=yiiMQdx1Rlqthj_99Lgp1Q&usg=AFQjCNH2L9FW0j2Xz8jrAGWtFUqfq2aoiw
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by acorntree(m): 3:55pm On Mar 07, 2015
Good job. Keep it up. I will contribute later
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by Fulaman198(m): 4:53pm On Mar 07, 2015
Nice write up 1,000 thumbs up
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by Abd08: 7:25am On Mar 11, 2015
Nyc job. We are following u bumper to bumper
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by alizenbohr: 5:30pm On Mar 12, 2015
guassian:


Kindly follow this tutorial from the beginning including comments and contribution. Links to some useful books is there where you can learn basics and i recommend this book for you "practical electronics for inventors". Just type the name on google search or copy and paste this link on your browser https://www.google.com/url?q=http://www1.appstate.edu/~curtincm/sculpture/suppliers/gizmos/Practical_Electronics_for_Inventors.pdf&sa=U&ei=PHP5VOytCMXgaqbxgcAH&ved=0CA8QFjAC&sig2=yiiMQdx1Rlqthj_99Lgp1Q&usg=AFQjCNH2L9FW0j2Xz8jrAGWtFUqfq2aoiw

Grandweez,
I could send you other beginner ebooks if you're interested.

Great job Guassian.
If I may ask, did you (are you) school(ing) in Nigeria?
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by GrAnDwEeZ(m): 6:19pm On Mar 12, 2015
alizenbohr:


Grandweez,
I could send you other beginner ebooks if you're interested.

Great job Guassian.
If I may ask, did you (are you) school(ing) in Nigeria?
schooling in nigeria. Sure I'll really appreciate.
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by alizenbohr: 8:09pm On Mar 13, 2015
GrAnDwEeZ:
schooling in nigeria. Sure I'll really appreciate.

Ok. You can send me a PM then I'll forward them to you over the weekend.
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by guassian: 11:10am On Mar 20, 2015
Hi there, this is the link for the next tutorial MODULE 3
Re: Embedded System Tutorial #include<module2.h> :what Is A Microcontroller? by guassian: 11:11am On Mar 20, 2015
Hi there, this is the link for the next tutorial MODULE 3 https://www.nairaland.com/2195254/embedded-systems-tutorial-include-module3.h

(1) (Reply)

Cold Storage. Coldroom Warehouse, HVAC Temperature Monitoring Systems / RC Hobby / Cars Running Water As Fuel

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 61
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.