Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 15th August 2007, 02:37 AM   (permalink)
Default 3 Inputs & 3 Outputs Coding problem

Hi I have three inputs & three outputs.
PORTA1, 2, 3 – inputs
PORTB1, 2, 3 – outputs connected to 3 LED’s

The inputs coming from a digital logic IC.

Inputs coming like this

When PORTA1 = turn on PORTB1
When PORTA1 & PORTA2 = turn on PORTB2
When PORTA1 & PORTA2 & PORTA3 = turn on PORTB3

I want to detect the coming PORTA inputs. I know only to detect one input at a time & don’t know how to detect two or three inputs at a time.

Code:
SW1	btfss	PORTA,1		;check button1
	goto	SW2
	goto	LED1
SW2	btfss	PORTA,2		;check button2
	goto	SW3
	goto	LED2
SW3	btfss	PORTA,3		;check button3
	goto	SW1
	goto	LED3


LED1	bsf	PORTB,1		;turn on LED1
	goto	$-1
LED2	bsf	PORTB,2		;turn on LED2
	goto	$-1
LED3	bsf	PORTB,3		;turn on LED3
	goto	$-1
Please help me

Thanks
Suraj143 is offline   Reply With Quote
Old 15th August 2007, 02:45 AM   (permalink)
Default

Hi,
btfss or btfsc is for checking a single bit of the register.

You can try taking the value of the whole port and compare it, for example:
movf PORTA, w
xorlw b'00000011'
btfsc STATUS, Z
goto LED2
goto SW3
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 15th August 2007, 02:55 AM   (permalink)
Default

Hi bananasiong thats the thing I needed.
It was very good & it didn't come to my mind.

I will apply your code to all three buttons it will work for sure.
I must study the other codes that I have not used before.

Thanks bananasiong.
Suraj143 is offline   Reply With Quote
Old 15th August 2007, 04:16 AM   (permalink)
Default

How about,
Code:
	clrf	PORTB		;Turn off all LEDs
	movfw	PORTA		;Read port A
	andlw	b'00001110'	;keep only bits 1,2 and 3
	xorlw	b'00000010'	;invert bit 1
	btfsc	STATUS,Z	;if it's zero then PortA was 00000010
	bsf	PORTB,1		;Turn on LED1	
	xorlw	b'00000100'	;invert bit 2 - bits 1 and 2 are now inverted
	btfsc	STATUS,Z	;if it's zero then PortA was 00000110
	bsf	PORTB,2		;Turn on LED2
	xorlw	b'00001000'	;invert bit 3 - all three bits now inverted
	btfsc	STATUS,Z	;if it's zero then PortA was 00001110
	bsf	PORTB,3		;Turn on LED3
Not immediately obvious but if you work out what it's doing it should give you a better understanding of the xor instruction.

Mike.
Pommie is online now   Reply With Quote
Old 15th August 2007, 05:03 AM   (permalink)
Default

Hi mike Thanks for your very useful coding. Now I really understand the function of XOR command. Thank you very much.

But a small question to ask when checking the second input (PORTA1 & 2)

Code:
	xorlw	b'00000110'
When checking the third input (PORTA1 & 2 & 3)

Code:
	xorlw	b'00001110'
Shouldn’t it like this?

Thanks
Suraj143 is offline   Reply With Quote
Old 15th August 2007, 05:41 AM   (permalink)
Default

Quote:
Originally Posted by Suraj143
But a small question to ask when checking the second input (PORTA1 & 2)

Code:
	xorlw	b'00000110'
When checking the third input (PORTA1 & 2 & 3)

Code:
	xorlw	b'00001110'
Shouldn’t it like this?

Thanks
Yes, if according to your first post.
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 15th August 2007, 06:07 AM   (permalink)
Default

Quote:
Originally Posted by Suraj143
Hi mike Thanks for your very useful coding. Now I really understand the function of XOR command. Thank you very much.

But a small question to ask when checking the second input (PORTA1 & 2)

Code:
	xorlw	b'00000110'
When checking the third input (PORTA1 & 2 & 3)

Code:
	xorlw	b'00001110'
Shouldn’t it like this?

Thanks
No, because W is not reloaded, the previous bits stay inverted. If Port A contained '00000110' then after xorlw '00000010', W will contain '00000100'. After xorlw '00000100', W will contain zero. If you reload W from port A then your code would be correct.

Mike.
Pommie is online now   Reply With Quote
Old 15th August 2007, 09:46 AM   (permalink)
Default

Some people write out those operands a little differently (below) where the 'test' value is XOR'ed with the previous 'test' value to cancel out or reverse the effects of the previous 'test' value. This might seem more intuitive since you can see the real 'test' value in the expression (the 1st value).


Code:
     clrf    PORTB          ; Turn off all LEDs
