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.

INT0 on 18F4620 halting PIC

Status
Not open for further replies.

richacm

New Member
Hi,

My brains not working at 100% what with olympics and kids etc I'm a bit sleep deprived! Anyway I'm having trouble with my PIC and I'm hoping someone can simply say oh its this.

I'm using a PIC18F4620 and it was working fine until I decided to place some extra functionality on port B.0. This is a switch where I can press it and I can then go and do other stuff. The trouble is that the PIC is waiting on the PORTB.0 = 1 line for me to press the key... Here is the relavent code...(its in MikroBasic but should be easy to follow)

Code:
  OSCCON = %01110110        ' set to 8Mhz
  TRISA  = %00011111        ' PORTA is input on RA0 to RA4.  RA5 to RA7 as output
  TRISB = %00000011         ' PORTB is output on all ports except RB0 and RB1
  TRISC = $0                ' PORTC is output
  TRISD = $0                ' PORTD is output
  TRISE = $0                ' PORTE is output
  PORTE = 0                 ' Initialise Port E - this is the filling status and controls tap switch
  PORTD = 0                 ' Initialise Port D - this is the LED display
                            ' Port C is the Ethernet
                            ' Port B is the EEprom
  ADCON1 = %00001011        ' Set AN0 to AN4 as analogue, rest are digital
  ADCON2 = %00111011
  CMCON = 0x07              ' Turn off all comparitors

  'Set up the timer
  'Clock = 8Mhz
  'Time per instruction = 4/8Mhz = 0.5uSec
  'Add in prescalar = 0.5uSec * 16 = 8uSec
  'Add in postscalar = 8uSec * 10 = 80uSec = time per Timer2 register increment
  'Add in register counting of 250 = 80uSec * 250 = 20mSec
  'Therefore 1 second occurs when cnt variable = 50
  T2CON = %01001111         ' prescalar = 1:16 postscalar = 1:10
  PR2 = 249                 ' count up to 250
  SetBit(PIE1, TMR2IE)      ' enable Timer2 interrupt

... Other setup ...

 INTCON = %11000000        ' set GIE, PEIE interupt
' ==========================================================
'
' Main processing loop
'
' ==========================================================

  while TRUE
    if PORTB.0 = 1 then
       handleFunction   ' Go off and callibrate
    end if

   ... Continue on with other bits and peices
  wend

I have been doing checks before and after the PORTB.0 = 1 and it is ok before and does nothing afterwards. If I do press the switch then "handleFunction" gets called fine at any time...which leads me to the conclusion that the PIC processor is waiting on the line PORTB.0 = 1

Thanks heaps,

Craig
 
Hi Craig,
I'm not familiar with mikrobasic but it looks like you are not clearing the interrupt flag (im not sure if mikrobasic clears the flag for you). If you don't clear the flag your program will loop forever at the handleFunction routine.

Stig
 
I'm not familiar with MikroBasic either, but to get an interrupt-on-change on Portb.0, shouldn't you be saying something along the lines of...

IF INTCON.1 = 1 'INTF flag indicates an ext interrupt occured on PortB.0
(ie checking the INTF flag, rather than if Portb.0 has gone High?)

Now do what you want it to do when interrupted

INTCON.1 = 0 'Clear Interrupt flag
ENDIF
RETFIE or Context restore (return all registers to their original state before the interrupt occured)

Excuse me if I haven't explained this well..

Regards
Jim
 
Last edited:
He didn't say he was trying to use IOC... Look at the INTCON setting...

Yeah sorry I think I mislead a bit in the title...I was originally going to ask whether the INT0 was causing problems.

Another interesting point is that if I set TRISB to 0 (all output) then I can read PORTB.0 fine...and I can press the button and PORTB.0 goes to 1. It does what I want it to do but I am confused as this is supposed to be an input pin so TRISB.0 should be 1 not 0.
 
Craig,
I read it again properly (As Mike suggested) :) . I use Proton Basic, where
the line would read

'WHILE Portb.0 = 1: WEND 'Wait for a change on the port.

Assuming you have PortB pullups ON, then the program will sit there indefinitely until the pin goes low.

Regards
Jim
 
Last edited:
Craig,
I read it again properly (As Mike suggested) :) . I use Proton Basic, where
the line would read

'WHILE Portb.0 = 1: WEND 'Wait for a change on the port.

Assuming you have PortB pullups ON, then the program will sit there indefinitely until the pin goes low.

Regards
Jim

Hi Jim, I don't have PORTB pullups on. I do have an external pulldown resistor so that it is 0V unless the switch is pressed in which case it is 5V. I'm also only doing a check on the status of PORTB...i.e. if PORTB.0 = 1 then ... etc. I wouldn't have thought that this would have halted anything?
 
Craig..

Can you remove the While/Wend statement, and just say
IF PortB.0 = 1 THEN
handlefunction
ENDIF

It sounds to me like its waiting for a port change still

Jim
 
Last edited:
Jim, believe it or not all I had to do was initiallise PORTB to 0 after I set the TRISB to be 00000011. I'm not sure why that caused this issue but it certainly resolved all problems I had.

Thanks for your time.

Craig
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top