Just after a bit of help with TMR0
I have hacked Darrell's SSPWM routine and am now adapting it to use TRM0 instead of TMR1.
Now I note you cant stop/start TMR0 I dont think that's a major issue in my application which is generating a 2khz pwm.
I'm confused with the reloading of the prescaler i'm going to use 1/4 with a preload of 6 and 8mhz int clock to generate an interrupt at 2000hz.
After the interrupt is dealt with and I reload TMR0 with my PWM duty + preload do I have to reassign the prescaler every time?
My hacked start SSPWM code is below.
Config
Start SSPWMCode:'OPTION_REG = %00000001 'Set Option Reg for TMR0 and prescaler 1/4
My hacked Int routine is belowCode:StartSPWM: 'Set DutyCycle before calling 'For TMR0 2khz pwm and 8mhz clock Ticks = xxx per cycle GIE = 1 PEIE = 1 TMR0_ON_TICKS = DutyCycle '(DutyCycle must be between 25 - 225 (10-90%) TMR0_OFF_TICKS = 255 - TMR0_ON_TICKS TMR0_ON_VAL = 255 - TMR0_ON_TICKS + 6 TMR0_OFF_VAL = 255 - TMR0_OFF_TICKS + 6 TMR0IE = 1 'Enable TMR0 interrupt enable bit (1 = enabled) TMR0IF = 0 'Clear TMR0 interrupt flag TMR0 = 255 'Load TMR0 with 255 First tick will cause an interrupt 1/4 Prescaler setup here ????? return
Code:asm INT_CODE if (CODE_SIZE <= 2) movwf wsave ; copy W to wsave register swapf STATUS,W ; swap status reg to be saved into W clrf STATUS ; change to bank 0 regardless of current bank movwf ssave ; save status reg to a bank 0 register movf PCLATH,w ; move PCLATH reg to be saved into W reg movwf psave ;6 ; save PCLATH reg to a bank 0 register endif btfss INTCON, TMR0IF ; is TMR0IF set? Timer0 Interrupt Flag GOTO NoTimerInt ; No. Bypass timer load btfss _SPWMstate ; Is Output High? GOTO TurnON ;9/15 ; No. TurnOFF bcf _CmdPwr ; Set CmdPwr Low bcf _SPWMstate ; MOVF _TMR0_OFF_VAL,W ; 1 ADDWF TMR0,F ; 1 ; reload timer with correct value GOTO TimerDone ;12/27 TurnON bsf _CmdPwr ; Set CmdPwr High bsf _SPWMstate ; MOVF _TMR0_ON_VAL,W ; 1 ADDWF TMR0,F ; 1 ; reload timer with correct value TimerDone 1/4 Prescaler setup here ????? bcf INTCON, TMR0IF ; 1/28 ; Clear Timer0 Interrupt Flag NoTimerInt Movf psave,w ; Restore the PCLATH reg Movwf PCLATH swapf ssave,w ; Restore the STATUS reg movwf STATUS swapf wsave,f swapf wsave,w ; 6/34 ; Restore W reg Retfie ; Exit the interrupt routine endasm





Bookmarks