PDA

View Full Version : 18F2420, instant interrupts, and TMR0



dhickman
- 11th February 2009, 19:40
i've been working with Darryl's instant interrupts for PWM LED's and they work great! now i'm moving over to work directly with timers with a new-to-me PIC and have hit a wall.

i've reduced my code down to what i think are the bare essentials and i still don't have blinky. here's the code that compiles:

@ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H
@ __CONFIG _CONFIG3H, _MCLRE_ON_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _DEBUG_OFF_4L

define OSC 8
clear

CCP1CON=0 'turn off capture and compare module 1
CCP2CON=0 'turn off capture and compare module 2
SSPCON1.5=0 'disables SPI and I2C modules on portC
ADCON1=%00001111 'turns all AD pins to digital and tells module to use Vdd and Vss
ADCON0.0=0 'disables AD module
CMCON.0=0 'disables comparator module for portA
CMCON.1=0 '(ditto)
CMCON.2=0 '(ditto)
CVRCON.7=0 'disables comparator voltage reference module for portA

TRISB=%11101111 'PortB.4 => HB LED pin set to output

LED1 VAR PORTB.4
loop var byte

INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts

'lets me know the PIC is doing something after programming
for loop=1 to 4
toggle led1
pause 50
next loop

;-----------------------------------------------------------------------------
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
;-----------------------------------------------------------------------------
T0CON=%10011000 ; TMR0ON, 16-bit, internal clock, high>low, no prescaler
@ INT_ENABLE TMR0_INT ; enable Timer 0 interrupts
;-----------------------------------------------------------------------------
Main:
PAUSE 1
GOTO Main
'---[TMR1 - interrupt handler]--------------------------------------------------
ToggleLED1:
TOGGLE LED1
@ INT_RETURN


i've read read read the datasheet and have done many, many searches on this forum to try and find the small thing i'm missing. this is why i'm finally posting here for assistance.

Bruce
- 12th February 2009, 15:49
Change your last config line to;
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _DEBUG_OFF_4L & _XINST_OFF_4L

You'll also want to write to OSCCON since it does not default to 8MHz.

dhickman
- 13th February 2009, 08:36
Bruce,

thanks loads! your correction worked. i didn't know what that fuse did but now i guess i can see the results.

what about utilizing the x4 PLL feature with the internal osc? i've tried setting OSCTUNE.6=1 but i don't see a difference.

am i supposed to tell PBP that i'm now 32MHz with the DEFINE OSC 32 statement instead of DEFINE OSC 8?

thanks again!

-=drew

Bruce
- 13th February 2009, 14:35
_XINST_OFF_4L disables extended instruction set, which you'll always want disabled on any 18F part that has it when using PBP.

The unprogrammed default for this bit is ON unless you disable it as shown above.

Hint: Look in your default PBP header file for what they show there.

Yes. If the PIC is operating at 32MHz, then you'll want to define it just like you would for any osc speed.

Write the value 'shown in your datasheet' to OSCCON to set it for 8MHz. Then flip the bit in OSCTUNE to enable PLL. It should then be running at 32MHz.

dhickman
- 9th March 2009, 20:46
Bruce,

i tried setting the OSCCON value and the OSCTUNE bit as well but i'm not getting the higher speed. here's my test code:

'use the following when using the MPASM assembler
@ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H
@ __CONFIG _CONFIG3H, _MCLRE_ON_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _DEBUG_OFF_4L & _XINST_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_ON_5L & _CP1_ON_5L

DEFINE OSC 32
clear

OSCCON=%11111110 'SLEEP mode, 8MHz internal osc, no startup but use primary, primary has stablized, select internal osc block
OSCTUNE.6=1 'enables PLL 4x speed multipler for the internal OSC
ADCON1=%00001111 'turns all AD pins to digital and tells module to use Vdd and Vss
ADCON0.0=0 'disables AD module

TRISB=%00000000 'PortB.4 => Heartbeat LED pin set to output

HBLED var PORTB.4 'alias for heartbeat LED

;------------------------------------------------------------------------
Main:
toggle hbled
pause 500
GOTO Main
;------------------------------------------------------------------------


when i use DEFINE OSC 8 the LED flashes as expected. when use DEFINE OSC 32 it flashes slowly which leads me to think i've not successfully activated the x4 PLL.

-=drew

Bruce
- 9th March 2009, 21:31
Hi Drew,

Try changing OSCCON=%11111110 to OSCCON=%11111100. The internal osc is the primary
osc in this configuration since you have it set in your config option. I.E. the intrnal osc is
now considered the primary oscillator.

If you had your config osc setting set for an external osc, then you could switch between
the primary (external osc) & internal osc by setting SCS.1.

Also note that OSCCON bits 2 & 3 are read only, so attempting to write to these bits is not
needed, and, of course, doesn't really change a bit. These bits are read-only and pretty
much just for you to read to determin the oscillator status.

Only registers that show R/W-x are readable/writeable. The x just signifies the default
value of the register bit at POR.

Darrel Taylor
- 9th March 2009, 21:59
Try changing OSCCON=%11111110 to OSCCON=%11111100. The internal osc is the primary osc in this configuration since you have it set in your config option. I.E. the intrnal osc is now considered the primary oscillator.
WOW!

I'd searched, and re-searched and could not find a reason.

But looking at FIGURE 2-8 (Clock diagram)
I can totally see how that makes a difference.

Nice Bruce!

dhickman
- 11th March 2009, 18:33
reading the OSCCON configuration was indeed a bit (pardon the pun) confusing because i thought i was selecting the internal osc by setting osccon.1 and osccon.0 to 1x. thanks for straightening me out. i've tested it and it does indeed work.

thanks again everyone (especially Bruce)!

now i'm running 32Mhz off of an internal osc. that's awesome!

-=drew