Từng bước tiếp cận và chinh phục giao thức CAN của dsPIC (30F4012)
Thời gian vừa qua tôi tìm cách làm giao thức CAN nhưng tìm trên PICVN thấy cũng không nhiều lắm và có vẻ như cũng chưa có tiền bối nào dạy cụ thể việc này, sau một thời gian mò mẫm tôi đã làm được cho 2 em dsPIC30f4012 nói chuyện được với nhau (tất nhiên là có sự trợ giúp của 2 em MCP2551) nên tôi mở ra mục này để nếu ai quan tâm có thể cùng tham gia và tiếp cận nhanh hơn với CAN trên dsPIC mà ở đây tôi chọn ví dụ là con 30F4012
Đầu tiên nếu bạn quan tâm, xin hãy đọc bài cơ bản về CAN tại đây : http://www.picvietnam.com/forum/showthread.php?t=1804 và hãy đọc về họ dsPIC30F đặc biệt là phần về CAN http://ww1.microchip.com/downloads/e...Doc/70046E.pdf đừng quên đọc về em dsPIC30f0412 Và em MCP2551 nữa nhé sau đó chúng ta sẽ tiếp tục làm việc.... (tôi mặc định là các bạn đã biết dùng MPLab C30) |
Phần cứng hết sức đơn giản nên tôi sẽ ko post lên nữa, bạn cứ nối TXD và RXD của dsPIC và MCP2551 lại với nhau, còn CANH và CANL của MCP2551 thì nối vào bus CANH và CANL là xong.
Phần thiết lập cấu hình cho CAN như sau: Code:
// Init Can Protocol Family Reference Manual phần các thanh ghi của CAN, nếu có gì chưa rõ, xin cứ đặt câu hỏi trực tiếp tại đây, ta sẽ cùng trao đổi :) |
còn đây là code của chương trình main và xử lý ngắt của CAN
Code:
#include <p30F4012.h> |
rất mong nhận được phản hồi và trao đổi cùng các bạn có quan tâm đến vấn đề này
|
Bạn ơi, bạn có thể viết tiếp ko, tôi cũng đang tìm hiểu về vấn đề này! Mong cả anh Namnq tham gia cho bọn em được nhờ !
|
Trích:
|
uh, thank bạn nhiều nhiều. Cậu có thể nói dõ hơn cho mình là đoạn ctr chính thực hiện công việc cụ thể gì không ? với cái phần cứng thì có cần điện trở định thiên và mạch xén đỉnh không ? Bài viết của bạn đã giúp mình rất nhiều !
|
if(BlueLedCounter>120000)
{ BlueLedCounter=0; if(Switch==0) { C1TX1B1 = OutData1[0]; C1TX1B2 = OutData1[1]; C1TX1CONbits.TXREQ = 1; } else{ C1TX0B1 = OutData0[0]; C1TX0B2 = OutData0[1]; C1TX0B3 = OutData0[2]; C1TX0B4 = OutData0[3]; C1TX0CONbits.TXREQ = 1; } } chương trình chính của mình rất đơn giản là cứ sau khoảng 1/2s thì đưa dữ liệu vào bộ đệm C1TX1B1 = OutData1[0]; C1TX1B2 = OutData1[1]; và yêu cầu truyền C1TX1CONbits.TXREQ = 1; còn câu hỏi còn lại của bạn mình ko hiểu lắm. |
em cung dang tim hieu ve mang CAN, cho e hoi dinh nghia nay trong Code
#define Switch _RE2 |
Switch quyết định dùng Buffer truyền 0(Dữ liệu được nạp có độ dài 8 byte) hoặc Buffer1(4 Byte) .Sau khi Frame dữ liệu được truyền thành công thì xảy ra một ngắt.Tại chương trình xử lý ngắt , dspic nhận tin và nếu tin nhắn được nhận vào Buffer 0 thì đèn red sáng ,còn được nhận và Buffer 1 thì đèn blue sang. Vậy theo chương trình của a ,sau khi chạy thì cứ sau khoảng 1/2s thì tương ứng với mỗi vi điều khiển lại có một đèn sáng(đỏ hoặc xanh) đúng không ạ!
|
Trích:
|
Trước hết em thay mặt những ngừơi lười cảm ơn anh rất nhiều, không có a không biết phải mò đến bao giờ!
Trong chương trình khởi tạo CAN ,khi khởi tạo bộ đệm 0 và 1 a khởi tạo là C1RX0CON = C1RX1CON = 0x0000; Như vậy bit FILHIT của hai thanh ghi trên đều xác lập cho phép bộ loc 0,và khi khởi tạo cho bộ lọc: C1RXF0SID = 0x0AA8; //CAN1 Receive Acceptance Filter0 SID C1RXF2SID = 0x1555; //CAN1 Receive Acceptance Filter2 SID C1RXF2EIDH = 0x0004; //CAN1 Receive Acceptace Filter2 Extended Identifier high byte C1RXF2EIDL = 0x8C00; //CAN1 Receive Acceptance Filter2 Extended identifier low byte tại sao lại sử dụng cả bộ lọc 2 ,em nghĩ bit DBEN khi được set sử dụng luôn bộ lọc 0 cho bộ đệm 1? Tiện thể a có thể nói rõ hơn một chút về chức năng của bộ lọc và mặt nạ được ko, e có đọc qua tài liệu nhưng hãy còn mơ hồ quá? Cảm ơn a rất nhiều! |
Bạn hãy xem sơ đồ cấu trúc của Can trong tài liệu về dòng 30F http://ww1.microchip.com/downloads/e...Doc/70046E.pdf của Pic hình 23-8
Filter 0 và 1 dùng để lọc và cho vào RX0, còn 2,3,4,5 là dùng để lọc cho vào RX1 ( như thế có nghĩa là cùng lúc có thể tồn tại 2 bộ lọc trên 1 pic, bộ lọc này chính là so sánh đoạn đầu của frame dữ liệu gửi tới để xem có cho vào bộ đệm nhận RX và sinh ngắt hay không, bạn nên đọc lại bài cơ bản về Can đã có link ở phần đầu để hiểu rõ hơn nhé. :) |
A cho em hỏi,chỉ xét đến bộ đệm nhận và bộ đệm truyền 0 cho đơn giản nhé!A chọn mặt nạ là 23 bit 1
C1RXM0SID = 0x1FFD; C1RXM0EIDH = 0x0FFF; C1RXM0EIDL = 0xFC00; , tức là bộ lọc 0 sẽ xét toàn bộ 23 bit của ID tin nhận đến Bộ lọc trùng với ID ,data sẽ được đưa vào bộ đệm 0.Vậy e không muốn tin này nhận được theo trương trình của a e chỉ việc thay đổi thanh ghi này đúng không? C1RXF0SID = 0x0AA8; //CAN1 Receive Acceptance Filter0 SID Ví dụ thành:C1RXF0SID = 0x0AAA; Em hỏi thế vì hình như e thay đổi thanh ghi này Dữ liệu vẫn được nhận."dữ liệu sau đó được e truyền lên qua UART" |
Xin lỗi vì lâu tôi không để ý, tưởng là chưa có ai quan tâm vấn đề này nên chưa trả lời câu hỏi trên cho bạn.
thứ nhất, tôi đang dùng ở chế độ nhận cơ bản ( standard data frame) nên là sẽ có mặt nạ là 11 bit. "Bộ lọc trùng với ID ,data sẽ được đưa vào bộ đệm 0.Vậy e không muốn tin này nhận được theo trương trình của a e chỉ việc thay đổi thanh ghi này đúng không?" Tôi cho rằng bạn chưa thực sự hiểu sự khác biệt giữa mặt nạ và bộ lọc . bộ lọc thì để lọc còn mặt nạ là cái cho phép lọc ở những bit nào. bạn hãy tham khảo thêm mục 23.6.2 Message Acceptance Filters trang 674 của tài liệu dsPIC30F Family Reference Manual để hiểu rõ hơn nhé. |
Trích:
Mình dốt nặc Kĩ thuật nên ... mong bạn đừng cười. Mình đang lam CAN nhưng gặp khó khăn. |
Em có thể tổng kết lại 1 cách cơ bản mối liên hệ giữa identifier, mask, và filter nhé. Các bác xem có gì sai xót thì sửa cho em. Em tham khảo tài liệu của dsPIC33F
identifier: có 2 loại là standard và extend (ở trong message của CAN bus) mask : có 3 thằng mask được cấu hình bởi C1RXMiSID với standard và C1RXMiEID với extend (i =0,1,2) filter: Có 16 thằng filter .Mỗi thằng filter gồm 2 thanh ghi cho standard id và extend id C1RXFiSID,C1RXFiSED với i từ 0 đến 15, Ví dụ: - Về cơ bản truyển nhận CAN: Node truyền gửi bản tin với 11 bít standard id (29 với extend). Các node nhận bóc lấy 11 bít này, so sánh từng bít với filter...Tại vị trí bít đó mà mask bằng 1, nếu bít của filter bằng bít của gói tin đuợc gửi đến...accepts, còn ko thì reject. Nếu mask = 0, thì accept tất mà không phụ thuộc vào filter của node nhận hay id của message tới. Phần này em đọc ở chỗ Mask truth table - Cấu hình thằng nhận: + b1: Chọn mask (3 thằng) nào cho thằng filter nào (15 thằng). Gồm 2 thanh C1FMSKSEL1,C1FMSKSEL2 Code:
C1FMSKSEL1bits.F0MSK=0x0; //mask 0 cho filter 0 Code:
C1RXM0SIDbits.SID = 0x7FF;//cấu hình mask0 cho standard id Code:
C1RXF0SIDbits.SID = 0x01D0; //cấu hình filter0 cho standard id Khi truyền ECAN: Làm thế nào để biết đường truyền rỗi? Nếu em muốn test ECAN của dsPIC33, em nối như sau : dspic33 ->mcp2551, đấu đường CAN_L và CAN_H với nhau, rồi xét ở mode normal hoặc loopback, thì có được không? |
Em copy nguyên code của bác mà không thấy nó truyền được. Em đã test thử nếu đổi về chế độ loopback thì nó truyền ngon, nhưng mà đổi sang chế độ normal nó không thấy truyền được.
Sau khi bit TXREQ được set lên thì cứ = 1 mãi. chắc là không truyền được. không hiểu có lỗi gì nữa nhỉ hic. |
anh hopeman à, em copy code cua anh vào dich bằng C30 (hình như anh ko cùng các init_ADC10 phải không ạ?) thì nó hiện ra các cái lỗi như sau, em thấy nhiều quá nên ko biết mình còn sót cái j nữa ko? Anh xem giùm em nhé!
Debug build of project `C:\Documents and Settings\PHAMQUOCVIET\Desktop\CAN_C30\CAN_4012_tes t.mcp' started. Language tool versions: pic30-as.exe v3.11, pic30-gcc.exe v3.11, pic30-ld.exe v3.11, pic30-ar.exe v3.11 Preprocessor symbol `__DEBUG' is defined. Thu May 06 14:49:41 2010 ---------------------------------------------------------------------- Make: The target "C:\Documents and Settings\PHAMQUOCVIET\Desktop\CAN_C30\Code_CAN_C30 _interrupt.o" is out of date. Executing: "C:\Program Files\Microchip\MPLAB C30\bin\pic30-gcc.exe" -mcpu=30F4012 -x c -c "Code_CAN_C30_interrupt.c" -o"Code_CAN_C30_interrupt.o" -D__DEBUG -g -Wall Code_CAN_C30_interrupt.c:25: error: syntax error before '{' token Code_CAN_C30_interrupt.c:39: warning: type defaults to 'int' in declaration of 'C1CFG2' Code_CAN_C30_interrupt.c:39: error: conflicting type qualifiers for 'C1CFG2' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1849: error: previous declaration of 'C1CFG2' was here Code_CAN_C30_interrupt.c:39: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:45: warning: type defaults to 'int' in declaration of 'C1INTF' Code_CAN_C30_interrupt.c:45: error: conflicting type qualifiers for 'C1INTF' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1863: error: previous declaration of 'C1INTF' was here Code_CAN_C30_interrupt.c:45: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:46: error: syntax error before '.' token Code_CAN_C30_interrupt.c:47: warning: type defaults to 'int' in declaration of 'C1INTE' Code_CAN_C30_interrupt.c:47: error: conflicting type qualifiers for 'C1INTE' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1885: error: previous declaration of 'C1INTE' was here Code_CAN_C30_interrupt.c:47: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:48: error: syntax error before '.' token Code_CAN_C30_interrupt.c:56: warning: type defaults to 'int' in declaration of 'C1RX0CON' Code_CAN_C30_interrupt.c:56: error: conflicting type qualifiers for 'C1RX0CON' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1820: error: previous declaration of 'C1RX0CON' was here Code_CAN_C30_interrupt.c:56: error: initializer element is not constant Code_CAN_C30_interrupt.c:56: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:62: warning: type defaults to 'int' in declaration of 'C1RXM0SID' Code_CAN_C30_interrupt.c:62: error: conflicting type qualifiers for 'C1RXM0SID' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1591: error: previous declaration of 'C1RXM0SID' was here Code_CAN_C30_interrupt.c:62: error: initializer element is not constant Code_CAN_C30_interrupt.c:62: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:66: warning: type defaults to 'int' in declaration of 'C1RXM0EIDH' Code_CAN_C30_interrupt.c:66: error: conflicting type qualifiers for 'C1RXM0EIDH' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1595: error: previous declaration of 'C1RXM0EIDH' was here Code_CAN_C30_interrupt.c:66: error: initializer element is not constant Code_CAN_C30_interrupt.c:66: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:70: warning: type defaults to 'int' in declaration of 'C1RXM0EIDL' Code_CAN_C30_interrupt.c:70: error: conflicting type qualifiers for 'C1RXM0EIDL' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1598: error: previous declaration of 'C1RXM0EIDL' was here Code_CAN_C30_interrupt.c:70: error: initializer element is not constant Code_CAN_C30_interrupt.c:70: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:75: warning: type defaults to 'int' in declaration of 'C1RXF0SID' Code_CAN_C30_interrupt.c:75: error: conflicting type qualifiers for 'C1RXF0SID' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1523: error: previous declaration of 'C1RXF0SID' was here Code_CAN_C30_interrupt.c:75: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:76: warning: type defaults to 'int' in declaration of 'C1RXF2SID' Code_CAN_C30_interrupt.c:76: error: conflicting type qualifiers for 'C1RXF2SID' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1543: error: previous declaration of 'C1RXF2SID' was here Code_CAN_C30_interrupt.c:76: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:77: warning: type defaults to 'int' in declaration of 'C1RXF2EIDH' Code_CAN_C30_interrupt.c:77: error: conflicting type qualifiers for 'C1RXF2EIDH' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1547: error: previous declaration of 'C1RXF2EIDH' was here Code_CAN_C30_interrupt.c:77: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:78: warning: type defaults to 'int' in declaration of 'C1RXF2EIDL' Code_CAN_C30_interrupt.c:78: error: conflicting type qualifiers for 'C1RXF2EIDL' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1550: error: previous declaration of 'C1RXF2EIDL' was here Code_CAN_C30_interrupt.c:78: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:84: warning: type defaults to 'int' in declaration of 'C1TX0CON' Code_CAN_C30_interrupt.c:84: error: conflicting type qualifiers for 'C1TX0CON' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1728: error: previous declaration of 'C1TX0CON' was here Code_CAN_C30_interrupt.c:84: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:85: warning: type defaults to 'int' in declaration of 'C1TX0SID' Code_CAN_C30_interrupt.c:85: error: conflicting type qualifiers for 'C1TX0SID' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1704: error: previous declaration of 'C1TX0SID' was here Code_CAN_C30_interrupt.c:85: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:86: warning: type defaults to 'int' in declaration of 'C1TX0EID' Code_CAN_C30_interrupt.c:86: error: conflicting type qualifiers for 'C1TX0EID' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1708: error: previous declaration of 'C1TX0EID' was here Code_CAN_C30_interrupt.c:86: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:87: warning: type defaults to 'int' in declaration of 'C1TX0DLC' Code_CAN_C30_interrupt.c:87: error: conflicting type qualifiers for 'C1TX0DLC' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1712: error: previous declaration of 'C1TX0DLC' was here Code_CAN_C30_interrupt.c:87: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:91: warning: type defaults to 'int' in declaration of 'C1TX1CON' Code_CAN_C30_interrupt.c:91: error: conflicting type qualifiers for 'C1TX1CON' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1700: error: previous declaration of 'C1TX1CON' was here Code_CAN_C30_interrupt.c:91: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:92: warning: type defaults to 'int' in declaration of 'C1TX1SID' Code_CAN_C30_interrupt.c:92: error: conflicting type qualifiers for 'C1TX1SID' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1676: error: previous declaration of 'C1TX1SID' was here Code_CAN_C30_interrupt.c:92: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:93: warning: type defaults to 'int' in declaration of 'C1TX1EID' Code_CAN_C30_interrupt.c:93: error: conflicting type qualifiers for 'C1TX1EID' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1680: error: previous declaration of 'C1TX1EID' was here Code_CAN_C30_interrupt.c:93: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:94: warning: type defaults to 'int' in declaration of 'C1TX1DLC' Code_CAN_C30_interrupt.c:94: error: conflicting type qualifiers for 'C1TX1DLC' c:/program files/microchip/mplab c30/bin/bin/../../support/dsPIC30F/h/p30F4012.h:1684: error: previous declaration of 'C1TX1DLC' was here Code_CAN_C30_interrupt.c:94: warning: data definition has no type or storage class Code_CAN_C30_interrupt.c:97: error: syntax error before '.' token Code_CAN_C30_interrupt.c:222:2: warning: no newline at end of file Halting build on first failure as requested. ---------------------------------------------------------------------- Debug build of project `C:\Documents and Settings\PHAMQUOCVIET\Desktop\CAN_C30\CAN_4012_tes t.mcp' failed. Language tool versions: pic30-as.exe v3.11, pic30-gcc.exe v3.11, pic30-ld.exe v3.11, pic30-ar.exe v3.11 Preprocessor symbol `__DEBUG' is defined. Thu May 06 14:49:44 2010 ---------------------------------------------------------------------- BUILD FAILED |
nhờ giúp về giao thức CAN
chào các anh
kỳ nạỳ e làm đồ án: truyền và nhận dữ liệu trong mang can sữ dụng vi xử lý của microchip, dùng protect để mô phỏng hiển thị nhiệt độ cảm biến được. e không biết cách đấu mắc các chân của pic tới mcp 2551 và hiên thị ra màn hình lcd như thế nào các anh có thể làm ơn hướng dẫn giúp em vẽ 1 mạch nguyên lý với cảm ơn anh nhiều |
nhờ giúp về giao thức CAN
cảm ơn anh
thật sự đây là lần tiên e tìm hiểu về can, mà tài liệu tiếng việt thì hiếm quá. ở trường thì em chỉ quen dùng ngôn ngữ assembly, trong chương trình anh viết em không hiểu được, mong anh thông cảm nha e đang học tại đà nẵng, kỳ này em đang làm đồ án về: truyền và nhận dữ liệu trong mạng can sử dụng vi xử lý của microchip em hơi kẹt không biết làm thế nào để viết chương trình cho giao thức CAN này anh có thể làm ơn chỉ cho em ít đường cơ bản được không a mong sớm được hồi âm của anh thân chào |
các bác cho em hỏi:
- khi truyền message thì chỉ có ID là dùng để nhận dạng message đó từ đâu tới thôi phải không? hay còn có phần nào khác nữa? - một con trong mạng CAN có thể nhận message từ bao nhiêu con trong mạng vậy? bởi em định làm mạng CAN gồm 10-16 con giao tiếp với nhau. Mong các bác giúp em. Cảm ơn nhiều!! |
hú hú có ai ko? :'(
|
Múi giờ GMT. Hiện tại là 12:32 PM. |
Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam