struct MOb { unsigned long id; unsigned char data [8]; }; void can_init (void); void can_tx (struct MOb msg); void can_rx (struct MOb msg); void can_init (void) { unsigned char i,j; //reset CAN interface CANGCON |= (1<baudrate // bit timing -> datasheet 264 (check table) // 250Kbps 16MHz cpu-clk CANBT1 = 0x0E;//16M/8=2M CANBT2 = 0x04;//3QT CANBT3 = 0x13;//2+2=4 // clear CAN interrupt registers CANGIE = 0; // none interrupts CANIE1 = 0; // none interrupts on MObs CANIE2 = 0; CANSIT1 = 0; CANSIT2 = 0; //start CAN interface CANGCON = (1<<1); //wait until module ready while (!(CANGSTA & (1< >8); CANIDT2 = (unsigned char) (msg.id>>16); CANIDT1 = (unsigned char) (msg.id>>24); //put data in mailbox for (i=0; i<8; i++) CANMSG = msg.data [i]; //enable transmission CANCDMOB |= (1< > 5); CANIDT2 = (unsigned char)(msg.id >> 13); CANIDT1 = (unsigned char)(msg.id >> 21); // set mask in order to receive only the message with the ID CANIDM4 = 248; CANIDM3 = 255; CANIDM2 = 255; CANIDM1 = 255; // enable extended ID CANIDM4 |= (1< >= 3; }
上一篇:我的电脑串口控制继电器,终于成功了!
下一篇:基于单片机的温度水位远程测控系统
推荐阅读最新更新时间:2024-03-16 14:24