我设计的CAN总线模块
源代码在线查看: 24c01-24c16读写驱动程序.txt
#define scl P12
#define sda P11
#include
/******************************************************/
//24c01-24c16读写驱动程序,
nop()
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
void s24(void) {nop();_nop_();scl=0;sda=1;scl=1;_nop_();sda=0;_nop_();scl=0;}
void p24(void) {nop();sda=0;scl=1;_nop_();sda=1;}
unsigned char rd24(void) {
unsigned char o=0;nop();sda=1;
scl=1; if(sda){o=o|0x80;} scl=0;
scl=1; if(sda){o=o|0x40;} scl=0;
scl=1; if(sda){o=o|0x20;} scl=0;
scl=1; if(sda){o=o|0x10;} scl=0;
scl=1; if(sda){o=o|0x08;} scl=0;
scl=1; if(sda){o=o|0x04;} scl=0;
scl=1; if(sda){o=o|0x02;} scl=0;
scl=1; if(sda){o=o|0x01;} scl=0;
sda=1;scl=1;scl=0;
return(o);}
void wd24(unsigned char dd) {nop();
sda=(dd&0x80); scl=1; scl=0;
sda=(dd&0x40); scl=1; scl=0;
sda=(dd&0x20); scl=1; scl=0;
sda=(dd&0x10); scl=1; scl=0;
sda=(dd&0x08); scl=1; scl=0;
sda=(dd&0x04); scl=1; scl=0;
sda=(dd&0x02); scl=1; scl=0;
sda=(dd&0x01); scl=1; scl=0;
sda=1; scl=1;}
unsigned char read(unsigned int address){
unsigned char dd;dd=((address&0x7ff)/256)
s24();wd24(0xa0|dd);scl=0;wd24(address);scl=0;
s24();wd24(0xa1|dd);scl=0;dd=rd24();p24();return(dd);}
void write(unsigned int address,unsigned char dd){
unsigned char ddd;ddd=((address&0x7ff)/256) s24();wd24(0xa0|ddd);scl=0;wd24(address);scl=0;wd24(dd);scl=0;p24();}
/******************************************************/