> I'm porting a software that is running ok on a PIC16F877 to a PIC18F452 > and now I'm having problems with the computed goto's. > > On the PIC16: > > MOVLW 0x01 > MOVWF PCLATH > MOVF state,W_ > ADDWF PCL,F_ > GOTO case0 > GOTO case1 > GOTO case2 > etc > > On the PIC18 strange things happen: > MOVLW 0x01 > MOVWF PCLATH > RLCF dkstate, W_ > ADDWF PCL > GOTO case0 > GOTO case1 > GOTO case2 > etc First, DOCUMENT YOUR CODE!!! If you had forced yourself to explain what each instruction was doing, you might have found the bug yourself. Second, why are you hard setting PCLATH to 1? This is very bad programming practise and a bug waiting to happen (maybe it just did). Your code will only work if all the GOTO instructions are located from 100h - 1FFh. This should be derived symbolically, or at least the same symbolic constant used to force the address of the table as derive the value to stick into PCLATH. Note that the PIC16 addresses program memory as whole words, with each instruction taking up one word. The PIC18 addresses program memory in bytes, with instructions taking up either 2 or 4 bytes. The PIC18 GOTO instruction takes up 4 bytes, so the DKSTATE needs to be multiplied be 4, not 2, before used to index into the table. Also, this type of GOTO table can only support up to 64 entries because that are all that fit into a 256 byte region. Larger tables require computation of the PCLATH value, since a single PCLATH value addresses a 256 byte region. Note also that the PIC18 has a PCLATU register in addition to PCLATH. You can get away with ignoring PCLATU on the 18F452 because it only has 32Kbytes of program memory, but this is another bug waiting to happen. I would use assembly conditionals to insert the proper code if this is ever ported to a machine with more than 64Kbytes. Or, at least generate an assembly error in that case so that someone is forced to inspect the code. It's easy to do now, a real pain 4 years from now when you've forgotten all about this little section of code. While you're at it, you can generate an assembly error automatically is someone tries to grow the table beyond 64 entries. ***************************************************************** Embed Inc, embedded system specialists in Littleton Massachusetts (978) 742-9014, http://www.embedinc.com -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body