[SOLVED] PIC16F84A - MPLAB programming

Status
Not open for further replies.

dannyelloko20

Newbie level 5
Joined
Mar 25, 2015
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
65
Hi everyone, Im new here so bear with me. I have a project Im working on and kinda need some help. This is pretty much what has to be done: Write an Assembly program using MPLAB IDE and “Project Wizard” that will read binary switch S10 from PORTA and if this value is logic “0”, add the following two Hex Numbers (NUM1 + NUM2) and store the sum in REG1 and the STATUS register in REG2. If S10 is logic “1”, store the difference (NUM1 - NUM2) in REG1 and the STATUS register in REG2. NUM1 = 0x5A and NUM2 = 0x39

This is what I came up with:

Code:
NUM1    EQU   0X5A
NUM2    EQU   0X39
REG1     EQU   0X40
REG2     EQU   0X41

ORG      0X00

START   BSF       STATUS,RP0
            MOVWF    TRISB
            BCF        STATUS,RP0
            MOVWF     PORTB
            
            CLRF      PORTA
            CLRF      PORTB
            MOVLW  0X5A
            MOVWF  NUM1
            CLRW
            MOVLW  0X39
            MOVWF  NUM2

            BTFSS    PORTA,1
            GOTO     SUB
        
            MOVWF  NUM1
            ADDWF  NUM2
            MOVF     REG1
            MOVWF  PORTB
    
SUB      MOVWF  NUM1
            SUBWF   NUM2
            MOVF     REG1
            END

I need some help debugging this code. Im stuck here and I dont know whats wrong.
Thanks
 

Lots of things, starting with not telling the program what processor you are using!

A good tip is to start an MPLAB program by copying the template Microchip provide into the MPLAB source editor so you have the 'bare bones' of the program to start from. I have not tried this but see what I did:
Code:
;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F84A. 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 (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F84A.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************


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

	__CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC

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

NUM1    EQU   0X5A
NUM2    EQU   0X39

	cblock 0x10
REG1
REG2
	endc

	ORG      0X00

Begin
		banksel TRISA				;first make PORTA an input and PORTB an output
		movlw 0x1F					;all PORTA pins to input mode
		movwf TRISA
		clrf  TRISB					;all PORT pins to output mode

; now check PORTA to see if we want to add or subtract. Assume switch is on RA0

		banksel PORTA				;port I/O is in bank zero
		btfss	PORTA,0				;skip next instruction if RA0 = 1 (subtract requested)
		goto	AddMode

; if the previous line didn't take us to AddMode, subtract was requested

		movlw	NUM2				;put NUM2 in the working register
		sublw	NUM1				;subtract it from NUM1 with the result in the W register
		movwf	REG1				;save the result in REG1
		movf	STATUS,W			;copy the status register contents to W
		movwf	REG2				;and then to REG2
		goto 	Finished

AddMode
		movlw	NUM1				;put NUM1 in the working register
		addlw	NUM2				;add NUM2 with the result in the W register
		movwf	REG1				;save the result in REG1
		movf	STATUS,W			;copy the status register contents to W
		movwf	REG2				;and then to REG2

Finished
		end
Sorry if the formatting is odd, it's a mix of your code, my code and template which may have used different tab settings.

Brian.
 

Thanks for your help. I now understand it better. I'm sure this wont my last post so I hope you can help with more projects.
 

I think writing in assembly language is a waste of time. But you will get better idea about controller ......
I recommend you to switch from assembly to embedded c. Try any compiler like HITEC C ANCI C for 12/16 or use micro c
 

I think writing in assembly language is a waste of time. But you will get better idea about controller ......
I recommend you to switch from assembly to embedded c. Try any compiler like HITEC C ANCI C for 12/16 or use micro c

I wish that was an option but unfortunately this is mandatory for my EET program. Thanks!
 

In many applications the use of 'C' makes programming far more complicated and unpredictable. The big advantage of asm is you have total control over it and in time critical applications that may be vital. A program written in 'C' may result in different timing even from one version to another of the same compiler or if different levels of optimization are chosen. Asm programs are also generally much smaller.

Brian.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…