Configuration Words for the PIC16F690

13 August, 2008 (16:35) | PIC, Tutorials | By: Joshua

So, do you remember, long ago, when I mentioned that I didn’t know why the PICkit 2 would have that warning about configuration words when importing the hex file? No? Well, I did. Anyway, I figured it out and wanted to share with you exactly what I’ve learned. And, as always, now that I know what it means and how to change configuration words, I feel pretty dumb. This tutorial is again specifically oriented for the PIC16F690 but I believe the concepts are universal.

My issue: In a board design, I was planning on using pin 3 for my A/D conversions. However, with the default configuration, pin 3 is setup as the Oscillator Clock Out. This does not jive well with having any form of input. So, I start poking around the data sheet, trying to figure out how to change that. I find it on page 195 of the PIC16F690 datasheet. It tells me what to change, and life is good. I setup my program to change those bits and when I save it, Hi-Tide tells me it doesn’t know what FOSC means. Or any of the other bits from the CONFIG register. Jumping up to page 194 was my answer. In a nice little blue box, it says:

“Address 2007h is beyond the user program memory space. It belongs to the special configuration memory space (2000h-3FFFh), which can be accessed only during programming. See ‘PIC12F6XX/16F6XX Memory Programming Specification’ (DS41204) for more information.”

Roughly translated: “Haha! You can’t change me with your current pool of knowledge! Or should I say puddle?” Insulted by a data sheet. Harsh.

But, eventually, the answer was made known to me.

Configuration words can only be changed when you’re programming it (even I understood that from the mean-spirited data sheet) but they’re still decided on in the C program. Near the beginning of your program, you should start a line with a double underscore (remember: 2 underscores) followed by CONFIG, like this: __CONFIG. Have I stressed the double underscore enough?

Open a parentheses directly after CONFIG and then simply type in the number you want for your configuration, close the parentheses, and tack on a semicolon. Using the information in the data sheet about the configuration word, you can set each bit as you need. Example of a default configuration would be:

__CONFIG(0b111111111111); or __CONFIG(0×0FFF);

HI-TIDE has an easier way of doing this. In the include file, it has defined names for the different configurations. In the left hand panel, you can find your include files, and they’ll be defined with more intelligible names in that file. For me, they were located near the very bottom of the file.

Header File for PIC16F690

And then you simply put what you want together like this:

__CONFIG(WDTEN & PWRTEN & MCLREN & BOREN & UNPROTECT & INTIO);

Which is much more intelligible. I can look at that and say, “Hmmm… I’m enabling the Watch Dog Timer, the Power Up Timer, the Master Clear Reset, Brown Out Reset, I’m leaving my code unprotected for anybody to steal, and changing the oscillator so that pin 3 is now a regular I/O pin.” Convenient, huh?

With this configuration line, you won’t get the warning message as you import your hex file into the PICkit 2. You also have greater control over your PIC and much more flexibility.

As always, this may seem crystal clear to me, but if it’s not to you, let me know and I’ll try and clear up as much confusion as I can.

Using the PICkit 2

2 August, 2008 (10:40) | PIC, Tutorials | By: Joshua

Using the PICkit 2

The PICkit 2 is a very common way to program your PIC. It can handle a wide variety of PICs and seems to be the established standard for the normal person to program their PIC microcontroller. Since last time we created a .hex file for this moment, lets go through the steps to actually use it. Again, due to the PICkit 2 Low-Pin Count Demo Board I’m going to be using the PIC16F690 as an example.

I’m assuming if you have the PICkit 2 hardware, it came with the software. It is a typical installation and doesn’t have any weird settings you have to choose. I just updated to version 2.52 but don’t see any really big differences between it and version 2.12 that I was using. If you can’t find your software installation CD somewhere, go to Microchip and search for PICkit 2. One of the first results should go to a page with a PICkit 2 V2.xx Install link.

Make sure your PICkit 2 is plugged into the USB of your computer and that it is properly hooked up to your microcontroller (either through a demo board or a breadboard). If you don’t have them hooked up before you start the program, then the PICkit 2 program will demand you go to Tools -> Check Communication for it to detect the PICkit 2 after it’s connected. Another added step… It should auto-detect your PIC and if everything is kosher, you should see this page:

PICkit 2 Upon Starting

