CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC18F46K80 Can Interface

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
timk



Joined: 03 Aug 2018
Posts: 11

View user's profile Send private message

PIC18F46K80 Can Interface
PostPosted: Wed Oct 16, 2019 7:31 am     Reply with quote

Hello!

Currently I want to use the CAN Interface of the PIC18F46K80.
I am using the CCS Compiler with the CCS IDE.
The Help Section of the CCS IDE says, that I should be able to init the CAN-Bus with an "can_init(void);" command.
But If I want to compile, the compiler says "Undefined Identifier".

How can I use these CAN-functions?
Or is there any other driver I could use for Communicating over CAN?

Thanks in Advance!
temtronic



Joined: 01 Jul 2010
Posts: 9093
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Oct 16, 2019 7:43 am     Reply with quote

The best place to start would be using the CAN examples, from the EXAMPLES folder.

I don't use CAN (too much overhead), but I'd try the CCS examples. Be sure to read the embedded comments in the code !

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: PIC18F46K80 Can Interface
PostPosted: Wed Oct 16, 2019 9:44 am     Reply with quote

timk wrote:
Hello!
The Help Section of the CCS IDE says, that I should be able to init the CAN-Bus with an "can_init(void);" command.
But If I want to compile, the compiler says "Undefined Identifier".

The CAN bus functions are not built-in to the compiler. You have to
#include a driver file. In the help file, it lists the CAN Bus driver files
that CCS has available:
Quote:
Relevant Include Files:

can-mcp2510.c - Drivers for the MCP2510 and MCP2515 interface devices.
can-18xxx8.c - Drivers for the built-in CAN module.
can-18F4580.c - Drivers for the built-in ECAN module

Since you are using the 18F46K80, the best one for your PIC is shown
in bold above.

The only CCS example file that uses that driver file is this one:
Quote:
Ex_can_ccs_a.c

Look in the CCS Examples directory on your PC to find this file.
timk



Joined: 03 Aug 2018
Posts: 11

View user's profile Send private message

PostPosted: Wed Oct 16, 2019 11:53 pm     Reply with quote

Okay, thank you.
I think I can work with that.

Haven't seen these examples before.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Oct 17, 2019 2:16 am     Reply with quote

We need a big red headline on the forum. Something like:

The CCS examples, are one of the main things you should always look at....

Very Happy
temtronic



Joined: 01 Jul 2010
Posts: 9093
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Oct 17, 2019 4:33 am     Reply with quote

I agree with Mr. T !
Actually the examples were one reason I bought PCM over 2 decades ago. Since I had/have ZERO C formal programming, they allowed me to see how to cut code. While my programs do not conform to official C standards for layout and such, they function. CCS even sent me an updated version of the Examples library and while some programs I can't use, they show HOW to access PIC features.
fkl



Joined: 20 Nov 2010
Posts: 44

View user's profile Send private message

PostPosted: Tue May 02, 2023 6:17 am     Reply with quote

hello.

PIC18F46K80

As an example, do I need to use ex_can_ccs_a.c ?

How to calculate these variables correctly in relation to 500K bus speed?
Code:

#define CAN_BRG_PRESCALAR           3    //Set CAN Baud Rate to 125K
#define CAN_BRG_PHASE_SEGMENT_1     6    //Tq = (2*(1+PRESCALAR))/Fosc
#define CAN_BRG_PHASE_SEGMENT_2     6    //Tq = (2*(1+3))/16000000 = 0.0000005
#define CAN_BRG_PROPAGATION_TIME    0    //Baud Rate = 1/(((PHASE_SEGMENT_1+1)+(PHASE_SEGMENT_2+1)+(PROPAGATION_TIME+1)+1)*Tq)
#define CAN_BRG_SYNCH_JUMP_WIDTH    0    //Baud Rate = 1/(((6+1)+(6+1)+(0+1)+1)*0.0000005) = 125000


