HomeArticlesProjectsBlogContact
Articles
Reader Experiments
Colin Mitchell
Colin Mitchell
Make sure to subscribe to our newsletter and be the first to know the news.

This page is dedicated to Reader Feedback in the form of experiments and ideas.

The first program was sent in by Miroslav Kostecki of elabtronics (www.labtron.com.au) the designers of Icon Assembler. I asked him for a program that incremented the 7-segment display. I expected a program similar to Experiment 4: Counting on the 7-segment display. I actually asked for a program in Icon Assembler so I could compare the ease of understanding the Icon Assembler approach. I received the Icon program and loaded it into Icon Assembler but it was so over-the-top that I closed the program and realised it was not for me.
The Icon program was spread over 3 pages and was filled with circles and triangles and half-finished words that made the concept 10 times more complex than the hand-assembly approach I am advocating.
I don’t mind if someone comes up with something that makes writing a program easier and more understandable, but to add to the complexity by 1,000% and attempt to hood-wink me with the suggestion that the new approach is the way to go, I think not.
However, here’s the first readers program. Copy it, put it into a text editor, burn it into a PIC chip, and put it in the PIC LAB-1 project.
This program serves as a very good example of how other people work and think. Miroslav Kostecki is a brilliant programmer and operates at a very high level of thought. Even a simple program such as the one below uses a higher level of thought than we have provided in our set of experiments, and is more complex than expected.
But this is very important. When you see a combination of reasoning from different minds, you can combine the common points and see how to tackle a specific problem from different stand-points.
The first thing you will notice is the layout.
It is different to our recommended format and the inclusion of the interrupt vector adds to the complexity. The interrupt instructions are not functional as the interrupt has not been set up, and maybe this program is left-over from a larger example.
All programs naturally starts at address “000” where it sees the instruction GOTO 5. The instruction ORG 0, does not have to be included as the assembler naturally starts programming at this address. This causes the micro to jump over the instruction GOTO Interrupt, at location 4, and operate on the instruction __CONFIG 0Bh, at address 5. This configuration turns off the watchdog timer, invokes the external RC oscillator mode, and turns on code-protect.
The instruction: MOVLW B’10000000’ OPTION puts the value 80h into the OPTION register to disable the pull-ups on port B. This feature only applies when any of port B lines are INPUT, and does not apply to this program - another unnecessary instruction.
The programmer has used the TRIS operation to determine which lines of each port will be input or output.
The micro then continues directly to Start, where it clears the count file (file 12h) and then goes to Main1.
The output port is initially cleared and so the program starts with a blank display.
The program basically consists of two loops. The “button not pressed” loop is in dloop, where the last value placed in file 06 will appear on the screen. The “button pressed” loop is Main1 and this includes CALL display.
If the button is NOT pressed, the micro carries out the first and third instructions of Main1 and then goes to dloop where it carries out the first and second instructions, in a continuous loop. The screen will display the last value placed in file 06.
When the button is pressed, the first instruction in dloop gets the micro out of the loop and back to Main1, where the micro goes to display. The instruction incf count,f in display increments the “jump value” for the table, performs a SUB and looks in the STATUS file to see if the zero flag has been set. If this flag is SET, the ‘end of table’ has been reached and the jump value is zeroed.
A display value is obtained from the table and passed to port B for outputting to the display. This action is repeated and the display is changed very rapidly to produce a “figure-8.”
As soon as the button is released, the micro goes to the above-mentioned routine with the last display-value appearing on the 7-segment display.
This is not what I requested and the program should be called RANDOM NUMBER or DICE.
By changing the values in the table to include only the numbers 1 to 6, and limiting the jump value to 6, a DICE program can be produced.
GOTO $ is also not used (it is a BASIC language instruction meaning GOTO String Data) and is only included to confuse you. All these misleading instructions from a pioneer of a program designed to help beginners understand programming! If someone can convince me that Icon Assembler is easier to understand than hand-assembly, I will offer a $10,000 prize!
My challenge to Icon Assembly is a program similar to Expt1 test but with lines of code that can be changed on the screen. It can have circles and triangles to make the program colourful and fancy but really only needs each instruction to be taken from a library and dragged into place. This concept will make a superb assembly program where you can see the whole program at a glance and the endless page allows you to scroll down the whole program.
The 7-segment program supplied by elabtronics is already 3 pages long in Icon Assembly and trying to follow the flow of the program is absolutely impossible.
The program below is already more complex than our layout, so you can imagine adding 1,000% more complexity to this is going to make it an Archimedean entanglement!

            ;Reader1.asm
            ;Project: Button increments 7-seg
