PDA

View Full Version : I am stumped!!!



tazntex
- 26th July 2010, 13:20
I am using a PIC 16F628A processor, and am trying to learn how to use DT's Interrupts but here is my problem:
If TRISB = %11111110 and PORTB %00000001 that should mean that bit 0 of PORTB is an ouput all others are input and with PORTB = %00000001 that means bit 1 is equal to one meaning high 5v and all others low, CORRECT???? Well, after check with my meter PORTB 0-7 are high or shall I say 5v, running the code below, I am experimenting with Interrupts on PORTB 4-7 wanting to detect when bits 4-7 go low and with keypad connected to PORTB 1-7, Bit 0 is an output set high going through forward through diode anode to pins 4-7 when button is pressed that particular bit is pulled low....
DEFINE OSC 4
CMCON = 7
VRCON = 0
pauseus 10
TRISA = %00000110
TRISB = %11111110
PORTA = %00000000
PORTB = %00000001
OPTION_REG.7 = 0 'enable pullups
OPTION_REG.6 = 0 'trigger on falling edge
led var portA.3
INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RBC_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE RBC_INT ; enable external (INT) interrupts

Main:
PAUSE 1
GOTO Main

'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
TOGGLE LED
PAUSE 50
@ INT_RETURN

My explanation my seem confusing but it is early and I haven't had my COFFEE!!!!

Thanks

aratti
- 26th July 2010, 13:32
If TRISB = %11111110 and PORTB %00000001 that should mean that bit 0 of PORTB is an ouput all others are input and with PORTB = %00000001 that means bit 1 is equal to one meaning high 5v and all others low, CORRECT???? Well, after check with my meter PORTB 0-7 are high or shall I say 5v,


This is correct! You set portB.0 to be an output all remaining to be inputs, but you also have activated the internal pullup so, all inputs are high waiting your command (short to ground) while your instruction "PORTB %00000001" will have effect only on portB.0.

Al.

tazntex
- 26th July 2010, 13:39
Thank you for clarifying that, I've spent all of Saturday trying to figure this out. Have you time for another question? If so, with the above setup why upon startup does my led blink?

aratti
- 26th July 2010, 13:49
with the above setup why upon startup does my led blink?

Cannot answer without the schematic. Post how you wired your system, and I will try to give you an answer.

Al.

tazntex
- 26th July 2010, 14:06
I have a keypad, 3 rows, 4 columns, my rows are connected to PortB bits 1-3, the columns are connected to PortB bits 4-7. I set PortB.0 to 1 (high) feeding four 1n4148 diodes anode to PortB.0 cathodes to bit 4-7. When a button is pressed on the keypad it pulls whichever pin 4-7 low. I have OPTION_REG.6 = 0 to trigger on the falling edge.


Thanks

tazntex
- 26th July 2010, 14:35
keypad ohms out good, brand new actually, but as I mentioned before PortB.1-3 are my rows and 4-7 are my columns with the above code, pressing a button on the keypad does not pull any pin low.

aratti
- 26th July 2010, 14:36
I don't know how you want to connect your 3x4 keypad but the way you did explain is not going to work.

See the attached schematic. You have to put portB.1 to portB.3 (one at the time) to be an output and set it to low.
If you push one key of your keypad then the interrupt will detect the change (portB.4/7 going low) and will jump in the interrupt subroutine where you will decode the key, knowing which portB.1/3 was active low and which portB.4/7 was activated.

Al.

tazntex
- 26th July 2010, 14:46
I changed TRISB = %11111110 to TRISB = %11110000 now pressing keypad the selected PORTB 4-7 pulls low to 2.2VDC, by the way PORTB.0 is connect first in series with a 330 ohm resistor, then to the four 1N4148's. I thought limiting the current to 15mA would be easier on the internal pull-ups.

tazntex
- 26th July 2010, 14:57
Thanks for the drawing, that is how this is connected, yes I am aware of "scanning the keypads" Bruce has been a great help, but I am just narrowing it down to pressing on button, pins 1-3 low, and pin 0 high just to see how interrupt's work. I am looking to use the interrupt to sleep all the time, detect when key is pressed, wake up, set PORTB.0, and 4-7 as inputs, bits 1-3 as out, pause about 50, then began scanning keypad something like this:

recycle:
@ Sleep
@ NOP
@ NOP
goto recycle

' my Interrupt handler
search:
PORTB = 0
TRISB = $f1
PAUSEUS 10
keypadscan:
FOR row = 1 TO 3
PORTB = 0
TRISB = (DCD row) ^ $fe
col = PORTB >> 4
IF col != $f THEN keypressed
NEXT row
GOTO recycle

Haven't got all this worked out yet, but thats another day

tazntex
- 26th July 2010, 15:06
I'm just stumped with the original code above why am I already in the interrupt when I power up, just to confirm that I changed the INT handler to:
'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
TOGGLE LED
pause 50
@ INT_RETURN

and sure enough the led is flashing...

By the way, what program are you using to upload your schematic?

tazntex
- 28th July 2010, 20:06
Hmmm, came back here today to focus on the post here from Bruce but it seems to have vanished, or am I losing my mind? Maybe its both :)

Anyhow, it was mentioned that so to speak that I should take a snapshot of PortB, I am so lost. If on power up PortB.0 is high and pullups are enabled, on the rest of the pins: TRISB = %11111110 PortB = %00000001 then with my meter focusing on pins 4-7 shows 5VDC or logic HIGH, should I do this:

WHILE (portb >> 4) != 15
pause 20
wend
INTCON.0 = 0
return

or substitute 15 for a 0?

Seems it would be 15 but with that I apparently staying in my interrupt routine.

