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.

How can I write 7 bits to PORTA?

Status
Not open for further replies.
Hi,

I'm writing a 24hr clock program using a 16F690. There is a definition I use:

#define display PORTC ;portC drives 6 digit 7 seg com. cathode display

I use a look up table to convert binary to seven seg e.g:

Lookuptable:
retlw b'00111111' ; = 0 on 7 seg display

Then I send the data to the display:

movwf display

All this works fine! However the seven seg data is only seven bits (obviously) and I want to use the 'spare' bit7 on portC for another output function. Is there an easy way to make my label/definition only apply to bits 0 to 6 on portC and my look up table only return bits 0 to 6?

Thanks in advance for any help.
 
Hi,

I'm writing a 24hr clock program using a 16F690. There is a definition I use:

#define display PORTC ;portC drives 6 digit 7 seg com. cathode display

I use a look up table to convert binary to seven seg e.g:

Lookuptable:
retlw b'00111111' ; = 0 on 7 seg display

Then I send the data to the display:

movwf display

All this works fine! However the seven seg data is only seven bits (obviously) and I want to use the 'spare' bit7 on portC for another output function. Is there an easy way to make my label/definition only apply to bits 0 to 6 on portC and my look up table only return bits 0 to 6?

Thanks in advance for any help.

hi,
You could OR the status of bit7 [from a image reg] with the segment pattern.

Is this what you are asking.?:)
 
Code:
; [COLOR=SeaGreen]define bit mask     [/COLOR]
[FONT=Courier New]     
[COLOR=Magenta]bitMask[/COLOR]   [COLOR=Navy]equ [/COLOR] b'01111111'
;[COLOR=SeaGreen] enter with [/COLOR][COLOR=SeaGreen]bits for 7 segment display in Wreg bits 6-0
    [/COLOR]             [COLOR=Navy]xorwf [/COLOR]      [COLOR=Magenta]PORTC,W[/COLOR]
                 [COLOR=Navy]andlw[/COLOR]       [COLOR=Magenta]bitMask[/COLOR]
                 [COLOR=Navy]xorwf[/COLOR]       [COLOR=Magenta]PORTC,F[/COLOR]
[/FONT]

You might need to work with a 'copy' of PORTC to avoid read-modify-write issues.

You can set/clear bit7 of PORTC using bsf/bcf instructions.
 
Hi eric,

Thanks for your prompt reply:

[You could OR the status of bit7 [from a image reg] with the segment pattern.]

erm, not really sure what your on about there! Sounds a bit over my head in programming terms!

It's just that in both my definition and the data I send to portC I wish to exclude the msb (without having to add lots of lines of code) so that I can use it for an output function not associated with the display. If the solution is complex I'd sooner leave things as they are and use an output pin other than RC7. Sorry if I'm not articulating my point properly :)
 
Code:
; [COLOR=SeaGreen]define bit mask     [/COLOR]
[FONT=Courier New]     
[COLOR=Magenta]bitMask[/COLOR]   [COLOR=Navy]equ [/COLOR] b'01111111'
;[COLOR=SeaGreen] enter with [/COLOR][COLOR=SeaGreen]bits for 7 segment display in Wreg bits 6-0
    [/COLOR]             [COLOR=Navy]xorwf [/COLOR]      [COLOR=Magenta]PORTC,W[/COLOR]
                 [COLOR=Navy]andlw[/COLOR]       [COLOR=Magenta]bitMask[/COLOR]
                 [COLOR=Navy]xorwf[/COLOR]       [COLOR=Magenta]PORTC,F[/COLOR]
[/FONT]

You might need to work with a 'copy' of PORTC to avoid read-modify-write issues.

You can set/clear bit7 of PORTC using bsf/bcf instructions.

^^^^^^^^^^^^^^^^^^^^

Hi Geko,

Any chance of a line-by-line tutorial? :)
 
hi AB,
As you have LED's being segment driven and multiplexed.?
You may want to change the state of PORT bit 7 at other times than when you want to refresh the display.
Use BSF PORTx.7 or BCF PORTx.7 for those.

If you only want to change PORTx.7 at the refresh times, save the state you want bit7 to be in say, reg1 bit7.

Then when you refresh, get the segment pattern from the table and OR it with reg1, then output it.

OK.?
 
Where you would write W to PORTC using movwf PORTC, just insert the code shown.

If will overwrite bits 6-0 of PORTC with whatever is in W and leave bit 7 of PORTC unchanged.

To change bit 7 of PORTC elsewhere in your program just use bsf PORTC,7 or bcf PORTC,7 as needed.
 
Last edited:
Geko's code will work but it is not obvious what it is doing. If you want an easier to understand piece of code, try,
Code:
; enter with bits for 7 segment display in Wreg bits 6-0  

	btfsc	PORTC,7		;is bit 7 set
	iorlw	128		;yes, so set bit 7 of W
	movwf	PORTC		;write it to the port

Mike.
 
In C I use this general purpose code.
Variable mask is the bits you want to change and val is their value.

LATA = (LATA & ~mask) | (val & mask);

The left side of the OR picks the bits you keep,
they are OR'ed with the bits you want to change.

It is short enough that it can be coded as a macro
which is useful inside interrupt handlers.

There is an example of this in JPUG1.
 
Last edited:
Geko's code will work but it is not obvious what it is doing. If you want an easier to understand piece of code, try,
Code:
; enter with bits for 7 segment display in Wreg bits 6-0  

[FONT=Courier New]    btfsc    PORTC,7        ;is bit 7 set
    iorlw    128        ;yes, so set bit 7 of W
    movwf    PORTC        ;write it to the port[/FONT]
Mike.

Worth mentioning that with Pommie's code:) it's important that bit 7 of the data in W is always 0, otherwise you may unintentionally set bit 7 of PORTC. If you know it will always be 0 the code above is okay, otherwise adding an andlw instruction will ensure that it works in all circumstances.


Code:
[I]; enter with bits for 7 segment display in Wreg bits 6-0  [/I]
[FONT=Courier New]    andlw    0x7F       ;ensure bit 7 of W reg is clear
    btfsc    PORTC,7    ;is bit 7 of PORTC set?
    iorlw    128        ;yes, so set bit 7 of W
    movwf    PORTC      ;write it to the port[/FONT]
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top