mcp2510的can总线收发器程序.doc

上传人:scccc 文档编号:13880891 上传时间:2022-01-25 格式:DOC 页数:7 大小:96KB
返回 下载 相关 举报
mcp2510的can总线收发器程序.doc_第1页
第1页 / 共7页
mcp2510的can总线收发器程序.doc_第2页
第2页 / 共7页
mcp2510的can总线收发器程序.doc_第3页
第3页 / 共7页
mcp2510的can总线收发器程序.doc_第4页
第4页 / 共7页
mcp2510的can总线收发器程序.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《mcp2510的can总线收发器程序.doc》由会员分享,可在线阅读,更多相关《mcp2510的can总线收发器程序.doc(7页珍藏版)》请在三一文库上搜索。

1、mcp2510的can总线收发器程序pcbomb发表于2008-6-30 14:47:00阅读全文(769) |回复(1) |引用通告(0) |编辑# i nclude # i nclude mcp2510.h void mcp_reset(void)设置成SPI方式SPI_init_hw();/init_cano();/SPI_mcp_reset();/ void mcp_read( unsigned char MCPaddr, unsigned char* readdata, unsigned char length ) unsigned char loopCnt;SPI_mcp_sele

2、ct(); / Select the MCP device at the SPI bus/ Start reading and set first addressSPI_mcp_RD_address(MCPaddr);for (loopCnt=0; loopCnt length; loopCnt+)/ Get a byte and store at pointer*readdata = SPI_putch(MCPaddr);/ Increment the pointers to next location/ Test+;MCPadd 叶+;readdata+;SPI_mcp_unselect(

3、); void mcp_write( unsigned char MCPaddr, unsigned char* writedata, unsigned char length ) unsigned char loopCnt;SPI_mcp_select();/ Start write and set first addressSPI_mcp_WR_address( MCPaddr );for (loopCnt=0; loopCnt length; loopCnt+)/ Write a byteSPI_putch( *writedata );/ Increment the pointer to

4、 next location writedata+;SPI_mcp_unselect();void mcp_read_can_id( unsigned char mcp_addr, unsigned char* ext, unsigned long* can_id ) unsigned char tbufdata4;*ext = 0;*cand = 0;mcp_read( mcp_addr, tbufdata, 4);*can_id = (tbufdataSIDH5);if ( (tbufdataSIDL & TXB_EXIDE_M) = TXB_EXIDE_M )*can_id = (*ca

5、n_id2) + (tbufdataSIDL & 0x03);*cand = 16;*can_id = *can_id +(tbufdataEID8v8) + tbufdataEID0;*ext = 1;/*buffer:0-1*/void mcp_read_can( unsigned char buffer, unsigned char* ext, unsigned long* can_id, unsigned char* dlc, unsigned char* rtr, unsigned char* data )unsigned char mcp_addr = buffer*16 + 0x

6、61, ctrl;mcp_read_can_id( mcp_addr, ext, can_id );mcp_read( mcp_addr-1, &ctrl, 1 );mcp_read( mcp_addr+4, dlc, 1 );if (/*(*dlc & RTR_MASK) | */(ctrl & 0x08)*rtr = 1;else*rtr = 0;*dlc &= DLC_MASK;mcp_read( mcp_addr+5, data, *dlc );void mcp_write_can_id( unsigned char mcp_addr, unsigned char ext, unsig

7、ned long can_id )unsigned int canid;unsigned char tbufdata4;canid = (unsigned int)(can_id & OxOFFFF);if ( ext = 1)tbufdataEIDO = (unsigned char) (canid & 0xFF);tbufdataEID8 = (unsigned char) (canid / 256);canid = (unsigned int)( can_id / 0x10000 );tbufdataSIDL = (unsigned char) (canid & 0x03);tbufda

8、taSIDL += (unsigned char) (canid & 0x1C )*8);tbufdataSIDL |= TXB_EXIDE_M;tbufdataSIDH = (unsigned char) (canid / 32 );elsetbufdataSIDH = (unsigned char) (canid / 8 );tbufdataSIDL = (unsigned char) (canid & 0x07 )*32);tbufdataEID0 = 0;tbufdataEID8 = 0;mcp_write( mcp_addr, tbufdata, 4 );/*buffer:0-2*/

