Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help compiling ASM to HEX for Pic16

Status
Not open for further replies.

dave254

New Member
Morning guys, I was hoping someone might be able to help. I have spent most of the day trying to work out how to compile an ASM file into a hex file to program a PIC16F916 and I haven't got a clue what I am doing! I am trying to build a Gravimeter project from here https://www.nutsvolts.com/magazine/article/january2013_Newton the pcb is built and ready do go, but I havent got a clue about how to program PIC's. I have a Pickit2 however this requires the code in hex. I was wondering if someone would be able to compile the below asm file into a hex file so I can upload it. Any help would be greatly appreciated.
Code:
;******************************************************************************
;   This file is a basic code template for code generation on the             *
;   PIC16F916. This file contains the basic code building blocks to build     *
;   upon.                                                                     *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on             *
;   features of the assembler.                                                *
;                                                                             *
;   Refer to the respective data sheet for additional                         *
;   information on the instruction set.                                       *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:        GRAVIMETER.asm                                          *
;    Date:            August 26, 2012                                         *
;    File Version:     REV 1                                                  *
;                                                                             *
;    Author:       Ron Newton                                                 *
;    Company:                                                                 *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Files Required: P16F916.INC                                              *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Notes:                                                                   *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Revision History:                                                        *
;                                                                             *
;******************************************************************************


    list        p=16f916      ; list directive to define processor
    #include    <p16f916.inc> ; processor specific variable definitions

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the
; .asm file. The lables following the directive are located in the respective
; .inc file.  See the data sheet for additional information on configuration
; word settings.
;
;------------------------------------------------------------------------------   

;1    MCLR    NC
;2    RA0        NC
;3    RA1        CAP +
;4    RA2        NC
;5    RA3        10K GND
;6    RA4        10K GND
;7    RA5        V1
;8    GND
;9    RA7        NC
;10    RA6        NC
;11    RC0        DAC DATA
;12    RC1        DAC CLOCK
;13    RC2        DAC CS
;14    RC3        PROGRAMING SWITCH
;15    RC4        RS PIN 39
;16    RC5        CS PIN 38
;17    RC6        R/W PIN 37
;18    RC7        E    PIN 36
;19 GND
;20    V POS
;21    RB0        PIN 35
;22    RB1        PIN 34
;23    RB2        PIN 33
;24    RB3        PIN 32
;25 RB4        PIN 31
;26    RB5        PIN 30
;27    RB6        PIN    29    CLK
;28    RB7        PIN    28    DAT

   
    __CONFIG _FCMEN_ON & _IESO_ON & _CP_OFF & _WDT_OFF & _BOD_OFF & _MCLRE_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

;------------------------------------------------------------------------------
; VARIABLE DEFINITIONS
;
; Available Data Memory divided into Bank 0,1,2, and 3.  Each Bank contains
; Special Function Registers, General Purpose Registers, and Access RAM
;
;------------------------------------------------------------------------------

;       Bit Equates
;****************************************************************************
DONE    equ     7               ;done measuring flag
EMPTY   equ     5               ;flag if component is connected
V1      equ     5               ;power for C reference ckt
msb_bit equ     7               ;define for bit 7
lsb_bit equ     0               ;define for bit 0
time    equ        3
sign   equ        4

    CBLOCK 0x20 ; Define GPR variable register locations


;****************************************************************************
;       User Registers
;****************************************************************************
;       Bank 0

Ttemp               ;temporary Time register
flags                  ;flags register
count                  ;RS232 register
txreg                  ;RS232 data register
delay                  ;RS232 delay register
offset              ;table position register
msb                    ;general delay register
lsb                    ;general delay register
TimeLO                 ;Time registers
TimeMID    
TimeHI     
BaseHI                ;base register
BaseMID
BaseLO
TIMER1
TIMER2
TIMER3
OUT
ROTATE
DAC_MID
DAC_LO
MISC

;        DOG REGISTERS
DATA1
COUNTER0
COUNTER1
ONES
TENS
HUNDREDS
THOUSANDS
DIGIT_1
DIGIT_2
DIGIT_3
DIGIT_4
TENK

;       Math related registers
ACCaHI                 ;24-Bit accumulator a
ACCaMID    
ACCaLO     
ACCbHI                 ;24-Bit accumulator b
ACCbMID    
ACCbLO     
ACCcHI                 ;24-Bit accumulator c
ACCcMID    
ACCcLO     
ACCdHI                 ;24-Bit accumulator d
ACCdMID    
ACCdLO     
temp                  ;temporary storage
    ENDC

W_TEMP         EQU    0x7D        ; w register for context saving (ACCESS)
STATUS_TEMP    EQU    0x7E        ; status used for context saving (ACCESS)
PCLATH_TEMP    EQU    0x7F        ; context saving of PC (ACCESS)
#DEFINE        RW                    PORTC,6        ;RW OUT
#DEFINE        RS                    PORTC,4        ;RS OUT
#DEFINE        E                    PORTC,7        ;E OUT
#DEFINE        DAC_CS                PORTC,2
#DEFINE        CLK                    PORTC,1
#DEFINE        DAC_SI                PORTC,0
#DEFINE        PROGRAM                PORTC,3
;------------------------------------------------------------------------------
; EEPROM INITIALIZATION
;
; The 16F916 has 256 bytes of non-volatile EEPROM, starting at address 0x2100
;
;------------------------------------------------------------------------------

DATAEE    ORG  0x2100
;        DE    0x00, 0x01, 0x02, 0x03


;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

    ORG     0x0000            ; processor reset vector
    PAGESEL init
        goto    init

