Programming PIC Microcontroller in C

Discussion in 'Embedded Systems and Microcontrollers' started by sarvanan, Sep 10, 2016.

  1. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    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.,
    Code (C):
    1. int a[5]={1,2,3,4,5};
    2. void main(void)
    3. {
    4. int *p,i;
    5. p=a;
    6. for(i=0;i<5;i++)
    7. printf(%d”,*(p+i));
    8. }
    In the same way, how to write complex program? Please let me know the tutorials.

    Thanks & regards,
    Sarvanan.
     
    Last edited by a moderator: Sep 10, 2016
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
  3. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    Hi Albert,

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

    Thanks & regards,
    Sarvanan.
     
  4. sarvanan

    Thread Starter Member

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

    Thanks & regards,
    Sarvanan.
     
  5. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    Google 'mplab x ide simulator tutorial' and choose which you fancy.

    The Microchip link I gave above is a sample project for this uC and XC8.
     
  6. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    K&R: C is C, whether it is applied to a Cray or a PIC.
     
  7. Robin Mitchell

    Well-Known Member

    Oct 25, 2009
    732
    200
    @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
     
  8. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    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.
     
  9. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    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.
     
  10. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    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.
     
  11. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    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.
     
  12. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    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.
     
  13. sarvanan

    Thread Starter Member

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

    With regards,
    Sarvanan.
     
  14. microtutor

    New Member

    Sep 14, 2016
    1
    0
    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.
     
  15. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    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.
     
  16. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    Are we going to have to do this for every PIC uC?
    Do you not have access to a www search engine?
     
  17. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    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.
     
  18. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    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.
     
  19. sarvanan

    Thread Starter Member

    Aug 8, 2016
    42
    0
    Thanks Albert.
     
Loading...