📄 cantest.c
字号:
s=peekb(SEG,base+SR);
if((s&0x0c)!=0x0c || (s&0xc0)!=0) return ERROR;
frame[1]=(send_msg.id & 7)<<5;
frame[1]=frame[1]+send_msg.dlen ;
s=((send_msg.rtr&0x01)?1:0);
frame[1]=frame[1] | (s<<4);
frame[0]=(BYTE)((send_msg.id>>3) & 0x00ff);
for(i=0;i<2;i++) /*write frames*/
pokeb(SEG,base+TXB+i,frame[i]);
for(i=0;i<send_msg.dlen;i++)
pokeb(SEG,base+TXB+2+i,send_msg.data[i]);
pokeb(SEG,base+CMR,0x01); /*request transmite*/
for(i=0;i<2;i++);
do{
s=peekb(SEG,base+SR);
if(s & 0xc0) return ERROR;
}while(!(s&0x08)); /* transmit completely*/
return OK;
}
int ErrorHandle(UI base)
{
BYTE s;
s=peekb(SEG,base+SR);
if(s&0x80)
{
s=peekb(SEG,base+IR);
pokeb(SEG,base+MOD,0x1a);
}
else
{
s=peekb(SEG,base+IR);
if(s&0x08)
pokeb(SEG,base+CMR,0x0c);
else
{
if(s&0x01)
return OK;
}
}
return ERROR;
}
/* receive message */
int canReceiveMsg(BYTE port,MSG_STRUCT *msg_ptr)
{
UI base;
BYTE frame[2];
int i;
base=port*0x200;
i=peekb(SEG,base+SR);
if(ErrorHandle(base)==ERROR)
return ERROR;
frame[0]=peekb(SEG,base+RXB);
frame[1]=peekb(SEG,base+RXB+1);
msg_ptr->rtr=((frame[1]&0x10)?1:0);
msg_ptr->dlen=frame[1]&0x0f;
msg_ptr->id=(frame[0]&0x00ff)<<3 | frame[1]>>5;
for(i=0;i<8;i++)
msg_ptr->data[i]=peekb(SEG,base+RXB+2+i);
pokeb(SEG,base+CMR,0x04); /*release receive buffer*/
return OK;
}
int intCanReceiveMsg(BYTE port,MSG_STRUCT *msg_ptr)
{
int i;
int result;
result=0;
switch (port)
{
case 0:
if(intail1!=inhead1)
{
msg_ptr->id=InBuffer1[inhead1].id;
msg_ptr->rtr=InBuffer1[inhead1].rtr;
msg_ptr->dlen =InBuffer1[inhead1].dlen;
for(i=0;i<8;i++)
msg_ptr->data[i]=InBuffer1[inhead1].data[i];
if(++inhead1==IBUFLEN) inhead1=0;
result=1;
}
break;
case 1:
if(intail2!=inhead2)
{
msg_ptr->id=InBuffer2[inhead2].id;
msg_ptr->rtr=InBuffer2[inhead2].rtr;
msg_ptr->dlen =InBuffer2[inhead2].dlen;
for(i=0;i<8;i++)
msg_ptr->data[i]=InBuffer2[inhead2].data[i];
if(++inhead2==IBUFLEN) inhead2=0;
result=1;
}
break;
}
return result;
}
/* install can's interrupt service's program*/
void install(BYTE j1,BYTE j2)
{
oldport1=getvect(j1);
oldport2=getvect(j2);
if(j1!=0) setvect(j1,port1);
if(j2!=0) setvect(j2,port2);
}
/*uninstall can's interrupt service program */
void uninstall()
{
setvect(vect(P_IRQ1),oldport1);
setvect(vect(P_IRQ2),oldport2);
}
/*according to user's config ,8259's mask code is set*/
void init_8259(BYTE k1,BYTE k2)
{
BYTE c,c1,c2 ;
UI t,t1,t2;
if(k1!=0){
t1=0x0001;
t1=t1<<k1;
}
else t1=0x0000;
t1=~t1;
if(k2!=0){
t2=0x0001;
t2=t2<<k2;
}
else t2=0x0000;
t2=~t2;
t=t1&t2;
t1=t&0x00ff;
t2=t&0xff00;
t2=t2>>8;
c1=(BYTE)t1;
c2=(BYTE)t2;
if(c2!=0xff) c1=c1&0xfb;
M_main = inportb(0x21 ) ;
c = M_main & c1; /*7b original,bit2,7 enable */
outportb(0x21, c) ; /* Set the main 8259 mask bits */
M_slave = inportb(0xa1) ;
c = M_slave & c2 ; /* 61 original,bit9,10,11,12,15 enable */
outportb(0xa1, c) ; /* Set the slave 8259 mask bits */
}
/*recover old 8259's mask code */
void old_8259()
{
outportb(0x21,M_main);
outportb(0xa1,M_slave);
}
/*if there is receive-interrupt of can1,the flag_receive1 is set1 */
void interrupt port1()
{
BYTE frame[2],s;
int i;
s=peekb(SEG,CAN1+SR);
if(s&0x80)
{
s=peekb(SEG,CAN1+IR);
pokeb(SEG,CAN1+MOD,0x1a);
}
else
{
s=peekb(SEG,CAN1+IR);
if(s&0x08)
pokeb(SEG,CAN1+CMR,0x0c);
else
{
if(s&0x01)
{
frame[0]=peekb(SEG,CAN1+RXB);
frame[1]=peekb(SEG,CAN1+RXB+1);
InBuffer1[intail1].rtr=((frame[1]&0x10)?1:0);
InBuffer1[intail1].dlen=frame[1]&0x0f;
InBuffer1[intail1].id=(frame[0]&0x00ff)<<3 | frame[1]>>5;
for(i=0;i<8;i++)
InBuffer1[intail1].data[i]=peekb(SEG,CAN1+RXB+2+i);
pokeb(SEG,CAN1+CMR,0x04); /*release receive buffer*/
if(InBuffer1[intail1].rtr)
{
flagRtr1.id=InBuffer1[intail1].id;
flagRtr1.rtr=1;
flagRtr1.dlen=InBuffer1[intail1].dlen;
/*add your remote transmit request program here */
/*canSendMsg(BYTE port,MSG_STRUCT send_msg) */
}
else
{
if(++intail1==IBUFLEN) intail1=0;
}
}
}
}
outportb(0x20,0x20);
outportb(0xa0,0x20);
}
/*if there is receive-interrupt of can1,the flag_receive1 is set1 */
void interrupt port2()
{
BYTE frame[2],s;
int i;
s=peekb(SEG,CAN2+SR);
if(s&0x80)
{
s=peekb(SEG,CAN2+IR);
pokeb(SEG,CAN2+MOD,0x1a);
}
else
{
s=peekb(SEG,CAN2+IR);
if(s&0x08)
pokeb(SEG,CAN2+CMR,0x0c);
else
{
if(s&0x01)
{
frame[0]=peekb(SEG,CAN2+RXB);
frame[1]=peekb(SEG,CAN2+RXB+1);
InBuffer2[intail2].rtr=((frame[1]&0x10)?1:0);
InBuffer2[intail2].dlen=frame[1]&0x0f;
InBuffer2[intail2].id=(frame[0]&0x00ff)<<3 | frame[1]>>5;
for(i=0;i<8;i++)
InBuffer2[intail2].data[i]=peekb(SEG,CAN2+RXB+2+i);
pokeb(SEG,CAN2+CMR,0x04); /*release receive buffer*/
if(InBuffer2[intail2].rtr)
{
flagRtr2.id=InBuffer2[intail2].id;
flagRtr2.rtr=1;
flagRtr2.dlen=InBuffer2[intail2].dlen;
/*add your remote transmit request program here */
/*canSendMsg(BYTE port,MSG_STRUCT send_msg) */
}
else
{
if(++intail2==IBUFLEN) intail2=0;
}
}
}
}
outportb(0x20,0x20);
outportb(0xa0,0x20);
}
/*frome IRQ code to intrrupt vector */
int vect(BYTE IRQ)
{
int i;
switch(IRQ)
{
case 0:
i=0;
break;
case 3:
i=11;
break;
case 4:
i=12;
break;
case 5:
i=13;
break;
case 6:
i=14;
break;
case 7:
i=15;
break;
case 9:
i=0x71;
break;
case 10:
i=0x72;
break;
case 11:
i=0x73;
break;
case 12:
i=0x74;
break;
case 15:
i=0x77;
break;
defaut:
i=-1;
}
return(i);
}
void init8259()
{
int i;
outportb(0xa0,0x11);for(i=0;i<2;i++);
outportb(0xa1,0x70);for(i=0;i<2;i++);
outportb(0xa1,0x02);for(i=0;i<2;i++);
outportb(0xa1,0x01);for(i=0;i<2;i++);
outportb(0xa1,0xff);for(i=0;i<2;i++);
outportb(0xa0,0x20);for(i=0;i<2;i++);
outportb(0x20,0x11);for(i=0;i<2;i++);
outportb(0x21,0x08);for(i=0;i<2;i++);
outportb(0x21,0x04);for(i=0;i<2;i++);
outportb(0x21,0x01);for(i=0;i<2;i++);
outportb(0x20,0x20);for(i=0;i<2;i++);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -