This is going to be an unusual tutorial since I am not yet going to get into specific programs or languages. At this point, the concepts of programming are far more important to the beginner. How a program affects hardware actions is the theme of this particular tutorial. For those of you who are miles beyond these concepts, this might be an eye opening review for you. Skip it or read it. Your choice. Beginners should stick with this series and enjoy learning the concepts. There are also some rudimentary engineering items discussed.
To some who want to be a do-it-yourself kind of person, the mere mention of electronics, computers, programming or wiring can cause an instant closure of the senses. The first step in any design or in any project you are examining is to be sure that all power requirements have been met safely and with full integrity to the parts being used. The full integrity means that power and ground are applied at exactly the right places and in the correct amount. If a part requires a 12v supply to work, you must be certain that this is a qualified input to the device. If it needs 5v, you must supply that voltage. Easy so far. You also have to provide a common, qualified ground as required by the part. If you don't do these basic steps, no amount of programming is going to get your device to work.
I like MicroChip parts. I always have since they were introduced. For a number of years, I was a MicroChip Third Party Consultant helping their customers through the learning process and I hope to do that here for you now. Relax. It's not that scary.
There are an amazing number of devices that you can use in design. So many that I often spend hours deciding which will best suit my needs. Rather than go through that process now, I selected a part that is suitable based solely on the number of pins that the part has. It is an early part number with the first Flash memory programming that didn't require an Ultraviolet light to clear the memory between programming attempts. Older parts used to have a small glass window in them that needed to have an Ultraviolet light concentrated on the memory area to clear it back to an un-programmed state. Even experienced programmers make mistakes and debugging a program often meant clearing the memory for the next attempt at genius. Today's chips can be reprogrammed without erasing the memory with Ultraviolet. They just overwrite and clear unused portions. Wonderful! It makes life easy.
The next step in this learning process is to understand what each pin in the device is doing. Here is a picture of a PIC16F84A footprint. That identifies each pin and its function in a graphic form. We will discuss each pin and how it is qualified and used by the programmer. That's you!
Not in number order, pin 14 is Vdd or power. In this case, +5v. Pin 5 is the Vss line, or ground. There we go. Qualify them and you are ready to start this thing. Notice that both of those pins have an arrow pointing towards the chip. That means it is an Input only. Neither of those pins can create an output. From an engineering standpoint, that is fine since it is common practice to connect all Vdd and Vss requirements to a shared bus for other parts to use. This will be discussed in another tutorial.
The next important pin to discuss is pin 4, the Master Clear line. When you drive this pin low (to ground) it is a signal for the part to reset itself to address 0 and clear all registers. This pin is always set high by a pull-up resistor of around 10k just to be sure that we don't have the part clearing itself due to electrical noise and other wayward imps that live in our designs. Normally, a push button switch is used to send the pin low by connecting the open side of the switch to the pin and the other side to ground or Vss. Pins 15 and 16 are the external clocking pins. If you use a resonator part (a crystal with two capacitors inside) both pins are used. If you use an external oscillator or a clock from another source, only pin 16 is used. Note the direction of the arrows. Again, as with most unused pins, set the other pin to high through a 10k pull-up resistor. Despite that, the clock from the external source will be available to use elsewhere in your circuit.
Next, a more complex but still understandable set of pins to discuss. The pins labeled RA0-RA4 have arrows that go both ways! That means they can either be an input or an output. Thus, your program can either decide to treat it as a control output setting any of the pins to a state of high or low. All 5 of those pins can be set to any output state you desire. All can be high, low or mixed up – both high and low. As an input, they can be set by your program to watch for a condition of a state of either high or low – one or zero. They can be changed from an input to an output and vice versa by a program command. The letter R indicates it is an internal Register in the chip being sent as an output to the world you have designed around this part. If you set a pin to a high or "one" state, the world outside can use this for anything. For example, you could turn on a light, power a transistor to set a relay or begin a chain of data for serial communication. The possibilities are as endless as the human imagination. The A indicates which register is being used. A (also known as Port A) is a 5 bit register and an important note must be made about it. As an output, it is an Open Collector device. Future tutorials will explain this but for now, all you need to remember is that it must be pulled to a high state by an external resistor of around 10 if you use the register as an output. As an input, the resistor is weak enough to allow a low or high state to be presented to the pin so good practice is to pull the pins high.
Port B (RB0-RB7) is also bidirectional and can be used as input or output. Port B has internal pull up resistors that can be set on or off by program control.As with Port A, all pins can be set to a high or low (one or zero) state by program control or set to inputs by program control. Each pin is set independently and can be a combination of any states or any use. The versatility of these devices is incredible. Don't fear them, use them.
There are a couple of special purpose pins that need more explaining. Pin 3 (RA4/T0CKI) can be programmed to use alternatively as an input clock for an internal timer named TMR0 or Timer0. That timer can be used as either a timer or counter. Programming TMR0 as an internal clock allows the device to measure time intervals when needed. As a counter, it can be used to either count time intervals or count events like the number of rotations on a wheel, for example. Both functions are set up and managed by program control.
Pin 6 (RB0/INT) can be used alternatively as an Interrupt. When it is programmed as such, the programmer can choose a high or low state to cause an interrupt which is handled immediately. The arrival of an interrupt will immediately cease all program function and execute an interrupt routine also set by the programmer. You can choose, for example, to shut down all heaters in your home if the temperature goes higher than 70f degrees. Relays that keep the heaters on can be shut off immediately when that condition occurs and is passed on to the chip.
There are lots more subtle functions that can be performed by the MicroChip parts. This is a simple description of the most basic function of the part. Whatever program you choose will allow you to manipulate all of these features. Whether you choose to write in Assembly, C, BASIC or any other high level language, you, as the programmer, are in charge of this device.
Just remember one very important thing. Your device will do exactly what you ask it to do. EXACTLY. Nothing more, nothing less.