Getting Started with Hi-Tech C – III
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.
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.