;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------


    org 4



    ; Interrupt context saving
    BANKSEL     PORTA              ;change banks
    MOVWF    W_TEMP           ; save off current W register contents
    MOVF     STATUS,W         ; move status register into W register
    MOVWF    STATUS_TEMP      ; save off contents of STATUS register
    MOVF     PCLATH,W         ; move pclath register into w register
    MOVWF    PCLATH_TEMP      ; save off contents of PCLATH register

;------------------------------------------------------------------------------
        movf    TMR0,W
        movwf   Ttemp
        btfsc   INTCON,T0IF        ;Service Timer 0 overflow
        call    ServiceTimer
        btfsc   PIR2,C2IF          ;Stops Timer0, Records Value
        call    ServiceComparator

;------------------------------------------------------------------------------

    ; Interrupt context restoring
    MOVF    PCLATH_TEMP,W     ; retrieve copy of PCLATH register
    MOVWF   PCLATH            ; restore pre-isr PCLATH register contents
    MOVF    STATUS_TEMP,W     ; retrieve copy of STATUS register
    MOVWF   STATUS            ; restore pre-isr STATUS register contents
    SWAPF   W_TEMP,F
    SWAPF   W_TEMP,W          ; restore pre-isr W register contents
    RETFIE                    ; return from interrupt
   
;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------

init
        Banksel    PORTA            ;change banks
        clrf    PORTA           ;clear PORTA, PORTB and PORTC
        clrf    PORTB
        clrf    PORTC
        clrf    INTCON          ;clear interrupt flags and disable interrupts
        BANKSEL    CMCON0            ;change banks
        movlw   0x07            ;turn off comparators, mode 111
        movwf   CMCON0
        call    delay20         ;wait for comparators to settle

        BANKSEL    OPTION_REG        ;change banks
        movlw   0x88            ;WDT prescalar,internal TMR0 increment
        movwf   OPTION_REG
        BANKSEL    TRISA            ;change banks
        clrf    TRISA           ;PORTA all outputs, discharges RC ckts
        clrf    TRISB            ;PORTB all outputs
        movlw    b'00001000'
        movwf    TRISC            ;PORTC output except RC4
        BANKSEL    VRCON            ;change banks
        movlw   0x0C            ;setup Voltage Reference
        movwf   VRCON
        BANKSEL    LCDCON
        CLRF    LCDCON
        BANKSEL    PORTA            ;change banks
        clrf    INTCON 
        clrf    flags           ;clear flags register
        bsf        DAC_CS            ;TURN OFF DAC

;********************TEST AREA*********************



;**************************************************


start
        BANKSEL    PORTA            ;change banks
        CALL    IT1                ;LOADING DATA STORED
        GOTO    HERE            ;BUTTON NOT PUSHED JUMP TO HERE
       
CALIBRATE
        CALL    LOAD            ;LOAD MESSAGE
        movlw    D'25'            ;TIMER FOR CALIBRATION DELAY and taking average 25 times
        movwf    TIMER1
        clrf    ACCbHI
        clrf    ACCbMID
        clrf    ACCbLO
S1   
        CALL    MEASURE            ;MEASURE
        movf    TimeHI,0        ;LOADING TIME INTO ACCa
        movwf    ACCaHI
        movf    TimeMID,0
        movwf    ACCaMID
        movf    TimeLO,0
        movwf    ACCaLO
        call    Add24            ;ADDING 25 TIMES
        DECFSZ    TIMER1,1        ;ADDS 25 TIMES
        GOTO    S1
        movlw    d'25'            ;LOADING 25
        movwf    ACCaLO
        clrf    ACCaHI            ;clearing upper an mid
        clrf    ACCaMID
        movf    ACCbHI,0
        movwf    ACCcHI            ;swapping ACCb toAAc
        movf    ACCbMID,0
        movwf    ACCcMID
        movf    ACCbLO,0
        movwf    ACCcLO
        clrf    ACCbHI            ;clearing ACCb
        clrf    ACCbMID
        clrf    ACCbLO
        call    Div24            ;divide by 25
               

        movf    ACCcLO,0        ;loading Base with average
        movwf    BaseLO
        movf    ACCcMID,0
        movwf    BaseMID
        movf    ACCcHI,0
        movwf    BaseHI
        CALL    IT2                ;STORE BASE INTO MEMORY
        GOTO    HERE            ;DONE START REGULAR PROGRAM

MEASURE
        bcf     PIR2,C2IF        ;clear comparator flag
        clrf    TimeHI          ;reset Time registers
        clrf    TimeMID
        clrf    TimeLO
Capacitor
        BANKSEL PORTA            ;change banks
        call    AnalogOn        ;turn analog on
        bsf     PORTA,V1        ;charge capacitor
CwaitU  btfss   flags,DONE      ;measurement completed flag
        goto    CwaitU   
        bcf     flags,DONE      ;clear measurement completed flag
        call    AnalogOff       ;turn analog off shorting cap

        RETURN
   

HERE
        BTFSS    PROGRAM
        GOTO    CALIBRATE
        movlw    d'10'
        movwf    TIMER1   
        clrf    ACCbHI
        clrf    ACCbMID
        clrf    ACCbLO
        CALL    MEASURE            ;measure capacitor
        call    SwapTtoA        ;swap Time to accumulator a
        movf      BaseHI,0        ;swap base capacitance value
        movwf   ACCbHI          ;to accumulator b
        movf    BaseMID,0
        movwf   ACCbMID
        movf    BaseLO,0
        movwf   ACCbLO
        BCF        flags,sign        ;CLEAR sign FLAG
        call    Sub24
;******************CHECKING TO SEE IF DATA IS HIGHER OR LOWER
        movlw    0xff
        subwf    ACCbHI,0
        btfss    3,2
        goto    AA                ;DATA IS LOWER JUMP TO AA
