Programming PIC Microcontroller in C

Thread Starter

sarvanan

Joined Aug 8, 2016
45
Hi members,

I am new to microcontroller programming. I want to program PIC 16F18345 microcontroller using C language on MPLABX IDE, XC8 compiler.

Please let me know good online tutorial where I can learn to program above said microcontrller in C language. I mean how to add headers, which header for what purpose and how to add libraries and what all libraries are available. I also need example sample program for pic16F18345.

One example, if i need to compile a program with pointer, an array what all needs to be done on mplabx ide selecting pic16f18345.
e.g.,
C:
int a[5]={1,2,3,4,5};
void main(void)
{
int *p,i;
p=a;
for(i=0;i<5;i++)
printf(“%d”,*(p+i));
}
In the same way, how to write complex program? Please let me know the tutorials.

Thanks & regards,
Sarvanan.
 
Last edited by a moderator:

Thread Starter

sarvanan

Joined Aug 8, 2016
45
And also please let me know the link for collection of sample code for pic16f18345, mplabx ide , xc8 compiler.

Thanks & regards,
Sarvanan.
 

AlbertHall

Joined Jun 4, 2014
12,347
Hi Albert,

Please let me know how to use Simulator to see the output.

Thanks & regards,
Sarvanan.
Google 'mplab x ide simulator tutorial' and choose which you fancy.

And also please let me know the link for collection of sample code for pic16f18345, mplabx ide , xc8 compiler.

Thanks & regards,
Sarvanan.
The Microchip link I gave above is a sample project for this uC and XC8.
 

Robin Mitchell

Joined Oct 25, 2009
819
@dannyf I think he is asking about the specifics of the C language that relates to the micro. For example, TMR4 register is not standard C and is a define for a register found in the SFR banks.

HiTec C would use LATA0 to access bit 0 whereas C18 requires LATAbits.LATA0
 

Thread Starter

sarvanan

Joined Aug 8, 2016
45
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. 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.

Thanks & regards,
Sarvanan.
 

AlbertHall

Joined Jun 4, 2014
12,347
1. xc.h includes values by the xc8 compiler and htc.h includes values used by the hitech C compiler.
2. The PIC chips have various settings in config which can only be changed by programming, not by software. See section 4 of the datasheet and in when you have set up a project in MPLABX look at window/pic memory views/configuration bits. It shows you the config bits and their descriptions. You can set values here and then 'generate to output' will produce code in the output window which you can copy and paste into your program.
3 & 4. Look at the XC8 users manual for how to write C code in XC8 and the PIC data sheet for details of the various peripherals and the SFRs which control them. The SFRs each have a memory address and you access them just the same as other ram addresses.
 

Thread Starter

sarvanan

Joined Aug 8, 2016
45
Thanks a lot Albert. You really taught me some basics.
Could you please tell or point me to the link which tells what are TRISA, GPIO etc and how are they programmed.
I am sorry if am asking stupid questions. I need basic programs and learn the conventions used in them and the reason why the are used.
Is these conventions TRISA, GPIO common for all PIC's? Do they work for PIC16F18345 also?

With regards,
Sarvanan.
 

AlbertHall

Joined Jun 4, 2014
12,347
Look at the PIC datasheet under I/O ports.
TRISA, TRISB etc control which pins of the port are to be inputs and which are outputs.
GPIO is the name for the only port on some earlier small chips. Mostly they are now known as PORTA, PORTB etc.
If you look at that section of the datasheet there will be a section on initialising the port. Do read that. Depending on which pic it is there may be things that you wouldn't think of e.g. ANSELA, ANSELB etc to select which pins are digital and which are analog (they default to analog so if you miss this you might spend a long time wondering why your digital I/O is wonky; also if the chip has one or more comparators which you are not using you need to disable those (COMCON) otherwise those pins will be analog causing similar problems. You need to get used to checking for the particular chip you are using as they are many different varieties despite many similarities.
 

AlbertHall

Joined Jun 4, 2014
12,347
It wouldn't hurt to read the datasheet cover to cover (though your chosen pic has one of the bigger datasheets) so you have some grasp of what the chip can do.
 

Thread Starter

sarvanan

Joined Aug 8, 2016
45
Thanks a lot Albert. I think now I have all the answers. I will go through the datasheet thoroughly.

With regards,
Sarvanan.
 
Hello Sarvanan,

Below are the links to 2 websites I learned from:

1) http://www.microcontrollerboard.com/

Even though the web site has info mainly about PIC16F877 chip, the tutorials can be applied to other PIC microcontroller chips as well.
All the info is FREE including, but not limited to:

And much more...

2) http://www.microchip.com/

This web has tons of the information about MicroChip PIC microcontrollers including video tutorials.

Hope this helps.
Lion K.
 

Thread Starter

sarvanan

Joined Aug 8, 2016
45
Hi,

Could somebody please point me to sample code in C language for PIC16LF1939?
For blinking, uart, adc, arithmetic operations, using port registers etc.

Thanks & regards,
Sarvanan.
 

Thread Starter

sarvanan

Joined Aug 8, 2016
45
Are we going to have to do this for every PIC uC?
Do you not have access to a www search engine?
Hi Albert,

I am very Sorry for this.
I did search but couldn't get specific for PIC16LF1939. But I understand and was able to write small code for this pic using data sheet and looking the header file specific to this pic. It's working.
I thought to get access to some sample code that is why asked. My intention was not to bother anybody. It was just an over enthusiasm. Thanks for your help and guidance. Sorry again.

Thanks & Regards,
Sarvanan.
 

AlbertHall

Joined Jun 4, 2014
12,347
Learn by doing. Write some code and try to fix the problems. There are many similarities between the various PICs and the datasheets are your guide always.
 
Top