|
|
View previous topic :: View next topic |
Author |
Message |
matthewsachs
Joined: 19 Jun 2016 Posts: 22
|
[SOLVED] TMR2 Interrupt Not Working on PIC18F27K40 |
Posted: Sat Mar 17, 2018 10:43 pm |
|
|
I am unable to get this simple code to work
Code: | #include <18F27K40.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES PUT //Power Up Timer
#FUSES BORV28 //Brownout reset at 2.8V
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(internal=48000000)
#define LED_OUT PIN_C2
//********************
// FUNCTION PROTOTYPES
//********************
void micro_init();
#int_TIMER2
void TIMER2_isr(void)
{
output_high(LED_OUT);
}
void main()
{
micro_init();
while(TRUE)
{
delay_cycles(1);
}
}
void micro_init()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_wdt(WDT_OFF);
setup_timer_2(T2_DIV_BY_1,255,16);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
clear_interrupt(INT_TIMER2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
} |
I can get it to work with Timer 3, but not Timer 2. Timer 3 doesn't have enough resolution for what I am trying to do (I need an interrupt period in microseconds) otherwise I would just use Timer 3, but it would be great to understand why I can't get Timer 2 to work.
If anyone has any suggestions please let me know.
The MCU is running off 5V derived from an LDO supplied by a quality bench power supply, it has a proper RC combo on MCLR pin, and I am using the latest compiler version v5.076
Here is the ASM Listing
Code: |
ROM used: 346 bytes (0%)
Largest free fragment is 65536
RAM used: 27 (1%) at main() level
27 (1%) worst case
Stack used: 1 locations (0 in main + 1 for interrupts)
Stack size: 31
*
00000: GOTO 0122
*
00008: MOVWF 04
0000A: MOVFF FD8,05
0000E: MOVFF FE0,06
00012: MOVLB 0
00014: MOVFF FE9,0C
00018: MOVFF FEA,07
0001C: MOVFF FE1,08
00020: MOVFF FE2,09
00024: MOVFF FD9,0A
00028: MOVFF FDA,0B
0002C: MOVFF FF3,12
00030: MOVFF FF4,13
00034: MOVFF FFA,14
00038: MOVFF FF5,15
0003C: MOVFF FF6,16
00040: MOVFF FF7,17
00044: MOVFF FF8,18
00048: MOVFF FFB,19
0004C: MOVFF 00,0E
00050: MOVFF 01,0F
00054: MOVFF 02,10
00058: MOVFF 03,11
0005C: MOVLB E
0005E: BTFSS xC6.1
00060: GOTO 006A
00064: BTFSC xCE.1
00066: GOTO 00C0
0006A: MOVFF 0E,00
0006E: MOVFF 0F,01
00072: MOVFF 10,02
00076: MOVFF 11,03
0007A: MOVFF 0C,FE9
0007E: MOVFF 07,FEA
00082: BSF 07.7
00084: MOVFF 08,FE1
00088: MOVFF 09,FE2
0008C: MOVFF 0A,FD9
00090: MOVFF 0B,FDA
00094: MOVFF 12,FF3
00098: MOVFF 13,FF4
0009C: MOVFF 14,FFA
000A0: MOVFF 15,FF5
000A4: MOVFF 16,FF6
000A8: MOVFF 17,FF7
000AC: MOVFF 18,FF8
000B0: MOVFF 19,FFB
000B4: MOVF 04,W
000B6: MOVFF 06,FE0
000BA: MOVFF 05,FD8
000BE: RETFIE 0
.................... #include <18F27K40.h>
.................... //////////// Standard Header file for the PIC18F27K40 device ////////////////
.................... ///////////////////////////////////////////////////////////////////////////
.................... //// (C) Copyright 1996, 2014 Custom Computer Services ////
.................... //// This source code may only be used by licensed users of the CCS C ////
.................... //// compiler. This source code may only be distributed to other ////
.................... //// licensed users of the CCS C compiler. No other use, reproduction ////
.................... //// or distribution is permitted without written permission. ////
.................... //// Derivative programs created using this software in object code ////
.................... //// form are not restricted in any way. ////
.................... ///////////////////////////////////////////////////////////////////////////
.................... #device PIC18F27K40
....................
.................... #list
....................
.................... #device adc=10
....................
.................... #FUSES NOWDT //No Watch Dog Timer
.................... #FUSES PUT //Power Up Timer
.................... #FUSES BORV28 //Brownout reset at 2.8V
.................... #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
....................
.................... #use delay(internal=48000000)
....................
.................... #define LED_OUT PIN_C2
....................
.................... //********************
.................... // FUNCTION PROTOTYPES
.................... //********************
.................... void micro_init();
....................
.................... #int_TIMER2 //4ms tick
.................... void TIMER2_isr(void)
000C0: MOVLB 0
.................... {
.................... output_high(LED_OUT);
000C2: BCF F8A.2
000C4: BSF F85.2
000C6: MOVLB E
000C8: BCF xCE.1
000CA: MOVLB 0
000CC: GOTO 006A
.................... }
....................
....................
.................... void main()
*
00122: BSF F81.7
00124: BCF F81.6
00126: CLRF FF8
00128: BCF FF2.5
0012A: BSF 07.7
0012C: MOVLB E
0012E: CLRF xDD
00130: CLRF xDA
00132: CLRF xDC
00134: MOVLW 07
00136: MOVWF xDE
00138: MOVLW 60
0013A: MOVWF xD8
0013C: MOVLB F
0013E: CLRF x11
00140: CLRF x19
00142: CLRF x21
00144: CLRF x3A
00146: CLRF x3B
00148: CLRF x3C
0014A: CLRF x39
0014C: CLRF x36
0014E: CLRF x37
00150: CLRF x38
00152: CLRF x35
.................... {
.................... micro_init();
00154: MOVLB 0
00156: BRA 00D0
....................
.................... while(TRUE)
.................... {
.................... delay_cycles(1);
00158: NOP
0015A: BRA 0158
.................... }
.................... }
....................
0015C: SLEEP
.................... void micro_init()
.................... {
.................... setup_adc_ports(NO_ANALOGS|VSS_VDD);
*
000D0: MOVLW 00
000D2: MOVLB F
000D4: MOVWF x58
000D6: MOVWF x11
000D8: MOVWF x19
000DA: MOVWF x21
.................... setup_adc(ADC_OFF|ADC_TAD_MUL_0);
000DC: MOVLW 08
000DE: MOVWF x5A
000E0: CLRF x5B
000E2: CLRF x57
000E4: CLRF x5C
000E6: CLRF F66
000E8: MOVLW 84
000EA: MOVWF F60
.................... setup_wdt(WDT_OFF);
000EC: MOVLB E
000EE: BCF xD2.0
....................
.................... setup_timer_2(T2_DIV_BY_1,255,16);
000F0: CLRF FC0
000F2: CLRF FBE
000F4: CLRF FBF
000F6: SETF FBC
000F8: MOVLW 8F
000FA: MOVWF FBD
....................
.................... setup_comparator(NC_NC_NC_NC);
000FC: MOVLB F
000FE: CLRF x3A
00100: MOVLW 07
00102: MOVWF x3B
00104: MOVWF x3C
00106: CLRF x39
00108: CLRF x36
0010A: MOVWF x37
0010C: MOVWF x38
0010E: CLRF x35
.................... setup_vref(FALSE);
00110: CLRF x31
....................
.................... clear_interrupt(INT_TIMER2);
00112: MOVLB E
00114: BCF xCE.1
.................... enable_interrupts(INT_TIMER2);
00116: BSF xC6.1
.................... enable_interrupts(GLOBAL);
00118: MOVLW C0
0011A: IORWF FF2,F
0011C: MOVLB 0
0011E: GOTO 0158 (RETURN)
.................... }
Configuration Fuses:
Word 1: FFFC NOEXTOSC RSTOSC_EXT NOCLKOUT CKS FCMEN
Word 2: FCFD MCLR PUT NOLPBOR BROWNOUT BORV28 ZCDDIS PPS1WAY STVREN NODEBUG NOXINST
Word 3: FF9F WDTSW NOWDT WDTWIN_SW WDTCLK_SW
Word 4: DFFF NOWRT NOWRTC NOWRTB NOWRTD SCANE NOLVP
Word 5: FFFF NOPROTECT NOCPD
Word 6: FFFF NOEBTR NOEBTRB |
Last edited by matthewsachs on Sun Mar 18, 2018 7:08 am; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 18, 2018 12:04 am |
|
|
Quote: | void micro_init()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_wdt(WDT_OFF);
setup_timer_2(T2_DIV_BY_1,255,16);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
clear_interrupt(INT_TIMER2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
} |
You didn't specify the Timer2 clock source.
The default Timer2 clock for this PIC is an external clock supplied by the
user on a pin. I assume you don't want that. Try it with this line instead:
Code: | setup_timer_2(T2_CLK_INTERNAL | T2_DIV_BY_1, 255, 16); |
|
|
|
matthewsachs
Joined: 19 Jun 2016 Posts: 22
|
|
Posted: Sun Mar 18, 2018 7:13 am |
|
|
Thank you VERY MUCH PCM.
That new line of code worked.
Just out curiosity, why does Timer 2 default to use an external clock. Is this a CCS "feature" or something specific to this device - I can't find anything to that effect in the timesheet (maybe I am not looking in the right section):
Quote: |
14.1 Timer2 Operation
In normal operation, TMR2 is incremented from 00h on
each clock (FOSC/4). A 4-bit counter/prescaler on the
clock input gives direct input, divide-by-4 and
divide-by-16 prescale options; these are selected by
the prescaler control bits, T2CKPS<1:0> of the T2CON
register. The value of TMR2 is compared to that of the
period register, PR2, on each clock cycle. When the
two values match, the comparator generates a match
signal as the timer output. This signal also resets the
value of TMR2 to 00h on the next cycle and drives the
output counter/postscaler (see Section 14.2 “Timer2
Interrupt”).
The TMR2 and PR2 registers are both directly readable
and writable. The TMR2 register is cleared on any
device Reset, whereas the PR2 register initializes to
FFh. Both the prescaler and postscaler counters are
cleared on the following events:
• a write to the TMR2 register
• a write to the T2CON register
• any device Reset (Power-on Reset, MCLR Reset,
Watchdog Timer Reset or Brown-out Reset)
TMR2 is not cleared when T2CON is written. |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sun Mar 18, 2018 8:07 am |
|
|
Almost nothing is ever directly 'set' by CCS. On fuses for example, the 'default', is what the fuses erase to (in the past CCS did set some stuff but they have gradually shifted to not doing things unless settings are vital).
If you look at figure 20-1, you can see that T2, 4 & 6, have a huge number of possible clock sources. Look then at table 20-1, and '0000' in the clock setup, selects 'Pin selected by T2INPPS'.
If you then look at the register doing this:
The 'Timer Clock Source Selection Register,' you see it says:
Reset States: POR/BOR = 0000.
So the hardware default on reset for this chip is to use the external pin.
If you look at the same timer in an older/more basic PIC18, you will find that on most there is no input selection, and the timer is 'hard wired' to use Fosc/4. It is not specific to 'this' device, but is specific to later devices that offer multiple clock sources for Timer2.
I've just edited this. You are looking at the data sheet for the 27K40?.
Section 14 in this sheet is on interrupts, not the timer. I suspect you have the wrong or an obsolete sheet.
Timer2 is section 20 in the correct sheet..... |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|