Basic question Configuration Bits

Thread Starter

AMSA

Joined Jun 30, 2015
10
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.
 

tshuck

Joined Oct 18, 2012
3,534
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.
 

Thread Starter

AMSA

Joined Jun 30, 2015
10
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.
 

RJohnson

Joined May 29, 2011
21
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.
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.
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
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.
 

RJohnson

Joined May 29, 2011
21
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.
 

Thread Starter

AMSA

Joined Jun 30, 2015
10
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.
 

RJohnson

Joined May 29, 2011
21
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.
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
 

Attachments

ErnieM

Joined Apr 24, 2011
8,377
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).
 

Thread Starter

AMSA

Joined Jun 30, 2015
10
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
 

MaxHeadRoom

Joined Jul 18, 2013
28,698
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);
.
Microchip came out with a later recommended method of setting config bits.
This is the assembly version for 18F46K22

Code:
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the
; .asm file. The lables following the directive are located in the respective
; .inc file.  See the data sheet for additional information on configuration
; word settings.
;
;------------------------------------------------------------------------------

     ;Setup CONFIG11H
     CONFIG FOSC = INTIO7, PLLCFG = OFF, PRICLKEN = OFF, FCMEN = OFF, IESO = OFF
     ;Setup CONFIG2L
     CONFIG PWRTEN = OFF, BOREN = OFF, BORV = 190
     ;Setup CONFIG2H
     CONFIG WDTEN = OFF, WDTPS = 1
     ;Setup CONFIG3H
     CONFIG MCLRE = EXTMCLR, CCP2MX = PORTB3, CCP3MX = PORTE0, HFOFST = OFF, T3CMX = PORTB5, P2BMX = PORTD2
     ;Setup CONFIG4L
     CONFIG STVREN = OFF, LVP = OFF, XINST = OFF
     ;Setup CONFIG5L
     CONFIG CP0 = OFF, CP1 = OFF, CP2=OFF, CP3=OFF
     ;Setup CONFIG5H
     CONFIG CPB = OFF, CPD = OFF
     ;Setup CONFIG6L
     CONFIG WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
     ;Setup CONFIG6H
     CONFIG WRTB = OFF, WRTC = OFF, WRTD = OFF
     ;Setup CONFIG7L
     CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
     ;Setup CONFIG7H
     CONFIG EBTRB = OFF
Max.
 

RJohnson

Joined May 29, 2011
21
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.
 

MaxHeadRoom

Joined Jul 18, 2013
28,698
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.
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.
 

Thread Starter

AMSA

Joined Jun 30, 2015
10
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.
 
Top