;****************DATA IS HIGHER REVERSE SUBTRACTION
        call    SwapBtoA
        movf    TimeHI,0         ;MOVING TIME INTO ACCb
        movwf   ACCbHI         
        movf    TimeMID,0
        movwf   ACCbMID
        movf    TimeLO,0
        movwf   ACCbLO
        call    Sub24            ;PERFORM SUBTRACTION AGAIN
        BSF        flags,sign        ;SET sign FLAG

;*****************MULTIPLYING TO GET CORRECT POSITION ON READ OUT**********
AA        movlw    0x01            ;LOADING 100,000 INTO ACCa
        movwf    ACCaHI
        movlw    0x86
        movwf    ACCaMID
        movlw    0xa0
        movwf    ACCaLO
        call    Mpy24            ;MULTIPLY DIFFERENCE BY 100,000

;***************************** SETTING UP FOR DIVISION TO GET PERCENTAGE**************
        movf    BaseHI,0
        movwf    ACCaHI
        movf    BaseMID,0
        movwf    ACCaMID
        movf    BaseLO,0
        movwf    ACCaLO
        call    Div24           ;divide difference by base

;******************CONVERT TO DECIMAL FOR READING******************        
aaa
        CALL     DECIMAL            ;convert
        CALL    DISPLAYING
        CALL    DOG
        BTFSS    flags,sign        ;CHECKING FOR + -
        goto    AB
        MOVLW    02DH            ;-
        CALL    SEND
        GOTO    AC
AB        MOVLW    02BH            ;+
        CALL     SEND   
AC        MOVF    DIGIT_4,0   
        CALL    SEND
        MOVLW    02EH            ;.
        CALL    SEND
        MOVF    DIGIT_3,0   
        CALL    SEND
        MOVF    DIGIT_2,0   
        CALL    SEND
        MOVF    DIGIT_1,0   
        CALL    SEND
        MOVLW    000H            ;SPACE
        CALL    SEND
        MOVLW    025H            ;%
        CALL     SEND

;**************************PREPARING FOR DAC***************
        clrf    ACCbHI            ;clear ACCbHI
        clrf    ACCbMID            ;clear ACCbMID
        clrf    ACCbLO            ;cler ACCbLO
        movlw    d'2'            ;move 2 for division
        movwf    ACCaLO            ;load
        clrf    ACCaHI            ;clear ACCaHI
        clrf    ACCaMID            ;clear ACCaMID
        call    Div24            ;divide  ACCc is already loaded
        movlw    08h                ;CHECKING FOR OVERFLOW WHICH WILL LOCK UP DAC
        subwf    ACCcMID,0
        btfss    3,0
        goto    DAC1
        clrf    ACCcHI
        clrf    ACCcMID
        clrf    ACCcLO
        bsf        flags,sign        ;keep from subtracting

DAC1    movf    ACCcHI,0        ;LOADING % CHANGE results are in ACCc reg
        movwf    ACCaHI
        movf    ACCcMID,0
        movwf    ACCaMID
        movf    ACCcLO,0
        movwf    ACCaLO

       
        movlw    0x08            ;LOADING 2048 FOR CENTERING DAC 2.5 VOLTS
        movwf    ACCbMID
        clrf    ACCbHI
        clrf    ACCbLO

        btfss    flags,sign        ;CHECK FOR SIGN
        goto    BB
   
        call    Add24            ;ADD % AND 2.5 VOLTS
        movf    ACCbMID,0
        movwf    DAC_MID
        movf    ACCbLO,0
        movwf    DAC_LO
        call    DAC
        goto    HERE

BB        call    Sub24            ;MINUS SUBTRACT FROM 2.5 VOLTS
        movf    ACCbMID,0
        movwf    DAC_MID
        movf    ACCbLO,0
        movwf    DAC_LO
        CALL    DAC
        goto    HERE               ;redo


;*****************************END OF MAIN PROGRAM**************
;**************************************************************
AnalogOn
        BANKSEL    CMCON0            ;change banks
        movf    CMCON0,F        ;for detection of change of comparator
        movlw   0x02            ;turn comparators on, mode 010
        movwf   CMCON0          ;4 inputs multiplexed to 2 comparators
        BANKSEL    TRISA            ;change banks
        movlw   0x0F            ;make PORTA<3:0> all inputs
        movwf   TRISA
        BANKSEL    VRCON            ;change banks
        bsf     VRCON,VREN        ;turn VRCON
        BANKSEL    PORTA            ;change banks
        call    delay20         ;20msec delay
        BANKSEL    CMCON0            ;change banks
        movf    CMCON0,F        ;clear comparator mismatch condition
         BANKSEL    PORTA
        CLRF     PIR2      ;clear comparator interrupt flag
        BANKSEL    PIE2
        BSF        PIE2,C2IE        ;TURN ON INTERRUPT
        BANKSEL    PORTA            ;change banks
        movlw    0E0H
        movwf    INTCON
        clrf    TMR0            ;clear TMR0 counter
        return

AnalogOff
        BANKSEL    PORTA            ;change banks
        CLRF    PORTA            ;GROUND ALL PORT A
        clrf    INTCON          ;disable all interrupts
        BANKSEL    TRISA            ;change banks
        clrf    TRISA           ;set PORTA pins to outputs, discharge RC ckt
        BANKSEL    VRCON            ;change banks
        bcf     VRCON,VREN      ;disable Vref
        BANKSEL    CMCON0            ;change banks
        movlw   0x07
        movwf   CMCON0          ;disable comparators

        BANKSEL    PORTA            ;change banks
        call    delay20         ;20msec delay
        BANKSEL    CMCON0            ;change banks
        movf    CMCON0,F        ;clear comparator mismatch condition
          BANKSEL    PORTA            ;change banks
        bcf     PIR2,C2IF       ;clear comparator interrupt flag
        BANKSEL    PIE2            ;TURN OFF INTERRUPT
        CLRF    PIE2
        BANKSEL    PORTA
        call    vlong           ;long delay to allow capacitors to discharge
        call    vlong  
        call    vlong
        return

