« A little PWM1001001 SOS »

The Microcontroller

08/24/08 | by anthony [mail] | Categories: Informative

Now that we've covered the basics we can finally get into the main purpose of this blog. From now on we will refer to a microcontroller with the acronym "MCU."

Section 1: The MCU

-So yea, what are they?

A MCU is a small computer on a chip. Most MCUs have RAM ROM and a CPU to process data stored in ROM. When a MCU is turned on, it instantly starts running the code stored in its data banks and executing commands. Most MCUs only have about a small amount of storage (usually a few KB ) but it more than enough to run complex algorithms and even stuff for advanced robotics. Microcontrollers are essential to all electronics they provide the ability to make a device “smart” without using complex analog integrated circuits or expensive computer systems. They are extremely fun for hobbyists as they can replace tons of expensive components with a moderately priced MCU.

-What can I do with them?

You can do almost anything. From dancing LEDS to PWM fan control to driving LCD screen and robotics. If you can code it, a microcontroller will do it. The fundamentals of a MCU are taking inputs and conditioning outputs through the use of code. Depending on the complexity of your code depends how complex the function of your chip can be. With a little creative thinking you can make even the low end chips do some extra ordinary things.

- What so special, I can do all of that with analog circuits!

Well, analog circuits aren’t as modular and expandable as MCUs. As soon as you compile your code your chip has taken on new functionality. Debugging and expanding functionality is extremely easy and efficient. A analog circuit that would take several integrated circuits as well as a dozen passive components can easily be replaced by one chip which does it all. MCUs can even use analog circuits to expand the function of circuits.

-What types of functions do these chips take on a day to day basis?

They're everywhere! Any interactive device uses MCUs. Your microwave, your television, your mouse, your cell phone, and pretty much everything use them.

-I mean with all this functionality, these things must cost a fortune!

Not at all, although high end chips can cost upwards of 20$, most mid range lines go for about 3-4$ each

Section 2: The world of MCUs


-This stuff sounds cool, but where do I start?

