Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

i need help in assembly language coding in PIC16F73

Status
Not open for further replies.

rangerskm

Full Member level 4
Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Visit site
Activity points
2,663
Code:
 list p=pic16f73
    #include "p16f73.inc"
    cblock 0x20
    r0,r1,r2,r3
    endc
    org 0x00
    movlw 0x0ff
    movwf r0
    movlw 0x0ff
    movwf r1
    movlw 0x0ff
    movwf r2
    movlw 0x0ff
    movwf r3
    movf r0,w
    addwf r2,1
    btfss status,0
    goto x
    movlw 0x01
    addwf r1,1 
x:  Movf r1,w
    addwf r3,1
    goto $
    end

can anyone explain the logic in this program
 

Code:
Hi,

That is a good example of how not the write code, it has no comments at all to say what its intentions are or how its doing it.

It should look more like this.



Code:
	 list p=pic16f73
    #include "p16f73.inc
    
    cblock 0x20		; specify user register
    r0,r1,r2,r3
    endc
    
    org 0x00		; start location
    
    movlw 0x0ff		; move h FF into register r0/1/2/&3
    movwf r0
    movlw 0x0ff
    movwf r1
    movlw 0x0ff
    movwf r2
    movlw 0x0ff
    movwf r3
    
    movf r0,w		; load r0 into W
    addwf r2,1		; add W to r2 and store back in r2
    
    btfss status,0  ; test the Status Carry bit 0 to see if a carry
    				; has occured during the Add operation
    goto x			; if no carry tgto  x
    
    movlw 0x01		; there was a Carry so Add 1 to r1
    addwf r1,1 
    
x:  Movf r1,w		; add r1 to r3
    addwf r3,1
    
    goto $			; loop here, avoid the $ use a label  loop goto loop
    end
 


Code ASM - [expand]
1
2
3
4
5
6
7
8
movlw 0x0ff
movwf r0
movlw 0x0ff
movwf r1
movlw 0x0ff
movwf r2
movlw 0x0ff
movwf r3



replace the above by


Code ASM - [expand]
1
2
3
4
5
movlw 0x0ff
movwf r0
movwf r1
movwf r2
movwf r3

 

is this program represents a 16 bit addition .if yes how it works.explain the calculation
 

Hi,

It looks like it, but as I originally said it is poor code if there are no comments as to its function.

The registers used do little to help clarify things.

Instead of r0,r1,r2,r3 it would be much clearer if they were work1, work2, resultL, resultH.

If you do that you should be able to follow the code and work out the logic for yourself.

However you might be better using MPLAB SIMulator and Stepping through the code line by line and viewing the Watch window change your registers as each step is executed.

The following is a much simpler way to do 16 bit addition, however as with you original code its incomplete because there is no check on when the 16 bits needs to carry over.

Code:
	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT

	cblock 0x20
	resultL
	resultH
	endc


	ORG	0

	movlw	0xCC            ; load CC into resultL & H
	movwf	resultL
	movwf	resultH

	movlw	 0xFF			; load new value
    addwf	 resultL,F		; add W to resultL and store back in r2
    
    btfss	 status,C  		; test the Status Carry  to see if a carry
    			 			; has occured during the Add operation
    goto	 loop			; if no carry to loop
    
    incf 	resultH  		; carry is set, so increment reseultH
    
loop goto	 loop			; loop here,

   end
 

Attachments

  • 000037.jpg
    000037.jpg
    62.6 KB · Views: 149

