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.

subtraction of two 8 bit numbers PIC12F508

Status
Not open for further replies.

be80be

Well-Known Member
I'm trying to subtract revived 0x31 that's saved in datarecved var.

But when I send 0x30 the test should be false and skip to turn led off it doesn't happen

here the code
Code:
Start		call Rcv_RS232  ; gets the data from the rx pin
			call LED        ; If we get data then lets turn on a led
			goto Start      ;No data come back and keep looking
LED			
			movlw 0x31    ; loads test_byte wit hex 31
			movwf test_byte
			movf  test_byte,W   ; moves to W
			subwf datarecved,W  ; this should have 0x31 
			btfss STATUS,Z      ; if this is 0
			goto  Led_on       ; led is on if 0
			goto  Led_off      ; led is off if 1
Led_on
			bsf   GPIO,5
			goto  Start ; returns to start to loop for new data
Led_off
			bcf   GPIO,5
			goto  Start
 
It's NOT zero so it's NOT set so it does NOT skip.
 
I tested it with 0x31 for test_byte and data_received, it gave zero, set status, and skipped to LED off. 0x30 gave LED on and status,Z was not set.

Isn't that how it is supposed to work?

John
 
Yes but it's not happening on hardware and I no it's sending the right thing
 
This may sound trivial to say, but if you post your exact schematic, I will breadboard it and see what happens. I only have 509's and 683's, but that shouldn't make a difference.

John
 
It's simple just 2 12f508 tx is on one chip and the receiver rx is on just one chip the TX part I know is working the RX part is receiving but the test is not getting the right value
View attachment 61231

TX code
Code:
 ;**********************************************************************
;                                                                     *
;    Filename:     serial.asm                                        *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:   Burt E Ratliff                                         *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P12F508.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************

 list      p=12F508            ; list directive to define processor
 #include <p12F508.inc>        ; processor specific variable definitions

 __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC

; pin assignments
   
 #define  TX  GPIO,1  ; transmit pin on GP0



;***** VARIABLE DEFINITIONS
      UDATA
 buffer     res    1            
 counter    res    1 
 Count      res    1 
 count1     res    1
 counta     res    1
 countb     res    1

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


; Internal RC calibration value is placed at location 0x1FF by Microchip
; as a movlw k, where the k is a literal value.

RESET   CODE    0x000           ; effective reset vector
        movwf   OSCCAL          ; update OSCCAL with factory cal value 
  goto    init              ; jump to main program
init 
            movlw b'00000000'   ; set I/O
            tris    GPIO 
Send 
            movlw 0x31
            call TX_RS232
			call Long_delay
			movlw 0x30
			call TX_RS232
			call Long_delay
            goto Send   ;loop for ever
  
TX_RS232    
            MOVWF   buffer             ;move W to Xmit_Byte
            MOVLW   0x08                  ;set 8 bits out
            MOVWF   counter
            BCF     TX
            CALL    Bit_Delay
Send_Loop   
            RRF     buffer , f         ;send one bit
            BTFSS   STATUS    , C
            BCF     TX
            BTFSC   STATUS    , C
            BSF     TX
            CALL    Bit_Delay
            DECFSZ  counter  , f         ;test if all done
            GOTO    Send_Loop
            BSF     TX
            CALL    Bit_Delay
            RETURN
Bit_Delay   MOVLW   0x18
            MOVWF   Count
Bit_Wait    NOP
            DECFSZ  Count , f
            GOTO    Bit_Wait
            RETURN
Long_delay  call delay_255
			call delay_255
			call delay_255
			call delay_255
			return

delay_255
	        movlw	0xff		;delay 255 mS
		    goto	d0
d0		    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
	    	return


  end

And the receive code
Code:
 ;**********************************************************************
;                                                                     *
;    Filename:     serial.asm                                        *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:   Burt E Ratliff                                         *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P12F508.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************

 list      p=12F508            ; list directive to define processor
 #include <p12F508.inc>        ; processor specific variable definitions

 __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC

; pin assignments
   
 #define  RX  GPIO,0  ; transmit pin on GP0