Note the notification box that says, “PICkit 2 found and connected. PIC Device Found.” If it doesn’t have the second line, make sure your PIC is connected properly. Again, the datasheets for the PIC you are using and the PICkit 2 are very useful and I usually have them on hand for quick reference. If you can’t find the answer in there, let me know and I’ll see what I can do.

Go to File -> Import Hex. Find your .hex file and select it. Your notification box may turn yellow and say, “Warning: No configuration words in hex file. In MPLAB use File-Export to save hex with config.” If so, go look at this post. Otherwise, it should say you had a successful Hex file import.

Click on the Write button and things should start flashing on the screen, the green bar should start going back and forth, and your LED might flicker a little bit. If you’ve done everything correctly up to this point, your notification box will be green with “Programming Successful”. Then, to supply voltage to your PIC and LED from your USB port, click on the VDD PICkit2 On checkbox. Your screen should look like this:

Screen after Programming

Your LED should be burning brightly. And that’s it! Everything you need to know to start experimenting and playing around. So, go wild. See what sort of crazy ideas you can do with your newfound power.

As always, leave a comment or e-mail me if you have any questions or concerns.

Getting Started with Hi-Tech C – III

29 July, 2008 (21:31) | PIC, Tutorials | By: Joshua

Intro to Hi-Tech C III

Finally, we’re going to get through the end of the basics of Hi-Tech C and Hi-Tide. By the end of this tutorial, you will have a hex file you can load onto your PIC that will turn on an LED. It should be noted that all of this is geared toward using the PIC16F690 but should be applicable to most other PICs.

Double click main.c underneath your project name and the main window will open up main.c with a little template code. Here we will write our simple little “Hello World!” code. Due to the fact that the PICkit 2 Low Pin Count Demo board has LEDs connected to RC0-RC3, we will use PORTC to light up our LED.

If you don’t have the demo board and are doing things with a breadboard, RC0-RC2 are pins 16-14 (yes, backwards from our normal thinking) and RC3-RC5 are pins 7-5. I would also strongly recommend downloading the datasheet from Microchip. Also, remember, when hooking up your LEDs, you’ll need a least a couple hundred ohm resistor in there and in this case, hook your cathode up to ground. And while I’m not going to take the time to explain it now, you’ll need to figure out how to get your programmer to communicate with your chip. I would suggest using the PICkit 2 datasheet (just Google it), it is quite informative.

First, we write our little code.

/* My first C program on a PIC using Hi-Tech C
 * by Josh
 */

#include <htc.h>
void main(void)
{
while (1) {	        // Throw this into a loop
    TRISC = 0x00; 	// Make PORTC an output
    PORTC = 0b00000001;	// Turn on the LED on RC0
	}
}

I like to write out the binary for this because it is indicative of which I/O on PORTC that I’m dealing with. Meaning: I write 0b00000001, then RC0 goes high. I write 0b00000010, then RC1 goes high. 0b00000011, then both go high. All the way up to RC7. Easy peasy, right? That’s the way I like it. So, depending on what pin you want to use, you can change that to whatever you want.

Also, Hi-Tide has some built-in libraries and the include statement allows you to just type in PORTC or RC2 or most any other simple name instead of actually having to look up the register address. Also, it helps finds errors becuase if Hi-Tide doesn’t recognize your name, it is probably wrong (example: TMR2IF versus TMRIF2 was a recent mistake of mine)

Now, save the code. Hi-Tide will take a moment because it is not only saving your code, it is checking it for errors. If there are any errors, they will pop up in the bottom box under the tab Problems. You shouldn’t have any issues at this point but if you plan on doing anything else, you definitely will.

And that’s it for the code! “Hello World” programs are supposed to be pretty straightforward.

To get it ready for the PIC, you’re going to need to export the code. Go to File -> Export. It’ll pop up a window asking for an export destination. Select File System and continue to the next page.

You will need to open the sub-categories of your project, and make sure to check the box next to Release as shown in the picture below. I must emphasize this because if you don’t click that button, it won’t actually export anything. And it is unbelievably frustrating when you change your code and the microcontroller acts the same way. Over and over again, hating electronics and life in general. Until you figure out you’ve been forgetting to check that box and … not that I’ve ever had that experience, of course.

Hi-Tide Export Screenshot

Observation – I’ve noted that if you don’t save your code before trying to export, you’ll receive a prompt to save but then you’ll most likely get an error when you try to actually export. I believe it is because it then attempts to export before it actually finishes saving, but I’m not certain.

