- Joined
- Jun 21, 2012
- Messages
- 4,878
The 14-bit CONFIGURATION word at location 0x2007, described on page on 54 does not enable or disable the analog-to-digital converter or the analog comparator. Nor can you read or write to it with instructions in your program: it's address is out of the range that program memory can address. However, the MPLAB X software can read and write to the CONFIGURATION word after it builds and downloads your program.Hi Hop, thanks for the detailed reply. I am sorry to say that it escapes me a bit. I looked over the relevant sections that you pointed out, page 45 of the pic12f675 datasheet as well as the referenced section in the IDE users manual and I am just not getting it. ...
When the PIC is erased, prior to downloading a program, all the bits in the CONFIGUATION word are set to "1". The __CONFIG directive tells the assembler which bits you want cleared to "0". To make it unnecessary for you to read the information on page 54 and code your own 14-bit pattern of bits, the configuration options are coded as symbols in pic12f675.inc and you just pick the ones you want to change from the default. Each of the options has a value, and this value is logically ANDed with all the other values in the __CONFIG string to create the 14-bit word that is written by MPLAB X to location 0x2007. That is why you need the #include pic12f675.inc statement at the beginning of your program. Actually, I think the #include and __CONFIG statements can go anywhere because the assembler builds its symbol table on the first pass through your code. But why make things difficult for the computer and hard to read for the human?
... I can share with you what I have included in my asm file:
__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
I set these directly through MPLAB's config menu which is buried and hard to find, but very useful.
The entire __CONFIG statement can be inserted as a line in your assembly source file. No need to find a "buried and hard to find" MPLAB config menu. MPLAB X will take care of setting/clearing the CONFIG bits when your program is built and downloaded. The symbols (macros actually, according to Microchip) in the string above are defined in the pic12f675.inc file. The #include assembler directive should be the first line in your program (after any comments you want to throw in ahead of it) followed by the __CONFIG directive. It is not necessary to include all the bits in the __CONFIG directive, only the ones you want to change from their default "1" state to the "0" state or, in the case of the 3-bit FOSC bits, if you want something other than the default "111" state for those bits.
In your case, you have selected INTRC_OSC_NOCLKOUT which resolves to "100" for those three bits, which means use the internal 4 Mhz oscillator but suppress the "clock out" signal on GP4 and don't use the "clock in" signal on GP5, thus freeing those two pins for use as general purpose digital I/O or analog inputs. What those two pins become depends on bits you set or clear in program-accessible control registers that I described in an earlier post. Apparently their use can be changed "on the fly" by your program, but that sounds much more complicated and more difficult to interface to external circuitry. I wouldn't go there just yet.
Your __CONFIG statement looks okay. The important things are you have MCLR enabled on GP3, meaning you can't use GP3 for anything else except to reset the PIC; and the watchdog timer is turned off, meaning no un-expected resets from the watchdog timing-out will occur.
Leaving code protection and data protection off is a good idea IMO, but unless you read and subsequently save and restore the oscillator calibration bits stored by the factory in the last location of program memory, that location and all other locations in program memory will be erased when you program the chip. In other words, the program memory will be erased and then your program will be downloaded on top of the erased memory locations. If your program doesn't specifically code for a particular program memory location, that location will remain erased after your program is downloaded.
I don't know how to automagically save and restore the oscillator calibration byte, other than to use MPLAB X IDE to manually read the last location in PIC program memory before it is programmed and write down on paper the oscillator calibration byte, which Microchip has stored as the instruction MVLW 0xzz, where 0xzz is the calibration byte (expressed in hex). In my program, I org 0x1ff, where 0x1ff is the last address in program memory, and then I code MVLW 0xzz as the instruction there, effectively restoring what the factory programmed there and that MPLAB X IDE erased before it downloaded my program. In your case, the last instruction in program memory is at address 0x3ff as shown on page 9 of your datasheet.
If anyone knows how to preserve the factory-coded oscillator calibrate literal move instruction, I would sure like to know about it. Remember, I am a rank amateur at this.