PIC18 sample code pls...

Page: 12 > Showing page 1 of 2
Author
Ling
Starting Member
  • Total Posts : 47
  • Reward points : 0
  • Joined: 2006/10/31 22:12:00
  • Location: 0
  • Status: offline
2007/04/11 23:04:29 (permalink)
0

PIC18 sample code pls...

Hi,
 
Does anyone have any sample CAN code to start with for PIC18 with MCP2551 transceiver? I need to read in ADCs and output data through CAN BUS. PLs help...sad
 
#1

31 Replies Related Threads

    casm
    Super Member
    • Total Posts : 861
    • Reward points : 0
    • Joined: 2003/11/07 12:51:28
    • Location: UK living in Hong Kong
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/11 23:31:21 (permalink)
    0
    Dear Ling,
     
    Have you looked at the Application Notes section of Microchips web site?
     
    http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1469&filter1=function
     
    Best regards,
    Clive
    #2
    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/11 23:36:48 (permalink)
    0
    Hi,
    What part of your project are you stuck on?
    CAN Bus configuration?
    ADC configuration?
     
    What PIC are you using?
     
    I doubt anyone on the forum will give you completed 100% working & tested code. However, you will get lots of help if you can show you are working on the problem as well.
     
    The Microchip website has alot of application notes and demo code for use with various CAN enabled PIC's - It might be worth while having a look...
     
    Some advice/tips for you;
    1. Read (and understand) the Bosch CAN 2.0A/B spec
    2. Implement the physical layer correctly
    3. Try not to use any pre-built CAN libraries - writing your own functions will teach you alot more about CAN.
     
    Hope it helps
    Artic
    #3
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/12 00:22:22 (permalink)
    0
    Hi Clive and Artic,
     
    Yes, I have tried reading up all the application notes about CAN but as a beginner its quite difficult to head start writting code for CAN. 
    These are my hardware requirement.
    - PIC18F2585 with 40MHz Crystal.
    - MCP2551
    - 2 analog devices
     
    I know a bit about C30 on ADC configuration and I assumed C18 will be the same by calling the libraries. I'm more trouble by the ECAN or how to get the ADCs data through ECAN. My question is can C18 support ECAN? Currently, I'm using the free C18 student edition to try out my project.
     
    Your advise pls...
    #4
    Artic
    Super Member
    • Total Posts : 2288
    • Reward points : 0
    • Joined: 2004/06/17 06:21:59
    • Location: Wales now in Sofia,BG
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/12 00:25:50 (permalink)
    0
    Hi,
    C18 support ECAN?
    Yes, ECAN is just another peripherial so C18 has the various ECAN registers pre-defined.
     
    Cheers
    Artic
    #5
    Sueco
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2005/01/19 07:46:39
    • Location: Valencia - Spain
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/12 00:27:04 (permalink)
    0
    ORIGINAL: Ling

    My question is can C18 support ECAN?


     
    I use C18 for ECAN controller on PIC18F2480 and CAN controller on PIC18F248. I use C30 for ECAN and CAN too. On C18 I used the libraries but they had some bugs to correct when I first used them... As Artic would say, it's better to write your own libraries, moreover if you're new to CAN.
    #6
    jajones480
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2006/01/30 13:41:45
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/18 05:06:43 (permalink)
    0
    Check out the book PIC Microcontroller: An Introduction to Software and Hardware Interfacing" by Huang.  It has a good section on CAN and has some good examples.  Most of the technical information is directly from the data sheet so make sure you have the data sheet handy if you get the book. 
     
    I've spent about a year working on CAN, starting out with absolutely no knowledge of how to implement CAN.  To date, I've written a fully functioning, fully configurable CAN library that can be configured for MODES 0, 1 and 2 and uses a message queue to TX CAN messages.  It's taken a lot of time to understand the nuances of the CAN implementation but it can be done.  Don't rely on libraries or other peoples code to learn how to use CAN.  The best way to do it is to just brute force it and learn the spec.  Understand ALL the registers that are needed for CAN and how to configure them properly. 
     
    Looking back it seems simple but I know when I first started it was very frustrating.  There is such an enormous amount of information to absorb its hard to know where to begin.  This forum is a GREAT resource if you have questions and are trying to work through problems on your own but just get stuck.  Don't expect people to just post source code for you to use because that doesn't help you understand what is going on inside the microcontroller.

    Here are a couple of ways you can try and break down your understanding into smaller bits:
    1.  Configuration - configuring CAN baud rates and modes.  Check out www.kvaser.com for their CAN baud rate configuration tool.  It will generate the BRGCON 1/2/3 values for you.  I use this and it's great.
    2.  TX CAN - configuring the TXBnCON registers for transmit (priority, enable RTR).  Also understanding how to set TXBn address registers.  There are 3 default TX buffers that can be used.  The TXBnSIDH/L registers are used to set the 11-bit transmit ID (i.e. the node you want to transmit to).
    3.  RX CAN - This, I believe, is the most complex area.  You have to understand how to set the masks (how many bits of the address do we actually want to arbitrate with?) and address filters (i.e. which address(s) does the node want to listen to?)
     
    Setting up CAN for use in loopback mode is the best way to start. 
     
    Good Luck!
     
    Good Luck!
    #7
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/24 01:42:15 (permalink)
    0
    Hi,
    Juz started to try the sample application program from AN878 and encountered an error showing "could not find of symbol" ECANReceiveMessage" in file D:\CAN\main.o.
     
    Any file missing that caused the error?
     
    These are the files in the *.mcw
     
    -Source file
     *main.c
    -Header files
     *p18f2585.h
     *ECAN.h
    -Library files
     *p18f2585.lib
    -Linker scripts
     *18f2585.lkr
     
     
    Rgds.
    #8
    Sueco
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2005/01/19 07:46:39
    • Location: Valencia - Spain
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/24 02:05:17 (permalink)
    0
    You need to include ECAN.c if ECAN routines are not included in p18f2585.lib, I think that's the reason.
    #9
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/26 01:00:15 (permalink)
    0
    Hi,
     
    I'm puzzled and stuck at setup configuration. Should I use the libraries or start with my own code( might take more time)...trying to find a easy and fast way.
    Before I start, would like to find out if its possible to tx data only on the CAN bus? My application is only to output the string of data?so in the code do I need to Rx? 
     
    Rgds.
    #10
    Sueco
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2005/01/19 07:46:39
    • Location: Valencia - Spain
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/26 01:48:51 (permalink)
    0
    It's indeed possible to implement just TX and disable all message reception. And if you don't know what this libraries are doing, just write your own code.
    #11
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/04/26 02:07:29 (permalink)
    0
    Thanks Sueco. I'll give it a try by writting my own code.
    #12
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/02 01:29:55 (permalink)
    0
    Hi I'm back with questions Smile,
     
    I'm trying to do a loopback test and I'm not sure if its working. From the scope, the waveform of the CANH and CANL was inversed. correct??
     
    CANCON= 0x08;
    TXB0D0 ='A';
    TXB0D1 ='B';
    TXB0D2 ='C';
    TXB0D3 ='D';
    TXB0SIDL= 0x00;
    TXB0SIDH= 0x20;
    TXB0DLC= 7;
    TXb0CON = 0x08;
    while(( TXB0CON & 0x08)==0x00);
     
    Rgds.
    Ling 
    #13
    Sueco
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2005/01/19 07:46:39
    • Location: Valencia - Spain
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/02 07:32:59 (permalink)
    0
    ORIGINAL: Ling
    From the scope, the waveform of the CANH and CANL was inversed. correct??


    When TX=0V, then bus should go dominant, i.e. CANH-CANL=2V
    When TX=5V, then bus should go recessive, i.e. CANH-CANL=0V
    #14
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/03 00:07:53 (permalink)
    0
    Hi,
     
    What is the difference between transmitting in BANKED method and WIN Bits at examples 23-3 and 23-4? Noticed ex.23-3 is using TXB0D0 and ex.23-4 is using RXB0D0. Your advise pls. 
     
    How do I convert 32bits 0x0123H in TXB0D0?
     
     
    Rgds.
    Ling
    #15
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/07 02:06:21 (permalink)
    0
    Can anyone advise if my tx message is correct if my data is result= 0x01D0
     
    txdata[0]= (result>>8)
    txdata[1]= (result<<8)
     
    TXB0D0 = txdata[0]
    TXB0D1 = txdata[1]
     
     
     From the watch window I managed to see this.
    TXB0D0 = 0x0001
    TXB0D1 = 0xD000
     
    Rgds.
    Ling
    #16
    Sueco
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2005/01/19 07:46:39
    • Location: Valencia - Spain
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/07 14:07:00 (permalink)
    0
    ORIGINAL: Ling

    txdata[0]= (result>>8)
    txdata[1]= (result<<8)

    TXB0D0 = txdata[0]
    TXB0D1 = txdata[1]


     
    It's wrong, I think. txdata[1] will be always 0. You should try:
     
    txdata[0]=(unsigned char)(result>>8);
    txdata[1]=(unsigned char)(result&0xFF);
     
    &0xFF is optional, but I put it almost always.
     
    The correct way to convert 16-bits types to 8-bits for CAN transmission is to use pointer or unions:
     
    typedef union{
    unsigned char byte[2];
    unsigned int word;
    } bytetoint;
     
    bytetoint result=0;
     
    result.word=(what you want to read);
    TXB0D0=result.byte[1]; //MSB
    TXB0D1=result.byte[0]; //LSB
     
    Or it's possible too:
     
    TXB0D0=*(unsigned char *)(&result+1);
    TXB0D1=*(unsigned char *)&result;
     
    if I'm not wrong this will assign TXB0D0 the MSB of result via pointers, and TXB0D1 the LSB.
    #17
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/07 17:21:43 (permalink)
    0
    Thanks Sueco,
     
    I'll give it a try.
    #18
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/08 18:13:21 (permalink)
    0
    Yes Sueco, its working, I'm able to see the conversion under the window watch. There are waveforms shown on the scope but too bad I can't show it here cos I have problem transfering the files. Are the any ways to test whether I'm transmitting the right data?
     
    Rgds.
    Ling
    #19
    Ling
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2006/10/31 22:12:00
    • Location: 0
    • Status: offline
    RE: PIC18 sample code pls... 2007/05/09 01:58:38 (permalink)
    0
    or can I say the CAN transmitting is working fine if the following tx buffers in the watch window shown the value I want? 
    TXB0D0=result.byte[1]; //MSB
    TXB0D1=result.byte[0]; //LSB
     
     
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2017 APG vNext Commercial Version 4.5