Program BIN1
The simple program called BIN1 is shown as Program 3.1. It consists of a list of 14-bit binary machine code instructions, represented as four-digit hex numbers (see Chapter 2). Bits 14 and 15 are assumed to be zero, so the codes are represented by hex numbers in the range 0000 to 3FFF. The program is stored at hex addresses 0000 to 0004 (five instructions) in program memory.
Program Analysis
The program instructions must be related to the PIC internal architecture, as outlined in Chapter 2, and specified in the data sheet. The instruction set in the data sheet explains the significance of each bit in each instruction.
This is a jump instruction, which causes the program to go back and repeat the previous instruction. This is achieved by the instruction overwriting the current program counter contents with the value 03, the destination address, which is given as the last two digits of the instruction code. The execution point is thus forced back to the previous instruction, so the program keeps repeating indefinitely. Most control programs have the same basic structure as this simple example; an initialization sequence and an endless loop, which will read the inputs and modify the outputs.
Program Execution
BIN1 is a complete working program, which initializes and clears port B, and then keeps incrementing it. The last two instructions, increment port B and jump back, will repeat indefinitely, with the value being increased by one each time. In other words, port B data register will act as an 8-bit binary counter. When it reaches FF, it will roll over to 00 on the next increment operation.
If you study the binary count table seen in Appendix A (Table A.3), you can see that the least significant bit is inverted each time the binary count is incremented. The least significant bit (LSB), RB0, will thus be toggled (inverted) every time the increment operation is repeated. The next bit, RB1, will toggle at half this rate, and so on, with each bit toggling at half the frequency of the previous bit. The most significant bit (MSB) therefore toggles at 1/128 of the frequency of the LSB. The output pattern generated is shown in Figure 3.5.
A PIC instruction takes four clock cycles to complete, unless it causes a jump, in which case it will take eight clock cycles (or two instruction cycles). The repeated loop in BIN1 will therefore take 4 þ 8 ¼ 12 clock cycles, and it will take 24 cycles for the RB0 to go low and high, the output period of the LSB. If the CR clock is set to run at 100 kHz, the clock period is 1/105 s ¼ 10 ms (frequency ¼ 1/period), giving an instruction cycle time of 40 ms. The loop will take 12 x 10 ¼ 120 ms, giving an output period of 240 ms, a frequency of 4167 Hz, and RB7 will then flash at 4167/128 ¼ 32.5 Hz. These outputs can be displayed on an oscilloscope or logic analyzer (virtual or real).
In the real hardware, the output changes too quickly to see unaided, but it is possible to reduce the clock speed by increasing the value of the resistance and/or the capacitor in the clock circuit. We will see later how to slow the outputs down without changing the clock, by adding a delay routine. In simulation mode, as we will see later, the timing can also be checked using debugging tools such as the stopwatch in MPLAB or the timing display in Proteus VSM.
The frequencies generated are actually in the audio range, and they can be heard by passing them to a small loudspeaker or piezo buzzer. This is a handy way of checking quickly that the program is working, and also immediately suggests a range of PIC applications e generating signals and tones at known frequencies. We will come back to this idea later, and see how to generate audio outputs, or a tone sequence to make a tune, such as a mobile phone ring tone.