Sorry for the poor explanation but I’m a little confused myself.
From the proton geeks site I’ve modified the code snippet “How to build a simple and stupid small timer routine” by Olivier de Broqueville to achieve basically what I’m after.
But I don’t really understand how it works as I managed to get approximately the required time by setting “mycounter_limit= 10” (trail and error).Code:device = 16F876a 'PIC device CMCON = 7 'disable comparators ADCON1 = 7 'disable ADCs TRISB = %11111111 'Portb as inputs TRISA = %00000000 'Porta as outputs porta = %00000000 'Set all outputs low ON_INTERRUPT Horloge DECLARE XTAL=4 SYMBOL GIE = INTCON.7 SYMBOL T0IF = INTCON.2 SYMBOL T0IE = INTCON.5 SYMBOL PS0 = OPTION_REG.0 SYMBOL PS1 = OPTION_REG.1 SYMBOL PS2 = OPTION_REG.2 SYMBOL PSA = OPTION_REG.3 SYMBOL T0SE= OPTION_REG.4 SYMBOL T0CS= OPTION_REG.5 DIM mycounter as WORD ' This is the TIME variable used to count the interrupts DIM mycounter_limit as WORD ' This is the limit to reach. Could have been a constant in the program ' to win some place (you would win 6 words) DIM SECONDS as BYTE GIE=0 GOTO INITIALISATION ' As always: place interrupt routine first and jump over... ' ----------------------------------------------------------------------------- ' INTERRUPT ROUTINE ' ----------------------------------------------------------------------------- Horloge: INC mycounter ' Increment the time variable at every interrupt T0IF=0 ' Clear the interrupt flag CONTEXT RESTORE Initialisation: T0SE =0 ' TIMER0 triggered by ascending edge T0CS =0 ' TIMER0 using internal clock PSA =0 ' Prescaler assigned to TIMER0 PS0 =1 ' Prescaler set at 1:256 PS1 =1 PS2 =1 mycounter_limit= 10 mycounter=0 SECONDS=0 TMR0=0 ' Reset TIMER0 T0IE=1 ' Autorisation of TIMER0 interrupt GIE=1 ' Autorisation of all interrupts MAIN: WHILE 1=1 ' Do this forever IF mycounter > mycounter_limit Then ' Are we above the TIME factor? ' As the test is ABOVE the clock runs a bit ' slow, which was the purpose mycounter=0 ' Reset the 'interrupt' counter SECONDS=SECONDS+1 ' Increment the seconds IF SECONDS > 59 THEN ' If 59 sec, SECONDS=0 ' and RESET seconds ENDIF ENDIF if SECONDS > 5 then porta =%00000001 else porta =%00000100 'Stop for 5 sec and 'check PIR end if WEND ' And do it forever END
He gives an explanation here : linky
but as I’m using 4mHz OSC the values are different. Can anyone help explain whats happening in the code as I never like to use something with out fully understanding it.
Thanks
Jim





Bookmarks