Sunday, July 6, 2014

Up and running with a PIC16

Up and running with a PIC16
This tutorial covers basics on how to hook up your PIC16F877A, write a small program and upload a program to your chip.
Section A : Wiring up the chip
Stuff you will need: A PIC16F877A, A breadboard, PICKIT 3, A 10k resistor, a 20Mhz xtal and an LED.
           

Crystal

Wiring this chip is very simple. You just need to follow the following instructions.
1.       Place your chip on a breadboard. Remember how to read the pins(small notch indicates pin 1)
2.       5V to Pin 1
3.       5v  to Pin 11
4.       Ground to Pin 12
5.       Crystal pins in Pins 13 and 14(Polarity does not matter)
6.       5v to Pin 32
7.       Ground to Pin 31
After successfully following these steps, your breadboard should look like this.


On breadboard

Section 2: Writing a small program
In this section, we take a look at how to write a led blinking program.
The program is first written in MikroBasic©, in BASIC LANGUAGE.
To proceed, follow these instructions:
1.       Get a free demo copy of Mikrobasic(http://www.mikroe.com/mikrobasic/pic/)
2.       Open Mikrobasic and click on new project.


Screen shot 1

1.       Click next on the new project wizard window pop up.
2.       Select PIC16F877A and Set clock to 20Mhz.


Screen shot 2

1.       Click on Next and Finish
2.       We will now write the program after taking a look at the fundamentals of BASIC Programming.
Format:
Program YourProgramName
Main:
     Code
End.

The following program will flash a led on port B.
program cassosl_1
main:
 TRISB = 0                            >Set Port B as output
 while true                          >Start an infinite while loop
  PORTB = %11111111      >Set all outputs of PortB to 1(HIGH)
  delay_ms(1000)               >Set a delay of 1000ms
  PORTB = 000000      >Set all outputs of PortB to 1(HIGH)
  delay_ms(1000)               >Set a delay of 1000ms
 wend                                   >End loop
 end.                                     >End Program

3.       Click Build. Many files will be generated in your save folder. You should look for an HEX file, in particular. Copy and paste it on your desktop.

Uploading your program:

In this step, we take a look at how to upload your program to your chip. In this step, you will need a programmer. The programmer is an interface between the chip and your computer. It usually has 6 output pins.
I use a PICKIT 3.

The PICKIT 3


Pin outs

You will connect lines from these output pins to your chip as follows:
PIN 1(ARROW) – Pin 1
PIN2 – Power
PIN3 – Ground
PIN4- Pin 4O
PIN5-Pin 39
PIN6-No connection
You should be having something like this.




Final layout

Next,  you should get MPLAB IDE and load it.
Click on Programmer?Pickit3.
You will get an error message. Click Programmer, settings, power. Set to 5V.
Next, click file, import and select the HEX file. Once loaded, click program.



Snap shot 3

Finally, connect a led to any pin out of port B and it should flash.







Saturday, July 5, 2014

The fantastic switching regulator

Time and again, we need to power a system from a battery rather than from mains. Now, the issue here is that most microcontrollers should be hooked up on 5V(or 3.3V). A battery would typically provide voltages above 10V. What you have below is a lithium polymer(LIPO) battery rated at 11.1V.

                                                          The LiPo -  a dangerous beauty

So, in that case, we would need to "step down"(to convert a higher voltage to lower voltage) that voltage. Now, there are two main solutions for that - either
1) A linear voltage regulator or
2) A switching regulator
Let's have a look at the first option, namely, the linear voltage regulator. Most of us know about this thing-it will get the job done all the time. So, what you have below is basically a functional 12V to 5V step down circuit using the linear regulator.

The linear regulator at work

Now, this is a nice circuit if you are doing simple things(flashing a few leds, reading a few sensors). However, things literally heat up when you increase the load. So, for instance if you are running a few motors, you may may observe the following symptoms - frequent restart or total shut down(your circuit appears dead). Why is that? You see, the linear regulator is device that dissipates heat and as your circuit demands more current, more heat is generated. Normally, your heat sink(fin like structure in the figure) would take care of that but sometimes even that may not be enough. If you stick with that, you may have to for impractical stuff like fans or whatever.
To understand the flaws that inhabit the linear regulator, we have dismantle the black box abstraction - we have to look at its principle of operation. What we have below is a simple schematic of the linear regulator.

Linear regulator schematic

In very simple terms, the output voltage is compared to a certain reference voltage and based on the result of this comparison, the resistance of the series-pass element(basically a transistor which you can use to regulate the current passing through a line) is varied from the two extreme modes of operation. Let's open a parenthesis here.
What you have below is a greatly abstracted characteristic curve of the transistor(we are not bothered about the details).

Simplified Characteristic curve(pardon this Blasphemy, God of electronics)

So, in this figure, you can actually visualise these two extreme modes of operation. Parenthesis closes here.
Ok, coming back to those two modes, we've said that the transistor is going to operate in the spectrum between those two modes. That is where the problem lies. In saturation mode, voltage drop across the element is low and power dissipation is low as well. In cutoff mode, the current through the element is low and the power dissipation is low. However, in between those two modes, we are dealing with a different animal. That is where the heat and the need for a ridiculously sized heat sink come from. In the figure below, you have a circuit for a heat pad I played with sometimes back. Notice the thin plate - THAT'S the heat sink.

   Five minutes after power on and you have a nice BBQ grill

With those elements in mind, we could argue that a better solution would operate either in cutoff or saturation. Turns out we have one which operates alternatively in both. Enter Switching regulator.
The first time I dealt with the switching regulator, I was really in a bad shape. I just needed something that would regulate my battery voltage down to 5V and give me currents around 1A. And it did just that. What is it all about? 


The MC34063 switching regulator

As you can see, it's just an 8 pin IC. Of course, you need some passive components(resistors, capacitors and  inductors) to get a functional circuit. There is a science behind the values of these components but there is a lot of maths involved(I discuss this in another post). However, if you need to step down 12V to 5V with currents of 1A, I've got that covered.
So, here's the schematic(made with Eagle):


Schematic
In the picture below, you have the circuit mounted on breadboard.
It's important that you use a fast schottky diode(1N5819) instead of the traditional diode. I had some trouble finding the 1N5819 locally but the 1N5822 should do fine.