CAN code for PIC 18f258

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
jcooper
Posts: 23
Joined: Tue Jul 08, 2008 7:29 pm
Contact:

CAN code for PIC 18f258

Post by jcooper »

I'm working with the CAN module on the pic 18f258 chip, and I'm wondering how to do a multi-register read or write. I want to be able to fill a struct or array of size 13bytes and write those bytes directly to registers TXBnCON through TXBnD7. Any suggestions on how I can do this? Better yet, do you have code written for the CAN module? The only CAN code that came with my mcc18 library was for the SPI CAN chip 2510.
No trees were harmed in the posting of this message. However, a large number of electrons were terribly inconvenienced.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: CAN code for PIC 18f258

Post by Benj »

Hello

No im afraid we do not currently have code to enable the onboard CAN registers. Hopefully Flowcode will be able to perform this soon. There is no multi register read or write as far as I can tell you will have to write each register value one at a time.

jcooper
Posts: 23
Joined: Tue Jul 08, 2008 7:29 pm
Contact:

Re: CAN code for PIC 18f258

Post by jcooper »

Is there some way I can set up a pointer to the TXBnCON location and then increment it as I go along? Or are the registers in the wrong part of memory for that?
No trees were harmed in the posting of this message. However, a large number of electrons were terribly inconvenienced.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: CAN code for PIC 18f258

Post by Benj »

Hello

Yes im sure this can be done however im not sure how it would be acheived on the mcc18 compiler. There should be a compiler manual which will detail things like pointers and addresses.

However if you wanted to make life easier for your self you could just assign the values by writing directly to the registers.

Eg

TXBnCON = 0x00;

jcooper
Posts: 23
Joined: Tue Jul 08, 2008 7:29 pm
Contact:

Re: CAN code for PIC 18f258

Post by jcooper »

That's easier in one way, but it makes for messy code when you've got three potential transmission buffers, plus two receive buffers, which take up 13 individual registers each. That's an extra 65+ lines of code, which could be downsized into 2 for-loops if I could use pointers for the registers.
No trees were harmed in the posting of this message. However, a large number of electrons were terribly inconvenienced.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: CAN code for PIC 18f258

Post by Steve »

Could you resort to index addressing using the INDF and FSR registers in embedded assembly code? Alternatively, you could create a "write_reg" macro (or in-line function) in C that takes 2 parameters (address and value) which writes the "value" to the register at location "address".

Post Reply