I am using 18F452
I tried to experiment with timer0... I set a prescale factor and was generating interrupts. For each interrupt, I was incrementing a variable whih was in the watch.
However when I set precale 1:2 and prescale 1:4, the simulator did not generate any interrupts...
I looked for all possible typo errors etc.. But It just does not make sense. If I just change one bit in the T0CON, >>TOPS0,TOPS1,TOPS2, The programme works...
Has anyone ever faced such a problem...
Please help me out...
MACRO.INC
Code:timer0.enable MACRO bsf T0CON,TMR0ON ;Sets bit7 of the T0CON register ENDM timer0.disable MACRO bcf T0CON,TMR0ON ;Clears bit7 of the T0CON register ENDM ;Macro to set timer0 to 8bit / 16bits. timer0.8Bits MACRO bsf T0CON,T08BIT ;Sets bit6 of the T0CON register ENDM timer0.16Bits MACRO bcf T0CON,T08BIT ;Clears bit6 of the T0CON register ENDM ;Macro to select the timer0 clock source timer0.counter MACRO bsf T0CON,T0CS ;Sets the bit5 of the T0CON register ENDM timer0.timer MACRO bcf T0CON,T0CS ;Clears the bit5 of the T0CON register ENDM ;Macro to select the timer0 Clock edge timer0.pTriggered MACRO bsf T0CON,T0SE ;Sets the bit4 of the T0CON register ENDM timer0.nTriggered MACRO bcf T0CON,T0SE ;Clears the bit4 of the T0CON register ENDM ;Macro to set the timer0 prescalers timer0.prescale1 MACRO bsf T0CON,PSA ;Sets the bit3 of the T0CON register ENDM timer0.prescale2 MACRO ;Prescale 1:2 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bsf T0CON,T0PS0 bsf T0CON,T0PS1 bcf T0CON,T0PS2 ENDM timer0.prescale4 MACRO ;Prescale 1:4 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bsf T0CON,T0PS0 bcf T0CON,T0PS1 bcf T0CON,T0PS2 ENDM timer0.prescale8 MACRO ;Prescale 1:8 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bcf T0CON,T0PS0 bsf T0CON,T0PS1 bcf T0CON,T0PS2 ENDM timer0.prescale16 MACRO ;Prescale 1:16 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bsf T0CON,T0PS0 bsf T0CON,T0PS1 bcf T0CON,T0PS2 ENDM timer0.prescale32 MACRO ;Prescale 1:32 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bcf T0CON,T0PS0 bcf T0CON,T0PS1 bsf T0CON,T0PS2 ENDM timer0.prescale64 MACRO ;Prescale 1:64 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bsf T0CON,T0PS0 bcf T0CON,T0PS1 bsf T0CON,T0PS2 ENDM timer0.prescale128 MACRO ;Prescale 1:128 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bcf T0CON,T0PS0 bsf T0CON,T0PS1 bsf T0CON,T0PS2 ENDM timer0.prescale256 MACRO ;Prescale 1:256 bcf T0CON,PSA ;Sets the bit3 of the T0CON register bsf T0CON,T0PS0 bsf T0CON,T0PS1 bsf T0CON,T0PS2 ENDM ;Macros to configure interrupts timer0.highPriority MACRO ;Set timer0 interrupt priority high bsf INTCON2,TMR0IP ENDM timer0.lowPriority MACRO ;Set timer0 interrupt priority low bcf INTCON2,TMR0IP ENDM timer0.interruptEnable MACRO bsf INTCON,TMR0IE ;Enables the timer0 interrupt. ENDM timer0.interruptDisable MACRO bcf INTCON,TMR0IE ;Disables the timer0 interrupt. ENDM timer0.clearInterruptFlag MACRO ;clears the timer0 interrupt flag bcf INTCON,TMR0IF ENDM ;Macro to check if interrupt has occured. timer0.ifInterrupt MACRO tmrservice ;Check Timer0 overflow btfsc INTCON,TMR0IF goto tmrservice ENDM
MAIN.ASM
SUBROUTINE.ASMCode:#include "p18f452.inc" #include "defs.inc" #include "macros.inc" ;Define Xtal Xtal EQU 8000000 PLL=FALSE ;Compute the clock time period IF PLL @clock_period SET 1/Xtal ELSE clock_period SET 4/Xtal ENDIF ;Declare Global Variables global terminate,test1,test2,test ;Get external labels extern delay, highPriorityInterrupt, lowPriorityInterrupt ;Allocate memory to variables mainData udata var1 res 1 tempData udata_ovr w_shadow res 1 test1 res 1 test2 res 1 test res 1 ;Interrupt Vectors resetV CODE 0X0000 ;Interrupt Vector goto main ;Goto mainroutine highInterV CODE 0x0008 ;High Priority Interrupt Vector goto highPriorityInterrupt lowInterV CODE 0x0018 ;Low Priority Interrupt Vector goto lowPriorityInterrupt ;Main routine mainCode CODE ;The main code. main: ;Initialize Interrupts interrupts.globalEnable interrupts.priorityEnable interrupts.peripheralEnable timer0.interruptEnable timer0.highPriority timer1.interruptEnable timer1.highPriority ;Initialize Timers timer0.timer timer0.8Bits timer0.prescale4 timer0.enable ;timer1.timer ;timer1.8Bits ;timer1.prescale8 ;timer1.disable movlw 0x01 movwf test1 movwf test2 movwf test terminate: bra terminate END
Code:#include "p18f452.inc" #include "macros.inc" ;Declare labels global global delay, highPriorityInterrupt, lowPriorityInterrupt ;get labels from other files extern terminate,test1,test2,test ;Define Variables subData udata delayCount res 1 pwmDuty res 1 timeTicks res 1 ;Subroutine Code. subroutine CODE ;Interrupt Service Routines highPriorityInterrupt: incf test timer0.ifInterrupt masterTimer ;timer1.ifInterrupt timer1int retfie lowPriorityInterrupt: retfie ;This is a subroutine to create a delay. delay: movwf delayCount delayLoop: decfsz delayCount bra delayLoop return 1 ;Subroutine to handle hardware timer interrupts. hwtimer: return 1 masterTimer: nop timer0.clearInterruptFlag incf test1 retfie timer1int: nop timer1.clearInterruptFlag incf test2 retfie END


Reply With Quote
