Mathew Cohen wrote: > Can someone tell me why the following code will not work. Is this the > correct way to initilise the timer and use the timer overflow > interrupt. > ORG H'50' I don't know if the programmer is smart enough to place code at $50 *after* code at $03, when placed first in the source, but the latter code is most likely to overlay the former anyway if it long enough. > Start > Call Initporta > Call Initportb > Call Initmr > GOTO Checkswitch > > ;--------------------------------------------------------------------- > ORG H'04' ; Page 11 document DS30430C > TmrInterupt > > BTFSC PORTB,7 > call Initmr > goto toggleleds Now let's see, when interrupted by the timer, you test your switch and perform a timer initialisation if PORTB.7 is high, and in either case jump to toggleleds? Toggleleds ends up in an infinite "copy" loop but I suppose if you have re-initialised the timer, it can interrupt again. The stack pointer will go insane, but in fact this doesn't matter. The simulator won't like it however. > > Initmr > CLRF TMR0 > BSF STATUS,RP0 ;select bank 1 > CLRF OPTION_REG > MOVLW B'00000111' > IORWF OPTION_REG,1 That's a long-winded way of setting up the option register, but.... > BCF INTCON,T0IF > BSF INTCON,T0IE You left bank 1 selected! > RETURN > > > Initporta > > BSF STATUS,RP0 > MOVLW 0 > TRIS PORTA You don't need to set up RP0 to use TRIS. That's the whole point! > BCF STATUS,RP0 > MOVLW 31 > MOVWF PORTA > > RETURN > > Initportb > > BSF STATUS,RP0 > MOVLW H'FF' > MOVWF TRISB > BCF STATUS,RP0 > RETURN > > Checkswitch Sets PORTA.4 (open collector) to inverse of PORTB.7 > BTFSS PORTB,7 > BSF PORTA,4 > BTFSC PORTB,7 > BCF PORTA,4 > GOTO Checkswitch > > > toggleleds This isn't going to work real well, as in Initmr, you left bank 1 selected! > BTFSS PORTA,1 > goto LedsOFF > BTFSC PORTA,1 > goto Ledson That's redundant - you get to Ledson either way! > Ledson > > MOVLW 15 > MOVWF PORTA > CALL Initmr > GOTO Checkswitch > > > LedsOFF > > MOVLW 0 > MOVWF PORTA > CALL Initmr > GOTO Checkswitch > > END The overall flow control is shall I say, most unconventional. The code initially drops through to Checkswitch which is dead-end code, but having initialised the timer to interrupt, this happens periodically, clearing the interrupt without restoring the stack (but that doesn't actually matter in the real silicon!) and toggles the LEDs before dropping back into CHeckswitch. The mis-setting of the bank select will prevent it from working, but otherwise it presumably will. Note that the combined maximum prescale and timer overflow will cause it when fixed to flash the LEDs at 1/65536 of the instruction clock, about 7 Hz with a 4 MHz crystal or 18 Hz with a 10 MHz crystal which may be difficult to visualise. If simulated, will give a continuous stream of stack errors but should otherwise work. I'd really like to know however, Mathew, whether you tried the code in my previous reply? -- Cheers, Paul B.