PDA

View Full Version : Coding Style - PIC 18F4620 config fuses



BrianT
- 8th January 2008, 09:30
Can anyone provide a style guide on how they handle the configuration fuses for the PIC 18F4620? I realise they will be different for every application.

I use the MeLabs serial programmer (USB version) to load the MCSP bootloader and it is here I can set or clear a host of registers. This does not require me to do anything in the code like @ __CONFIG etc so I have not been bothering to set any explicit commands in my PBP code.

By doing nothing in the code, the PIC resorts to the defaults which work OK but which do NOT result in lowest power operation.

How do others handle this?

Cheers
BrianT

mackrackit
- 8th January 2008, 10:25
I do not use a boot-loader and the programmers I use are the PICSTART PLUS or PICKIT2 and mostly MPLAB, so this may not be of use to you.

I set the fuses in the *.inc file. I have found that once the fuses are set for the chip I do not change them often. But that is me. I will make a note in the code of how the fuses are set.

I know...why not set the fuses in the code if I make a note of how the *.inc is set in the code. For me it just seems to work better.

mister_e
- 8th January 2008, 16:08
Always in my code using the ol' __CONFIG syntax (for PBP), new syntax when using ASM or C18 pragma.

See USBDemo code.

If i use a bootloader, i'll paste the config fuses in my code.. but comment them. So if i decide to remove the bootloader, i'll have my fuses ready.

Darrel Taylor
- 8th January 2008, 20:05
Brian,

I don't use bootloaders very often, so this isn't the way I normally do things.
But I thought I'd give some food for thought on setting configs with either a bootloader or ICSP on 18F's.


ASM
;--[CONFIG1H]--------------------------------------------------
CFG_1H = _OSC_HSPLL_1H ; Oscillator
CFG_1H = CFG_1H & _FCMEN_OFF_1H ; Fail-safe Clock Monitor
CFG_1H = CFG_1H & _IESO_OFF_1H ; Int/Ext Osc Switchover
;--[CONFIG2L]--------------------------------------------------
CFG_2L = _PWRT_ON_2L ; Power-ON Timer
CFG_2L = CFG_2L & _BOREN_ON_2L ; Brown Out
CFG_2L = CFG_2L & _BORV_2_2L ; Brown Out voltage
;--[CONFIG2H]--------------------------------------------------
CFG_2H = _WDT_ON_2H ; Watch Dog Timer
CFG_2H = CFG_2H & _WDTPS_512_2H ; WDT Prescaler
;--[CONFIG3H]--------------------------------------------------
CFG_3H = _MCLRE_ON_3H ; Master Clear Reset
CFG_3H = CFG_3H & _LPT1OSC_OFF_3H ; Low Power TMR1 Osc
CFG_3H = CFG_3H & _PBADEN_OFF_3H ; PORTB Analog
CFG_3H = CFG_3H & _CCP2MX_PORTC_3H ; CCP Multiplexer
;--[CONFIG4L]--------------------------------------------------
CFG_4L = _STVREN_ON_4L ; Stack Over/Under Reset
CFG_4L = CFG_4L & _LVP_OFF_4L ; Low Voltage Programming
CFG_4L = CFG_4L & _XINST_OFF_4L ; Extended Instructions
CFG_4L = CFG_4L & _DEBUG_OFF_4L ; Hardware Debugger

;--------------------------------------------------------------
ifdef LOADER_USED ; If bootloader is used
#include "RTconfig.inc" ; change Configs at runtime
WriteConfig?CC _CONFIG1H, CFG_1H
WriteConfig?CC _CONFIG2L, CFG_2L
WriteConfig?CC _CONFIG2H, CFG_2H
WriteConfig?CC _CONFIG3H, CFG_3H
WriteConfig?CC _CONFIG4L, CFG_4L
else ; If bootloader NOT used
__CONFIG _CONFIG1H, CFG_1H ; set __configs in program
__CONFIG _CONFIG2L, CFG_2L
__CONFIG _CONFIG2H, CFG_2H
__CONFIG _CONFIG3H, CFG_3H
__CONFIG _CONFIG4L, CFG_4L
endif
ENDASM
;----------------------------------------------[ END Configs]--


If the bootloader is being used. It includes the Run-Time Config module from ...
http://www.picbasic.co.uk/forum/showthread.php?t=4093

Then sets the configuration manually at run-time.
The module checks the configs on every reset, but only writes them if they are different.

If the bootloader is Not being used, it saves space by not including the Run Time Config module and puts the __CONFIG's in the program like normal.

Be carefull when changing Oscillator settings!
If you set a mode that does not provide a system clock, or gives a clock at a frequency that the bootloader does not expect, ...
You will have to re-Flash the bootloader to make it work again.
You may even want to comment out the WriteConfig?CC _CONFIG1H line, just to make sure it doesn't get changed.

HTH,