|
|
View previous topic :: View next topic |
Author |
Message |
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
Can Bus Help? |
Posted: Mon Nov 29, 2010 12:57 am |
|
|
I am studying about the can bus. Working on CCS can bus module.
I want to ask a question that
Code: |
#include <18F4580.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#device ICD=TRUE
#use delay(clock=20000000)
#include <can-18xxx8.c>
#define WRITE_REGISTER_D_ID 0x400 //ID for NODE D
void write_7_segment(int value)
{
const int lcd_seg[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
//change the value from GP0 to GP5 and put high on GP6
int buffer[3];
buffer[0]=0x1E; //output lacth for the GP pins.
buffer[1]=0x7F; //caused GP7 to be unchanged
buffer[2]=lcd_seg[value];
[b]can_putd(write_REGISTER_D_ID, buffer, 3,1, TRUE, FALSE);[/b]
}
void main()
{
int i=0;
can_init();
can_putd(0x400,0,0,1,TRUE, FALSE);
delay_ms(1000);
while(TRUE)
{
write_7_segment(i);
delay_ms(1000);
if(++i==10)
i=0;
}
}
|
can_putd(write_REGISTER_D_ID, buffer, 3,1, TRUE, FALSE);
In this line write_REGISTER_D_ID is the standard ID.
Want to ask that the Buffer change the control ID. DLC bits of canbus frame and what 3 , 1 , True, False means?
Don't understand that statement. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 29, 2010 2:11 am |
|
|
Look at the function definition of can_putd() in this file:
c:\program files\picc\drivers\can-18xxx8.c
There are comments above the function definition that explain all the
parameters used by the function. |
|
|
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
|
Posted: Mon Nov 29, 2010 5:35 am |
|
|
thanks man now i understand its meaning.
I did the next exercise. Few problems i am facing in it.
Code: |
#include <18F4580.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#device ICD=TRUE
#use delay(clock=20000000)
#include <can-18xxx8.c>
#define WRITE_REGISTER_C_ID 0x300
enum colors {RED=0,YELLOW=1,GREEN=2}; //enum: creates a list of integer constants.
void write_c_led(colors led, short on)
{
int buffer[3];
buffer[0]=0x1E; //output lach for GP pins of MCP250xx
buffer[1]=0x02<<led;
if(on)
buffer[2]=0;
else
buffer[2]=0xFF;
while
(!(can_putd(WRITE_REGISTER_C_ID, buffer, 3, 1, TRUE, FALSE)));
}
void main()
{
int i=0;
can_init();
can_putd(0x100,0,0,1,TRUE, FALSE);
delay_ms(1000);
while(TRUE)
{
write_c_led(GREEN,i>1);
write_c_led(YELLOW,i>4);
write_c_led(RED,i>7);
delay_ms(1000);
if(++i==10)
i=0;
}
}
|
Void write_c_led(colors led, short on)
{
int buffer[3];
buffer[0]=0x1E; //output lach for GP pins of MCP250xx
buffer[1]=0x02<<led;
if(on)
buffer[2]=0;
else
buffer[2]=0xFF;
while
(!(can_putd(WRITE_REGISTER_C_ID, buffer, 3, 1, TRUE, FALSE)));
}
over here i don't understand the
buffer[1]=0x02<<led;
and why put this ! negative operator before the can_putd. |
|
|
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
|
Posted: Mon Nov 29, 2010 6:30 am |
|
|
sarmad_101 wrote: | thanks man now i understand its meaning.
I did the next exercise. Few problems i am facing in it.
Void write_c_led(colors led, short on)
{
int buffer[3];
buffer[0]=0x1E; //output lach for GP pins of MCP250xx
buffer[1]=0x02<<led;
if(on)
buffer[2]=0;
else
buffer[2]=0xFF;
while (!(can_putd(WRITE_REGISTER_C_ID, buffer, 3, 1, TRUE, FALSE)));
}
over here i don't understand the
buffer[1]=0x02<<led;
and why put this ! negative operator before the can_putd. |
This isn't really a forum for how to learn to code in C but here it is quickly:
buffer[1]=0x02<<led; means to assign to buffer[1] the value 2 shifted upward by 'led' number of binary places. Basically every shift left/up multiples by two. So if you take the value 2 and shift it zero times you have a value of 2. If you shift it once you get 2 * 2 = 4. If you shift it twice you get 2 * 2 * 2 = 8.
Then, the ! operator means "not" so line means to keep trying to use the can_putd function until it returns true. The line basically reads "while can_putd is NOT true try it again"
You should look into a good book on C and read up on binary shifting and logic statements. |
|
|
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
|
Posted: Mon Nov 29, 2010 6:35 am |
|
|
sorry forks...!!!
I got the thing just a min before your post by finding CCS C Reference manual and your post clarify it more.
Thanks a lot. |
|
|
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
|
Posted: Tue Nov 30, 2010 5:48 am |
|
|
I made this program to check the can put_d command
Code: |
#include <18F4580.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#device ICD=TRUE
#use delay(clock=20000000)
#include <can-18xxx8.c>
#define WRITE_REGISTER_C_ID 0x300
void main()
{
can_init(); // initializes the CAN bus
can_putd(0x100,0,0,1,TRUE, FALSE);
//send an on-bus message
//to wake up mcp250x0's
//wait for node c to power-up
while(TRUE)
{
int buffer[3];
buffer[0]=0x1E; // output lach for GP pins of MCP250xx
buffer[1]=0xFF;
buffer[2]=0x01;
while
(!(can_putd(WRITE_REGISTER_C_ID, buffer, 3, 1, TRUE, FALSE))); // keep try until it returns true
delay_ms(1000);
buffer[0]=0x1E; // output lach for GP pins of MCP250xx
buffer[1]=0xFF;
buffer[2]=0x0F;
while
(!(can_putd(WRITE_REGISTER_C_ID, buffer, 3, 1, TRUE, FALSE)));
delay_ms(1000);
}
}
|
The one thing i don't understand is about the buffer[1].
If i put it 0xFF the circuit runs. If i made this 0x00 or other circuit stops.
Why is that so.
Or if i don't take 3 buffer take only two the same thing happened. No output. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 30, 2010 1:19 pm |
|
|
Quote: | If i put it 0xFF the circuit runs. If i made this 0x00 or other circuit stops.
Why is that so. |
We have no idea what circuit or board you are using. We are not
mind-readers. If you bought the board, post a link to the documentation
for it. If you built the board, post a link to a schematic for it. |
|
|
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 02, 2010 6:40 pm |
|
|
You're using the CCS CAN Bus development board:
http://www.ccsinfo.com/product_info.php?products_id=CANbuskit
To write to the GPIO register, you have to send 3 bytes. These are the
address of the GPIO register, a bitmask to show which bits in the GPIO
register are to be changed, and the value to be written to the GPIO register.
If you set the 'mask' parameter to 0x00, you are telling the MCP25050
not to change any of the bits in the GPIO register. A '1' bit in the mask
means the bit should be changed (set to the value specified in that bit
position in the 3rd byte).
To fix this, you need to decide what bits you want to change, and edit
the mask value to show this. For example, to change bit 2 (bits are
numbered 0 to 7), you would set the mask to this: 0b00000100
This is 0x04 in hex. |
|
|
|
|
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
|