PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Tìm Kiếm Bài Trong Ngày Ðánh Dấu Ðã Ðọc Vi điều khiển

Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 07-02-2012, 06:37 PM   #1
anhduongbk52
Nhập môn đệ tử
 
Tham gia ngày: Jul 2010
Bài gửi: 1
:
Trong file 18f4580.c có định nghĩa hàm:
void can_init(void) {
can_set_mode(CAN_OP_CONFIG); //must be in config mode before params can be set
can_set_baud();
curfunmode=CAN_FUN_OP_LEGACY;

// RXB0CON
// filthit0=0
// jtoff=0
// rxb0dben=1 buffer zero will overflow into buffer one
// rxrtrro=0
// rxm1:0=0 will recive all valid IDs
RXB0CON=0;
RXB0CON.rxm=CAN_RX_VALID;
RXB0CON.rxb0dben=CAN_USE_RX_DOUBLE_BUFFER;
RXB1CON=RXB0CON;

CIOCON.endrhi=CAN_ENABLE_DRIVE_HIGH;
CIOCON.cancap=CAN_ENABLE_CAN_CAPTURE;
CIOCON.tx2src=CAN_CANTX2_SOURCE; //added 3/30/09 for PIC18F6585/8585/6680/8680
CIOCON.tx2en=CAN_ENABLE_CANTX2; //added 3/30/09 for PIC18F6585/8585/6680/8680

can_set_id(RX0MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID); //set mask 0
can_set_id(RXFILTER0, 0, CAN_USE_EXTENDED_ID); //set filter 0 of mask 0
can_set_id(RXFILTER1, 0, CAN_USE_EXTENDED_ID); //set filter 1 of mask 0

can_set_id(RX1MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID); //set mask 1
can_set_id(RXFILTER2, 0, CAN_USE_EXTENDED_ID); //set filter 0 of mask 1
can_set_id(RXFILTER3, 0, CAN_USE_EXTENDED_ID); //set filter 1 of mask 1
can_set_id(RXFILTER4, 0, CAN_USE_EXTENDED_ID); //set filter 2 of mask 1
can_set_id(RXFILTER5, 0, CAN_USE_EXTENDED_ID); //set filter 3 of mask 1

// set dynamic filters
can_set_id(RXFILTER6, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER7, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER8, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER9, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER10, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER11, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER12, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER13, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER14, 0, CAN_USE_EXTENDED_ID);
can_set_id(RXFILTER15, 0, CAN_USE_EXTENDED_ID);

set_tris_b((*0xF93 & 0xFB ) | 0x08); //b3 is out, b2 is in

can_set_mode(CAN_OP_NORMAL);
}




Hàm này gọi ra hàm con là void can_set_mode(CAN_OP_MODE);

Trong lần gọi đầu can_set_mode(CAN_OP_CONFIG); //must be in config mode before params can be set
Thì CONFIG thành công
Còn khi trả về chế độ normal: can_set_mode(CAN_OP_NORMAL);
thì chương trình nhảy liên tục tại

void can_set_mode(CAN_OP_MODE mode) {
CANCON.reqop=mode;
while( (CANSTAT.opmode) != mode );
}
không thoát ra được.
Khi mô phỏng với MPLAB SIM thì ra thế này:
http://s1236.photobucket.com/albums/...untitled-1.jpg

Nạp vào mạch thì Các ứng dụng khác vẫn chạy, chỉ không giao tiếp được Can thôi.
Mong các anh chị giúp đỡ.Em xin cảm ơn
File Kèm Theo
File Type: rar CANPROBLEM.rar (1.5 KB, 53 lần tải)

thay đổi nội dung bởi: anhduongbk52, 07-02-2012 lúc 06:57 PM.
anhduongbk52 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời

Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 02:23 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam