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.

Don't care

Status
Not open for further replies.

bananasiong

New Member
Hi,
I'm using PortB for the patterns of 7-segmen displays, RB1 to RB7. If I want to use RB0 as other output port, in the looked up table, can I do like this:
retlw b'0110100x'? So that RB0 is not affected.
I don't want RB0 to be affected by the multiplexing of 7-segment displays.

Thanks
 
You have to combine the bits. The easiest way is to put a zero in your table and or the bit in. EG,
Code:
	call	TableStuff;	get the value from the table
	btfsc	PORTB,0;	Test state of port b
	iorlw	0x01;		if it's set then set bit 0 of W
	movwf	PORTB;		write it to the port

HTH

Mike.
 
retlw b'0110100x'? So that RB0 is not affected.
No, not directly, but you could mask off RB0 before writing the byte to PORTB:
Instead of x, make all bit0's in your lookup table 0's. Then do something like this:

call table
;data for portB now in W....
BTFSC PORTB, RB0 ; check bit0
IORLW b'00000001' ; set bit0 in W if RB0 was set
MOVWF PORTB ; Write data from W to portB
 
Thanks, but the multiplexing happens during interrupt, every 4 ms. And I need to control RB0 in the main routine.
Then I think the pin has to be left unused.
 
The code posted (twice) above will work fine in the IRQ as long as in your main routine you only use bsf and bcf instructions.

Mike.
 
Oh ya, I got it. Thanks :D:D
Then my interrupt routine will have 2 us more, I need to recalculate the delay time.

Thanks a lot.
 
Why do you need to recalculate your delay time if it's on interrupt?

You shouldn't have to have any delay. Can you post your ISR code?

Mike.
 
Nono.. I didn't state clearly. I use interrupt vector to count for the delay time. Previously, my interrupt happens every 4 ms, and the instructions in ISR takes around 17 us to be executed. Then I count for around 250 times to get 1 second. Now 2 us is added into the interrupt vector, I need to change abit the value of PR2 and the 250, to make it be more accurate.
 
If you are using PR2 then your interrupt should stay at 4mS. I assume you have something like a prescaler of 16 and a PR2 value of 250 to give a 4mS ISR. Adding 2uS will not effect the timing of the ISR.

Mike.
 
Last edited:
I'm using 4 MHz crystal, so 1 us per instruction. Prescaler 4 and postscaler 4 and PR2 is actually 245. So interrupt happens every 3.92 ms. The instructions in the ISR takes around 17 us. So interrupt takes 3.937 ms, I count 254 times to get 1 second. If 2 us added to the ISR, multiply around 250 times, then around 500 us is increased every second. I need to calculate it because I need the accuracy.
 
Another method that preserves the RB0 bit. Data in display array uses b6..b0 bits.

Code:
;
ISR_LEDs
        rrf     PORTB,W         ; put RB0 bit state in 'C'
        rlf     INDF,W          ; LED data <<1 and pickup RB0 bit from 'C'
        movwf   PORTB           ; update display
;
You might show us the few lines of code you're using to update your display now.
 
Last edited:
bananasiong said:
I'm using 4 MHz crystal, so 1 us per instruction. Prescaler 4 and postscaler 4 and PR2 is actually 245. So interrupt happens every 3.92 ms. The instructions in the ISR takes around 17 us. So interrupt takes 3.937 ms, I count 254 times to get 1 second. If 2 us added to the ISR, multiply around 250 times, then around 500 us is increased every second. I need to calculate it because I need the accuracy.

It sounds like you are writing to PR2 in your ISR. You should set PR2 to 250, the prescaler to 16, postscaler to 1 and your interrupts will be at 4mS exactly. Do not rewrite the PR2 value in your ISR or your timing will be completely messed up.

Mike.
 
I expect the interrupts to be at 4 ms if you load the PR2 register with 249 (precaler = 16, postscaler = 1). Do you agree?
 
I must admit that I don't normally use Timer 2 as I prefer the 16 bit period of Timer 1.

However, as I understand it (with PR2=1),
Timer2 starts of equal to zero.
The prescaler gets incremented until it reaches 16.
Timer2 gets incremented.
Timer2 = PR2.
Timer2 gets reset.
Go around again.

So I think it's 16uS. But, I may be wrong. Edit, if you care to try it in the simulator, I would be interested in the result. I may, time permitting, try it myself tomorrow (it's 10PM here).

Mike.
 
Last edited:
You always load PR2 with the value you want -1. I'm sure I've read why somewhere but promptly forgot about it since all you really need to remember is the -1 part (grin). So that means that if you want the interrupt flag set at 32-usecs you'd load PR2 with 2-1.
 
Last edited:
Mike said:
You always load PR2 with the value you want -1. I'm sure I've read why somewhere but promptly forgot about it since all you really need to remember is the -1 part (grin).

From the datasheet of the PIC16F628A:
The TMR2 register value increments from 00h until it
matches the PR2 register value and then resets to 00h
on the next increment cycle. The PR2 register is a
readable and writable register. The PR2 register is
initialized to FFh upon Reset.

Pommie, I've already made some experiments :) I'll read your comments tomorrow.
 
I just found it in the data sheet,

• TMR2 is reset to 00h on the next increment cycle.

So, yes it should be 249.

What happens with a prescaler of 1 and a postscaler of 16?

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top