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.

I/O Question, best way to output a byte on two ports?

Status
Not open for further replies.

blueroomelectronics

Well-Known Member
I have available some I/O on two output ports (PIC)

RA 765x321x
RB xx543xxx

I want one byte of I/O (8 bits) it may be asynchronous.

Any suggestions, ideas?
 
Last edited:
RA looks like it's the most of a full byte,
you could read in the RA byte, use a bitmask to prevent the two x bits from being overwritten and write out your data byte, which will give you all but bits 4 and 0, which you should be able to further mask out from your data byte to the leftover bits on RB. You'd still have 3 extra bits on RB so you could use one of those as a strobe 'data ready' bit if you need it to be synchronous.

That's a bit of codeing though, you could also just bit bang a synchronous serial shift register. Many people do that to increase the number of I/O pins on non speed sensative designs.
 
Last edited:
Not very elegant but does the job.
Code:
	movfw	Byte
	andlw	b'11101110'
	movwf	PORTB
	btfss	Byte,0
	bcf	PORTA,3
	btfsc	Byte,0
	bsf	PORTA,3
	btfss	Byte,3
	bcf	PORTA,4
	btfsc	Byte,3
	bsf	PORTA,4

Mike
 
If your using mikroBasic, you could do something like this

Code:
Program Port_Mask

Dim PORTX as Byte

sub procedure [B]Write_Byte[/B](Dim PORTX as Byte)

    TRISA.1 = 0                   ' Change to outputs
    TRISA.2 = 0
    TRISA.3 = 0
    TRISA.5 = 0
    TRISA.6 = 0
    TRISA.7 = 0
    TRISB.3 = 0
    TRISB.4 = 0

    PORTA.1 = PORTX.0        ' Build PORTX's data on the pins
    PORTA.2 = PORTX.1
    PORTA.3 = PORTX.2
    PORTA.5 = PORTX.3
    PORTA.6 = PORTX.4
    PORTA.7 = PORTX.5
    PORTB.3 = PORTX.6
    PORTB.4 = PORTX.7

end sub

sub procedure [B]Read_Byte[/B](Dim PORTX as Byte)

    TRISA.1 = 1                  ' Change to inputs
    TRISA.2 = 1
    TRISA.3 = 1
    TRISA.5 = 1
    TRISA.6 = 1
    TRISA.7 = 1
    TRISB.3 = 1
    TRISB.4 = 1

    PORTX.0 = PORTA.1       ' Build PORTX's data
    PORTX.1 = PORTA.2
    PORTX.2 = PORTA.3
    PORTX.3 = PORTA.5
    PORTX.4 = PORTA.6
    PORTX.5 = PORTA.7
    PORTX.6 = PORTB.3
    PORTX.7 = PORTB.4

end sub

Main:

      PORTX = 123             ' Put the value "123" into PORTX
      [B]Write_Byte[/B](PORTX)     ' Update all of the pins for PORTX

      [B]Read_Byte[/B](PORTX)     ' Read the contents of PORTX

      [B]Write_Byte[/B](234)        ' Write the value "234" to PORTX

      Goto Main
End.
 
Pommie said:
Not very elegant but does the job.
Code:
	movfw	Byte
	andlw	b'11101110'
	movwf	PORTB
	btfss	Byte,0
	bcf	PORTA,3
	btfsc	Byte,0
	bsf	PORTA,3
	btfss	Byte,3
	bcf	PORTA,4
	btfsc	Byte,3
	bsf	PORTA,4
Mike

That is exactly my method of creating a virtual port for 4-bit LCD interfacing. Write to Virtual port and call VP_Update to automatically map individual bits of a byte or bytes to corresponding i/o channels. Either full port masking with mathmatical operators or simple BTFSS, it still elegant in my book. Its elegant because its Virtual :)
 
Last edited:
blueroomelectronics said:
I have available some I/O on two output ports (PIC)

RA 765x321x
RB xx543xxx

I want one byte of I/O (8 bits) it may be asynchronous.

Any suggestions, ideas?
I'm not sure I understand the question. Could you rephrase?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top