PDA

View Full Version : interrupt the 240ac power line by using the pin rb0/int(zero crossing detector) of pi



texas5
- 9th September 2008, 15:15
i try to interrupt the 240ac power line by using the pin rb0/int(zero crossing detector) of pic16f877a.the code as shown below(search from internet):

void interrupt()
{
PORTA = PORTA - 1; //decrement the value on PORTA
INTCON.INTF = 0; //enable new RB0/INT interrupts
OPTION_REG=0;
}

void main()
{
OPTION_REG.INTEDG=1;
TRISA = 0x00; //set all PORTA pins as outputs
PORTA = 0xff; //make all PORTA pins high
INTCON.INTE = 1; //enable RB0/INT interrupts
INTCON.GIE = 1; //enable all un-masked interrupts
}

are those coding will work.can someone help me briefly translate it into basic pro language cause i use microcode studio basic pro to compile.

skimask
- 9th September 2008, 18:29
A little bit of legwork on your end wouldn't hurt your case either ya know...
(Comments in boldface, obviously)
[code]
void interrupt() If you don't know how to use interrupts, this would be a good chance to learn how to use them
{
PORTA = PORTA - 1; //decrement the value on PORTA PortA = PortA -1, although I don't think I'd use this method. I'd use another variable to do the counting and then copy that variable into portA
INTCON.INTF = 0; //enable new RB0/INT interrupts The datasheet for your particular PIC will show you which bit of INTCON enables RB0/INT interrupts
OPTION_REG=0;
}

void main()
{
OPTION_REG.INTEDG=1; Again, the datasheet will show you which bit select the edge to trigger from
TRISA = 0x00; //set all PORTA pins as outputs
PORTA = 0xff; //make all PORTA pins high
INTCON.INTE = 1; //enable RB0/INT interrupts same as above
INTCON.GIE = 1; //enable all un-masked interrupts same as above
}
[/QUOTE]

texas5
- 9th September 2008, 23:38
thanx for the comment skimask.but i don't clearly understand about your comment on:

"PortA = PortA -1, although I don't think I'd use this method. I'd use another variable to do the counting and then copy that variable into portA"

what method do you prefer to use???what is the meaning of PortA = PortA -1 in this case???sory to ask,i'm a newbie in this thing.thanx once again for your reply...

skimask
- 10th September 2008, 00:41
"PortA = PortA -1, although I don't think I'd use this method. I'd use another variable to do the counting and then copy that variable into portA"

what method do you prefer to use???what is the meaning of PortA = PortA -1 in this case???sory to ask,i'm a newbie in this thing.thanx once again for your reply...

If you use PortA = PortA - 1, you may run into issues with the PIC.
PortA=PortA - 1 will read the actual values on the pins, subtract one, and write it back.
Sounds good in theory, doesn't always work that way in practice. Might work well, might not.
I think a better way would be to read that PortA value into a temporary variable, subtract 1, then write that value back. At least then you have a copy of that value in that temporary variable to mess with, and without messing with the actual Port itself.

texas5
- 10th September 2008, 03:36
thanx.actually,u has help me a lot.one more thing...can i compile the coding using microcode studio basic pro???

skimask
- 10th September 2008, 04:21
thanx.actually,u has help me a lot.one more thing...can i compile the coding using microcode studio basic pro???

What 'coding'?

texas5
- 10th September 2008, 10:04
nothing.sory.hehe.i've tested my hardware today.it's work man.thanx to you.i modify the above code into:

DEFINE OSC 8
TRISA.1=0
TRISA.2=0

MAIN:

INTCON.4 = 1 ;enable RB0/INT interrupts
INTCON.7 = 1 ;enable all un-masked interrupts

if INTCON.1 = 1 Then ;RB0/INT interrupts occured
goto EDGE
ENDIF

EDGE:

IF OPTION_REG.6=1 then ;interrupt on rising edge
LOW PORTA.1
else
OPTION_REG.6=0 ;interrupt on falling edge
LOW PORTA.2
endif

GOTO MAIN

thanx once again...