;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     Delay Routines                                                     -*
;*--------------------------------------------------------------------------*
;****************************************************************************
vlong   movlw   0xff            ;very long delay, approx 200msec
        movwf   msb
        goto    d1
delay20                         ;20 msec delay
        movlw   .26
        movwf   msb
        goto    d1
delay16                         ;16 msec delay
        movlw   .21
        movwf   msb
        goto    d1
delay1                          ;approx 750nsec delay
        movlw   .1     
        movwf   msb
d1      movlw   0xff
        movwf   lsb
d2      decfsz  lsb,F
        goto    d2
        decfsz  msb,F
        goto    d1
        return
;____________________________________________________________________________

;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     Swap Time to Accumulator a                                         -*
;*--------------------------------------------------------------------------*
;****************************************************************************
SwapTtoA
    BANKSEL    PORTA            ;change banks
        movf    TimeHI,W
        movwf   ACCaHI
        movf    TimeMID,W
        movwf   ACCaMID
        movf    TimeLO,W
        movwf   ACCaLO

        return
;****************************************************************************
SwapBtoA
        BANKSEL    PORTA            ;change banks
        movf    BaseHI,W
        movwf   ACCaHI
        movf    BaseMID,W
        movwf   ACCaMID
        movf    BaseLO,W
        movwf   ACCaLO
        return
;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     ISR to service a Timer0 overflow                                   -*
;*--------------------------------------------------------------------------*
;****************************************************************************
ServiceTimer

        incf    TimeMID,F       ;increment middle Time byte
        btfsc   STATUS,Z        ;if middle overflows,
        incf    TimeHI,F        ;increment high Time byte      
        bcf     INTCON,T0IF     ;clear TMR0 interrupt flag
        return
;____________________________________________________________________________

;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     ISR to service a Comparator interrupt                              -*
;*--------------------------------------------------------------------------*
;****************************************************************************
ServiceComparator

        BANKSEL    PORTA            ;change banks
        clrf     INTCON            ;disable TMR0 interrupts
        movf    Ttemp,W
        movwf   TimeLO
        bsf     flags,DONE      ;set DONE flag
        BANKSEL    PIE2
        clrf    PIE2
        BANKSEL    PORTA
        bcf     PIR2,C2IF       ;clear comparator interrupt flag
        return
;************************DAC PROGRAM**********************

DAC                            ;LABEL

    BCF        DAC_CS            ;SET CS LOW TO TURN ON
D1    BCF        DAC_SI            ;
    CALL    CLOCK_OUT        ;WRITE TO DAC
    CALL    CLOCK_OUT        ;UNBUFFERED
    BSF        DAC_SI
    CALL    CLOCK_OUT        ;GAIN1
    CALL    CLOCK_OUT        ;ACTIVE

    MOVLW    4                ;FOUR BITS
    MOVWF    ROTATE            ;COUNTER
    MOVF    DAC_MID,0        ;MOVING DAC MID INTO OUT
    MOVWF    OUT                ;MOVE
    BCF        3,0                ;CLR C
    RLF        OUT,1
    RLF        OUT,1
    RLF        OUT,1
    RLF        OUT,1
D2    RLF        OUT,1            ;ROTATE MSD OUT INTO C
    BTFSS    3,0                ;CHECK TO SEE IF C IS HIGH
    BCF        DAC_SI            ;ANALOG OUT LOW
    BTFSC    3,0                ;CHECK TO SEE IF C IS LOW
    BSF        DAC_SI            ;ANALOG OUT LOW
    CALL    CLOCK_OUT        ;CLOCK OUT
    DECFSZ    ROTATE,1        ;DO 4 TIMES
    GOTO    D2                ;DO AGAIN

    MOVLW    8                ;EIGHT BITS
    MOVWF    ROTATE            ;COUNTER
    MOVF    DAC_LO,0        ;MOVING DAC LO INTO OUT
    MOVWF    OUT                ;MOVE
    BCF        3,0                ;CLR C
D3    RLF        OUT,1            ;ROTATE MSD OUT INTO C
    BTFSS    3,0                ;CHECK TO SEE IF C IS HIGH
    BCF        DAC_SI            ;ANALOG OUT LOW
    BTFSC    3,0                ;CHECK TO SEE IF C IS LOW
    BSF        DAC_SI            ;ANALOG OUT LOW
    CALL    CLOCK_OUT        ;CLOCK OUT
    DECFSZ    ROTATE,1        ;DO 8 TIMES
    GOTO    D3                ;DO AGAIN
    BSF        DAC_CS            ;SET CS HIGH TO TURN LOAD
    RETURN

CLOCK_OUT                    ;LABEL
    BSF        CLK                ;CLOCK HIGH
    BCF        CLK                ;CLOCK LOW
    RETURN                    ;RETURN

   
;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     24-bit Addition                                                    -*
;*-                                                                        -*
;*-     Uses ACCa and ACCb                                                 -*
;*-                                                                        -*
;*-     ACCa + ACCb -> ACCb                                                -*
;*--------------------------------------------------------------------------*
;****************************************************************************
Add24
        movf    ACCaLO,W
        addwf   ACCbLO,1          ;add low bytes
        btfsc   STATUS,C        ;add in carry if necessary
        goto    A2