Getting into microcontrollers is much easier than it may seem. It’s always good to have a background in passive and analog components but its not necessary (will make things harder if you don't). There are many different types of microcontrollers. Some use proprietary languages some run on common languages like C/C++/Basic. Some require expensive software, some come with free software. For the purpose of this article I am going to be explaining how to work with the PIC series of microcontrollers manufactured by Microchip.

-So why the PIC?

They are low cost microcontrollers which are extremely easy to program for and develop with. They offer the latest support for their devices as well as free development software. PIC microcontrollers are widely used by hobbyists because of their ease of use and their cost effectiveness.

-What do I need to start?

You need a programmer,an actual MCU, and software to develop with. I highly recommend the PICKit2 offered by Microchip. They go for about 50USD and they include a USB programmer, tons of guides, the microchip IDE, and a midrange 20 pin PIC16F690.

http://www.microchip.com/stellent/id...cName=en023805

If you plan on using microchip MCUs you need their IDE (developers suite) offered free on their website (as well as included with PICKit2)

-There are literally thousands of MCUs to choose from! Which one do I use?

I personally like the 20 pin PIC16F690, I use in all my projects and I highly recommend it. It’s an 8-bit microcontroller with a 20MHZ internal clock it has 7KB of internal storage, but it’s more than enough for some basic robotics and any other projects.

You can buy them at http://www.mouser.com/Search/ProductDetail.aspx?qs=fM4xO01eazPcr2WIDQJ1Dg%3d%3d

Section 3: The mindset of a developer.

Rather than continue with my quirky Q&A style I am going to discuss some ideologies and strategies associated with working with MCUs before I get into programming and your first program.
You should have a good idea of how to work with binary numbers as well as hexadecimal. You will need to know them. I recommend you read the blog post before this one to get an idea of working with binary and hex if you haven't.

Every MCU has its own data sheet. They’re about 300 pages long and they contain every nook and cranny of the hardware. They are ESSENTIAL to develop for any MCU. When you get a device, you must understand how to reference data from a data sheet. The data sheet for the PIC16F690 is located below:

http://ww1.microchip.com/downloads/en/DeviceDoc/41262E.pdf

For the most part, you are programming WITH the hardware in mind. You are going to sending data to memory locations to enable functions and check statuses of certain memory locations. This is different than writing a program for windows or programming in C. The main parts of a microcontroller are its registers, registers are spots in the memory that have a function associated with them, and they start at 0x00 and end at 0x1fff. Most of it is empty memory dedicated for storage of your program, however in the beginnings there are dedicated locations for the functions of the MCU. These are where you set up ports and manipulate I/O data. The way Microchip sets up there memory is in two banks. Bank 1 is where you set up information on the ports and bank 0 is where you manipulate data. For the most part, the running part of your program is going to be in bank 0 and you only switch to bank 1 to set up ports.

The basic flow of a program is setting up environmental variables, then setting up ports, then going to a main routine that calls subroutines to analyze data and condition outputs. Programs for MCU tend to be very linear compared to code written for software. One thing to keep in mind is that your code operates very very fast. A PIC operates around 4MHZ and takes 4 clocks to do one instruction, thus an average PIC will operate at about 1 million instructions per second and an average instruction taking only 1 microsecond to complete. This both works with as well as against you. Flashing an LED isn't as simple as doing a loop that turns on and off a LED you have to add a delay routine in between so that the LED being on and off is visible to the naked eye. Considerations like these are learned as you work with your hardware and begin to notice what works and what doesn't work.

The Software

We will mostly be working with Microchip's MPLAB IDE. This software suit includes pretty much everything you need to use to develop for your PIC. It includes many different assemblers, compilers, simulators and programmers to make sure that your code will work flawlessly. Once you setup a new project and set up your hardware you will begin writing your code.

Section 4: The Hardware

Remember how i said how important the data sheet is? We're going to open up the data sheet of the PIC16f690 and explore the insides and learn how to work with this hardware. This data sheet includes data for several MCUs all are fairly similar except they come in different packages. We are going to be looking at the data for the PIC16F690 20pin DIP MCU.

One of the first things we see are the pin outs for our MCU. The first pin is the VDD which is the supply voltage, we always provide a 5v regulated source to our micrcontrollrs for safe operation, however it can accept a wide range of voltages (Try not to go above 9 V). Pin 20 is the ground. These two pins are the only two power pins essential to operation. There are several other pins you should also take into consideration when building circuits outside of the developer. Pin 4 is the MCLR which is a reset pin and also the pin that is used to trigger programming. You usually provide a logic high to this pin if you turn it on in the config word (The configuration file for the MCU set when programming). We also have a Clock in and Clock out on pins 2 and 3 respectively. All microcontrollers need a clock source to increment the code and keep things running. Most microcontrollers have an internal oscillator at 20 MHZ that is more than enough for us. However, you can set the clock manually by providing a clock source to these pins and configuring the config word to not use the internal oscillator.

After that we have our multipurpose input/output(i/o) pins. In the PIC16F690 the i/o pins are separated by ports a b and c. ports a and b arnt full 8 bit i/o ports but port c is. For the most part i use port c for output and a or b for input, in the end its just developers preference.

Different pins will have different functions and can be used for different things. All of that is described in the data sheets. You also want to keep note of what pins are used for external interrupts.

Next on our list is the register block (starts page 29). The next part of the data sheet goes on to explain the general location of all the registers and their hex location. Simple enough.

Our PIC microcontroller is a 8 bit device. Which means most functions can only handle a max of 8 bits. The table after the register block expands the basic registers and explains what each bit does (page 34).

Lets look at the status register. The status register controls the status of the micrcontroller. certain flags are set when certain things happen in the micrcontroller. The status register is also the go to register when you have to change banks. It is located at 03h (same as 0x03) and has 8 bits to it. This table does a basic layout of what functions are located at each bit, but if we want a more in depth explanation we can go look it up further into the data sheet.

The Status Register.

Page 38 explains the status register's bits more in depth. The mains bits were going to be using are 0, 2, and 5.

Bit 0: Bit 0 is the carry bit. It is set when a bit overflows (goes over 255 or below 0)

Bit 2: Bit 2 is the zero bit, it is set when the result of a arithmetic operation is 0 or negative.

Bit 5: Bit 5 is the RP0 bit. We set or clear this bit to change which bank we are in. The design of the PIC microcontroller is that there are two banks, bank 0 and bank 1. As explained earlier, bank 1 is where you set up ports and bank 0 is where you manipulate data.

The data sheet offers explanations for all the registers and everything so if you ever get stumped, go to the data sheet.

Section 5: Your First program

Although you can use C to develop for the PIC1690, I prefer assembly language. It’s much easier to work in assembly than it is with C as assembly is more tied to the hardware than C is. The following code was included in Microchip’s PICKIT2 it is not mine and I am merely including it for Demo purposes.

#include p16f690 .inc
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
org 0
Start:
bsf STATUS,RP0 ; select Register Page 1
bcf TRISC,0 ; make IO Pin C0 an output
bcf STATUS,RP0 ; back to Register Page 0
bsf PORTC,0 ; turn on LED C0 (DS1)
goto $ ; wait here
end

The Registers used: STAUS, TRISC and PORTC

STATUS holds information on general MCU info, it has 8 bits to it however, were interested in the RP0 bit which controls what bank we are in.

TRISC is the tri-state register for portc, this also has 8 bits, by setting it high, we make that port an input. By setting it to 0, we make it an output.

PORTC this handles the data on the ports. We can check if inputs are high or low and we can set outputs high or low.

#include p16f690.inc The first line includes a file that has preset names for common register location (Such as STATUS, PORTA/B/C and TRISA/B/C

__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

This line set the config word for the MCU, this includes information about clock weather to use internal timers or not. For most intensive purposes, this general config can be used which uses the internal oscillator and turns off the MCLR

org 0 Tells the compiler this is the start of the program.

Start:
A label, we uses these to label parts in a program.

bsf STATUS,RP0 BSF, means BIT SET HIGH, the next lines tell it to goto the STATUS register and bit RP0, RP0 sets what bank the program is in, by setting it to 1(high) we are in bank 1 and thus can manipulate port information

bcf TRISC,0 BCF, means BIT CLEAR, The next lines tell it to goto the TRISC register (controlling portc I/O) and set the first bit, bit 0, to low, thus making it an output (1 is input, 0 is output)

bcf STATUS,RP0 This clears RP0 in STATUS, bringing us back to bank 0. Now we can manipulate data.

bsf PORTC,0 Sets the first bit of PORTC to high, enabling the LED

goto $ Stops the program

end Tells the compiler the code is finished.

What you can do with this you should build your code. By building your code you call MPLAB's assembler which takes your code and turns it into a HEX file that the PIC can read natively. After you do that it is highly recommended you run MPLAB's simulator and begin testing your code. You can watch the special registers and see how your code manipulates the registers as well as the I/O.

From here we can begin loading our code onto the actual MCU and set it up in a circuit. The PICKIT already contains a development area and several components to test the function of your code. However, for more complex programs you will be taking your PIC off the programmer and onto a breadboard to build even more sophisticated projects.

If you’ve survived so far, give yourself a pat on the back, for you have entered the world of MCUs. The possibilities are endless and with a little ingenuity the world is yours.

Permalink

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Array

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Array
February 2012
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Ads by Google

This blog is dedicated to working with digital circuitry and the use of microcontrollers, small compact computers on a chip. I will be encompassing many techniques to develop projects, tools to use to write and assemble code and i will be sharing any projects i am currently working on. User feedback is a must! I do not know it all, hell im not even that experienced, but without a general place to get all the info needed i find it very hard to get into the world of microcontrollers without pursing a CE degree. So come one come all and enter the world of mystery and creativity!

Search

XML Feeds

powered by b2evolution