Click finish and your code will be converted to .hex for your microcontroller pleasure. It also produces a lot of other files but I’m not a programmer and I don’t know what they’re for. If you’re like me, you can ignore them and move on. If you’re not, find out, let me know, and I’ll put it up here for other curious saps such as yourself.

Next time we’ll walk through getting the .hex file on the Pickit 2 but that’s pretty straightforward in comparison to all this stuff. If I haven’t got the next tutorial up by the time you read this, try it by yourself. It should be pretty painless. If not, write me to tell me to hurry up. I need a prodding every now and then.

And hey, I don’t write this for my health. If you have any questions or comments, please feel free to let them fly. That’s why we’re here.

A Simple Schematic and Board in EAGLE – Part I

29 July, 2008 (20:42) | Eagle, Tutorials | By: Chris

Seven Segment LED – Schematic

With the most basic pieces of the software covered, you should feel comfortable with the idea of doing a simple project. I’ll only be using parts from the libraries supplied with EAGLE in order to keep the focus on the schematic and board themselves, though we will cover creating custom parts and libraries in the near future.

In this example, we’ll be designing a seven segment led display controlled by four DIP switches. While this isn’t something that you would likely ever need to build, it is a useful exercise to learn the basic features of the software. Below are the icons and descriptions for the tools that will be used in this example.

Toolbar Buttons

Add – adds components to the schematic

Autoroute – attempts to route all incomplete connections (airwires) on the board

Board (Schematic) – switches from the schematic to the board (and vice versa)

Copy – copies components (and applicable values) already placed on the schematic

DRC – performs a design rule check on your board (I’ll cover this with creating gerber files)

ERC – performs an electrical rule check (more on this later)

Errors – lists the errors found by the DRC and ERC

Group – allows multiple components to be selected

Invoke – allows adding normally implicit pins to the schematic

Mirror – flips components

Move – moves components

Net – creates an electrical connection between two pins on the schematic

Ratsnest – cleans up the airwires showing connections that still need to be made on the board

Ripup – removes already routed traces from the board

Rotate – rotates components

Route – routes traces on the board (only used on airwires)

Value – changes the value of a component (such as a resistor)

Wire – routes traces on the board without being restricted to airwires

Once you know the project requirements, you should figure out what parts you need to fulfill those requirements. For this project, we’ll use the following parts:

Qty

Description

Part

Purpose

1

Common Cathode 7 Segment LED Display

7SEG-CK

1

BCD to Seven Segment Decoder

4511D

1

4x DIP Switch

SW_DIP-4

to set the LED value

1

BNC Connector

B35N57

to power the board

7

0603 Resistors

R-US_R0603

current limiting resistors

As stated earlier, all of the parts I’ll be using can be found in the libraries included with EAGLE. To start, right click on the eagle folder on the EAGLE Control Panel and select New Project. Name your project, right click on its red folder icon, and select New -> Schematic. On the left toolbar, click on the Add button. When the Add dialog appears, type “*7*segment*” in the search box and press Enter. EAGLE’s search function can be finicky, so I tend to be pretty liberal in my use of asterisks. Towards the bottom of the list of parts, there should be one called 7SEG-CK (any common cathode seven segment LED will work). Select it, press OK and click to place it on the schematic. Repeat this process for the other parts, searching for the part number listed in the table above. In the end, you should have a schematic that looks something like the image below.

If you didn’t place 7 resistors, you can click the copy button on the left toolbar, click on the resistor you did place, then place the new one just like you would have if you had selected it from the Add dialog.  For this design, the output side of the DIP switches (marked with “On”) needs to be connected to the inputs of the 4511D (IA, IB, IC, ID).  LT and BI on the are connected to VDD, while LE is connected to GND.  The outputs (A-G) are connecteed through the resistors to the corresponding pins on the Seven Segment LED.  The cathodes and decimal point of the LED are connected to ground, while the inputs of the DIP switches and the center pin of the BNC are connected to VDD.

In order to make connecting the pins easier, I suggest rearranging the parts so that the signals flow from left to right.  This is also good practice as it makes it easier for someone (including yourself) to read your schematics in the future.  To move the parts, left click on the move button then click on the center of the part, move it where you want it, and left click to place it.  To move a group of parts (such as the resistors), click the group button and click and drag around the parts you want to select them.  To move them, click the move button, but instead of left clicking, right click the group, select move group, then place the group where you want it by left clicking.  You can see mine in the previous picture.

