View previous topic :: View next topic |
Author |
Message |
ssrodrigo
Joined: 29 May 2009 Posts: 7
|
16F88+MCP2515 - How to test? |
Posted: Fri May 29, 2009 3:20 pm |
|
|
Hello guys! I need some help to finish my college´s final project. I´m trying to do a test using CAN protocol. I need to send and receive messages but I´m not finding how to program the PIC 16F88 with MCP2515 to do this. I already tested the LED blink and it works fine. How can I do that? I have two hardwares as show the image:
Thanks in advance! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ssrodrigo
Joined: 29 May 2009 Posts: 7
|
Thanks |
Posted: Fri May 29, 2009 4:12 pm |
|
|
Ok, I´ll test and let you know if it works.
Thanks a lot! |
|
|
ssrodrigo
Joined: 29 May 2009 Posts: 7
|
Files can-mcp251x.h and can-mcp251x.c |
Posted: Fri May 29, 2009 5:32 pm |
|
|
When I open the files "can-mcp251x.h" and "can-mcp251x.c" it shows on the files definitions: "can-mcp2510.c ////
//// CAN Library routines for Microchip's MCP2510" and not "can-mcp251x.c" and the first lines of the programs is: #include <can-mcp2510.h>
//IO pins connected to MCP2510"... Is that right???? I have to change all the parameters manually to mcp251x ???
Thanks in advance |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ssrodrigo
Joined: 29 May 2009 Posts: 7
|
Great! |
Posted: Fri May 29, 2009 5:44 pm |
|
|
According to this document, the pins of 2510 and 2515 are the same!
One more time... thank you! |
|
|
ssrodrigo
Joined: 29 May 2009 Posts: 7
|
Erros when compile |
Posted: Fri May 29, 2009 7:49 pm |
|
|
When I try to compile the code:
Code: | #include <16F88.h>
//#device ICD=TRUE, ADC=10
#fuses XT, NOWDT, NOPROTECT, NOLVP, PUT, BROWNOUT // DEBUG
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)
//#define CAN_DO_DEBUG 1
#include <can-mcp251x.c>
void main(void) {
int32 can_id;
int can_data[8];
int can_length, counter;
struct rx_stat rxstat;
can_init();
can_set_mode(CAN_OP_LOOPBACK);
counter = 0;
puts("Starting");
can_data[0] = 0x55;
while(1)
{
if(kbhit())
{
getch();
if(can_putd(42, can_data, 1, 3, TRUE, FALSE))
puts("tx ok");
while(!can_kbhit());
if(can_getd(can_id, &can_data[0], can_length, rxstat))
puts("rx ok");
counter++;
}
}
} |
I´m getting these errors:
*** Error 100 "C:\Arquivos de programas\PICC\Examples\testloop.c" Line 5(5,66): USE parameter value is out of range Not a number: PIN_C6
*** Error 12 "C:\Arquivos de programas\PICC\Examples\testloop.c" Line 19(1,5): Undefined identifier puts
*** Error 12 "C:\Arquivos de programas\PICC\Examples\testloop.c" Line 24(4,9): Undefined identifier kbhit
*** Error 12 "C:\Arquivos de programas\PICC\Examples\testloop.c" Line 26(1,6): Undefined identifier getch
*** Error 12 "C:\Arquivos de programas\PICC\Examples\testloop.c" Line 29(1,5): Undefined identifier puts
*** Error 12 "C:\Arquivos de programas\PICC\Examples\testloop.c" Line 33(1,5): Undefined identifier puts
Why???? This code I made a copy of the link that you sended do me and modify it using 16f88 and can-mcp251x.c. Even when I try to compile the code without any modification, I receive the same errors... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 29, 2009 8:38 pm |
|
|
PIC's do not all have the UART Tx and Rx signals on the same pins.
Go to the Data Sheet Finder page (bookmark this page) and download
the 16F88 data sheet:
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2046
Find the pins used for Tx and Rx on the 16F88. Edit the #use rs232()
statement and put in those pins, instead of PIN_C6 and PIN_C7. |
|
|
ssrodrigo
Joined: 29 May 2009 Posts: 7
|
Right! |
Posted: Fri May 29, 2009 9:33 pm |
|
|
Now I try the line:
Code: | #use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B2, ERRORS) |
and the compile runs ok! I'll only be able to go to electronic lab on
monday to test with the hardware
As soon as I test the .hex file I come back here to give the ok... I hope so.
Thanks a lot! Regards |
|
|
ssrodrigo
Joined: 29 May 2009 Posts: 7
|
A new problem... |
Posted: Thu Jun 04, 2009 11:23 pm |
|
|
I tested with the hardware and I was able to test the send/receive
messages using the CAN protocol but... now, I'm having a new problem...
If I keep inside the program that´s inside of the hardware that receives
the messages the line
Code: | #use rs232(baud=9600, xmit=PIN_B5,rcv=PIN_B2, ERRORS) |
the test does not work anymore...
If we look at the drawing on the first post, we can check that the 16F88
only has one pin TX and one pin RX. So, to use the MCP2515 and the
serial port at the same time, we inserted a jumper (S3 on the draw).
When the pins 1 and 2 are closed, the CAN is activate and when the pins
2 and 3 are closed, we activate the serial port. I need to insert in the
program that receives the messages some function that turn on the
RS232 after the PIC has all the messages ok (these messages are saved
inside of a vector, as show on the program). Is that possible? I hope so...
Here is the program that we are using:
Code: | #include <16F88.h>
//#device ICD=TRUE, ADC=10
#fuses HS, NOWDT, NOPROTECT, NOLVP, PUT, BROWNOUT // DEBUG
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B2)
//#define CAN_DO_DEBUG 1
#include <can-mcp251x.c>
#define BOARD1_ID 24
#define BOARD2_ID 25
void pisca3();
void main(void) {
int x, cont;
int32 can_id;
int can_data[8];
int buffer[90];
int can_length;
struct rx_stat rxstat;
can_init();
set_tris_a(0);
set_tris_b(0);
setup_comparator(NC_NC_NC_NC);
cont = 0;
while (1) {
delay_ms(20);
if(can_getd(can_id, &can_data[0], can_length, rxstat)) {
if (can_id == BOARD2_ID) {
output_high(PIN_A4);
buffer[cont] = can_data[0];
cont++;
if (cont == 90) break;
}
}
delay_ms(20);
output_low(PIN_A4);
}
//continua depois do break...
pisca3();
for (x=0; x<10; x++) {
delay_ms(1000);
}
pisca3();
for (x=0;x < 90; x++) {
putc(buffer[cont]);
}
}
void pisca3() {
delay_ms(300);
output_high(PIN_A4);
delay_ms(300);
output_low(PIN_A4);
delay_ms(300);
output_high(PIN_A4);
delay_ms(300);
output_low(PIN_A4);
delay_ms(300);
output_high(PIN_A4);
delay_ms(300);
output_low(PIN_A4);
}
|
Is there another way to use the RS232 without use the line
Code: | #use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B2) | ?
Sorry about this BIG post, but I REALLY need to finish this project!
Thanks in advance,
Regards |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 05, 2009 1:20 pm |
|
|
You can specify different pins and use a software UART. Or, you could
use software SPI (on different pins).
In fact, the CCS MCP251x driver does use software SPI. So, just change
the pins. The #define statements for the pins are given at the top of the
driver file.
You could give up one of your switches and use its pin for SDO. You
could put a jumper wire between S3-1 and the left side of R8. This would
move SDO to the RA2 pin on the PIC. Then jumper S3-2 and S3-3
together to use the hardware Rx pin for the serial port. |
|
|
|