Eduardo, read the datasheet... here it is clearly explained... Eduardo Garcia wrote: > Hello Harold! > > Thanks a lot your help! > I did the suggested and the code works fine. > > But, I still don't know why Program Counter wasn't with the correct value= . I = > thought that GOTO instruction copy all 20 bits to PC, but seems to copy o= nly = > the PCL contents. > > I say it because in simulator, when I do a GOTO 0x400, PCLATH assumes 0x0= 000 = > instead 0x0400. > > Thanks, > Eduardo Garcia > > > > ----- Original Message ----- = > From: "Harold Hallikainen" > To: "Microcontroller discussion list - Public." > Sent: Wednesday, June 27, 2007 8:25 PM > Subject: Re: [PIC] Problems with COMPUTED GOTO and PCLATH > > > = >>> Hi folks, >>> I'm with problems when doing a "computed goto" with a PIC18F4680. >>> >>> I've debugged a lot of times and I don't know what's happening. >>> >>> I did a sample program to illustrate my problem: >>> >>> The code below works fine because the "computed goto" program area don't >>> change the PCLATH value. PCLATH always is zero. >>> >>> ----------------------------------------------------------------------- >>> PROGRAM CODE 0x0030 >>> Program: >>> goto Main >>> >>> >>> ANOTHER_AREA CODE 0x80 >>> Main: >>> movlw .2 >>> addwf PCL >>> goto Main0 >>> goto Main2 >>> goto Main4 >>> >>> Main0: >>> nop >>> goto Main >>> >>> Main2: >>> nop >>> goto Main >>> >>> Main4: >>> nop >>> goto Main4 >>> -----------------------------------------------------------------------= --- >>> >>> However, IF the Main routine is in 0x100 program memory area (or 0x200, >>> 0x300, and so on), the problem happens and after the addwf a jump to a >>> 0x0nn >>> area is performed. >>> >>> THIS CODE DOESN'T >>> WORKS------------------------------------------------------------------= -------------------------------------------- >>> PROGRAM CODE 0x0030 >>> blProgram: >>> goto Main >>> >>> >>> ANOTHER_AREA CODE 0x100 >>> Main: >>> movlw .2 >>> addwf PCL >>> goto Main0 >>> goto Main2 >>> goto Main4 >>> >>> Main0: >>> nop >>> goto Main >>> >>> Main2: >>> nop >>> goto Main >>> >>> Main4: >>> nop >>> goto Main4 >>> -----------------------------------------------------------------------= --------------------------------------------------------------------------- >>> >>> >>> I see in simulator that PCLATH value never is changed. So, when I use >>> ADDWF >>> PCL, the program jumps to a 0nn relative area insted 1nn. >>> >>> "goto" shouldn't change the complete program counter? >>> >>> Anybody know what I'm doing wrong?? >>> >>> Sorry my bad english but I hope you understood me. >>> >>> Thanks, >>> Eduardo Garcia >>> >>> = >> Your English is fine! >> >> The goto changes the full program counter, but the addwf PCL ONLY changes >> PCL. >> >> I'd do something like this: >> >> >> movlw high(JumpTable) ; set pclath to start of jump table >> movwf PCLATH >> RLFNC state,w ; get state*2 since each bra takes 2 addresses >> addwf low(JumpTable),w ; add in low half of start of table >> skpnc >> incf PLCATH,f ; add carry if needed >> movwf PCL ; jump into table >> JumpTable >> bra main0 >> bra main1 >> bra main2 >> >> Note the use of bra instead of goto. If you use goto, you'll have to >> allocate 4 addresses per state. Using bra lets you do a RLFNC to multiply >> by two as you get the state into w. >> >> Hope that helps! >> >> Harold >> -- = >> FCC Rules Updated Daily at http://www.hallikainen.com - Advertising >> opportunities available! >> -- = >> http://www.piclist.com PIC/SX FAQ & list archive >> View/change your membership options at >> http://mailman.mit.edu/mailman/listinfo/piclist = >> = > > > = > _______________________________________________________ = > Yahoo! Mail - Sempre a melhor op=E7=E3o para voc=EA! = > Experimente j=E1 e veja as novidades. = > http://br.yahoo.com/mailbeta/tudonovo/ > = > > = -- = http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist