Help with CANSetFilter function

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Reza29ir
Posts: 23
Joined: 02 Jul 2013 20:55

Help with CANSetFilter function

#1 Post by Reza29ir » 02 Jul 2013 21:08

Hello everybody
I have read the CAN Library and my first project with two nodes(as in example) worked well but:
There are some constant in this library with no explanation, for example:

Code: Select all

const char
    _CAN_FILTER_B1_F1 = 0,
    _CAN_FILTER_B1_F2 = 1,
    _CAN_FILTER_B2_F1 = 2,
    _CAN_FILTER_B2_F2 = 3,
    _CAN_FILTER_B2_F3 = 4,
    _CAN_FILTER_B2_F4 = 5;
also there is a function as below:

Code: Select all

void CANSetFilter(char CAN_FILTER, long value, char CAN_CONFIG_FLAGS);
I don't know what this function realy does.
If I have 10 nodes,how should I set these parameters?

Thanks

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Help with CANSetFilter function

#2 Post by janko.kaljevic » 03 Jul 2013 09:43

Hello,

On PIC18F device internal CAN module has two receive buffers, and Buffer 0 has two filters while Buffer 1 has four filters.
With constants stated in your post you actually choose which filter you will set and its value.

Also to completely set receive buffers you will also need to set appropriate mask too.

For example:

Code: Select all

  CANSetMask(_CAN_MASK_B2, 0xFFFFFFFE, _CAN_CONFIG_XTD_MSG);      
  CANSetFilter(_CAN_FILTER_B2_F4, 12345, _CAN_CONFIG_XTD_MSG); 
This will set mask for second receive buffer to 0xFFFFFFFE which means that Least Significant Bit (LSB) in ID will be ignored.
Also it will set fourth filter for second buffer to ID = 12345.

As result of this, when message with ID 12345 or 12344 occur it will be accepted and stored in second receive buffer.

As you can see this way you set one mask and one filter and are able to receive two different IDs.
All you need to do is to list all IDs that you want to accept and accordingly set both masks and all filters.

Best regards.

Reza29ir
Posts: 23
Joined: 02 Jul 2013 20:55

Re: Help with CANSetFilter function

#3 Post by Reza29ir » 10 Jul 2013 20:17

Dear janko
Thanks alot for your useful information.
as you wrote the following code

Code: Select all

  CANSetMask(_CAN_MASK_B2, 0xFFFFFFFE, _CAN_CONFIG_XTD_MSG);     
  CANSetFilter(_CAN_FILTER_B2_F4, 12345, _CAN_CONFIG_XTD_MSG); 
can only accepts messeges with
ID=12345
ID=12344
But I send a messege with ID=1 and this filter accepted it!!!!!
would you please tell me the reason?
Best Regards

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Help with CANSetFilter function

#4 Post by janko.kaljevic » 11 Jul 2013 15:45

Hello,

In my previous post I described how to correctly set filters and masks for CAN module.
And with those settings you will accept only IDs 12345 and 12344 and they will be stored second buffer.
I tested it and you can find modified example in attachment.

Did you set other masks and filters on this CAN node?
And if you can post here your code and i will check it out.

Best regards.
Attachments
CAN.rar
(18.4 KiB) Downloaded 206 times

Reza29ir
Posts: 23
Joined: 02 Jul 2013 20:55

Re: Help with CANSetFilter function

#5 Post by Reza29ir » 11 Jul 2013 21:36

Dear Janko
Here is my code

Code: Select all

