Talking Electronics PIC Theory


There are 12 unusual (different - annoying) things to be aware of when programming the '508A micro. 
As you may know, all the programs in our programming course are created in a PIC16F84 chip and down-loaded into a '508A when you are completely satisfied with the performance of the program.
All the features and instructions for the PICF84 are the same as the '508A (we are only using the features in the '84 that are also contained in the '508A) except for a number of things.
These are the things we have pulled out of the chapters of the course and combined in this chapter so you don't fall into any "traps."

Point 1: To convert the code from PICF84 to '508A, the SetUp instructions must be changed:


SetUp   BSF 03,5
            MOVLW 08
            MOVWF 06
            BCF 03,5            
(These are the first 4 lines of code you used to burn a program into a PIC16F84. But this code will not work for a '508A. It must be changed to:)


SetUp    MOVLW 08
      TRIS 06
   OPTION 0DFh            ;to make GP2 output

The OPTIONS register must be loaded with 0DFh so that bit5 in OPTIONS is "0."
On reset, all the OPTIONS bits are set to 1. Bit5 (T0CS) must be set
to "0" to allow GP2 to become an output via the TRIS register.

Point 2: All sub-routines and tables
for a '508A must be placed at page0 (000 to 0FFh) as the CALL instruction can only access this page.
GOTO instructions will access all memory
(the full memory for a '508A is: 000 to 01FE as 01FF contains the Oscillator Calibration Value), but CALL instructions only access 000 to 0FF locations. 
If you are going to CALL a sub-routine,
the SUB-ROUTINE can go over the page boundary but a table cannot. The page boundary is 0FFh (this is actually address location 255 in numerical terms - don't forget the first address location is 000). The next location 100h is address location 256 and is the beginning of the next page. The CALL instruction cannot address anything from this location to the end of memory!
Sub-routines (but not tables) can be placed in page1
(i.e: 100h to 1FFh) and CALLed by a fairly complex method. See CALL in the "Library of Routines" for placing sub-routines in page1. (page 0 is: 000h to 0FFh)

Point 3: The stack is only 2-high. This means only 2
embedded CALLs can be made. See CALL in the "Library of Routines" chapter for more details. In other words you can have a main program with a call instruction such as CALL Delay1. In the Delay1 routine you can have a further call instruction such as CALL Tone3. BUT in the Tone3 routine you cannot have any further CALLs. The Tone3 routine can only have a GOTO or RETURN. (Use RETLW 00) 

Point 4: The '508A does not have ADDLW instruction. See
"Library of Routines" and "Explaining the Instruction-set" for more details. If you want to ADD a number (a "number" in PIC talk is called a LITERAL) to the working register (Called register W), you must use 3 instructions - "Library of Routines."

Point 5: The '508A does not have a RETURN instruction.
Assemblers recognise RETURN as RETLW 00h. Use RETLW 00h when programming.

Point 6: The '508A instruction-set does not have SUBLW
instruction. See "Library of Routines" and "Explaining the Instruction-set" for more details. 

Point 7: The program length for the '508A is 511 bytes
long. Make sure your program is not longer than 510 bytes. 

Point 8: GP3 is an input-only pin. It cannot be an output

Point 9: The input/output lines are: GP0
, GP1, GP2, GP4 and GP5. These are located in a file we call a PORT. The file (or PORT) number is "6."   Thus to read the data on the first Input line (GP0), bit 0 is tested via the instruction BTFSC 06,0. If the bit is zero (clear) the micro will skip the next instruction in the program.  
To make output line GP0 HIGH, use the instruction: BSF 06,0. But two instructions are needed before setting the bit. See Library of Routines "OUTPUT."

Point 10: The files common to the PIC
16F84 and '508A are: 0C to 1F (25 files).

Helpful hints: 
When creating a program, always think
; remember and use the instructions recognised by the '508A micro. This way the code can be developed in the 'F84 and instantly burnt into a '508A and it will run! 
Only use the files that are common to both micros
(0C to 1F) and remember the three instructions in the 'F84 instruction set that are not available for the '508A. They are: ADDLW, RETURN, and SUBLW.
Since the stack for the '508A is only 2-high, programs should be written
as fairly long routines with as few CALLs as possible. 
This may involve putting sub-routines into the main routine instead
of CALLing them. 
all programming as simple as possible. It may be very inviting to create and use sophisticated instructions but if a problem develops, it will take a lot longer to trouble-shoot the fault
Don't use a complex instruction if a few simple instructions will
do the same thing.
Document everything, especially how or why each instruction
has been used. With most programs, you put an enormous amount of thought into creating some of the instructions and unless these instructions are fully documented, you will find it very difficult to work them out when you go over the program at a later date. 
Remember Boolean operations are very powerful (they can reduce the
instruction-count by 50% or more) and you should check what they are doing to make sure it is what you want. 
Perform the Boolean operation(s) on a sample value and see if the
outcome is as expected. 
If you have trouble getting the program to work, insert
the following 17 lines of Beep1
code in page0, It must be inserted after "Tables."  Refer to the "Library of Routines: "PROGRAM LAYOUT" for the correct layout of a program. This is necessary so that Beep1 routine is only accessed by the micro when the following instruction is seen: GOTO Beep1. This instruction is NOT the second line of code. It is a separate instruction that is inserted in the program being investigated to see how far the micro has progressed through the instructions.
You insert a GOTO Beep1 instruction in the program under investigation and when the micro hits the instruction it goes to the Beep1 routine and produces a continual   beep  . . .  beep . . . beep . . .beep. 
You must make sure you are not using files: 14h,15h in any of the programs you are investigating as they are used in the beep routines. 

Beep1     CALL Beep 
              GOTO Beep1

Beep       MOVLW 40h           ;The duration of the beep
              MOVWF 14h           ;The loop file
is file 14h

BpC        MOVLW 80h       
    ;The duration of HIGH & LOW
              MOVWF 15h
BpD        DECFSZ 15h,1
              GOTO BpD
01            ;To toggle GP0
06,1          ;Toggle GP0
              DECFSZ 14h,1
              GOTO BpC 

BeepDel  DECFSZ 1A,1
              GOTO BeepDel
              DECFSZ 1B,1
              GOTO BeepDel

If you don't get a beep . . .beep . . . you know the micro has not reached the particular location. 
If a beep . . . beep . . .beep is heard,
remove GOTO Beep1 and insert GOTO Beep2 a little further down the program. Beep2 has a different sound and you can be sure the micro has reached the next part of the program!  The 5 lines of Beep2 code can be inserted after Beep1 (i.e: after Tables in Page0)

Beep2     CALL Beep 
              CALL Beep 
              CALL BeepDel
              CALL BeepDel
              GOTO Beep2

This chapter will be updated as other hints and "tricks" are uncovered.