指针的一种用法(优化代码)
时间:11-29
来源:互联网
点击:
/************************************************************************
函数名 :unsigned char CAN_Send_dat(CAN_DAT *can_tmp,unsigned char pir)功能 :can发送参数 :*can_tmp 数据包 pir: 优先级 0~3返回 :1->缓冲池满需要等待下次装入发送缓冲池 0->成功放入缓冲池************************************************************************/unsigned char CAN_Send_dat(CAN_DAT *can_tmp,unsigned char pir){unsigned char n,flag;unsigned char temp;unsigned char *P_addr,*P_addr_temp;flag = 0;if((TXB0CON&0x08)==0){P_addr = (unsigned char*)(&TXB0CON);flag = 1;}if((TXB1CON&0x08)==0){P_addr = (unsigned char*)(&TXB1CON);flag = 1;}if((TXB2CON&0x08)==0){P_addr = (unsigned char*)(&TXB2CON);flag = 1;}if(flag==1){P_addr_temp = P_addr;*P_addr = pir;P_addr++;if(can_tmp->IDR_RTR&0x02)//扩展标识符{*P_addr = ((can_tmp->ID[2]>>5)|(can_tmp->ID[3]<3));temp = can_tmp->ID[2]&0x03;temp|=((can_tmp->ID[2]&0x1C)<3);temp|= 0x08;P_addr++;*P_addr = temp;P_addr++;*P_addr = can_tmp->ID[1];P_addr++;*P_addr = can_tmp->ID[0];P_addr++;}else{*P_addr = ((can_tmp->ID[1]<5)|(can_tmp->ID[0]>>3));;P_addr++;*P_addr = (can_tmp->ID[0]<5);P_addr+=3;}*P_addr = can_tmp->length;P_addr++;for(n=0;nlength;n++){*P_addr =can_tmp->DATA[n];P_addr++;}*P_addr_temp |= 0x08;return 0;}return 1;}/************************************************************************函数名 :unsigned char CAN_Send_dat(CAN_DAT *can_tmp,unsigned char pir)功能 :can发送参数 :*can_tmp 数据包 pir: 优先级 0~3返回 :1->缓冲池满需要等待下次装入发送缓冲池 0->成功放入缓冲池************************************************************************/unsigned char CAN_Send_dat(CAN_DAT *can_tmp,unsigned char pir){unsigned char n;unsigned char temp;unsigned char *P_addr;if((TXB0CON&0x08)==0){// TXB0CON&=0xfc;//bit3=0标准标识符,bit3=1扩展标识符,bit7-5:标准标识符2-0位,bit7-5 扩展ID[16-17]TXB0CON = pir;//|= pir; //设置TXB0为最高优先级3if(can_tmp->IDR_RTR&0x02)//扩展标识符{TXB0EIDL = can_tmp->ID[0];//扩展ID[0-7]TXB0EIDH = can_tmp->ID[1];//扩展ID[8-15]TXB0SIDL = can_tmp->ID[2]&0x03;//扩展ID[16-17]temp= ((can_tmp->ID[2]&0x1C)<3);TXB0SIDL |= temp;//标准[0-3]TXB0SIDL |= 0x08;TXB0SIDH=((can_tmp->ID[2]>>5)|(can_tmp->ID[3]<3));//标准标识符的10-3位}else{TXB0SIDL = (can_tmp->ID[0]<5);//标准[0-3]TXB0SIDH = ((can_tmp->ID[1]<5)|(can_tmp->ID[0]>>3));}//if(can_tmp->IDR_RTR&0x01)can_tmp->length|=0x40;TXB0DLC=can_tmp->length;P_addr = &TXB0D0;for(n=0;n length;n++){*P_addr =can_tmp->DATA[n];P_addr++;}TXB0CON=TXB0CON|0x08; /* bit3(TXREQ)=1请求发送 */return 0;}if((TXB1CON&0x08)==0){// TXB1CON&=0xfc;TXB1CON = pir;//|= pir;if(can_tmp->IDR_RTR&0x02)//扩展标识符{TXB1EIDL = can_tmp->ID[0];//扩展ID[0-7]TXB1EIDH = can_tmp->ID[1];//扩展ID[8-15]TXB1SIDL = can_tmp->ID[2]&0x03;//扩展ID[16-17]temp= ((can_tmp->ID[2]&0x1C)<3);TXB1SIDL |= temp;//标准[0-3]TXB1SIDL |= 0x08;TXB1SIDH=((can_tmp->ID[2]>>5)|(can_tmp->ID[3]<3));//标准标识符的10-3位}else{TXB1SIDL = (can_tmp->ID[0]<5);//标准[0-3]TXB1SIDH = ((can_tmp->ID[1]<5)|(can_tmp->ID[0]>>3));}//if(can_tmp->IDR_RTR&0x01)can_tmp->length|=0x40;TXB1DLC=can_tmp->length;P_addr = &TXB1D0;for(n=0;n length;n++){*P_addr =can_tmp->DATA[n];P_addr++;}TXB1CON=TXB1CON|0x08; return 0;}if((TXB2CON&0x08)==0){//TXB2CON&=0xfc;TXB2CON = pir;if(can_tmp->IDR_RTR&0x02)//扩展标识符{TXB2EIDL = can_tmp->ID[0];//扩展ID[0-7]TXB2EIDH = can_tmp->ID[1];//扩展ID[8-15]TXB2SIDL = can_tmp->ID[2]&0x03;//扩展ID[16-17]temp= ((can_tmp->ID[2]&0x1C)<3);TXB2SIDL |= temp;//标准[0-3]TXB2SIDL |= 0x08;TXB2SIDH=((can_tmp->ID[2]>>5)|(can_tmp->ID[3]<3));//标准标识符的10-3位}else{TXB2SIDL = (can_tmp->ID[0]<5);//标准[0-3]TXB2SIDH = (can_tmp->ID[1]<5|(can_tmp->ID[0]>>3));}// if(can_tmp->IDR_RTR&0x01)can_tmp->length|=0x40;TXB2DLC=can_tmp->length;P_addr = &TXB2D0;for(n=0;n length;n++){*P_addr =can_tmp->DATA[n];P_addr++;}TXB2CON=TXB2CON|0x08; return 0;}return 1;}
指针优化代 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)