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.

Receiving Data

Status
Not open for further replies.

GatorGuy

New Member
Nigel, I have been reading your tutorials on the RS232. The one I am most interested in is receiving data from the RS232. You have done a great job writing the tutorials, but being pretty new to assembly I have a harder time understanding parts of it. Is there a simply way of receiving simple data through an input pin? Example, my software sends an “a” out of COM1. I want to PIC to receive it and compare it to subs in its programming.

EG.

<CODE TO RECEIVE DATA>
If data equals <this>
Call sub1
If data equals <that>
Call sub2

Sorry if anything looks wrong(well all of it) I am at work and some of the keys are out on the keyboard in my office.
 
GatorGuy said:
Nigel, I have been reading your tutorials on the RS232. The one I am most interested in is receiving data from the RS232. You have done a great job writing the tutorials, but being pretty new to assembly I have a harder time understanding parts of it. Is there a simply way of receiving simple data through an input pin? Example, my software sends an “a” out of COM1. I want to PIC to receive it and compare it to subs in its programming.

EG.

<CODE TO RECEIVE DATA>
If data equals <this>
Call sub1
If data equals <that>
Call sub2

Sorry if anything looks wrong(well all of it) I am at work and some of the keys are out on the keyboard in my office.

One of my IR tutorials 5.3 does just that, it tests which remote control button was pressed and jumps to the required routine, this is a piece of the section that tests the keys (it shows just two of them). The first part checks for the value 'But2' which is set as a constant at the beginning of the program, the actual value it checks is in 'Cmd_Byte', if it fails it jumps to Key2 and tries that - if it passes, it runs the code that follows. The actual test is only the first four lines.

There's a complete section on comparisons on the PICList, which is well worth looking at.

Code:
Key1		movlw	But2			;test for button 1
		subwf   Cmd_Byte, w
		btfss   STATUS    , Z
		goto	Key2			;try next key if not correct code

		movf	LED_PORT,	w	;read PORTB (for LED status)
		movwf	tmp3			;and store in temp register
		btfss	tmp3,	LED1		;and test LED bit for toggling
		bsf	LED_PORT,	LED1	;turn on LED
		btfsc	tmp3,	LED1
		bcf	LED_PORT,	LED1	;turn off LED
		bcf	Flags2,	New		;and cancel new flag	
		call	EE_Write		;save the settings	
		retlw	0x00

Key2		movlw	But3			;test for button 1
		subwf   Cmd_Byte, w
		btfss   STATUS    , Z
		goto	Key3			;try next key if not correct code

		movf	LED_PORT,	w	;read PORTB (for LED status)
		movwf	tmp3			;and store in temp register
		btfss	tmp3,	LED2		;and test LED bit for toggling
		bsf	LED_PORT,	LED2	;turn on LED
		btfsc	tmp3,	LED2
		bcf	LED_PORT,	LED2	;turn off LED
		bcf	Flags2,	New		;and cancel new flag
		call	EE_Write		;save the settings		
		retlw	0x00
 
Nigel Goodwin said:
GatorGuy said:
Nigel, I have been reading your tutorials on the RS232. The one I am most interested in is receiving data from the RS232. You have done a great job writing the tutorials, but being pretty new to assembly I have a harder time understanding parts of it. Is there a simply way of receiving simple data through an input pin? Example, my software sends an “a” out of COM1. I want to PIC to receive it and compare it to subs in its programming.

EG.

<CODE TO RECEIVE DATA>
If data equals <this>
Call sub1
If data equals <that>
Call sub2

Sorry if anything looks wrong(well all of it) I am at work and some of the keys are out on the keyboard in my office.

One of my IR tutorials 5.3 does just that, it tests which remote control button was pressed and jumps to the required routine, this is a piece of the section that tests the keys (it shows just two of them). The first part checks for the value 'But2' which is set as a constant at the beginning of the program, the actual value it checks is in 'Cmd_Byte', if it fails it jumps to Key2 and tries that - if it passes, it runs the code that follows. The actual test is only the first four lines.

There's a complete section on comparisons on the PICList, which is well worth looking at.

