Writing C Programs For Pic

Posted on -
  1. Best Programs For Writing Books

Hi Members,Thanks all of you for prompt response. Looks like as I am new to microcontroller programming I could not ask question properly.I am going to use PIC16F18345, MPLABX IDE, XC8 compiler to compile C code.I wanted to know the basics of microcontrller programming.

For

Here are the questions:1. What are xc.h, htc.h etc. Header files and when they are used?2. What are Configuration bits and how they are used?3. How to write code in C to use the ports, timers, counter and timers and UART/USART?4.

How to program SFR's?Let me know the answers of basic questions and also you can point me the web link where i can learn easily and in systematic way. Please point me to simple sample codes for basic microcontroller programming in C learning.Thanks & regards,Sarvanan. Sarvanan, I think there are two parts to what you need to learn here. One is C programming and the other is PIC microcontroller programming.

For

My suggestion will be to learn them in those two steps. TRISA, GPIO, etc are registers specific to the PIC, not C.

You need to look through the Data Sheets for the specific micro to know their functions and how they are used. First, are you proficient in C language? If not, learn that first. Pick up a book or tutorial and learn that first.

After you somewhat comfortable with that then learn the PIC, using a development board. Sarvanan Hi Stephani, Thanks for the mail. I know C language programming well. I know that TRISA and GPIO's are PIC specific.

May be i mixed my questions or questions were not clear that's why it gave you another impression. I will go through the datasheet thoroughly. I will ask questions if I don't understand anything in the datasheet. With regards, Sarvanan.It is good that you know C language well. In that case, get a PIC development board with the micro that is the same or most similar to the one you want to use. Look through the sample code in the wikidot and walk through those.

I am using PIC32, but I imagine it will be similar for PIC16. The examples will have code to configure the micro.

Look through the datasheets for the particular areas you have questions. Don't confuse yourself, buy a good book on PIC Microcontroller programming or any good tutorial website. Start with simple tasks like blinking an LED, by this you'll learn few registers like PORT, LAT, TRIS, etc. And some few functions as well and hopefully you'll learn how to apply some C syntax to execute those simple tasks and if you have a specific question then you can ask. After this then you can learn more peripherals as you progress like timers, adc, UART. It won't make any sense to you if some one can try to explain any of those terms to you at this stage. Have you watched mplabxpress tutorial series?

What are xc.h, htc.h etc. Header files and when they are used? With XC8, always include xc.h, never include htc.h, include others if you need. What are Configuration bits and how they are used? Hard-coded values that govern PIC behaviour, mostly clock and protection, watchdog timer, some pin assignment, could be anything else.

Really, anything, as long Microchip thinks it would be fun to watch you pulling your hairs out because you're unable to change the damn thing at run-time. These are listed in the datasheet and are specific to the PIC. How to write code in C to use the ports, timers, counter and timers and UART/USART?4.

Best Programs For Writing Books

How to program SFR's?SFRs are listed in the datasheet and also explained therein, including ports, timers etc. Some are accessed as a whole.

For these you can use the SFR name as a variable in your C. Some have bit-fields inside. You still can access them as a whole, but you can also access bit-fields directly. You add 'bits.'

To the SFR name then append the field name. For example, if SFR name is PORTA and you want to access a bit-field called RA0, you do:x = PORTAbits.RA0. As others have mentioned, in the world of embedded, flashing an LED (or 'blinky') is the equivalent of Hello World in the non-embedded world. Before you do anything else, get a blinky working! I did a video on YouTube a few months ago on doing a blinky from scratch on a PIC16F18313, from components and a breadboard through to writing the C program to do it.

If you just want to look at the code in MPLAB X, skip ahead to about 6:45. The PIC16F18345 is essentially a 20 pin version of the PIC16F18313, so the example will work with your chip too: even the pinout is compatible: if you take the top eight pins of the PIC16F18345 they map directly to the 8 pins of the PIC16F18313, you can ignore the other pins just to get the blinky going.