Can interrupt in dsPIC

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Can interrupt in dsPIC

Post 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
can rx interrupt.jpg (59 KiB) Viewed 12288 times
also, if I try to compile to chip, I get
compiler message.jpg
compiler message.jpg (164.74 KiB) Viewed 12288 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

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 interrupt in dsPIC

Post 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
}

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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
can rx interrupt 2.jpg (50.37 KiB) Viewed 12274 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
compiler message 2.jpg (96.54 KiB) Viewed 12274 times

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 interrupt in dsPIC

Post 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
}
}

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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 !!
ID_KING.c
(59.32 KiB) Downloaded 339 times
ID_KING.fcf_pic16
(13.5 KiB) Downloaded 281 times

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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!!

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 interrupt in dsPIC

Post 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
 }
}

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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
can rx interrupt 3.jpg (42.03 KiB) Viewed 12224 times
and this was the message from compiler
compiler message 3.jpg
compiler message 3.jpg (175.29 KiB) Viewed 12224 times


Am I forgetting some step? maybe in the project options: "custom C code" ? or something in the "Chip Configure" ?

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 interrupt in dsPIC

Post 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.

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post by r_romeo »

They are there, just the window is not showing it, I double checked :(

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 interrupt in dsPIC

Post 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.
Attachments
CAN_RX_INT.fcf_pic16
(4.5 KiB) Downloaded 253 times

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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
can-test-1-tx.fcf_pic16
(7.5 KiB) Downloaded 228 times
can-test-3-rx-interrupt.fcf_pic16
(11.5 KiB) Downloaded 260 times
Is a problem with my configuration (probably ) ? or is a bug in flowcode?

thanks again for your support

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 interrupt in dsPIC

Post 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.

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post by r_romeo »

thanks Ben, that works in getting the data, but not in the RX ID, that's weird

Any idea ?

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post by r_romeo »

Any idea?

Mikat
Posts: 258
Joined: Wed Nov 29, 2006 6:32 pm
Location: Finland
Has thanked: 7 times
Been thanked: 36 times
Contact:

Re: Can interrupt in dsPIC

Post 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

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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
can-test-3-rx-interrupt.fcf_pic16
(10 KiB) Downloaded 207 times
can-test-1-tx.fcf_pic16
(7.5 KiB) Downloaded 166 times

Mikat
Posts: 258
Joined: Wed Nov 29, 2006 6:32 pm
Location: Finland
Has thanked: 7 times
Been thanked: 36 times
Contact:

Re: Can interrupt in dsPIC

Post 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 :shock: )...

Mika

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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

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 interrupt in dsPIC

Post 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.

Mikat
Posts: 258
Joined: Wed Nov 29, 2006 6:32 pm
Location: Finland
Has thanked: 7 times
Been thanked: 36 times
Contact:

Re: Can interrupt in dsPIC

Post 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

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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

Mikat
Posts: 258
Joined: Wed Nov 29, 2006 6:32 pm
Location: Finland
Has thanked: 7 times
Been thanked: 36 times
Contact:

Re: Can interrupt in dsPIC

Post 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

r_romeo
Posts: 54
Joined: Wed Nov 02, 2011 3:57 pm
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Can interrupt in dsPIC

Post 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

Mikat
Posts: 258
Joined: Wed Nov 29, 2006 6:32 pm
Location: Finland
Has thanked: 7 times
Been thanked: 36 times
Contact:

Re: Can interrupt in dsPIC

Post 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

Post Reply