Code:
Key1		movlw	But2			;test for button 1
		subwf   Cmd_Byte, w
		btfss   STATUS    , Z
		goto	Key2			;try next key if not correct code

		movf	LED_PORT,	w	;read PORTB (for LED status)
		movwf	tmp3			;and store in temp register
		btfss	tmp3,	LED1		;and test LED bit for toggling
		bsf	LED_PORT,	LED1	;turn on LED
		btfsc	tmp3,	LED1
		bcf	LED_PORT,	LED1	;turn off LED
		bcf	Flags2,	New		;and cancel new flag	
		call	EE_Write		;save the settings	
		retlw	0x00

Key2		movlw	But3			;test for button 1
		subwf   Cmd_Byte, w
		btfss   STATUS    , Z
		goto	Key3			;try next key if not correct code

		movf	LED_PORT,	w	;read PORTB (for LED status)
		movwf	tmp3			;and store in temp register
		btfss	tmp3,	LED2		;and test LED bit for toggling
		bsf	LED_PORT,	LED2	;turn on LED
		btfsc	tmp3,	LED2
		bcf	LED_PORT,	LED2	;turn off LED
		bcf	Flags2,	New		;and cancel new flag
		call	EE_Write		;save the settings		
		retlw	0x00


I think I have it.

DATA1 Equ d'10101010'
;INCDATA is what was received from the input pin

movlw DATA1
subfw INCDATA, w ; subtract f from w
btfss STATUS, z ; if z equals 1 then skip
goto nextone

Did I take that right?
 
GatorGuy said:
I think I have it.

DATA1 Equ d'10101010'
;INCDATA is what was received from the input pin

movlw DATA1
subfw INCDATA, w ; subtract f from w
btfss STATUS, z ; if z equals 1 then skip
goto nextone

Did I take that right?

Yes, that's right!.

There are a number or ways of doing it, for example you can use XOR instead of subtract, but this is simple and works well, and it also doesn't affect the value of your data in INCDATA, so you can test it again if the first test fails.
 
Nigel Goodwin said:
GatorGuy said:
I think I have it.

DATA1 Equ d'10101010'
;INCDATA is what was received from the input pin

movlw DATA1
subfw INCDATA, w ; subtract f from w
btfss STATUS, z ; if z equals 1 then skip
goto nextone

Did I take that right?

Yes, that's right!.

There are a number or ways of doing it, for example you can use XOR instead of subtract, but this is simple and works well, and it also doesn't affect the value of your data in INCDATA, so you can test it again if the first test fails.

I didnt think about this when I typed that but if you subtract F from W in that example wouldnt z equal 0 not 1? 0 being that INCDATA matched DATA1. So if you used btfss it would skip the goto and run the code in that sub.?.
 
GatorGuy said:
I didnt think about this when I typed that but if you subtract F from W in that example wouldnt z equal 0 not 1? 0 being that INCDATA matched DATA1. So if you used btfss it would skip the goto and run the code in that sub.?.

The PIC subtract instruction works in a really strange way!, the code in my sample works perfectly.
 
I believe you. Just seems strange. I am away from my equipment to try any of this right now so I cant test what I want to do. Thank you for all of your help Nigel!
 
GatorGuy said:
I didnt think about this when I typed that but if you subtract F from W in that example wouldnt z equal 0 not 1? 0 being that INCDATA matched DATA1. So if you used btfss it would skip the goto and run the code in that sub.?

If INCDATA matched DATA1 then the result is ZERO in a subtraction.

What you have overlooked is that "Z" is not the numerical result of the comparison but the zero flag of the STATUS file register. The code is testing whether this "zero flag" is set or not.

The flag is set (i.e. equals 1) if the comparison result is ZERO. Got it?
 
eblc1388 said:
GatorGuy said:
I didnt think about this when I typed that but if you subtract F from W in that example wouldnt z equal 0 not 1? 0 being that INCDATA matched DATA1. So if you used btfss it would skip the goto and run the code in that sub.?

If INCDATA matched DATA1 then the result is ZERO in a subtraction.

What you have overlooked is that "Z" is not the numerical result of the comparison but the zero flag of the STATUS file register. The code is testing whether this "zero flag" is set or not.

The flag is set (i.e. equals 1) if the comparison result is ZERO. Got it?

Oh okay! That makes more sense now. And I take it that if the subtraction equals anything else the flag is set to 0?
 
GatorGuy said:
And I take it that if the subtraction equals anything else the flag is set to 0?

Yes. The ZERO bit in the STATUS file register is only SET whenever the result of an arithmetic or logic operation is zero.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top