const long Node1_ID0 = 12300,
           Node1_ID1 = 12301,
           Node1_ID2 = 12302,
           Node1_ID3 = 12303,
              ID_2nd = 12344,
              ID_3rd = 1;      // node IDs
  //***********************************
  // Initialize CAN module
  // set CONFIGURATION mode
  //***********************************
  CANInitialize(1,3,3,3,1,Can_Init_Flags);
  CANSetOperationMode(_CAN_MODE_CONFIG,0xFF);
  CANSetMask(_CAN_MASK_B1,-1,_CAN_CONFIG_XTD_MSG);          // set all mask1 bits to ones
  CANSetMask(_CAN_MASK_B2,0xFFFFFFFE,_CAN_CONFIG_XTD_MSG);          // set all mask2 bits to ones
  CANSetFilter(_CAN_FILTER_B2_F4,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node ID       */
/
As I told when I send a messege with ID=0 or ID=1 the messege is accepted.
I read the datasheet and I noticed that when RXFnSIDH,RXFnSIDL,RXFnEIDH,RXFnSIDL are not initialized
They may be 1 or zero.So in my case although I have set the B2_F4 filter,I guessed maybe the other filter value be zero.
so I changed my code as below

Code: Select all

  //***********************************
  // Initialize CAN module
  // set CONFIGURATION mode
  //***********************************
  CANInitialize(1,3,3,3,1,Can_Init_Flags);
  CANSetOperationMode(_CAN_MODE_CONFIG,0xFF);
  CANSetMask(_CAN_MASK_B1,-1,_CAN_CONFIG_XTD_MSG);          // set all mask1 bits to ones
  CANSetMask(_CAN_MASK_B2,0xFFFFFFFE,_CAN_CONFIG_XTD_MSG);          // set all mask2 bits to ones
  CANSetFilter(_CAN_FILTER_B2_F4,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node ID       */
  CANSetFilter(_CAN_FILTER_B2_F3,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F3 to 2nd node ID       */
  CANSetFilter(_CAN_FILTER_B2_F2,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F2 to 2nd node ID       */
  CANSetFilter(_CAN_FILTER_B2_F1,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F1 to 2nd node ID       */
  CANSetFilter(_CAN_FILTER_B1_F2,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B1_F2 to 2nd node ID       */
  CANSetFilter(_CAN_FILTER_B1_F1,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B1_F1 to 2nd node ID       */
So it means that I set all filters to the ID_2nd(12344) and it worked!!!
What is you idea?

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Help with CANSetFilter function

#6 Post by janko.kaljevic » 12 Jul 2013 14:20

Hello,

Yes you are right.

Uninitialized Filter will have value of 0 and ID = 0 will be accepted.
Usually ID = 0 is used as broadcast ID (check CANOpen protocol) and it is understandable.

If you do not want to accept these IDs, you will have to set all Filters on desired values.

Best regards.

Reza29ir
Posts: 23
Joined: 02 Jul 2013 20:55

Re: Help with CANSetFilter function

#7 Post by Reza29ir » 18 Jul 2013 20:41

Dear Janko
Thanks so much of your replies.
I want to use the Hardware UART and CAN Module in my project with PIC18F458.
Does the CAN module depend to crystal frequency?
I mean can I use 11.0592MHZ crystal ?
Regards

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Help with CANSetFilter function

#8 Post by janko.kaljevic » 19 Jul 2013 08:12

Hello,

Yes, baud rate is dependent on MCU frequency.
You can use Canculator application to calculate desired baud for your MCU frequency.
http://www.libstock.com/projects/view/360/canculator

Best regards.

Reza29ir
Posts: 23
Joined: 02 Jul 2013 20:55

Re: Help with CANSetFilter function

#9 Post by Reza29ir » 22 Jul 2013 21:24

Dear Janko
Thanks So much of your answers
I downloaded the CANcalculator
when I enter the 8MHZ for MCU Freq, there are so many options in CAN data parameteres that can be chosen.
But when I change the MCU Freq to 11.0592MHZ I see no option to be selected!!!
is there any special problem with this crystal?
Regards

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: Help with CANSetFilter function

#10 Post by p.erasmus » 22 Jul 2013 21:33

is there any special problem with this crystal?

Yes it not a good frequency for CAN !!
Use 8Mhz with the PLL and you can run CAN and the UART with no problem ,
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Reza29ir
Posts: 23
Joined: 02 Jul 2013 20:55

Re: Help with CANSetFilter function

#11 Post by Reza29ir » 01 Aug 2013 21:59

Dear p.erasmus ,jonko Thanks
In my project I'm going to add a MMC(Multi Media Card).The MCU is PIC18F458
It's very strange that MMC_FAT routines swallow the RAM!!!!
Am I right or something is wrong?
another question
when I have some constant variable,e.g,constant array,...isn't there any way to store this variable in FLASH memory?and not occupying the RAM space?
In Codevision AVR its possible by the following code:
flash int a=20;
Best Regards

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Help with CANSetFilter function

#12 Post by janko.kaljevic » 05 Aug 2013 17:09

Hello,

In MMC_Fat library you will need to have buffer of 512 bytes (sector size) and this is why it occupies a lot of RAM.
You can declare your array as constant and this will store it in flash and keep the RAM unoccupied.

To do this just use const key word:

Code: Select all

const int a=20;
Best regards.

Post Reply

Return to “mikroC PRO for PIC General”