9、void mcp_write_can( unsigned char buffer, unsigned char ext, unsigned long can_id, unsigned char dlc, unsigned char rtr, unsigned char* data )unsigned char mcp_addr = buffer*16 + 0x31;mcp_write(mcp_add 叶5, data, dlc ); / write data bytes mcp_write_can_id( mcp_addr, ext, cand ); / write CAN id if ( r

10、tr = 1)dlc |= RTR_MASK; / if RTR set bit in bytemcp_write(mcp_add 叶4), & dlc, 1 );/ write the RTR and DLC/*检测是否发送完毕,是否已经可以进行发送*unsigned char mcp_transmit_busy(unsigned send_buf) unsigned char temp;switch(send_buf)case 0:temp=SPI_mcp_RD_status();if(temp&0x04)return 1;return 0;break;case 1:temp=SPI_mc

11、p_RD_status();if(temp&0x10)return 1;return 0;break;case 2:temp=SPI_mcp_RD_status();if(temp&0 x40)return 1;return 0;break;读取当前的MCP251C发送状态读取当前的MCP251C发送状态读取当前的MCP251C发送状态*发送数据*void mcp_transmit(unsigned char mcp_addr)SPI_mcp_write_bits(mcp_addr, TXB_TXREQ_M, TXB_TXREQ_M);# include spi_mcp.h void SPI_

12、mcp_select (void) CAN_CS_c;void SPI_mcp_unselect (void) CAN_CS_s;void SPI_mcp_reset ( void )-SPI_mcp_select(); SPI_putch(RESET);SPI_mcp_unselect(); -void SPI_mcp_RD_address(unsigned char output)unsigned char store; CAN_CS_c;store = SPI_putch( READ );/ Write the command/ store = SPI_putch( 0x00 ); /

13、Write high byte in address store = SPI_putch( output ); / Write low byte in address unsigned char SPI_mcp_RD_data(unsigned char output) unsigned char store; CAN_CS_c;store = SPI_putch( READ );/ Write the command/ store = SPI_putch( 0x00 ); / Write high byte in address store = SPI_putch( output ); /

14、Write low byte in address store = SPI_putch( output ); / Write low byte in address CAN_CS_s;return store;void SPI_mcp_WR_address(unsigned char output)-unsigned char store; CAN_CS_c;store = SPI_putch( WRITE ); / Write the command / store = SPI_putch( 0x00 ); / Write high byte in address store = SPI_p

15、utch( output ); / Write low byte in addressvoid SPI_mcp_WR_data(unsigned char output,unsigned char data) unsigned char store;CAN_CS_c;store = SPI_putch( WRITE ); / Write the command/ store = SPI_putch( 0x00 ); / Write high byte in address store = SPI_putch( output ); / Write low byte in address stor

16、e = SPI_putch( data );CAN_CS_s;unsigned char SPI_mcp_RD_status ( void )unsigned char store;CAN_CS_c;store = SPI_putch( RD_STAT ); / Write the commandstore = SPI_putch( RD_STAT ); / Write any byte to get a byte in return CAN_CS_s;return store; / Command done OKunsigned char SPI_mcp_RD_RX_STAT ( void

17、)unsigned char store;CAN_CS_c;store = SPI_putch( RD_RX_STAT ); / Write the commandstore = SPI_putch( RD_RX_STAT ); / Write any byte to get a byte in return CAN_CS_s;return store; / Command done OKvoid SPI_mcp_write_bits( unsigned char MCPaddr, unsigned char data, unsigned char mask ) CAN_CS_c;SPI_putch( BIT_MOD );SPI_putch( MCPaddr );SPI_putch( mask ); SPI_putch( data );CAN_CS_s;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1