A1      movf    ACCaMID,W
        addwf   ACCbMID,1         ;add mid bytes
        btfsc   STATUS,C        ;add in carry if necessary
        incf    ACCbHI,1
        movf    ACCaHI,W
        addwf   ACCbHI,1          ;add high bytes
        retlw   0
A2      incf    ACCbMID,1
        btfsc   STATUS,Z
        incf    ACCbHI,1
        goto    A1
;____________________________________________________________________________

;****************************************************************************
;*--------------------------------------------------------------------------*      
;*-     Subtraction ( 24 - 24 -> 24 )                                      -*
;*-                                                                        -*
;*-     Uses ACCa, ACCb, ACCd                                              -*
;*-                                                                        -*
;*-     ACCa -> ACCd,                                                      -*
;*-     2's complement ACCa,                                               -*
;*-     call Add24 ( ACCa + ACCb -> ACCb ),                                -*
;*-     ACCd -> ACCa                                                       -*
;*--------------------------------------------------------------------------*
;****************************************************************************
Sub24
        movf    ACCaHI,W                ;Transfer ACCa to ACCd
        movwf   ACCdHI
        movf    ACCaMID,W
        movwf   ACCdMID
        movf    ACCaLO,W
        movwf   ACCdLO
        call    compA                   ;2's complement ACCa
        call    Add24                   ;Add ACCa to ACCb
        movf    ACCdHI,W                ;Transfer ACCd to ACCa
        movwf   ACCaHI
        movf    ACCdMID,W
        movwf   ACCaMID
        movf    ACCdLO,W
        movwf   ACCaLO
        retlw   0
;____________________________________________________________________________

;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     Multiply ( 24 X 24 -> 56 )                                         -*
;*-                                                                        -*
;*-     Uses ACCa, ACCb, ACCc, ACCd                                        -*
;*-                                                                        -*
;*-     ACCa * ACCb -> ACCb,ACCc  56-bit output                            -*
;*-     with ACCb (ACCbHI,ACCbMID,ACCbLO) with 24 msb's and                -*
;*-     ACCc (ACCcHI,ACCcMID,ACCcLO) with 24 lsb's                         -*
;*--------------------------------------------------------------------------*
;****************************************************************************
Mpy24
        call    Msetup
mloop   rrf     ACCdHI,1          ;rotate d right
        rrf     ACCdMID,1
        rrf     ACCdLO,1
        btfsc   STATUS,C        ;need to add?
        call    Add24
        rrf     ACCbHI,1
        rrf     ACCbMID,1
        rrf     ACCbLO,1
        rrf     ACCcHI,1
        rrf     ACCcMID,1
        rrf     ACCcLO,1
        decfsz  temp,1            ;loop until all bits checked
        goto    mloop
        retlw   0

Msetup
        movlw   0x18            ;for 24 bit shifts
        movwf   temp
        movf    ACCbHI,W        ;move ACCb to ACCd
        movwf   ACCdHI
        movf    ACCbMID,W
        movwf   ACCdMID
        movf    ACCbLO,W
        movwf   ACCdLO
        clrf    ACCbHI
        clrf    ACCbMID
        clrf    ACCbLO
        retlw   0
;____________________________________________________________________________

;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     Division ( 56 / 24 -> 24 )                                         -*
;*-                                                                        -*
;*-     Uses ACCa, ACCb, ACCc, ACCd                                        -*
;*-                                                                        -*
;*-     56-bit dividend in ACCb,ACCc ( ACCb has msb's and ACCc has lsb's)  -*
;*-     24-bit divisor in ACCa                                             -*
;*-     quotient is stored in ACCc                                         -*
;*-     remainder is stored in ACCb                                        -*
;*--------------------------------------------------------------------------*
;****************************************************************************
Div24
        call    Dsetup

dloop   bcf     STATUS,C
        rlf     ACCcLO,1         ;Rotate dividend left 1 bit position
        rlf     ACCcMID,1
        rlf     ACCcHI,1
        rlf     ACCbLO,1
        rlf     ACCbMID,1
        rlf     ACCbHI,1

        btfsc   STATUS,C        ;invert carry and exclusive or with the
        goto    clear           ;msb of the divisor then move this bit
        btfss   ACCaHI,msb_bit  ;into the lsb of the dividend
        incf    ACCcLO,1
        goto    cont
clear   btfsc   ACCaHI,msb_bit
        incf    ACCcLO,1

cont    btfsc   ACCcLO,lsb_bit  ;check the lsb of the dividend
        goto    minus
        call    Add24           ;if = 0, then add divisor to upper 24 bits
        goto    check           ;of dividend
minus   call    Sub24           ;if = 1, then subtract divisor from upper
                                ;24 bits of dividend

check   decfsz  temp,f          ;do 24 times
        goto    dloop

        bcf     STATUS,C
        rlf     ACCcLO,1          ;shift lower 24 bits of dividend 1 bit
        rlf     ACCcMID,1         ;position left
        rlf     ACCcHI,1
        btfsc   ACCbHI,msb_bit  ;exlusive or the inverse of the msb of the
        goto    w1              ;dividend with the msb of the divisor
        btfss   ACCaHI,msb_bit  ;store in the lsb of the dividend
        incf    ACCcLO,1
        goto    wzd
w1      btfsc   ACCaHI,msb_bit
        incf    ACCcLO,1
wzd     btfss   ACCbHI,msb_bit  ;if the msb of the remainder is set and
        goto    wend
        btfsc   ACCaHI,msb_bit  ;the msb of the divisor is not
        goto    wend
        call    Add24           ;add the divisor to the remainder to correct
                                ;for zero partial remainder

