About Microcontroller Configuration
For PICs
This section applies to Microchip PIC microcontrollers. For AVR and LGT microcontrollers see the sections below.
Every Microchip PIC has a CONFIG word. This is an area of memory on the chip that stores settings which govern the operation of the chip.
The following asects of the chip are governed by the CONFIG word:
- Oscillator selection - will the chip run from an internal oscillator, or is an external one attached?
- Automatic resets - should the chip reset if the power drops too low? If it detects it is running the same piece of code over and over?
- Code protection - what areas of memory must be kept hidden once written to?
- Pin usage - which pins are available for programming, resetting the chip, or emitting PWM signals?
The exact configuration settings vary amongst chips. To find out a list of valid settings, please consult the datasheet for the microcontrollers that you wish to use.
This can all be rather confusing - hence, GCBASIC will automatically set some config settings, unless told otherwise:
- Low Voltage Programming (LVP) is turned off. This enables the PGM pin (usually B3 or B4) to be used as a normal I/O pin.
- Watchdog Timer (WDT) is turned off. The WDT resets the chip if it runs the same piece of code over and over - this can cause trouble with some of the longer delay routines in GCBASIC.
- Master Clear (MCLR) is disabled where possible. On many newer chips this allows the MCLR pin (often PORTA.5) to be used as a standard input port. It also removes the need for a pull-up resistor on the MCLR pin.
-
An oscillator mode will be selected, based on the following rules:
- If the microcontroller has an internal oscillator, and the internal oscillator is capable of generating the speed specified in the #chip line, then the internal oscillator will be used.
- If the clock speed is over 4 Mhz, the external HS oscillator is selected
- If the clock speed is 4 MHz or less, then the external XT oscillator mode is selected.
Note that these settings can easily be individually overridden whenever needed. For example, if the Watchdog Timer is needed, adding the line
#config WDT = ON
This will enable the watchdog timer, without affecting any other configuration settings.
For AVR
This section applies to Atmel AVR microcontrollers. Generally, Atmel AVR microcontrollers do have a similar configuration settings, but they are controlled through "Configuration Fuses". GCBASIC cannot set these - you MUST use the programmer software.
The exception to the general case are the ATTiny4-5-9-10 and ATTiny102-104. These microcontrollers have software selectable frequencies for the following frequencies:
ChipMHz 8 ChipMHz 4 ChipMHz 2 ChipMHz 1 ChipMHz 0.5 ChipMHz 0.25 ChipMHz 0.125 ChipMHz 0.0625 ChipMHz 0.03125
Therefore, you can use ( an example )
#chip tiny10, 0.25
For LGT
This section applies to LGT microcontrollers.
All LGT microcontroller have software selectable frequencies for the following frequencies:
ChipMHz 8 ChipMHz 4 ChipMHz 2 ChipMHz 1 ChipMHz 0.5 ChipMHz 0.25 ChipMHz 0.125 ChipMHz 0.0625 ChipMHz 0.03125
Therefore, you can use ( an example )
#chip #chip LGT8F328P, 0.25
Using Configuration
For PICs only.
Once the necessary CONFIG options have been determined, adding them to the program is easy. On a new line type "#config" and then list the desired options separated by commas, such as in this line:
#config OSC = RC, BODEN = OFF
GCBASIC also supports this format on 10/12/16 series chips:
#config INTOSC_OSC_NOCLKOUT, BODEN_OFF
However, for upwards compatibility with 18F chips, you should use the = style config settings.
It is possible to have several #config lines in a program - for instance, one in the main program, and one in each of several #include files. However, care must then be taken to ensure that the settings in one file do not conflict with those in another.
For more help, see #config Directive