What processor clock frequency is needed for 500K bus speed, 16MHz (#use delay(internal=16000000)) - will it be sufficient or do I need to increase the frequency to 64MHz and use an external clock crystal?
temtronic



Joined: 01 Jul 2010
Posts: 9093
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 02, 2023 6:58 am     Reply with quote

the equations you need to use are in the comments !
How you calculate is up to you..
1) cut a program using them....
2) create a spreadsheet using them
or
3) grab a pencil and pad and use them.....

another option is to have the CAN program automatically figure all this out based upon desired baud rate and clock value.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue May 02, 2023 7:02 am     Reply with quote

Use one of the online calculators.
The formulae are there in the MicroChip data sheet, and application notes,
but the calculators are easier.
[url]
https://www.medo64.com/2014/05/canbus-setup/
[/url]

MicroChip have their own one, but this is simpler to use.

You can do 500K at 16MHz OK.

'Internal' is potentially slightly problematical, since it can easily take you
outside the allowed tolerance. Just like USB, where the clock accuracy
required is higher than the accuracy of most internal oscillators, CAN is
quite strict on it's timings. It normally requires a master oscillator that
is better than 0.5% accurate. The internal oscillator only manages +/-2%.

[url]
https://www.analog.com/en/design-notes/design-considerations-for-can-bus-and-asynchronous-serial.html
[/url]

So you don't 'need' to increase to 64MHz. Will depend on what you actually
want the processor to do. But you do really need a crystal or ceramic
resonator.
fkl



Joined: 20 Nov 2010
Posts: 44

View user's profile Send private message

PostPosted: Tue May 02, 2023 7:34 am     Reply with quote

Code:

//setup pins
TRISB2 = 0;
TRISB3 = 1;

CANCON = 0b10000000; //set to Configuration mode
while ((CANSTAT & 0b11100000) != 0b10000000);

BRGCON2bits.SEG2PHTS =  1; //freely programmable SEG2PH

BRGCON1bits.BRP    = 0;
BRGCON2bits.PRSEG  = 6; //7 Tq
BRGCON2bits.SEG1PH = 5; //6 Tq
BRGCON3bits.SEG2PH = 1; //2 Tq
BRGCON1bits.SJW    = 1; //2 Tq

ECANCONbits.MDSEL = 2; //Enhanced FIFO mode

CANCON = 0b00000000; //set to Normal mode
while ((CANSTAT & 0b11100000) != 0b00000000);

please tell me how to assign correctly calculated values ​​(variable names are different in the calculator, it is for another language), if it doesn’t make it difficult
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue May 02, 2023 7:48 am     Reply with quote

Just read the data sheet. The names are self explanatory.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue May 02, 2023 9:17 am     Reply with quote

BRP Baud Rate Prescaler
SEG1PH Segment 1 phase
SEG2PH Segment 2 phase
SJW Synchronization jump width
PRSEG Propagation time

If you look at the CCS names, the relationship is obvious.
fkl



Joined: 20 Nov 2010
Posts: 44

View user's profile Send private message

PostPosted: Tue May 02, 2023 10:31 am     Reply with quote

Code:

#define CAN_BRG_PRESCALAR           0 
#define CAN_BRG_PHASE_SEGMENT_1     5
#define CAN_BRG_PHASE_SEGMENT_2     1
#define CAN_BRG_PROPAGATION_TIME    6
#define CAN_BRG_SYNCH_JUMP_WIDTH    1


//Set CAN Baud Rate to 500K
//Fosc=16MHz

correct?
fkl



Joined: 20 Nov 2010
Posts: 44

View user's profile Send private message

PostPosted: Thu May 11, 2023 6:26 am     Reply with quote

In the help ccs c only lists the functions of the CAN bus, in general there is a more detailed description of these functions?
In particular, how to add filters and masks correctly?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri May 12, 2023 2:12 am     Reply with quote

Two things here.
First you need to read up on CAN itself.
Second get the Microchip application note 70070D.

The acceptance filters and filter masks are part of the CAN specification.
How these are implemented on the PIC, the application note covers.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group