;***** VARIABLE DEFINITIONS
      UDATA
 buffer     res    1            
 counter    res    1 
 Count      res    1 
 RXdata     res    1
 datarecved res    1
 test_byte  res    1
;**********************************************************************


; Internal RC calibration value is placed at location 0x1FF by Microchip
; as a movlw k, where the k is a literal value.

RESET   CODE    0x000           ; effective reset vector
        movwf   OSCCAL          ; update OSCCAL with factory cal value 
  goto    init              ; jump to main program
init 
            movlw b'00000001'   ; set I/O
            tris    GPIO 
		
Start		call Rcv_RS232  ; gets the data from the rx pin
			call LED        ; If we get data then lets turn on a led
			goto Start      ;No data come back and keep looking
LED			
			movlw 0x31    ; loads test_byte wit hex 31
			movwf test_byte
			movf  test_byte,W   ; moves to W
			subwf datarecved,W  ; this should have 0x31 
			btfss STATUS,Z      ; if this is 0
			goto  Led_on       ; led is on if 0
			goto  Led_off      ; led is off if 1

Led_on
			bsf   GPIO,5
			return ; returns to start to loop for new data
Led_off
			bcf   GPIO,5
			return

Rcv_RS232   BTFSC   RX      ;wait for start bit
            GOTO    Rcv_RS232
            CALL    Start_Delay	          ;do half bit time delay
            BTFSC   RX      ;check still in start bit
            GOTO    Rcv_RS232
            MOVLW   0x08                  ;set up to read 8 bits
            MOVWF   counter
            CLRF    RXdata
Next_RcvBit CALL    Bit_Delay
            BTFSS   RX
            BCF     STATUS    , C
            BTFSC   RX
            BSF     STATUS    , C
            RRF     RXdata  , f
            DECFSZ  counter  , f         ;test if all done
            GOTO    RXdata
            CALL    Bit_Delay
            MOVF    datarecved, W
            return

Start_Delay MOVLW   0x0C
            MOVWF   Count
Start_Wait  NOP
            DECFSZ  Count , f
            GOTO    Start_Wait
            RETURN

Bit_Delay   MOVLW   0x18
            MOVWF   Count
Bit_Wait    NOP
            DECFSZ  Count , f
            GOTO    Bit_Wait
            RETURN

;End of serial routines
          end

The receive and transmit code is from Nigel site Like
 
Last edited:
I'm trying to subtract revived 0x31 that's saved in datarecved var.
But when I send 0x30 the test should be false and skip to turn led off it doesn't happen

So subwf 0x31 - 0x31 doesn't set Z to zero

First you say 31 - 30, now you say 31 - 31. Which one is it?
 
Last edited:
duffy it is testing 0x31 then it sends 0x30 what the hell dose that got to do with it. The code should work the led should be off if I send a 31 and on if I send 30


It should toggle the LED not happening here. Duffy thanks for the help but go post some where else if your not read the code i don't see any value in your post


Duffy I think you need to read before you post I didn't say 30 Jap said that the test is not working I read some where last not that subwf may not work with a 12f508 base line chip or it could be oscillator not running the same speed.

But I do now it's sending right 31 the 30 about a sec apart.
 
Last edited:
Let me clarify that earlier this morning, I simulated with a 12F683 using seed bytes of 0x30 and 0x31 as both test_byte and datarecved byte, and it worked as expected.

After reading your comment about the 12F508, I re-tested using both the 12F508 and 12F509 in simulation. The results for each chip were the same as follows:

Code:
datarecved        test_byte        status        LED        z
0x30                 0x30                 1F       off          1
0x31                 0x30                 1B       on          0
0x30                 0x31                 18       on          0

Here is the code I actually simulated:
Code:
		LIST
test_byte			EQU	0x11	; 
datarecved		EQU	0x0B	;
d3			EQU 0x0C 

;================================
;       Main Code               
;================================
	ORG	0x3FF			; Processor reset vector

Main
	ORG		0x000		; 
	MOVLW	B'00000000'	; 
	TRIS	GPIO		; Configure pins as either I or O
	MOVLW	B'00000000'	;
						; 
	OPTION				; 

