Should I still enable the INTCON1 register? I have it set to %1001000 to use the RB0 interrupt. I have the HWPM (which is to be the interrupt source) set as follows:
HPWM 1,64,3600 ' Supposed to send HPWM 3.6KHZ at 25% duty
Should I still enable the INTCON1 register? I have it set to %1001000 to use the RB0 interrupt. I have the HWPM (which is to be the interrupt source) set as follows:
HPWM 1,64,3600 ' Supposed to send HPWM 3.6KHZ at 25% duty
I tried compiling that and I get
Symbol not previously defined (INTCON1)
The updated code is as follows:
' Connect analog input to (RA0)
' Connect clocks to PORTB
' PORTB.0 is the Vphase1 clock
' PORTB.1 is the Vphase2 clock
' PORTB.2 is the Hphase1 clock
' PORTB.3 is the Hphase2 clock
' The reset will be connected to a HWPM clock pin #17
' Connect pin 2 from the DB9 connector to PORTC.6
' Have a +5V source ready to touch PORTA.2 to trigger clocking process
include "modedefs.bas"
' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
DEFINE OSC 20 ' Sets clock speed to 20Mhz
Define ADC_CLOCK 4 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
HPWM 1,64,3600 ' Supposed to send HPWM 3.6KHZ at 25% duty
INTCON1 = %1001000 ' Enable RB0 Interrupt
INTCON.7 = 0 ' Disables global interrupts
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00001101 ' Set PORTA analog, except for pin 2
TRISB = %00000000 ' Set PORTB to all output
Pause 500 ' Wait .5 second
Pause 500 ' Wait .5 second
horizpulse var byte
vertpulse var byte
adval var byte ' Create adval to store result
horizpulse = 1 ' Initialize counters, initial states
vertpulse = 1
PORTB.0 = 0
PORTB.1 = 0
PORTB.2 = 1
PORTB.3 = 0
buttonloop:
IF PORTA.2 = 1 then
GOTO vertloop
else
GOTO buttonloop ' Waits for 5V that signals shutter is closed
ENDIF
vertloop:
horizpulse = 1
PORTB.0 = 1
PAUSEUS 139
PORTB.0 = 0
PORTB.1 = 1
PAUSEUS 139
PORTB.0 = 1
PORTB.1 = 0
PAUSEUS 139
PORTB.0 = 0
PAUSEUS 270
vertpulse = vertpulse + 1
IF vertpulse < 512 THEN
GOTO horizloop
ELSE
GOTO buttonloop
ENDIF
horizloop:
INTCON.1 = 0 ' Resets interrupt flag to 0
Resetcheck: ' Loops back to make sure
PAUSEUS 2 ' the PORTB.2 goes low and
IF INTCON.1 = 0 THEN ' PORTB.3 goes high as close
GOTO Resetcheck ' to the external trigger's
ELSE ' rising edge as possible
ENDIF
PORTB.2 = 0
PORTB.3 = 1
PAUSEUS 139
PORTB.3 = 0
PORTB.2 = 1
PAUSEUS 15
ADCIN 0, adval ' A/D 8 bit value from PORTA.0 into adval
SEROUT2 PORTC.6,16416,[BIN8 adval,13,10] ' Output 8 bit word serially to PC from C.6
PAUSEUS 87
horizpulse = horizpulse + 1
IF horizpulse < 784 THEN
GOTO horizloop
ELSE
PAUSEUS 270
GOTO vertloop
ENDIF
END
There is no INTCON1 register. That would be why you get the error. Look at your datasheet.
To monitor interrupt "flag bits" you do not need to enable any interrupt enable bits or global interrupts.
Any event or condition that will trigger the interrupt, will set the interrupts flag bit. This happens regardless of whether you have "any" interrupt enabled or disabled.
A "Flag bit" indicates that an interrupt event occurred.
The interrupt "Enable bit" allows program execution to branch to the interrupt vector address (if global interrupts are enabled) when the flag bit is set by the event that caused the interrupt.
You can setup the RB0/INT0 on the 18F4520 to set the interrupt flag bit on a low-to-high or high-to-low transition on RB0/INT0. See the datasheet under INTCON2.
Then all you need to do is monitor INTCON.1 (this means INTCON register bit #1 NOT INTCON1) which is the "flag bit" inidicating the transition (event) on RB0/INT0 has occured.
After reading this bit, then reset it (clear it to 0) so you can check it again later to see if the event has happend again.
OK, I checked the manual and bit 6 controls the external 0 flag. I set it to '1' for the rising edge.
INTCON2 = %0100000 ' External Interrupt Flag on Rising Edge
Do I have to set the PORTB pull up enable bit?
Also, RB0 will be read as an input (wired from the HWPM pin 17), so I will have to reconfigure my TRISB register to accept an input on PORTB, right?
thanks again for the help.
Yes. You definitely need to make RB0/INT0 an input first.
Any time you make a pin an input, then you'll want to hold that pin at logic 1 or 0 through a pull-up or pull-down resistor or some "constant" external logic.
With the hardware PWM output pin connected to the RB0/INT0 input, you could possibly get by without the pull-up or pull-down, but it's cheap insurance that could save you a lot of head-scratching down the road.
A floating input will return random logic values when read, which will cause major problems. Especially with the pin configured as an interrupt source triggered by logic transitions.
If there's any chance that the external pin or device providing the interrupt logic input could be disconnected or go to a high-impedance state, then the pull-up or pull-down resistor is essential.
I always use one. If the interrupt logic is configured to trigger on a high-to-low transition, then I use a pull-up, and let the external signal provide the low-going trigger pulse. If it's setup for low-to-high transitions, then I use a pull-down resistor.
What is a good value to use for the pull-down resistor? And if I understand your logic, I shouldn't need the pull-up enable bit (bit 7) on either. So all I should have to do is leave INTCON2 as is and change TRISB to %00000001, right?
I normally use 10K or > for pull-up/pull-downs, but pretty much any high value will work.
For INTCON2, you only need to worry about bit 6 INTEDG0: External Interrupt 0 Edge Select bit, and yes, you want to set the pin to an input.
Last edited by Bruce; - 26th February 2005 at 20:23.
Bookmarks