wend    retlw   0               ;quotient in 24 lsb's of dividend
                                ;remainder in 24 msb's of dividend

Dsetup       
        movlw   0x18            ;loop 24 times
        movwf   temp
       
        retlw   0
;____________________________________________________________________________

;****************************************************************************
;*--------------------------------------------------------------------------*
;*-     2's Complement                                                     -*
;*-                                                                        -*
;*-     Uses ACCa                                                          -*
;*-                                                                        -*
;*-     Performs 2's complement conversion on ACCa                         -*
;*--------------------------------------------------------------------------*
;****************************************************************************
compA   
        comf    ACCaLO,1          ;invert all bits in accumulator a
        comf    ACCaMID,1
        comf    ACCaHI,1
        incf    ACCaLO,1          ;add one to accumulator a
        btfsc   STATUS,Z
        incf    ACCaMID,1
        btfsc   STATUS,Z
        incf    ACCaHI,1
        retlw   0
;___________________________________________________
;------------------------------------------------------------------------------
DECIMAL                                            ;CONVERSION FROM HEXADECIMAL TO DECIMAL
        BANKSEL        PORTA            ;change banks
        SWAPF        ACCcMID,W
        ANDLW        0FH
        ADDLW        0F0H
        MOVWF        THOUSANDS
        ADDWF        THOUSANDS,F
        ADDLW        0E2H
        MOVWF        HUNDREDS
        ADDLW        32H
        MOVWF        ONES

        MOVF        ACCcMID,0
        ANDLW        0FH
        ADDWF        HUNDREDS,F
        ADDWF        HUNDREDS,F
        ADDWF        ONES,F
        ADDLW        0E9H
        MOVWF        TENS
        ADDWF        TENS,F
        ADDWF        TENS,F

        SWAPF        ACCcLO,W
        ANDLW        0FH
        ADDWF        TENS,F
        ADDWF        ONES,F

        RLF            TENS,F
        RLF            ONES,F
        COMF        ONES,F
        RLF            ONES,F
           
        MOVF        ACCcLO,W
        ANDLW        0FH
        ADDWF        ONES,F
        RLF            THOUSANDS,F
       
        MOVLW        07H
        MOVWF        TENK

        MOVLW        0AH
LB1        ADDWF        ONES,F
        DECF        TENS,F
        BTFSS        3,0
        GOTO        LB1
LB2        ADDWF        TENS,F
        DECF        HUNDREDS,F
        BTFSS        3,0
        GOTO        LB2

LB3        ADDWF        HUNDREDS,F
        DECF        THOUSANDS,F
        BTFSS        3,0
        GOTO        LB3

LB4        ADDWF        THOUSANDS,F
        DECF        TENK,F
        BTFSS        3,0
        GOTO        LB4

        RETURN
;------------------------------------------------------------------------------

SEND    MOVWF        DATA1
        CALL        WRDATA_CHK
        CALL        DELAY30uS
        RETURN
WRDATA_CHK
        CALL        CHK_BUSY
        BSF            RS
        BCF            RW
        BSF            E
        MOVF        DATA1,0
        MOVWF        PORTB
        BCF            E
        MOVLW        .255
        MOVWF        PORTB
        RETURN
WRINS_CHK
        CALL        CHK_BUSY
WRINS_NOCHK
        BCF            RS
        BCF            RW
        BSF            E
        MOVF        DATA1,0
        MOVWF        PORTB
        BCF            E
        MOVLW        .255
        MOVWF        PORTB
        RETURN
CHK_BUSY
        BCF            RS            ;RS LOW
        BSF            RW            ;RW HIGH
        BSF            E            ;ENABLE HIGH
        BANKSEL        TRISB        ;CHANGING OUTPUT TO INPUT
        MOVLW        .255
        MOVWF        TRISB        ;LOAD
        BANKSEL        PORTA        ;CHANGE BANKS
;        BTFSC        BUSY        ;CHECK TO SEE IF BUSY
;        GOTO        $-1            ;HOLD
        BANKSEL        TRISB        ;CHANGING INPUT TO OUTPUT
        CLRF        TRISB        ;LOAD
        BANKSEL        PORTA        ;CHANGE BANKS
        BCF            E            ;LOWER ENABLE
        RETURN                    ;RETURN

DELAY40MS
        MOVLW        .105
        MOVWF        COUNTER1
        DECFSZ        COUNTER0,1
        GOTO        $-1
        DECFSZ        COUNTER1,1
        GOTO        $-3
        RETURN
DELAY30uS
        MOVLW        .16
        MOVWF        COUNTER0
        DECFSZ        COUNTER0,1
        GOTO        $-1
        RETURN
DELAY2MS
        MOVLW        6
        MOVWF        COUNTER1
        DECFSZ        COUNTER0,1
        GOTO        $-1
        DECFSZ        COUNTER1,1
        GOTO        $-3
        RETURN
