Data Table
A program may be required to output a set of predefined data bytes, for example, the codes to light up a seven-segment display with the correct pattern for each display digit.
The data set can be written into the program as a table within a subroutine, and the data list accessed using CALL and RETLW. To fetch the table value required, the position in the table is placed in W. ‘0’ will access the first item, ‘1’ the second and so on. At the top of the subroutine, ADDWF PCL is used to add this table pointer value to the program counter register so that the execution point jumps to the required item in the list.
RETLW is then used to return the table value in W, and it can then be moved to the required file register.
Program 6.6, TAB1, shows how such a table may be used to generate an arbitrary sequence at the LEDs in our BIN demonstration hardware. In this case, it is a bar graph display, which lights the LEDs from one end, using the binary sequence 0, 1, 3, 7, 15, 31, 63, 127, 255.
GPRs labeled ‘timer’ and ‘point’ are used. Port B is set as outputs, and subroutines are defined for a delay and to provide a table of output codes. In the main loop, the table pointer register ‘point’ is initially cleared, and will then be incremented from 0 to 9 as each code is output. The value of the pointer is checked each time round the loop to see if it is 9 yet. When 9 is reached, the program jumps back to ‘newbar’, and the pointer is reset to zero.
For each output, the pointer value (0e8) is placed in W and the ‘table’ subroutine called. The first instruction, ‘ADDWF PCL’, adds the pointer value to the program counter. At the first call, this value is zero, so the next instruction, ‘RETLW 000’, is executed. The program returns to the main loop with the value 00 in W. This is output to the LEDs, the delay is run, and the pointer value incremented. The new value is tested to see if it is 9 yet, and if not, the next call is made to the table, until finally the ninth code (0FF) is returned to the main output loop for display. After this, the test of the pointer being equal to 9 succeeds, the jump back to ‘newbar’ is taken, and the process repeats. Note the use of ‘W’ as the destination for the result of the subtract (SUBWF) instruction. This is necessary to avoid the pointer value being overwritten with the result of the subtraction.
For full details on topics in this chapter related to the assembler, refer to the ‘MPASM User’s Guide’ at www.microchip.com.
1. State (a) the number of clock cycles in a PIC instruction cycle, and the number of
instruction cycles taken to execute the instructions (b) CLRW and (c) RETURN. (3)
2. If the PIC clock input is 100 kHz, what is the instruction cycle time? (2)
3. Calculate the preload value required in TMR0 to obtain a delay of 1 ms between the load operation and the T0IF going high, if the clock rate is 4 MHz and the prescale
ratio selected is 4:1. (3)
4. List the bits in the SFRs that have to be initialized to enable an RB7:RB3 interrupt. (2)
5. State one advantage each of the RC, X T, HS and INTOSC clock options. (4)
6. State the assembler directive that must be used in all PIC programs. (2)
7. Explain the difference between a subroutine and a macro, and one advantage of each. (4) Answers on pages 420e1. (Total 20 marks)
1. Calculate the time taken to execute one complete cycle of the output obtained from TAB1 with a clock rate of 100 kHz. Check this result by simulation.
2. Modify the program TIM1 to use a timer interrupt rather than polling to control the delay.
3. Devise a program to measure the period of an input pulse waveform at RB0, which has
a frequency range of 10e100 kHz. When measured, the input period should be stored in
a GPR called ‘period’ as a value where 0A16 ¼ 10 ms and 6416 ¼ 100 ms (resolution of 1 bit per microsecond). The MCU clock frequency is 4 MHz.