I've been experimenting with ON INTERRUPT and Darryl Taylor's Instant Interrupts:

DEFINE OSC 4
CMCON=%00000111
VRCON = 0
PAUSEus 10
TRISA = %00000110
TRISB = %11111110
PORTA = %00000000
PORTB = %00000001
OPTION_REG.7 = 0
OPTION_REG.6 = 0
INTCON = %00001000

led var portA.3
INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RBC_INT, _check, PBP, yes
endm
INT_CREATE

ENDASM
@ INT_ENABLE RBC_INT
PAUSE 50

cycle:
gosub readportb
HIGH led
pause 100
LOW led
pause 100
@ Sleep
@ Nop
@ Nop
goto cycle

readportb:
WHILE (portb >> 4) != 15
PAUSE 20
WEND
INTCON.0 = 0
return


check:
PAUSE 250
WHILE (portb >> 4) != 15
PAUSE 20
WEND
@ INT_RETURN


Any Suggestion, preferably in Crayola so its clear?

Thanks

Bruce
- 28th July 2010, 20:50
Try this;


DEFINE OSC 4
CMCON=%00000111
VRCON = 0
PAUSEus 10
TRISA = %00000110
TRISB = %11110000 ' using upper 4-bits for interrupt
PORTA = %00000000
PORTB = %00000001
OPTION_REG.7 = 0
OPTION_REG.6 = 0

led var portA.3

INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RBC_INT, _check, PBP, yes
endm
INT_CREATE
ENDASM

@ INT_ENABLE RBC_INT

cycle:
HIGH led
pause 100
LOW led
pause 100
@ Sleep
goto cycle

check:
WHILE (portb >> 4) != 15
PAUSE 10
WEND
@ INT_RETURN
The purpose of WHILE (portb >> 4) != 15 is to wait until a button that generated the interrupt is released. It also reads the port, which is required before DT_INTs can clear the interrupt flag.

To clear the interrupt-on-change flag bit you first have to read the port, then clear the flag. If you read the port while a button is still pressed, then clear the flag, another interrupt will be generated once the button is released.

tazntex
- 28th July 2010, 21:22
Thanks a million Bruce, I will try that now.

P.S. I about fell on the floor laughing when I refreshed the page and say the crayon:)

Bruce
- 28th July 2010, 21:31
It took me a while to find one the color of my truck..;o)

Experiment with this one too. You can have it wake from sleep without an interrupt handler.


DEFINE OSC 4
CMCON=%00000111
VRCON = 0
PAUSEus 10
TRISA = %00000110
TRISB = %11110000 ' using upper 4-bits for interrupt
PORTA = %00000000
PORTB = %00000001
OPTION_REG.7 = 0
OPTION_REG.6 = 0
led var portA.3

INTCON = %00001000 ' int-on-change enabled. RBIE=1, RBIF=0

cycle:
HIGH led
pause 100
LOW led
pause 100
@ Sleep ' sleep until button press
WHILE (portB >> 4) !=15 ' wait for button release
PAUSE 10 ' and read port so RBIF can
WEND ' be cleared below.
INTCON.0=0 ' now clear RBIF int flag
goto cycle

END
Global interrupts are disabled. Just enabled the int-on-change by setting RBIE, clearing RBIF, and it sleeps until a button is pressed.

DT_INTs is more fun, but you can wake one up without an interrupt handler.

tazntex
- 28th July 2010, 22:06
I compiled the code as shown, when powered up the led continues to blink but when I press a button on the keypad which would cause (PORTB >> 4) != 15 it still blinks.....


I have even tried it with the following changes with same response:
cycle:
gosub checkstate
HIGH led
pause 100
LOW led
pause 100
@ Sleep
goto cycle

checkstate:
while (PORTB >> 4) != 15
pause 20
wend
return

its almost like it never see an interrupt to get out of cycle. I also confirmed on PortB by checking with my meter on switch closure. With a switch closed and the bit I checked was PortB.4 I measured it at 2.192Vrms, switch open 4.99Vrms.

Thanks for hanging around

tazntex
- 28th July 2010, 22:15
Bruce, just wanted to mention we must have posted about the same time the code that I just tried was the crayon version, I am about to try your latest.

Again, Thank you.

Bruce
- 28th July 2010, 22:31
Make sure you have the watchdog timer disabled when using the assembler @ SLEEP command.

tazntex
- 28th July 2010, 22:42
Ok, I didn't know about that...Thank you.

Bruce
- 28th July 2010, 23:13
Sorry. My fault for not mentioning this. The @ SLEEP instruction puts it to sleep forever until a watchdog timeout, or an interrupt wakes it up.

When it wakes up due to watchdog timer timeout, it executes the instruction immediately after the @ SLEEP, and lands on your goto cycle.

Any time you use the assembler version @ SLEEP you'll need to make sure you have WDT disabled unless you want it to wake up with the watchdog timer timeout.

Bruce
- 29th July 2010, 20:37
Taz,

Try this version with the nop just after @ SLEEP and see if this helps.


DEFINE OSC 4

CMCON=%00000111
VRCON = 0
PAUSEus 10
TRISA = %00000110
TRISB = %11110000 ' using upper 4-bits for interrupt
PORTA = %00000000
PORTB = %00000001

OPTION_REG.7 = 0
OPTION_REG.6 = 0

led var portA.3

INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RBC_INT, _check, PBP, yes
endm
INT_CREATE
ENDASM

@ INT_ENABLE RBC_INT

cycle:
HIGH led
pause 100
LOW led
pause 100
@ Sleep
@ NOP
goto cycle

check:
WHILE (portb >> 4) != 15
PAUSE 10
WEND
@ INT_RETURN