PIC Tutorial One - LED'sFor the first parts of this tutorial you can use the Main Board LED, with jumper J1 set, or you can use the LED board on PortB, the later parts use more than one LED and the LED board will be required. Download zipped tutorial files. Tutorial 1.1 This simple program repeatedly switches all the output pins high and low. ;Tutorial 1.1 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings ;(oscillator type etc.) org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf TRISB movwf TRISA ;set PortA all outputs bcf STATUS, RP0 ;select bank 0 Loop movlw 0xff movwf PORTA ;set all bits on movwf PORTB nop ;the nop's make up the time taken by the goto nop ;giving a square wave output movlw 0x00 movwf PORTA movwf PORTB ;set all bits off goto Loop ;go back and do it again end The first three lines are instructions to the assembler, and not really part of the program at all, they should be left as they are during these tutorials - the __Config line sets the various configuration fuses in the chip, in this case it selects the internal 4MHz oscillator. The next line 'org 0x0000' sets the start address, it does vary across the PIC range, but most modern ones start from the lowest address - zero. Lines 5 and 6 are specific to the 16F628, 'movlw 0x07' means 'MOVe the Literal value 7 into the W register', the W register is the main working register, 'movwf CMCON' means 'MOV the value in W to File CMCON', CMCON is a register in the 16F628 that is used to select the operation of the comparator hardware. So these two lines set CMCON to 7, this disables the comparator, and makes their I/O lines available for general use. The next five lines set the direction of the I/O pins, first we have to select 'bank 1', some registers are in 'bank 0' and some in 'bank 1', to select 'bank 1' we need to set the bit RP0 in the STATUS register to '1' - the 'bsf' (Bit Set File) command sets a bit to one. The 'bcf' (Bit Clear File) at the end of these five lines, sets RP0 back to '0' and returns to 'bank 0'. The 'movlw', as before, moves a literal value into the W register, although this time the value passed is a binary value (instead of the hexadecimal 0x00), signified by the 'b' at the start of the value, in this case it's simply zero, and this value is then transferred to the two TRIS registers (TRIState) A and B. This sets the direction of the pins, a '0' sets a pin as an Output, and a '1' sets a pin as an Input - so b'00000000' (eight zeros) sets all the pins to outputs, b'10000000' would set I/O pin 7 as an input, and all the others as outputs - by using a binary value it's easy to see which pins are inputs (1) and which are outputs (0). This completes the setting up of the chip, we can now start the actual 'running' part of the program, this begins with a label 'Loop', the last command 'goto Loop' returns the program to here, and it loops round for ever. The first instruction in this section 'movlw 0xff' moves the hexadecimal number 0xff (255 decimal, 11111111 binary) to the W register, the second and third then transfer this to the PortA and PortB I/O ports - this 'tries' to set all 16 pins high (I'll explain more later!). The next two instructions are 'nop' 'NO Operation', these simply take 1uS to execute, and do nothing, they are used to keep the outputs high for an extra 2uS. Following that we have a 'movlw 0x00' which moves 0x00 (0 decimal, 00000000 binary) to the W register, then we transfer them to the ports as before, this sets all 16 outputs low. The last 'goto Loop' instruction goes back and runs this section of the program again, and thus continues switching the port pins high then low. Tutorial 1.2 As you will have noticed from the first part, the LED's don't flash!. This isn't strictly true, they do flash - but much too quickly to be visible. As the PIC runs at 4MHz each instruction only takes 1uS to complete (except for 'jump' instructions, which take 2uS), this causes the LED's to flash tens of thousands of times per second - much too quick for our eyes!. This is a common 'problem' with PIC programming, it runs far faster than the world we are used to, and often we need to slow things down!. This second program also repeatedly switches all the output pins high and low, but this time introduces a time delay in between switching. ;Tutorial 1.2 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf TRISB movwf TRISA ;set PortA all outputs bcf STATUS, RP0 ;select bank 0 Loop movlw 0xff movwf PORTA ;set all bits on movwf PORTB nop ;the nop's make up the time taken by the goto nop ;giving a square wave output call Delay ;this waits for a while! movlw 0x00 movwf PORTA movwf PORTB ;set all bits off call Delay goto Loop ;go back and do it again Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end This simply adds a couple of extra lines in the main program, 'call Delay', this is a call to a subroutine, a part of the program which executes and then returns to where it was called from. The routine is called twice, once after the LED's are turned on, and again after they are turned off. All the 'Delay' subroutine does is waste time, it loops round and round counting down until it finishes and returns. The extra part added at the beginning of the program (cblock to endc) allocates a couple of variables (count1 and count2) to two of the 'general purpose file registers', these start at address 0x20 - the cblock directive allocates the first variable to 0x20, and subsequent ones to sequential addresses. The 'Delay' routine delays 250mS, set in it's first line (movlw d'250') - the 'd' signifies a decimal number, easier to understand in this case - so we turn on the LED's, wait 250mS, turn off the LED's, wait another 250mS, and then repeat. This makes the LED's flash 2 times per second, and is now clearly visible. By altering the value d'250' you can alter the flash rate, however as it's an eight bit value it can't go any higher than d'255' (0xff hexadecimal). This routine introduces a new command 'decfsz' 'Decrement File and Skip on Zero', this decrements the file register specified (in this case either count2, or count1) and if the result equals zero skips over the next line. So this first section using it, d2 decfsz count2 ,f goto d2 decrements count2, checks if it equals zero, and if not continues to the 'goto d2' line, which jumps back and decrements count2 again, this continues until count2 equals zero, then the 'goto d2' is skipped over and count1 is decrements in the same way, this time looping back to the start of the count2 loop, so it runs again. This is called a 'nested loop', the inner loop takes 1mS to run, and the outer loop calls the inner loop the number of times specified in count1 - so if you load 0x01 into count1 the entire Delay routine will take 1mS, in the example used we load d'250' (hexadecimal 0xfa) into count1, so it takes 250mS (1/4 of a second). The other new command introduced is 'retlw' 'RETurn from subroutine with Literal in W', this returns to where the subroutine was called from, and returns an optional value in the W register (it's not used to return a value here, so we assign 0x00 to it). I mentioned above that the routine (as written) can only delay a maximum of 255mS, if we wanted a longer delay we could introduce another outer loop which calls 'Delay' the required number of times, but if we just wanted to make it flash once per second (instead of twice) we could simply duplicate the 'call Delay' lines, nop ;giving a square wave output call Delay ;this waits for a while! call Delay ;second delay call added movlw 0x00 this gives a 500mS delay, leaving the LED on for 1/2 a second, by adding a second 'call Delay' to the 'off time' the LED will stay off for 1/2 a second as well. There's no requirement to keep these symmetrical, by using one 'call Delay' for the 'on time', and three for the 'off time' the LED will still flash once per second, but only stay on for 1/4 of the time (25/75) - this will only use 50% of the power that a 50/50 flash would consume. There are huge advantages in using subroutines, a common routine like this may be required many times throughout the program, by storing it once as a subroutine we save lots of space. Also, if you need to alter the routine for any reason, you only need to alter it in one place, and the change will affect all the calls to it. As your PIC programming skills develop you will find you create a library of useful little routines, these can be 'stitched together' to create larger programs - you'll see the 'Delay' subroutine appearing quite a lot in later tutorials, and other subroutines will also make many appearances - why keep reinventing the wheel?. Tutorial 1.3 The previous two examples simply turn all pins high or low, often we only want to affect a single pin, this is easily achieved with the 'bcf' and 'bsf' commands, 'bcf' 'Bit Clear File' clears a bit (sets it to 0), and 'bsf' 'Bit Set File' sets a bit (sets it to 1), the bit number ranges from 0 (LSB) to 7 (MSB). The following example flashes PortB, bit 7 (RB7) only, the rest of the pins remain at 0. ;Tutorial 1.3 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf TRISB movwf TRISA ;set PortA all outputs bcf STATUS, RP0 ;select bank 0 clrf PORTA clrf PORTB ;set all outputs low Loop bsf PORTB, 7 ;turn on RB7 only! call Delay ;this waits for a while! bcf PORTB, 7 ;turn off RB7 only!. call Delay goto Loop ;go back and do it again Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 ;delay 1mS movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end The 'movwf PORTA' and 'movwf PORTB' lines have been replaced by the single line 'bsf PORTB, 7' (to turn the LED on), and 'bcf PORTB, 7' (to turn the LED off). The associated 'movlw 0xff' and 'movlw 0x00' have also been removed, as they are no longer required, the two 'nop' commands have also been removed, they are pretty superfluous - it's not worth adding 2uS to a routine that lasts 250mS!. Tutorial 1.4 If you want to use a different pin to RB7, you could simply alter the '7' on the relevant two lines to whichever pin you wanted, or if you wanted to use PortA, alter the PortB to PortA - however, this requires changing two lines (and could be many more in a long program). So there's a better way! - this example (functionally identical to the previous one) assigns two constants at the beginning of the program, LED, and LEDPORT - these are assigned the values '7' and 'PORTB' respectively, and these constant names are used in the 'bsf' and 'bcf' lines. When the assembler is run it replaces all occurrences of the constant names with their values. By doing this is makes it MUCH! easier to change pin assignments, and it will be used more and more in the following tutorials. In fact, if you look at the 'P16F628.INC' which sets the defaults for the chip, this is simply a list of similar assignments which take a name and replace it with a number (PORTB is actually 0x06). ;Tutorial 1.4 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc LED Equ 7 ;set constant LED = 7 LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB' org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf TRISB movwf TRISA ;set PortA all outputs bcf STATUS, RP0 ;select bank 0 clrf PORTA clrf PORTB ;set all outputs low Loop bsf LEDPORT, LED ;turn on RB7 only! call Delay ;this waits for a while! bcf LEDPORT, LED ;turn off RB7 only!. call Delay goto Loop ;go back and do it again Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 ;delay 1mS movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end This works exactly the same as the previous version, and if you compare the '.hex' files produced you will see that they are identical. Suggested exercises:
Tutorials below here require the LED board Tutorial 1.5 This uses the LED board, and runs a single LED across the row of eight. ;Tutorial 1.5 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB' LEDTRIS Equ TRISB ;set constant for TRIS register org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf LEDTRIS bcf STATUS, RP0 ;select bank 0 clrf LEDPORT ;set all outputs low Loop movlw b'10000000' movwf LEDPORT call Delay ;this waits for a while! movlw b'01000000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00100000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00010000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00001000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000100' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000010' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000001' movwf LEDPORT call Delay ;this waits for a while! goto Loop ;go back and do it again Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end Tutorial 1.6 We can very easily modify this routine so the LED bounces from end to end, just add some more 'movlw' and 'movwf' with the relevant patterns in them - plus the 'call Delay' lines. ;Tutorial 1.6 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB' LEDTRIS Equ TRISB ;set constant for TRIS register org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf LEDTRIS bcf STATUS, RP0 ;select bank 0 clrf LEDPORT ;set all outputs low Loop movlw b'10000000' movwf LEDPORT call Delay ;this waits for a while! movlw b'01000000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00100000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00010000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00001000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000100' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000010' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000001' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000010' movwf LEDPORT call Delay ;this waits for a while! movlw b'00000100' movwf LEDPORT call Delay ;this waits for a while! movlw b'00001000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00010000' movwf LEDPORT call Delay ;this waits for a while! movlw b'00100000' movwf LEDPORT call Delay ;this waits for a while! movlw b'01000000' movwf LEDPORT call Delay ;this waits for a while! goto Loop ;go back and do it again Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end Tutorial 1.7 Now while the previous two routines work perfectly well, and if this was all you wanted to do would be quite satisfactory, they are rather lengthy, crude and inelegant!. Tutorial 1.5 uses 24 lines within the loop, by introducing another PIC command we can make this smaller, and much more elegant. ;Tutorial 1.7 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB' LEDTRIS Equ TRISB ;set constant for TRIS register org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf LEDTRIS bcf STATUS, RP0 ;select bank 0 clrf LEDPORT ;set all outputs low Start movlw b'10000000' ;set first LED lit movwf LEDPORT Loop bcf STATUS, C ;clear carry bit call Delay ;this waits for a while! rrf LEDPORT, f btfss STATUS, C ;check if last bit (1 rotated into Carry) goto Loop ;go back and do it again goto Start Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end This introduces the 'rrf' 'Rotate Right File' command, this rotates the contents of the file register to the right (effectively dividing it by two). As the 'rrf' command rotates through the 'carry' bit we need to make sure that is cleared, we do this with the 'bcf STATUS, C' line. To check when we reach the end we use the line 'btfss STATUS, C' to check when the CARRY bit is set, this then restarts the bit sequence at bit 7 again. Tutorial 1.8 We can apply this to tutorial 1.6 as well, this time adding the 'rlf' 'Rotate Left File' command, this shifts the contents of the register to the left (effectively multiplying by two). ;Tutorial 1.8 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB' LEDTRIS Equ TRISB ;set constant for TRIS register org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf LEDTRIS bcf STATUS, RP0 ;select bank 0 clrf LEDPORT ;set all outputs low Start movlw b'10000000' ;set first LED lit movwf LEDPORT Loop bcf STATUS, C ;clear carry bit call Delay ;this waits for a while! rrf LEDPORT, f btfss STATUS, C ;check if last bit (1 rotated into Carry) goto Loop ;go back and do it again movlw b'00000001' ;set last LED lit movwf LEDPORT Loop2 bcf STATUS, C ;clear carry bit call Delay ;this waits for a while! rlf LEDPORT, f btfss STATUS, C ;check if last bit (1 rotated into Carry) goto Loop2 ;go back and do it again goto Start ;finished, back to first loop Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end Tutorial 1.9 So far we have used two different methods to produce a 'bouncing' LED, here's yet another version, this time using a data lookup table. ;Tutorial 1.9 - Nigel Goodwin 2002 LIST p=16F628 ;tell assembler what chip we are using include "P16F628.inc" ;include the defaults for the chip __config 0x3D18 ;sets the configuration settings (oscillator type etc.) cblock 0x20 ;start of general purpose registers count ;used in table read routine count1 ;used in delay routine counta ;used in delay routine countb ;used in delay routine endc LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB' LEDTRIS Equ TRISB ;set constant for TRIS register org 0x0000 ;org sets the origin, 0x0000 for the 16F628, ;this is where the program starts running movlw 0x07 movwf CMCON ;turn comparators off (make it like a 16F84) bsf STATUS, RP0 ;select bank 1 movlw b'00000000' ;set PortB all outputs movwf LEDTRIS bcf STATUS, RP0 ;select bank 0 clrf LEDPORT ;set all outputs low Start clrf count ;set counter register to zero Read movf count, w ;put counter value in W call Table movwf LEDPORT call Delay incf count, w xorlw d'14' ;check for last (14th) entry btfsc STATUS, Z goto Start ;if start from beginning incf count, f ;else do next goto Read Table ADDWF PCL, f ;data table for bit pattern retlw b'10000000' retlw b'01000000' retlw b'00100000' retlw b'00010000' retlw b'00001000' retlw b'00000100' retlw b'00000010' retlw b'00000001' retlw b'00000010' retlw b'00000100' retlw b'00001000' retlw b'00010000' retlw b'00100000' retlw b'01000000' Delay movlw d'250' ;delay 250 ms (4 MHz clock) movwf count1 d1 movlw 0xC7 movwf counta movlw 0x01 movwf countb Delay_0 decfsz counta, f goto $+2 decfsz countb, f goto Delay_0 decfsz count1 ,f goto d1 retlw 0x00 end This version introduces another new command 'addwf' 'ADD W to File', this is the command used to do adding on the PIC, now with the PIC only having 35 commands in it's RISC architecture some usual commands (like easy ways of reading data from a table) are absent, but there's always a way to do things. Back in tutorial 1.2 we introduced the 'retlw' command, and mentioned that we were not using the returned value, to make a data table we make use of this returned value. At the Label 'Start' we first zero a counter we are going to use 'clrf Count', this is then moved to the W register 'movf Count, w', and the Table subroutine called. The first line in the subroutine is 'addwf PCL, f', this adds the contents of W (which has just been set to zero) to the PCL register, the PCL register is the Program Counter, it keeps count of where the program is, so adding zero to it moves to the next program line 'retlw b'10000000' which returns with b'10000000' in the W register, this is then moved to the LED's. Count is then incremented and the program loops back to call the table again, this time W holds 1, and this is added to the PCL register which jumps forward one more and returns the next entry in the table - this continues for the rest of the table entries. It should be pretty obvious that it wouldn't be a good idea to overrun the length of the table, it would cause the program counter to jump to the line after the table, which in this case is the Delay subroutine, this would introduce an extra delay, and return zero, putting all the LED's out. To avoid this we test the value of count, using 'xorlw d14' 'eXclusive Or Literal with W', this carries out an 'exclusive or' with the W register (to which we've just transferred the value of count) and the value 14, and sets the 'Z' 'Zero' flag if they are equal, we then test the Z flag, and if it's set jump back to the beginning and reset count to zero again. The big advantage of this version is that we can have any pattern we like in the table, it doesn't have to be just one LED, and we can easily alter the size of the table to add or remove entries, remembering to adjust the value used to check for the end of the table!. A common technique in this tables is to add an extra entry at the end, usually zero, and check for that rather than maintain a separate count, I haven't done this because doing it via a counter allows you to have a zero entry in the table - although this example doesn't do that you may wish to alter the patterns, and it could make a simple light sequencer for disco lights, and you may need a zero entry. Suggested exercises:
|