While it isn’t technically necessary, you’ll want to add supply schematics (VDD and GND) to the schematic.  Look for them in the supply and supply2 libraries.  For now, don’t worry about the name of the supply, just pick a positive supply and a ground.  Connect the pins as described above by first clicking on the net button, clicking on a pin, then clicking on the pin you want to connect the first pin to.  For example, after selecting the net tool, click on LE, then click on your ground symbol.  If done correctly, the net should terminate on the second pin.  Otherwise, you will continue seeing it as you move your mouse.  If you want to change the bend of your net, simply right click before you click on the second pin.  You’ll see the net change.  If you look on the top toolbar, you’ll see EAGLE cycling through the diffferent bends.  Finish connecting the pins and your schematic will look similar to the one below.

Now that the schematic is done, you need to check for errors by clicking the ERC button.  When I click on it, I have errors. While it looks like everything is connected, there are some implicit pins that will cause errors.  These pins aren’t normally shown on the block diagram.  In this case, the power and ground pins for the 4511D are hidden.  If you selected VDD and VSS for your power and ground supplies, you won’t see this error because the power and ground pins of the 4511D are implicitly connected to VDD and VSS.  If you need to show these implicit pins, click the invoke button and click on the 4511D.  EAGLE will show a dialog box with any available schematic symbols.  Select the row with P for the gate value, click OK, and place the power pins on the schematic.  Connect VDD to your positive supply and VSS to your ground supply and your schematic should look similar to mine.

This is already a very wordy post.  If anyone could suggest some cheap (read free) desktop video recording software that can record at a decent frame rate, please email me or post your suggestion in the comments.  In the next post I’ll cover turning this schematic into a board.  If you want to get ahead, just click the Board/Schematic icon and start playing around with the board.

Getting Started with Hi-Tech C – II

22 July, 2008 (00:07) | PIC, Tutorials | By: Joshua

Now that the program is on the computer and ready to go, we can get to the interesting parts. In this brief tutorial, we’ll get you started on making a project and figuring out where to put your code.

Creating a Project

To start out, you need a project. The project is the directory where all the files are stored and, while overkill at first, becomes helpful once the programs get more difficult and involved. We’ll only talk about what you need to actually get something done and we’ll ignore the rest until later.

Though you don’t necessarily have to do it this way, we’ll start from the workbench. You’ll start at this page (you can see the current projects I have in the left panel):

HI-TECH C Workbench

Go to File -> New -> Project It’ll pop-up a page asking which wizard you’d like to use to create the project. Select the PICC-Lite Project.

Select a Wizard for the project

You may have more choices than this, but this is the one you want to choose. On the next page, choose whatever project name you want. I’ll put in Monkey_Shorts because I’m kinda crazy that way. Continue to the next page, where you’ll want to select Hex Project Type (and for me it’s the only choice) and then Release as the build configuration. Next, choose your chip. Since a 16F690 came with the PICkit 2 Low Pin Count Demo board I bought, that is the chip I’m most familiar with. If you have a different one, pick that.

Microcontroller Selection

The next page seems somewhat pointless to me. What does the compiler care about the package of the chip? Isn’t is the same chip either way, receiving the same programming? Anyway, make sure the Create main function box is selected, and then click Finish.

Now you can see the new project in the left panel and you can also see main.c. Left clicking this file will bring up main.c for editing in the main window.

Workbench with Project

In the next and final “Getting Started with Hi-Tech C” tutorial, we’ll finish this up by writing a small “Hello World” program and figuring out how to export a .hex file for our PIC.

EAGLE – An Introduction

17 July, 2008 (23:21) | Eagle, Tutorials | By: Chris

EAGLE (Easily Applicable Graphical Layout Editor) is a PCB design tool with a freely available Light version. While there are restrictions on the use and limitations on the capabilities of the freeware version, it provides students and hobbyists with a great tool at no cost. In the coming weeks and months, articles posted here will explain how to use this tool to the best of our knowledge and ability. With the free version of EAGLE, support is limited to email and what can be found online. There are certain quirks and nuances that can be frustrating, such as merging two boards (schematics, layouts, or both). Before getting into this, we need to cover the basics.

After installation is complete, you’ll see a screen similar to the one below. We’ll start by covering the “Libraries” and “Projects” trees, as they are the two you’ll need to get started.