movfw PORTA ; Read port A andlw b'00001110' ; keep only bits 1,2 and 3
xorlw b'00000010' ; skpnz ; 00000010? no, skip, else bsf PORTB,1 ; Turn on LED1 xorlw b'00000110'^b'00000010' skpnz ; 00000110? no, skip, else bsf PORTB,2 ; Turn on LED2 xorlw b'00001110'^b'00000110' skpnz ; 00001110? no, skip, else bsf PORTB,3 ;Turn on LED3

Last edited by Mike, K8LH; 15th August 2007 at 09:52 AM.
Mike, K8LH is offline   Reply With Quote
Old 15th August 2007, 10:37 AM   (permalink)
Default

Wow more code to try I was wondering what to select.

Thanks Mike, K8LH for your coding.
I have never used skpnz. Now I’m going to place them in my normal routines.

And also Can you tell me what’s the meaning of “^” character.
Suraj143 is offline   Reply With Quote
Old 15th August 2007, 10:39 AM   (permalink)
Default

Quote:
Originally Posted by Pommie
No, because W is not reloaded, the previous bits stay inverted. If Port A contained '00000110' then after xorlw '00000010', W will contain '00000100'. After xorlw '00000100', W will contain zero. If you reload W from port A then your code would be correct.

Mike.
Oh I see now I understood.

Thanks Mike for your well support.
Suraj143 is offline   Reply With Quote
Old 16th August 2007, 04:24 AM   (permalink)
Default

Quote:
Originally Posted by Suraj143
I have never used skpnz. Now I’m going to place them in my normal routines.

And also Can you tell me what’s the meaning of “^” character.
Hi,
"^" is XOR, you can test and see the result in disassembly listing.
While for skpnz, you can refer to the attachment.
Attached Files
File Type: pdf PIC_Instruction_Reference.pdf (116.0 KB, 5 views)
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 16th August 2007, 05:04 AM   (permalink)
Default

Oh I see it’s a XOR function. Your PDF contains many new instructions. It was very helpful & useful for a beginner.

Thank you very much banansiong.
Suraj143 is offline   Reply With Quote
Old 17th August 2007, 02:46 AM   (permalink)
Default

Hi I have configured my inputs to THREE buttons that is RA4, RA5, RA6.
They are active low inputs (used 10K pull up resistors for each)

But when I run this code all the time the LED2 is turn ON. & my code was not working well.

I have missed something.
Please help me

Thanks

Code:
		org	0x00
		movlw	0x07
		movwf	CMCON
		bsf	STATUS,RP0
		clrf	TRISB		
		movlw	b'01110000'	;make RA4,RA5,RA6 as inputs (active low)
		movwf	TRISA
		bcf	STATUS,RP0
		clrf	PORTA
		clrf	PORTB		
				
SW1		movfw	PORTA	
		andlw	b'01110000'	;keep only bit 4,5,6
		xorlw	b'00010000'	;invert bit 5(check button0)
		btfsc	STATUS,Z
		call	LED0
		xorlw	b'00100000'	;invert bit 6(check button1)
		btfsc	STATUS,Z
		call	LED1
		xorlw	b'01000000'	;invert bit 7(check button2)
		btfsc	STATUS,Z
		call	LED2					
		goto	SW1
		
LED0		movlw	01h
		movwf	PORTB
		return		
LED1		movlw	02h
		movwf	PORTB
		return	
LED2		movlw	04h
		movwf	PORTB
		return	
				
		end
Suraj143 is offline   Reply With Quote
Old 17th August 2007, 03:39 AM   (permalink)
Default

Hi,
You should update w register from PORTA everytime you've changed the value in the w register. Get what I mean? Before checking button 1 and button 2, load again PORTA to w.
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 17th August 2007, 03:58 AM   (permalink)
Default

Hi You mean something like this?
Code:
SW1		movfw	PORTA	
		andlw	b'01110000'	;keep only bit 4,5,6
		xorlw	b'00010000'	;invert bit 5(check button0)
		btfsc	STATUS,Z
		call	LED0
		movfw	PORTA
		xorlw	b'00100000'	;invert bit 6(check button1)
		btfsc	STATUS,Z
		call	LED1
		movfw	PORTA
		xorlw	b'01000000'	;invert bit 7(check button2)
		btfsc	STATUS,Z
		call	LED2					
		goto	SW1
Suraj143 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
PIC A/D Sampling multiple inputs NJ Roadmap Micro Controllers 10 21st February 2007 01:53 AM
PIC 16F628A UART Coding Problem Gayan Soyza Micro Controllers 11 1st February 2007 11:58 AM
Weird 74LS90 counter problem Torben Electronic Projects Design/Ideas/Reviews 11 8th December 2006 09:07 AM
Mysterious Problem! matasoft Micro Controllers 15 11th September 2004 07:49 PM
Creative live 5.1 problem... nassus General Electronics Chat 9 10th June 2004 05:50 AM



All times are GMT. The time now is 03:03 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.