this is the program for 16 bit addition
this is 16 bit addition i have to set a carry value in reg CRY after the 2 high bit addition,i need to know whether carry bit of status register is cleared automatically or not,
as i mentioned carry bit is checked 2 times in program ,i am not able to check the carry bit in second definition of program whats the problem in it.
Code:
LIST P=PIC16F73
#INCLUDE "P16F73.INC"
CBLOCK 20H
DT0L
DT0H
DT1L
DT1H
RSTL
RSTH
CRY
ENDC
ORG 0X00
MOVLW 0XFF
MOVWF DT0L
MOVLW 0XFF
MOVWF DT0H
MOVLW 0XFF
MOVWF DT1L
MOVLW 0XFF
MOVWF DT1H
MOVF DT0L,0
ADDWF DT1L,0
BTFSC STATUS,0
INCF DT0H,1
MOVWF RSTL
MOVF DT1H,0
ADDWF DT0H,0
BTFSC STATUS,0
INCF CRY,1
MOVWF RSTH
END
 

Hi,

Have modifed a few instructions so the code is clearer to follow, see below.

I have not altered the logic of your code, it does extactlly what you have programmed it to do.

You really need to use Mplabs SIM and a WATCH window and single step though the code and 'see' the registers changing after each instruction.

Have taken a few screenshots of your code as I stepped though it - think you should be able to see what is happening.. ?

The Carry bit of the Status register is not 'cleared' as such, its either set to 0 or 1 dependant on the last instruction that used it.
If your first addition set it to 1, then your second addition will either set it to 1 or 0 depending on the ADDs result.
There are several instructions that change the C bit.


Code:
	#INCLUDE "P16F73.INC"
	CBLOCK 20H
	DT0L
	DT0H
	DT1L
	DT1H
	RSTL
	RSTH
	CRY
	ENDC
	ORG 0X00

	

	MOVLW 0XFF		; load FF to DT0L/H
	MOVWF DT0L
	MOVLW 0XFF
	MOVWF DT0H

	MOVLW 0XFF		; load FF to DT1L/H
	MOVWF DT1L
	MOVLW 0XFF
	MOVWF DT1H

;	MOVF DT0L,0		; load DT0L to W  better to code as follows
	MOVF DT0L,W

;	ADDWF DT1L,0	; add DT0l to DT1l and place result in W
					; better coded as -
	ADDWF DT1L,W

	BTFSC STATUS,C
	INCF DT0H,F
	MOVWF RSTL
	MOVF DT1H,W		; which is FF
	ADDWF DT0H,W    ; which is 00
	BTFSC STATUS,C  ; result is FF
	INCF CRY,F		; so do not increment
	MOVWF RSTH		; store result 
	END     ; progam to flash all of PortB pins on and off every 4 seconds
 

Attachments

  • New folder.zip
    269.6 KB · Views: 111
actually it should produce a carry bit in CRY reg as this is 16 bit addition i dont know the mistake in it.
 

actually it should produce a carry bit in CRY reg as this is 16 bit addition i dont know the mistake in it.

Hi,

No, the way you have coded it, it does not produce a carry bit in CRY.

Follow the code and screenshots I provided .

You load your 4 files with FF.

You Add DT0L and DT1L , FF+FF = FE plus the Carry bit of the Status register to 1

If C is 1 then Increment DT1H , FF+1 = 00

However Incrementing a file does not use the C bit, so you have nothing to check if a Carry over has occurred.
You really need to 'increment' the file by ADDing +1 so you can use the C bit

Next you Add DT0H to DT1H, FF + 00 = FF , Carry bit = 0 because no carry has been created by the addition.

CRY is not incremented because no Carry over has been produced.
 

you are right brother no carry is generated, there is mistake in my logic ,i have to correct it, can you guide me in that. 2 16 bit addtion is ffff+ffff=1fffe so i have to produce 1 as the carry value in CRY REG can u help me to rewrite the logic in it.
 

Hi,

As said, you need to change your INCF DT0H to an ADD 1 and then BTFSx on the C flag, if its set to 1 then you need to increase DT1 by 1

You also need to check DT1H in case result rolls over from FF FF to 01 00 00 etc

You might find it easier to first draw a flow chart of your logic or use Mplab Sim , as shown in those screenshots, so you can check your logic, instruction by instruction.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top