![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
quad3
Joined: 23 Jan 2007 Posts: 5
|
CAN - can_putd |
Posted: Tue Jun 05, 2007 5:41 am |
|
|
Hi,
I'm trying to get 2 PIC to communicate with each other via CAN.
The ouput from 1 PIC is from PC (character from keyboard) using this char
Code: | char out_data;
out_data = getchar();
printf("\r\n\n you pressed %c", out_data); |
but data in CAN messages are int
Code: | int1 can_putd(int32 id, int * data, int len, int priority, int1 ext, int1 rtr) {
int i;
int * txd0;
int port; |
My question is, how do I display the correct data at the receiving node? For example if I key in the letter 'a' at keyboard, the letter 'a' will be displayed at the receiver's terminal.
Here are the two codes that I'm using:
1. Transceiver
Code: | #include <18F458.h>
#fuses XT,NOPROTECT,NOLVP,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#include <can-18xxx8.c>
int16 ms;
#int_timer2
void isr_timer2(void) {
ms++; //keep a running timer that increments every milli-second
}
#define SEND_TO_PORT_B 0x201
#define SEND_TO_PORT_C 0x202
void main() {
char out_data;
int1 tx_rtr=1;
int1 tx_ext=0;
int tx_len=8;
int tx_pri=3;
int i;
printf("\r\n\r\nCCS CAN EXAMPLE\r\n");
setup_timer_2(T2_DIV_BY_4,79,16);
can_init();
can_set_mode(CAN_OP_NORMAL);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
printf("\r\nRunning...");
while(TRUE)
{
if ( can_tbe() && (ms > 500))
{
printf("\r\n\Transmit buffer ready");
out_data = getchar();
printf("\r\n\n you pressed %c", out_data);
ms=0;
//Data for Port B
if (out_data=='a' || out_data=='b' || out_data=='c'){
i=can_putd(SEND_TO_PORT_B, out_data, tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
if (i != 0xFF) { //success, a transmit buffer was open
printf("\r\nSend data to Port B");
printf("\r\nPUT %U: ID=%LU LEN=%U ", i, SEND_TO_PORT_B, tx_len);
printf("PRI=%U EXT=%U RTR=%U\r\n DATA = ", tx_pri, tx_ext, tx_rtr);
for (i=0;i<tx_len;i++) {
printf("%c ",out_data);
}
printf("\r\n");
}
else { //fail, no transmit buffer was open
printf("\r\nFAIL on PUTD\r\n");
}
}
//Data for Port C
if (out_data=='d' || out_data=='e' || out_data=='f'){
i=can_putd(SEND_TO_PORT_C, out_data, tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
if (i != 0xFF) { //success, a transmit buffer was open
printf("\r\nSend data to Port B");
printf("\r\nPUT %U: ID=%LU LEN=%U ", i, SEND_TO_PORT_C, tx_len);
printf("PRI=%U EXT=%U RTR=%U\r\n DATA = ", tx_pri, tx_ext, tx_rtr);
for (i=0;i<tx_len;i++) {
printf("%c ",out_data);
}
printf("\r\n");
}
else { //fail, no transmit buffer was open
printf("\r\nFAIL on PUTD\r\n");
}
}
}
}
} |
2. Receiver
Code: | #include <18F458.h>
#fuses XT,NOPROTECT,NOLVP,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#include <can-18xxx8.c>
int16 ms;
int i;
#int_timer2
void isr_timer2(void) {
ms++; //keep a running timer that increments every milli-second
}
#define DATA_FROM_A 0x201
void main() {
struct rx_stat rxstat;
int32 rx_id;
char in_data[8];
int rx_len;
printf("\r\n\r\nCCS CAN EXAMPLE\r\n");
setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
can_init();
can_set_mode(CAN_OP_NORMAL);
enable_interrupts(INT_TIMER2); //enable timer2 interrupt
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
printf("\r\nRunning...");
while(TRUE)
{
if ( can_kbhit() ) //if data is waiting in buffer...
{
printf("\r\nData in receive buffer");
if(can_getd(rx_id, &in_data[0], rx_len, rxstat)) { //...then get data from buffer
if (rx_id == DATA_FROM_A) {
printf("\r\nGOT: BUFF=%U ID=%LU LEN=%U OVF=%U ", rxstat.buffer, rx_id, rx_len, rxstat.err_ovfl);
printf("FILT=%U RTR=%U EXT=%U INV=%U", rxstat.filthit, rxstat.rtr, rxstat.ext, rxstat.inv);
printf("\r\n DATA = ");
for (i=0;i<rx_len;i++) {
printf("%c ",in_data[i]);
}
printf("\r\n");
}
}
else {
printf("\r\nFAIL on GETD\r\n");
}
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 05, 2007 11:08 pm |
|
|
Quote: |
char out_data;
i=can_putd(SEND_TO_PORT_B, out_data, tx_len,tx_pri,tx_ext,tx_rtr);
i=can_putd(SEND_TO_PORT_C, out_data, tx_len,tx_pri,tx_ext,tx_rtr);
|
The 2nd parameter in can_putd() is a pointer. Look at the source code
for can_putd() in this file: c:\program files\picc\drivers\can-18xxx8.c
Here's what it says:
Quote: |
int1 can_putd(int32 id, int * data, int len, int priority, int1 ext, int1 rtr)
// Parameters:
// id - ID to transmit data as
// data - pointer to data to send
// len - length of data to send
// priority - priority of message. The higher the number, the
// sooner the CAN peripheral will send the message.
// Numbers 0 through 3 are valid.
// ext - TRUE to use an extended ID, FALSE if not
// rtr - TRUE to set the RTR (request) bit in the ID, false if NOT
//
// Returns:
// If successful, it will return TRUE
// If un-successful, will return FALSE
|
In addition to that, you're telling the driver that you have a buffer that
is 8 bytes in length, but your "buffer" is actually only one char:
Quote: |
char out_data;
int1 tx_rtr=1;
int1 tx_ext=0;
int tx_len=8;
int tx_pri=3;
int i; |
Look at all the CCS examples for the CAN bus. Here are the filenames:
c:\program files\picc\examples\ex_can_ccs_b.c
c:\program files\picc\examples\ex_can.c
c:\program files\picc\examples\ex_can_ccs_a.c
Also, use the forum's search page to look for all threads that contain
either can_putd or can_getd. Study the examples. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|