Basic question Configuration Bits

Discussion in 'Embedded Systems and Microcontrollers' started by AMSA, Jun 30, 2015.

  1. AMSA

    Thread Starter New Member

    Jun 30, 2015
    5
    0
    Hello guys,

    I am new into this microcontroller world.

    I have one question regarding the configuration bits. Why in some cases I have seen people configuring the "configuration bits" with #pragma and in some other cases using __CONFIG(FOSC_INTOSC &WDTE_OFF & PWRTE_OFF & MCLRE_ON& CP_OFF & BOREN_OFF & CLKOUTEN_ON & IESO_OFF & FCMEN_OFF);

    What's the difference?

    Regards and thank you in advance.
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    It depends on which version of which compiler they are using.

    Look up the manual for the compiler you are using to know which format you should use.
     
  3. AMSA

    Thread Starter New Member

    Jun 30, 2015
    5
    0
    I see, however I was looking for something more like this: MPLAB-C18 Libraries-51297f. Here you can find all the functions in the C18 compiler. There is anything like this for the XC8?

    By the way, I have seen a bunch of tutorials where people configure their "configuration bits" but without saying why they are doing that and why it is necessary. Of course there are some configuration bits that are obvious why to configure in a specific way, but there are other that are not. I would like to know if someone knows any reference where the configuration bits are explained in general, or in this case for the 18F4550 and when to use each of them and why they are used.
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
  5. RJohnson

    New Member

    May 29, 2011
    21
    3
    For Microchip 8 bit parts (XC8) just look at the .pdf datasheet file for the part. In your case the PIC18F4550.pdf file. Then find "Special features of the CPU" section.
    XC8 uses the #pragma directive to set configuration bits. This will be in the C compiler user guide.

    It's a little different for 16 bit parts using XC16 compiler. Then you need to look at the header file for the part too.

    I agree this can be a bit confusing but one should know in advance how you want to configure the chip. The settings are not destructive and can be changed and re-compiled in MPLAB-X.
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,344
    1,025
    Good advice. And XC8 will do the #pragmas for you:
    Click Window->PIC Memory Views->Configuration Bits. An interactive window will appear with all of the config bits for the PIC displayed.
    Click in the Option column and use the drop-down to select from the available settings for each configuration setting. This is where you need to read the datasheet to see what each does. Be SURE to visit every option - no shortcuts here.
    When you are happy, click Generate Source Code to Output. An output window will open with #pragmas (or __CONFIG as applicable) for all of the config settings the PIC needs.
    Copy/paste the text into your source and you are good to go.

    Have fun.
     
  7. RJohnson

    New Member

    May 29, 2011
    21
    3
    Thanks John,
    I have seen that but have always resorted to the longer harder method.
    Goes to show we can always learn new ways to do things.
     
  8. AMSA

    Thread Starter New Member

    Jun 30, 2015
    5
    0
    Hi guys, thank you all for the reply. Ok, Johnson you are but for example, if I want to interact with an LCD, where can I find the LCD commands that might exist in the XC8 compiler to configure the LCD, initialize the LCD and so on? Like what happens with MikroC, they have libraries which have implemented those functions, in which can initialize the LCD, write to the LCD, etc.
     
  9. RJohnson

    New Member

    May 29, 2011
    21
    3
    LCD writing is a whole different can of worms.
    You will NOT find LCD commands in the XC8 compiler.
    I recently wrote code for a 4 bit interface to Lumex LCM-X02004DSR LCD Display using Hitachi HD44780.
    Lumex provided no instructions and no help after a phone call.
    This Hitachi chip is fairly common.
    I did this using XC8 on a PIC18F14K22 chip.
    I ended up doing a lot of research on the internet for HD44780 before I found something that worked and then
    I modified it for my purposes. Maybe I can dig out the link.
    Prior to this I only used SPI serial for LCD and that was even a bit to get working.
    I also wasted a lot of time on a NewHaven LCD with SPI interface only to find the device had problems with the signals.
    Took a scope to find it. So no NewHaven with SPI for me. Will try 4 bit on it next.
     
  10. JohnInTX

    Moderator

    Jun 26, 2012
    2,344
    1,025
    XC8 does not have the extensive peripheral library support that MikroC does. You can search Microchip.com for XLCD for their version of a library. I haven't used it, preferring to write my own. Likewise, I don't usually like the 'backpack' approach. It can be useful when you need to save IO lines but many that I've seen/helped to debug introduce their own set of issues as @RJohnson found out.

    If you search on AAC you'll find several threads that concern the Hitachi 44780 controller in most dot matrix LCD modules. Here are a few that came up in my posting list. There are doubtless many others.
    1 2 3
    These address some poster's problems but also include enough source code to give you an idea how to code the display and issues to consider.

    The attached datasheet contains all you need to know to write the low level display driver - init, write a character, 4 bit/8bit etc. If you want to roll your own, do the research, flow out the code, write it and give it a try. If you need help, post the schematic and code - there are plenty of folks here that know the 44780 inside and out.

    Good luck.
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I've used the Microchip XLCD library extensively over the years and I would not recommend it: there are just too many things that need tweaking in it before it will work for you.

    I even want to re-tweak the version I use after learning certain facts after my last round of tweaks to make it run an LCD powered by 5V from a controller at 3.3V (you can just connect them together as the 44780 is fine with a 3.3V logical one).
     
  12. AMSA

    Thread Starter New Member

    Jun 30, 2015
    5
    0
    Thank you for the reply.

    If the XLDC (it was and example, I could have mentioned the ADC library, to work with ADC or I could have mentioned another one) is so bad, what is the alternative? I have to make my own library? How's that done?

    Regards
     
  13. RJohnson

    New Member

    May 29, 2011
    21
    3
    Yes, you will have to make your own. At least I think it's the best way as that's how you learn the most.
    Here's a link to what I found. I modified these files for my use and you probably will have to as well.
    But it is a start. :)

    http://embeddedlifehelp.blogspot.com/p/blog-page_2.html

    Good luck.....
     
  14. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,524
    2,369
    Microchip came out with a later recommended method of setting config bits.
    This is the assembly version for 18F46K22

    Code (Text):
    1.  
    2. ; CONFIGURATION WORD SETUP
    3. ;
    4. ; The 'CONFIG' directive is used to embed the configuration word within the
    5. ; .asm file. The lables following the directive are located in the respective
    6. ; .inc file.  See the data sheet for additional information on configuration
    7. ; word settings.
    8. ;
    9. ;------------------------------------------------------------------------------
    10.  
    11.      ;Setup CONFIG11H
    12.      CONFIG FOSC = INTIO7, PLLCFG = OFF, PRICLKEN = OFF, FCMEN = OFF, IESO = OFF
    13.      ;Setup CONFIG2L
    14.      CONFIG PWRTEN = OFF, BOREN = OFF, BORV = 190
    15.      ;Setup CONFIG2H
    16.      CONFIG WDTEN = OFF, WDTPS = 1
    17.      ;Setup CONFIG3H
    18.      CONFIG MCLRE = EXTMCLR, CCP2MX = PORTB3, CCP3MX = PORTE0, HFOFST = OFF, T3CMX = PORTB5, P2BMX = PORTD2
    19.      ;Setup CONFIG4L
    20.      CONFIG STVREN = OFF, LVP = OFF, XINST = OFF
    21.      ;Setup CONFIG5L
    22.      CONFIG CP0 = OFF, CP1 = OFF, CP2=OFF, CP3=OFF
    23.      ;Setup CONFIG5H
    24.      CONFIG CPB = OFF, CPD = OFF
    25.      ;Setup CONFIG6L
    26.      CONFIG WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
    27.      ;Setup CONFIG6H
    28.      CONFIG WRTB = OFF, WRTC = OFF, WRTD = OFF
    29.      ;Setup CONFIG7L
    30.      CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
    31.      ;Setup CONFIG7H
    32.      CONFIG EBTRB = OFF
    33.  
    34.  
    Max.
     
  15. RJohnson

    New Member

    May 29, 2011
    21
    3
    Max,
    Your right. To make things interesting they do it differently in .asm and .c files.
    I don't think your version will work in C. See section 5.14.4 in C Compiler User's Guide.
    Correct me if I'm wrong here.
     
  16. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,524
    2,369
    I can't comment on that, I only used assembly, the older method shown in the OP was also used originally in the assembly version also.
    Max.
     
  17. AMSA

    Thread Starter New Member

    Jun 30, 2015
    5
    0
    Thank you all. I have understood now how to configure the "configuration bits". Regarding the libraries, I will study a bit more in the near future when I start using them and then see if there is any problem using them in my projects.
     
Loading...