Page 1 of 2
Can interrupt in dsPIC
Posted: Tue Jan 10, 2012 12:03 pm
by r_romeo
Hi, I am trying to generate a CAN rx interrupt , I am using the examples given in
http://www.matrixmultimedia.com/mmforum ... dler#p9038
http://www.matrixmultimedia.com/mmforum ... upt#p27696
I am also using the example given in the HELP file for custom interrupts
I know I have to do the following
enable code:
in register
IEC1, set
C1IE (CAN1 (Combined) Interrupt Enable bit) I am working with a dsPIC30f4012 with just one CAN chanel
in register
C1INTE, set RX0IE (Receive Buffer 0 Interrupt Enable bit) for this code I will just Enable the bufer0
disable code
RX0IE =0 //I keep the global CAN Interrupt Enable bit, but disable the Receive Buffer 0 Interrupt, I guess this is OK (?)
Handler code
if (c1intf & (1 << RX0IF))
{
FCM_%(); // call selected macro
clear_bit(c1intf,RX0IF);// clear interrupt
}
This is all I know now, If I try to do this in flowcode,
I insert interrupt block
interrupt in-----custom
go to properties
and just the enable and handler spaces are available, no the disable one

- can rx interrupt.jpg (59 KiB) Viewed 13979 times
also, if I try to compile to chip, I get

- compiler message.jpg (164.74 KiB) Viewed 13979 times
I know I have some variables undeclared, and also I am using the registers and bits from datasheet, but not sure if they are declared in flowcode/C
since I am new in C code , any light in this matter will be a great help
Thanks
Re: Can interrupt in dsPIC
Posted: Tue Jan 10, 2012 12:15 pm
by Benj
Hello,
To see all the available register and bit names for your device you can look at the .h file here.
C:\Program Files\Matrix Multimedia\Flowcode PIC24&dsPIC V4\Tools\C_tools\support\dsPIC30F\h
For the dsPIC compiler you need to use uppercase for register names and bit names. Also if you are using . notation you will need to include bits after the uppercase register name.
if (C1INTFbits.RX0IF)
{
FCM_%(); // call selected macro
C1INTFbits.RX0IF = 0; // clear interrupt
}
Re: Can interrupt in dsPIC
Posted: Tue Jan 10, 2012 2:32 pm
by r_romeo
Thanks Ben, that is very handy, now I know the registers and bits exist in flowcode
Interesting that the syntax is different for PICs and dsPICs since I was just copying the example in the Help file
Do you know if I am doing something wrong in the interrupt window?, just the enable and handler spaces are available, no the disable one (as in the picture)
now the interrupt windows is

- can rx interrupt 2.jpg (50.37 KiB) Viewed 13965 times
looks that I have a syntax error in the IF command as the hex compilation tells me, but I don't know why !!!???

- compiler message 2.jpg (96.54 KiB) Viewed 13965 times
Re: Can interrupt in dsPIC
Posted: Tue Jan 10, 2012 5:28 pm
by Benj
Hello,
If you create an interrupt icon and put it into disable mode and then select the custom interrupt type then the disable text field should be editable. i.e. you put the required code into the icon that is using it.
If you attach your C code file to the forums or the post lines surrounding line 2432 then I will have a look and see if I can spot whats wrong with the code.
Aha think I have worked it out. For dsPIC each interrupt has it's own service routine so instead use code like this.
Code: Select all
void _ISR __C1Interrupt(void)
{
if (C1INTFbits.RX0IF)
{
FCM_%(); // call selected macro
C1INTFbits.RX0IF = 0; // clear interrupt
}
}
Re: Can interrupt in dsPIC
Posted: Tue Jan 10, 2012 6:21 pm
by r_romeo
you were right , in disable mode I can set the disable code
I entered the code you proposed and there was another compilation error , so I am leaving my files for checking
notice that if I take out the interrupt (at the beginning) the code works fine
Tanks !!
Re: Can interrupt in dsPIC
Posted: Thu Jan 12, 2012 11:51 am
by r_romeo
I have been trying , but still I am in the darkness ......
The code works fine in the hardware, in the simulation there is a problem with input of CAN TX ID, if done in macros and not at the properties dialog. That is a known problem I can live with, just in case you try to simulate.
Any help will be great , since is placing the base for custom CAN interrupts.
Thanks!!
Re: Can interrupt in dsPIC
Posted: Thu Jan 12, 2012 12:42 pm
by Benj
Hello,
Using this as your handler code seems to work correctly.
Code: Select all
void _ISR _C1Interrupt(void)
{
if (C1INTFbits.RX0IF)
{
FCM_%n(); // call selected macro
C1INTFbits.RX0IF = 0; // clear interrupt
}
}
Re: Can interrupt in dsPIC
Posted: Fri Jan 13, 2012 4:04 pm
by r_romeo
Hi Ben
that's weird man, still not passing the compiler stage
I entered the code you give me,