DOG        CALL        DELAY40MS
        MOVLW        039H            ;FUNCTION SET INSTRUCTION SET 0
        MOVWF        DATA1
        CALL        WRINS_NOCHK
        CALL        DELAY30uS
        MOVLW        039H            ;FUNCTION SET
        MOVWF        DATA1
        CALL        WRINS_NOCHK
        CALL        DELAY30uS
        MOVLW        01CH            ;SET BIAS 5 VOLT
        MOVWF        DATA1
        CALL        WRINS_CHK
        CALL        DELAY30uS
        MOVLW        074H            ;CONTRAST SET ADJUSTMENT 5 VOLT   
        MOVWF        DATA1
        CALL        WRINS_CHK
        CALL        DELAY30uS
        MOVLW        052H            ;POWER/ICON/CONTRAST 5 VOLT
        MOVWF        DATA1
        CALL        WRINS_CHK
        CALL        DELAY30uS
        MOVLW        069H            ;FOLLOWER CONTROL
        MOVWF        DATA1
        CALL        WRINS_CHK
        CALL        DELAY30uS
        MOVLW        038H            ;FUNCTION SET  INSTRUCTION SET 1
        MOVWF        DATA1
        CALL        WRINS_NOCHK
        CALL        DELAY30uS
        MOVLW        00CH            ;DISPLAY ON CURSOR OFF
        MOVWF        DATA1
        CALL        WRINS_CHK
        CALL        DELAY30uS
        MOVLW        001H            ;CLEAR DISPLAY
        MOVWF        DATA1
        CALL        WRINS_CHK
        CALL        DELAY2MS
        MOVLW        006H            ;ENTRY MODE SET
        MOVWF        DATA1
        CALL        WRINS_CHK
        CALL        DELAY30uS   
        RETURN
LOAD       
        CALL        DOG                ;DISPLAY MESSAGE
        MOVLW        053H        ;S
        CALL        SEND
        MOVLW        054H        ;T
        CALL        SEND
        MOVLW        041H        ;A
        CALL        SEND
        MOVLW        04EH        ;N
        CALL        SEND
        MOVLW        044H        ;D
        CALL        SEND
        MOVLW        000H        ;SPACE
        CALL        SEND
        MOVLW        042H        ;B
        CALL        SEND
        MOVLW        059H        ;Y
        CALL        SEND

        RETURN

;**************************** DISPLAYING *****************************8

DISPLAYING   
        MOVLW    0                ;LOAD INTO 0 THE LOW ADDRESS OF LOOKUP TABLE LOOKUP TABLE ADDRESS
        ADDWF    ONES,0            ;ADD THE OFFSET TO THE LOW ADDRESS (ADRES)
        MOVLW    3                ;LOAD 3 INTO HIGH ADDRESS OF LOOKUP TABLE
        BTFSC    STATUS,C        ;CHECK TO SEE IF THE OFFSET ADDED TO LOW LOOKUP TABLE WENT ABOVE 255
        ADDLW    1                ;IF IT DID ADD 1 TO HIGH TABLE BYTE (PCLATH)
        MOVWF    PCLATH
        MOVF     ONES,W           ;MOVE CONTROL INTO W
        CALL    DISPLAY          ;GO TO LOOK UP TABLE      
        MOVWF    DIGIT_1
        CLRF    PCLATH

        MOVLW        0
        ADDWF        TENS,0
        MOVLW        3
        BTFSC        STATUS,C
        ADDLW        1
        MOVWF        PCLATH
        MOVF        TENS,0
        CALL        DISPLAY
        MOVWF        DIGIT_2
        CLRF        PCLATH

        MOVLW        0
        ADDWF        HUNDREDS,0
        MOVLW        3
        BTFSC        STATUS,C
        ADDLW        1
        MOVWF        PCLATH
        MOVF        HUNDREDS,0
        CALL        DISPLAY
        MOVWF        DIGIT_3
       
        MOVLW        0
        ADDWF        THOUSANDS,0
        MOVLW        3
        BTFSC        STATUS,C
        ADDLW        1
        MOVWF        PCLATH
        MOVF        THOUSANDS,0
        CALL        DISPLAY
        MOVWF        DIGIT_4
        CLRF        PCLATH
        RETURN

        ORG    300
DISPLAY
        ADDWF    PCL,1                ;ADD W TO PROGRAM COUNTER AND LOOK UP DATA  "." = DECIMAL #
        RETLW    B'00110000'            ;0
        RETLW    B'00110001'            ;1
        RETLW    B'00110010'            ;2
        RETLW    B'00110011'            ;3
        RETLW    B'00110100'            ;4
        RETLW    B'00110101'            ;5
        RETLW    B'00110110'            ;6
        RETLW    B'00110111'            ;7
        RETLW    B'00111000'            ;8
        RETLW    B'00111001'            ;9


;+ = 02BH
;- = 02DH
;%

    ;********GETTING DATA FROM EEPROM ****************
IT1   
        BANKSEL    EEADR
        MOVLW    1                 ; Data Memory
        MOVWF     EEADR             ; Address to read
        BANKSEL    EECON1
        BCF     EECON1,EEPGD     ; Point to Data
        BSF     EECON1,RD         ; EE Read
        BANKSEL    EEDATA
        MOVF     EEDATA,W         ; W = EEDATA
        BANKSEL    PORTA
        MOVWF    BaseHI            ;load

        BANKSEL    EEADR
        MOVLW    2                ; Data Memory
        MOVWF     EEADR             ; Address to read
        BANKSEL    EECON1
        BCF     EECON1,EEPGD     ; Point to Data
        BSF     EECON1,RD         ; EE Read
        BANKSEL    EEDATA
        MOVF     EEDATA,W         ; W = EEDATA
        BANKSEL    PORTA
        MOVWF    BaseMID            ;load

        BANKSEL    EEADR
        MOVLW    3                 ; Data Memory
        MOVWF     EEADR             ; Address to read
        BANKSEL    EECON1
        BCF     EECON1,EEPGD     ; Point to Data
        BSF     EECON1,RD         ; EE Read
        BANKSEL    EEDATA
        MOVF     EEDATA,W         ; W = EEDATA
        BANKSEL    PORTA
        MOVWF    BaseLO            ;load
        CALL        DOG
        MOVLW        04CH        ;L
        CALL        SEND
        MOVLW        04FH        ;O
        CALL        SEND
        MOVLW        041H        ;A
        CALL        SEND
        MOVLW        044H        ;D
        CALL        SEND
        MOVLW        049H        ;I
        CALL        SEND
        MOVLW        04EH        ;N    
        CALL        SEND
        MOVLW        047H        ;G
        CALL        SEND
        CALL        DISPLAY_DELAY
        BTFSS        PROGRAM
        GOTO        $-1

        RETURN

