Can interrupt in dsPIC
Moderator: Benj
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Can interrupt in dsPIC
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
also, if I try to compile to chip, I get
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
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
also, if I try to compile to chip, I get
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
- 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
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
}
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
}
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
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
looks that I have a syntax error in the IF command as the hex compilation tells me, but I don't know why !!!???
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
looks that I have a syntax error in the IF command as the hex compilation tells me, but I don't know why !!!???
- 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
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.
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
}
}
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
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 !!
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 !!
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
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!!
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!!
- 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
Hello,
Using this as your handler code seems to work correctly.
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
}
}
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
Hi Ben
that's weird man, still not passing the compiler stage
I entered the code you give me,
and this was the message from compiler
Am I forgetting some step? maybe in the project options: "custom C code" ? or something in the "Chip Configure" ?
that's weird man, still not passing the compiler stage
I entered the code you give me,
and this was the message from compiler
Am I forgetting some step? maybe in the project options: "custom C code" ? or something in the "Chip Configure" ?
- 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
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.
From the looks of it your just missing the semi colons from the end of the lines of code in the enable code section.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- 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
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
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
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
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
thanks Ben, that works in getting the data, but not in the RX ID, that's weird
Any idea ?
Any idea ?
-
- 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
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
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
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
"The two bytes are laid out as follows.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 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
-
- 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
So the id_lo is always 0 at the can bus, not just the software at the rx end? How about the id_hi.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
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);
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
![Shocked :shock:](./images/smilies/icon_eek.gif)
Mika
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
The SET ID always has worked for me , now the "weird " behave is just after the interrupt .
The ID HI is also 0
The ID HI is also 0
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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
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...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.
I have 30f6012 with in couple of days, and I need CAN too, so probably I need to play with same things then...
Mika
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
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
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
-
- 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
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
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
-
- Posts: 54
- Joined: Wed Nov 02, 2011 3:57 pm
- Has thanked: 14 times
- Been thanked: 3 times
- Contact:
Re: Can interrupt in dsPIC
Yes, I can see the IDs and data with my analyzer, I know what I am sending but in the reception end is wrong
-
- 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
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
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