<strong>PICBASIC</strong> <strong>PLUS</strong> CompilerAlso the STATUS, PCLATH, FSR, and Working register must be returnedto their original conditions (context restoring). Again, the CON-TEXT command may be used, but this time with RESTORE after it.i.e. CONTEXT RESTORE.The CONTEXT RESTORE command also returns the PIC back to themain body code where the interrupt was called from.Precautions.Note that the code between the context saving and restoring does notnecessarily need to be in assembler, a small subset of BASIC commandsSHOULD work just as well, but care must be used as to thecommands that are placed within the interrupt handler itself, as mostcommands are non re-entrant. I 2 CBUS, SERIAL, and LCD routinesetc, should be avoided, as they use system variables that may be alreadyin use in the main body code.Also, DELAY commands should be avoided at all costs, as theseplace the PIC into a timed loop, thus, altering any interrupt timing.IF-THEN, REPEAT-UNTIL, and WHILE-WEND comparisons shouldbe safe, along with register or variable altering commands.LOOKUP and LOOKDOWN are permitted, but not LOOKUPL orLOOKDOWNL.Because a hardware interrupt may occur at any time, I cannotfully guarantee that a SYSTEM variable will not be disturbedwhile inside the interrupt handler, therefore, the safest way touse an interrupt is to write the code in assembler. This willguarantee that no system variables are being altered. Alternatively,the assembler code may be viewed in order to ascertain whether anysystem variables are being used. These are: -PP0, PP0H, PP1, PP1H, PP2, PP2H, PP3, PP3H, PP4, PP4H, PP5,PP5H, PP6, PP6H,PP7, PP7H, GEN, GENH, GEN2, GEN2H, GEN3,GEN3H, GPR, BPF.The code within the interrupt handler should be as quick and efficientas possible because, while it’s processing the code the main programis halted.When using assembler interrupts, care should be taken to ensure thatthe watchdog timer does not time-out. Placing a CLRWDT instructionat regular intervals within the code will prevent this from happening.An alternative approach would be to disable the watchdog timer altogetherat programming time.82Copyright Crownhill 2001
<strong>PICBASIC</strong> <strong>PLUS</strong> CompilerThe following example illustrates the use of a TMR0 interrupt as acrude clock: -' LCD clock program using ON_INTERRUPT' Uses TMR0 and prescaler.' Nap and Sleep should not be used.' Buttons may be used to set Hours and MinutesREMARKS onDEVICE 16F628ON_INTERRUPT GOTO ClockintDIM Hour as BYTE ' Hour variableDIM Dhour as BYTE ' Display Hour variableDIM Minute as BYTE ' Minute variableDIM Second as BYTE ' Second variableDIM Ticks as BYTE ' Pieces of Seconds variableDim Update as BYTE ' Variable to indicate Update of LCDCMCON = 7' PORTA to digital (for 16F62X devices)CLS' Clear the LCDHour=0:Minute=0:Second=0:Ticks=0 ' Set initial time to 00:00:00Update = 1' Force first display' Set TMR0 to interrupt every 16.384 milliseconds (ms)OPTION_REG = %01010101 ' Set TMR0 configurationINTCON = %10100000 ' Enable TMR0 interrupts' ** Main program loop Updates the LCD with the time **Main: PORTA = 0 ' PORTA lines low to read buttonsTRISA = %00001111' Enable all button pins as inputs' Check any button pressed to set timeIF PORTA.3 = 0 THEN Dec_MinIF PORTA.2 = 0 THEN Inc_Min ' Last 2 buttons set MinuteIF PORTA.1 = 0 THEN Dec_HourIF PORTA.0 = 0 THEN Inc_Hour ' First 2 buttons set HourC_Upd: IF Update = 1 THEN' Check for time to Update screen' Display time as hh:mm:ssDhour = Hour ' Change Hour 0 to 12IF (Hour // 12) = 0 THEN Dhour = Dhour + 1283Copyright Crownhill 2001