Start		;call Rcv_RS232  ; gets the data from the rx pin
			;call LED        ; If we get data then lets turn on a led
			;goto Start      ;No data come back and keep looking
LED			
			movlw	0x30
			movwf	datarecved
			movlw 	0x31  ; loads test_byte wit hex 31
			movwf	 test_byte
			movf  	test_byte,W   ; moves to W
			subwf 	datarecved,W  ; this should have 0x31 
			btfss 	STATUS,z      ; if this is 0
			goto  	Led_on       ; led is on if 0
			goto  	Led_off      ; led is off if 1
Led_on
			bsf   GPIO,5
			goto  Start ; returns to start to loop for new data
Led_off
			bcf   GPIO,5
			goto  Start

	END




John
 
I know the code testing was good but I still can't figure why on the hardware it's not working If i send 0x31 the led comes on send 0x30 it don't go off.

Im going to add a iscp header so I can program without pulling the chip and try sending the command from the pc till I get this working.

I haven't been using ASM in a while. Maybe write it in basic and see what happens. Na going to get this working.
 
You solution is probably best, but just grasping at straws, what if the communication is dropping a bit? Are you sending in reverse order? If a bit is missed, the received byte would probably get shifted and neither is equal to 0x30.

There are lots of permutations one can imagine for that type of situation, such as the timing being a little off and causing the last bit transmitted to be missed. One test of that might be to send 0x24 and test against 0x48 and 0x12.

Whatever you find to be the cause, please let the rest of us know.

John
 
The sending is right i checked that with realterm. The chip with the RX code maybe not receiving right but I tried it in oshonsoft and it showed it to be working right as far as receiving goes but the led didn't work as planed.
 
Hi Burt,

I thought I might have spotted a problem but I was wrong. Sorry!

Regards, Mike
 
Last edited:
Well I no one thing it's not this part it works fine it in the sending or receiving end I'm wondering if it not the dang chip I had to reset the OSCCAL 5 times it keep coming up it was wrong with the pickit2

Code:
RESET   CODE    0x000           ; effective reset vector
        movwf   OSCCAL          ; update OSCCAL with factory cal value 
  goto    init              ; jump to main program
init 
            movlw b'00010000'   ; set I/O
            tris    GPIO 
			movlw b'00000000'
			movwf   GPIO
		
Start		movlw 0x31 
			movwf datarecved
			call LED        ; If we get data then lets turn on a led
			call Long_delay
	        movlw 0x30 
			movwf datarecved
			call LED        ; If we get data then lets turn on a led
			call Long_delay
			goto Start      ;No data come back and keep looking
LED			
			movlw 0x31    ; loads test_byte wit hex 31
			movwf test_byte
			movf  test_byte,W   ; moves to W
			subwf datarecved,W  ; this should have 0x31 
			btfss STATUS,Z      ; if this is 0
			goto  Led_on       ; led is on if 0
			goto  Led_off      ; led is off if 1

Led_on
			bsf   GPIO,5
			return ; returns to start to loop for new data
Led_off
			bcf   GPIO,5
			return
      
Long_delay  call delay_255
			call delay_255
			call delay_255
			call delay_255
			return

delay_255
	        movlw	0xff		;delay 255 mS
		    goto	d0
d0		    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
	    	return

  END

I changed the TX and RX pins RX on gp4 and TX on GP2 so i can use the pickit2 to program fast don't have to unhook and power up lol
 
Yep here a pic of the Tx it has to be in the RX part but I found out if I use GP2 for TX it doesn't send on hardware I think it maybe picking up noise some how
View attachment 61245
 
Well I had the RX pic echo back it not sending what it getting I send it a 0x31 I get a 0x0B
 
Well I hooked the The sending pic TX to a pic running hardware Uart and code running the same test. It works if I test for not <> but if you do a == test it doesn't work. And the echo back from RX pic only sends the test value

Like 0x31 <> 0x31 it echo back the 0x30 So to make this work I need to tell the TX to stop sending a simple delay is not cutting it even a 2 second one between sends
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top