LIST p=16C84,r=DEC  ; Put assembler into PIC16C84 mode.
            ; r=DEC means decimal numbers are
            ; assumed if 'B' or 'h' not specified
                ;include "p16f84.inc"

   ;**************Declare Variables************************
    count       equ 12
    x           equ 13
   ;**************Initialise interrupt subroutine**********
    goto 5
    ORG 4
    goto interrupt
    ORG 5
  ;**************Initialise Ports*************************
    __CONFIG  0Bh
    MOVLW   B'10000000'
    OPTION
    CLRF    5
    MOVLW   B'00011111'
    TRIS    5
    CLRF    6
    MOVLW   B'00000000'
    TRIS    6

  ;**************Start Of Main Program********************
  Start     ; Button Increments a Seven Segment Display
            ; PORTA,0 is connected to the button
            ; PORTB to display, format=gfedcba0

    clrf    count       ;count  =  0
Main1   btfsc   5,0     ;If PORTA Bit 0 OFF Skip Next
    call    display
    call    Delay
    goto    Main1
    goto    $           ;Safety Caching Loop

   ;**************Subroutines******************************

 interrupt
    retfie          ;Interrupt

 Delay
 dloop  btfss   5,0 ;If PORTA Bit 0 ON Skip Next
    goto    dloop
    decfsz  x,f     ;x  =  x -1 , Skip Next If Zero
    goto    dloop
    return          ;Return

display
    incf    count,f     ;count  =  count + 1
    movlw   0Ah         ;w  =  10
    subwf   count,w     ;w  =  count - w
    btfsc   3,2         ;If STATUS Bit2 OFF Skip Next
    clrf    count       ;count  =  0
    movf    count,w     ;w  =  count
    call    table
    movwf   6           ;PORTB  =  w
    return              ;Return

table   addwf   2,f     ;PCL  =  PCL + w
    retlw   H'3F'       ;w = H'3F'
    retlw   H'06'       ;w = H'06'
    retlw   H'5B'       ;w = H'5B'
    retlw   H'4F'       ;w = H'4F'
    retlw   H'66'       ;w = H'66'
    retlw   H'6D'       ;w = H'6D'
    retlw   H'7D'       ;w = H'7D'
    retlw   H'07'       ;w = H'07'
    retlw   H'7F'       ;w = H'7F'
    retlw   H'6F'       ;w = H'6F'

    End
    ;Reader 2.asm
                     ;Project: To come
 List P = 16F84
 #include <p16F84.inc>
  __CONFIG 1Bh ;_CP_OFF & _PWRTE_ON  _WDT_OFF & _RC_OSC

        ORG 0           ;This is the start of memory for the program.
SetUp   BSF 03,5        ;Go to Bank 1
        CLRF 06         ;Make all port B output
        MOVLW 01        ;Load W with 0000 0001
        MOVLW 05        ;Make RA0 input
        BCF 03,5        ;Go to Bank 0 - the program memory area.
        CLRF 1F         ;Clear the button-press file
        CLRF 06         ;Blank the display
        GOTO Main

Delay   NOP             ;Create approx 250mS delay
        DECFSZ 1A,1
        GOTO Delay
        DECFSZ 1B,1
        GOTO Delay
        RETURN

Delay2  NOP             ;Create 1mS debounce delay
        DECFSZ 1A,1
        GOTO Delay2
        RETURN

Sw      BTFSS 05,0      ;Test the push button
        GOTO Sw3        ;Button not pressed
        BTFSC 1F,2      ;Test end-of-flash flag
        RETURN
        BTFSC 1F,0      ;First pass?
        RETLW 00        ;No
        BTFSS 1F,1      ;Test first-press flag
        GOTO Sw2        ;First press
        BSF 06,0        ;Button has been pressed twice. Illuminate LED
        CALL Delay      ;Keep LED on
        CALL Delay      ;Keep LED on
        BCF 1F,1        ;Clear second-push flag bit
        BSF 1F,2        ;Set end-of-flash flag
        BCF 06,0        ;Turn LED off
        RETURN
        BSF 1F,1        ;Set the first-press flag
        BSF 1F,0        ;Set button pass flag bit
        RETURN
        BCF 1F,0        ;Clear button pass flag bit
        BCF 1F,2        ;Clear end-of-flash flag
        RETURN

Main    CALL Sw
        CALL Delay2
        other instructions  ;Carry out other instructions
        other instructions
        BCF 1F,0        ;Clear first-push flag bit
        GOTO Main

        END             ;Tells assembler end of program


Colin Mitchell

Colin Mitchell

Expertise

electronics
writing
PIC-Chips

Social Media

instagramtwitterwebsite

Related Posts

TODO
Transistor Test
© 2021, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media