;**************STORING DATA IN EEPROM**************
IT2   
        BANKSEL    EECON1   
        BTFSC     EECON1,WR     ;Wait for write
        GOTO     $-1             ;to complete
        BCF     STATUS,RP0        ;Bank 2
        MOVLW     1             ;Data Memory
        MOVWF     EEADR         ;Address to write
        BANKSEL    PORTA
        MOVF    TimeHI,0        ;Data Memory Value
        MOVWF     BaseHI         ;STORE IN BASE AND EPROM
        BANKSEL        EEDATA
        MOVWF         EEDATA         ;to write
        BANKSEL        EECON1
        BCF     EECON1,EEPGD     ;Point to DATA ;memory
        BSF     EECON1,WREN     ;Enable writes
        BCF     INTCON,GIE         ;Disable INTs.
        MOVLW     0X55              ;
        MOVWF     EECON2         ;Write 55h
        MOVLW     0AAh             ;
        MOVWF     EECON2         ;Write AAh
        BSF     EECON1,WR         ;Set WR bit to ;begin write
        BSF     INTCON,GIE         ;Enable INTs.
        BCF        EECON1,WREN     ;Disable writes

        BANKSEL    EECON1   
        BTFSC EECON1,WR     ;Wait for write
        GOTO $-1             ;to complete
        BCF STATUS,RP0        ;Bank 2
        MOVLW 2                ;Data Memory
        MOVWF EEADR         ;Address to write
        BANKSEL    PORTA
        MOVF    TimeMID,0        ;Data Memory Value
        MOVWF     BaseMID         ;STORE IN BASE AND EPROM
        BANKSEL    EEDATA
        MOVWF EEDATA         ;to write
        BANKSEL    EECON1
        BCF EECON1,EEPGD     ;Point to DATA ;memory
        BSF EECON1,WREN     ;Enable writes
        BCF INTCON,GIE         ;Disable INTs.
        MOVLW 0X55              ;
        MOVWF EECON2         ;Write 55h
        MOVLW 0AAh             ;
        MOVWF EECON2         ;Write AAh
        BSF EECON1,WR         ;Set WR bit to ;begin write
        BSF INTCON,GIE         ;Enable INTs.
        BCF EECON1,WREN     ;Disable writes

        BANKSEL    EECON1   
        BTFSC EECON1,WR     ;Wait for write
        GOTO $-1             ;to complete
        BCF STATUS,RP0        ;Bank 2
        MOVLW 3             ;Data Memory
        MOVWF EEADR         ;Address to write
        BANKSEL    PORTA
        MOVF    TimeLO,0        ;Data Memory Value
        MOVWF     BaseLO         ;STORE IN BASE AND EPROM
        BANKSEL    EEDATA
        MOVWF EEDATA         ;to write
        BANKSEL    EECON1
        BCF EECON1,EEPGD     ;Point to DATA ;memory
        BSF EECON1,WREN     ;Enable writes
        BCF INTCON,GIE         ;Disable INTs.
        MOVLW 0X55             ;
        MOVWF EECON2         ;Write 55h
        MOVLW 0AAh             ;
        MOVWF EECON2         ;Write AAh
        BSF EECON1,WR         ;Set WR bit to ;begin write
        BSF INTCON,GIE         ;Enable INTs.
        BCF EECON1,WREN     ;Disable writes
        BANKSEL        PORTA
        CALL        DOG
        MOVLW        053H        ;S
        CALL        SEND
        MOVLW        054H        ;T
        CALL        SEND
        MOVLW        04FH        ;O
        CALL        SEND
        MOVLW        052H        ;R
        CALL        SEND
        MOVLW        049H        ;I
        CALL        SEND
        MOVLW        04EH        ;N    
        CALL        SEND
        MOVLW        047H        ;G            
        CALL        SEND
        CALL        DISPLAY_DELAY
        BTFSS        PROGRAM
        GOTO    $-1
        RETURN

DISPLAY_DELAY
        MOVLW        10
        MOVWF        TIMER3
        DECFSZ        TIMER1,1
        GOTO        $-1
        DECFSZ        TIMER2,1
        GOTO        $-3
        DECFSZ        TIMER3,1
        GOTO        $-5

        RETURN


CHECK
        BANKSEL        PORTA
        CALL        DOG
        MOVLW        043H        ;C
        CALL        SEND
        MOVLW        048H        ;H
        CALL        SEND
        MOVLW        045H        ;E
        CALL        SEND
        MOVLW        043H        ;C
        CALL        SEND
        MOVLW        04BH        ;K
        CALL        SEND

                RETURN
  END
 

Attachments

  • GRAVIMETER.ASM
    38.2 KB · Views: 266
Last edited by a moderator:
I don,t know if the forum software can handle hex files, but try the attachment here.

JimB
 

Attachments

  • Gravimeter.HEX
    5 KB · Views: 262
Hmm...
Seems to work OK.

JimB
 
I've just assembeled the .asm file (Downloaded from your link.) using masm (Version 5.86). I have uploaded the resulting .hex file., You will probably have to install MPLABX (version 5.30 or earlier as newer versions don't include mpasm. ) You can download MPLABX from the Microchip website.

Edit I now see that Jim has beat me to it. (I have downloaded the hex file from the website and imported it into the PICKT2 software. I don't have any PIC16F916s to try programming one..)
Les.
 

Attachments

  • GRAVIMETER.HEX
    5 KB · Views: 252
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top