Before we start the course, let’s shoot ahead and see how to tackle a simple statement.
It’s nice to know what is ahead and what you are working towards.
A request came in last week for help with a PIC program. The writer asked:
Hi, I am trying to build a project that works like an answering machine. I am having problems programming the PIC16F84 to get it to do this:
Do “W”. Check for “X” to happen. If “X” does not happen in “Y” time do “Z”.
I hope you understand what I am asking.
This is a typical requirement when writing almost any program and although we don’t know enough from the statement to create a complete program (or sub-routine) we can explain how to go about the task.
There are two things you must remember:
Flow diagram for “X”, “Y” and “Z”
The counter/timer section “poles” or “loops” an input line looking for a change. This is done many times and forms a TIME DELAY. The counter/timer is loaded with a value and decrements the count to zero. When the counter/timer is zero, the program executes the “time-up” path.
MOVLW 40h MOVWF 1Ch ;Load count-down file 1C Do "W" BSF 06h,0 ;Make RB0 HIGH CALL Delay ;Select Page0 BCF 06,0 ;Make RB0 LOW AA CALL Delay BTFSS 06,1 ;Is input "X" HIGH? GOTO BB RETLW 00 ;Yes. Return to Main BB DECFSZ 1Ch ;Decrement count-down file GOTO AA ;Time not up. Do again Do "Z" BSF 06,1 ;Time up. Do "Z" CALL Delay BCF 06,1 ;Make RB1 LOW RETLW 00 ;Return to Main Delay MOVLW 80h MOVWF 1Ah ;Load a value into file 1A Del2 MOVLW 60h MOVWF 1Bh ;Load a value into file 1B Del3 DECFSZ 1Bh GOTO Del3 DECFSZ 1Ah GOTO Del2 RETLW 00 ;Return to the sub-routine above
Program for “X”, “Y” , “Z” statement above
The time “Y” is made up of the loops in the count-down file 1C and includes the length of time created by the Delay.
The length of time taken by each of the operations “W” and “Z” can be adjusted by the Delay sub-routine.
At the moment you don’t know anything about the instructions we have used in the program above or how to read them but you can see approximately how may lines of code are required to carry out the statement above.
The advantage of working in a low level language such as the instructions in the program above, is the ability to tell the microcontroller exactly what you want it to do, in the simplest, fastest way. Producing a program in Machine Code or Mnemonic code is the closest you can get to the type of instructions that are directly read by the microcontroller and this is called a low-level language. A high level language is an interpreter language such as BASIC or C++.
There are a number of “tricks” in the program above and these are generally the placement of an instruction in an unusual place. One of these is the placement of CALL Delay
as the 6th instruction.
AA CALL Delay BTFSS 06,1 ;Is input "X" HIGH? GOTO BB RETLW 00 ;Yes. Return to Main DECFSZ 1Ch ;Decrement count-down file
This allows the Delay sub-routine to be called during each loop of the counter/timer. Later in the course you will be shown the meaning of each instruction and you will see why the only place for the CALL Delay
instruction is before decrementing the count file.
The other point you can observe at this early stage is the simplicity of the instructions. Each letter stands for a word. For example, BTFSS 06,1
reads: T
est B
it 1
of F
ile 06
and S
kip if it is S
et (1)
. The letters making up the instruction are called a Mnemonic and this simply means a “set of letters that are easy to remember.”
We cannot cover any more of the program until the instructions are fully explained.
That’s what we will do on the next set of pages:
Quick Links
Legal Stuff
Social Media