Under the “Projects” tree, you’ll find a few example projects to begin. Expand the examples section and find one of the example projects; it will have at least one .sch file and one .brd file.  A .sch file is a schematic file; it’s a block diagram representation of the board you’re designing.  The .brd file is a board file and it shows the physical layout of the board. Double-click either file to open it and Eagle should open the matching board or schematic. I opened the singlesided example, as shown below. In this example you’ll see various components, and a lot of yellow lines between the components on the board. These yellow lines, or air wires, represent signals from the schematic that need to be routed.  We’ll go into more detail about how to use these in the next post.

These signals can be routed using EAGLE’s autorouter, by manually routing each signal, or a combination of both. Depending on your needs, any one of the three can be a good choice. To see how the autorouter works, click on the autorouter button , located towards the bottom of the left sidebar. The autoroute dialog appears.

Select the asterisk in the drop-down menus labeled “1 Top” and “16 Bottom” to allow the autorouter to route in any direction. Selecting N/A disables the layer, and selecting any of the other options sets the preferred routing direction for that layer. Press OK and watch as EAGLE routes all of the signals for you. EAGLE won’t always be able to route all of your signals, but it can at least get you started.

In the next post, we’ll cover how to make your own schematic and board from scratch.  Until then, play around in the software to get used to the interface and some of the terminology that EAGLE uses.

Getting Started with Hi-Tech C – I

16 July, 2008 (22:35) | PIC, Tutorials | By: Joshua

The History

Most people don’t really thrill at the idea of writing huge amounts of code in assembly, or, if you’re normal, any amount of code in assembly. Well, this can cause some problems if you want to use a 10-17 series PIC. Basically, MPLab doesn’t support C for anything less than an 18 series PIC. Rude, eh? However, if you really need a 10-17 series PIC (such as the rfPICs) there is another option: HI-TECH C.

HI-TECH is a third party that has received the stamp of approval from Microchip as the official C compiler provider. In its free state, there are serious limitations in what kind of PICs you can use and how much of the memory on those PICs you can use. However, these limitations have not caused me any problems in my learning, hobbyish stage.

So, I know you’re thinking, “Enough, enough! How do you use it?” Since this is a high-powered environment/compiler designed for people who do this for a living, there are a lot of gadgets and features that can confuse somebody (at least me) who only wants to write a small “Hello World” program and get it on their microcontroller. Once the steps are shown to do it, they’re quite simple. I’ll show them to you.

Getting the Software

Go to the HI-TECH Demos Web-Page and scroll down to the bottom of the screen where it says Free Software. You will want to click on “HI-TECH PICC-LITE” under the operating system of your choice. The next page will require you to sign up (life nowadays, need to register to pick your nose anymore…) and then the download will begin. About four megabytes later, you will open the installer. The steps are straightforward, just make sure you select the box **HI-TIDE Integrated Development Environment

HI-TECH Installation Splash Screen

Adding the environment path is up to you. I didn’t because I’m not much of a command line guy anymore but I don’t think you’ll hurt anything either way. After this, your installer will download HI-TIDE, the environment, which is about 80 megabytes and took me about 5 minutes on my 1.5 MB DSL line.

Once everything is installed and you open Hi-Tide for the first time, you will see this screen:

Get Started Page

Click on Workbench, which should take you here:

HI-TECH Workbench

Now you’re ready to get down to it. Next, we’ll go through the microcontroller’s version of “Hello World” as I show how to create a project, write some code, and put it on your PIC processor using a 16F690 as an example.

Any feedback about this post, either in regards to content, grammar, or style, would be most welcome. I will most likely change this post multiple times until I like it. Please either leave a comment or e-mail me and let me know what you think.

Web-Page Up and Running!

31 May, 2008 (18:34) | Admin Posts | By: Joshua

If you consider a stumbling shuffle running, I should say. Since we don’t really care too much about how things look, and we’re really not web-page designers, it has been hard to put this together. And, as you can tell, there is still a lot of work to be done.

However! We did not start this web-page to be bogged down in web maintenance! We want to do projects, we want to do cool things, we want to blow stuff up! So, let’s worry about the fine tuning later and get into the cool stuff. Hopefully very soon, this page will be full of the incredibly interesting and intelligent ramblings of myself and Chris as we traverse the wild unknown of Electrical Engineering.