- can rx interrupt 3.jpg (42.03 KiB) Viewed 13915 times
and this was the message from compiler

- compiler message 3.jpg (175.29 KiB) Viewed 13915 times
Am I forgetting some step? maybe in the project options: "custom C code" ? or something in the "Chip Configure" ?
Re: Can interrupt in dsPIC
Posted: Fri Jan 13, 2012 5:36 pm
by Benj
Hello,
From the looks of it your just missing the semi colons from the end of the lines of code in the enable code section.
Re: Can interrupt in dsPIC
Posted: Fri Jan 13, 2012 9:19 pm
by r_romeo
They are there, just the window is not showing it, I double checked

Re: Can interrupt in dsPIC
Posted: Mon Jan 16, 2012 12:02 pm
by Benj
This file is compiling correctly so you should be able to copy the icon out of this one and into your Flowcode file.
Re: Can interrupt in dsPIC
Posted: Thu Jan 26, 2012 7:55 pm
by r_romeo
Now the interruption is been produced, looks that was a spelling mistake, thanks Ben
now the problem I have is in the reception:
if I get the Rx ID in the receiver , always is zero, I have tried changing the ID in the TX and RX module with different IDs . The same with the data.
I am checking the signal with a logic analyzer
I am looking in the Microchp datasheets and I cant find the problem
here are the files I am using
Is a problem with my configuration (probably ) ? or is a bug in flowcode?
thanks again for your support
Re: Can interrupt in dsPIC
Posted: Fri Jan 27, 2012 2:15 pm
by Benj
Hello,
At the start of your CAN_RX macro add a component macro to call the CheckRX function. This function is responsible for filling the data buffers etc that are then read by the GetRXID and GetRXData functions.
Re: Can interrupt in dsPIC
Posted: Mon Jan 30, 2012 6:21 pm
by r_romeo
thanks Ben, that works in getting the data, but not in the RX ID, that's weird
Any idea ?
Re: Can interrupt in dsPIC
Posted: Wed Feb 01, 2012 7:59 pm
by r_romeo
Any idea?
Re: Can interrupt in dsPIC
Posted: Thu Feb 02, 2012 12:45 pm
by Mikat
Hi. Do you get any data at the msg id bits?
Because the msg id is quite complicated, just look at you code, and it seems that you sent id_lo 33...
If you receive id lo = 1, flowcode shows it id_lo value 32 and 2 64 etc... if you don't convert the data...
But I don't remember anymore how exactly flowcode does the tx id (Ben ?), I should look my old code...
Looking at the datasheet, looks that Matrix code fetches the msg_id right..
Mika
Re: Can interrupt in dsPIC
Posted: Fri Feb 03, 2012 5:00 am
by r_romeo
Mikat wrote: the msg id is quite complicated, just look at you code, and it seems that you sent id_lo 33...
If you receive id lo = 1, flowcode shows it id_lo value 32 and 2 64 etc... if you don't convert the data...
But I don't remember anymore how exactly flowcode does the tx id (Ben ?), I should look my old code...
Looking at the datasheet, looks that Matrix code fetches the msg_id right..
Mika
"The two bytes are laid out as follows.
The 8 Most Significant Bits (the first 8 ) of the ID are put into the High byte.
The 3 Least Significant Bits (the final 3 bits) are put into the Low byte - but in the 3 Most Significant Bit positions (i.e. the first 3)"
I am checking the ID by a logic analyzer with CAN reading .
I am able to read the data, but the RX ID low always = 0
Re: Can interrupt in dsPIC
Posted: Fri Feb 03, 2012 7:39 am
by Mikat
r_romeo wrote:
I am checking the ID by a logic analyzer with CAN reading .
I am able to read the data, but the RX ID low always = 0
So the id_lo is always 0 at the can bus, not just the software at the rx end? How about the id_hi.
Yep looked the set tx id code, and it seems that it goes wrong...
Ben correct me if I am wrong, the bitwise operations are not my strongest area, but I tested the code by flowcode simulation, and it seems that the set tx code sets only the C1TXnSID bits 8-15...
The result always changes min 256...
I mean the code:
Code: Select all
C1TX0SID = ((hi << 5) | (lo >> 3)) & 0x00FF; //Transmit Buffer 0 S-ID
C1TX0SID = C1TX0SID | ((hi << 8) & 0xFF00);
If I set the tx_id hi 1 the C1TX0SID value is 256, no matter what is the tx_id lo value...
And if I set the tx_id:s at hi 0b11111111 and lo 11100000 the C1TX0SID is 0b1111111100000000, when it should be 0b11111xxx111111xx...
The C1TX0SID register has msg_id bits 10-6 at bits 10-15 and the lowest 6 msg_id bits at location 7-2, so the msg_id is split at 5 and 6 bit and those register bits 15-10 and 7-2 (could it be done more difficult

)...
Mika
Re: Can interrupt in dsPIC
Posted: Mon Feb 06, 2012 6:01 pm
by r_romeo
The SET ID always has worked for me , now the "weird " behave is just after the interrupt .
The ID HI is also 0
Re: Can interrupt in dsPIC
Posted: Tue Feb 07, 2012 3:37 pm
by Benj
Hello,
The code in the check rx macro looks to be correct so I'm not really sure what is happening to your ID.
I will see if we have one of these devices available and do some testing.
Maybe you could try using check rx with the parameter 1 instead of 0. In your CAN component properties buffer 1 is set to accept all messages whereas buffer 0 only accepts two unique IDs. Not sure why the data is coming through correctly at the moment though.
Re: Can interrupt in dsPIC
Posted: Tue Feb 07, 2012 5:33 pm
by Mikat
Benj wrote:Hello,
Maybe you could try using check rx with the parameter 1 instead of 0. In your CAN component properties buffer 1 is set to accept all messages whereas buffer 0 only accepts two unique IDs. Not sure why the data is coming through correctly at the moment though.
Agree with that, it's easier to first test the can bus with buffer which receives all messages, and then start to use filter and mask...
I have 30f6012 with in couple of days, and I need CAN too, so probably I need to play with same things then...
Mika
Re: Can interrupt in dsPIC
Posted: Tue Feb 07, 2012 5:52 pm
by r_romeo
Hi Ben and Mika
I think I did that experiment also, but I am not completely sure. Now I am away from home, but I will do it over the weekend
Re: Can interrupt in dsPIC
Posted: Tue Feb 07, 2012 6:38 pm
by Mikat
Ok.
I et my pcb:s probably at the next week, before that I can't test anything..
By the way, you say that you have can bus logic analyzer, does that show the id right?
So is the id right in the can bus, but not at the receive end?
Mika
Re: Can interrupt in dsPIC
Posted: Wed Feb 08, 2012 4:14 pm
by r_romeo
Yes, I can see the IDs and data with my analyzer, I know what I am sending but in the reception end is wrong
Re: Can interrupt in dsPIC
Posted: Wed Feb 08, 2012 6:11 pm
by Mikat
Just check little bit at your code.
Did you check the msg ID only the led at bits 0-3?
Because when you set the id low at 33 your sent code, that is actually msg id 1 because the matrix code takes 3 msb at the id lo so the values are 1= 32=2,64,3=96 etc..
So far so good, that goes through the filter, BUT if you don't modify the value, the Matrix code sets again the 3msb bits at the msg id lo, so the id 1 actually gives msg id lo value 32... And that you can't see at leds in bits 0-3.. So try to use calculation which is in the can component help file, end of it, or put the leds in bits 6-8, and you should get the id right (I hope)...
Mika