📄 inter_can_t.c
字号:
/////////can 交互通讯发送程序//////////////
/////////inter_can_t.c////////////////////
#include "candeal1.h"
#define LEN 8
#define MSGNUMA 2
#define MSGNUMB 5
#define MSGNUMC 6
#define MSGNUMD 7
#define NUMAPEND 0x0002
#define NUMBPEND 0x0010
xdata uchar rr1[LEN];
xdata uchar rr2[LEN];
union longchar{
long tempval;
struct {uchar hi1;uchar hi0;uchar low1;uchar low0;}bytek;
};
union floatchar{
float tempval;
struct {uchar hi1;uchar hi0;uchar low1;uchar low0;}bytek;
};
xdata union intchar tcfint;
xdata union longchar tcflong;
xdata union floatchar tcffloat;
bit isnewdata_A,isnewdata_B;
void canini(){
clear_msg_objects();
init_msg_object_RX (MSGNUMA,5);//初始化发送
init_msg_object_RX (MSGNUMB,6);
init_msg_object_TX (MSGNUMC,2);
init_msg_object_TX (MSGNUMD,4);
EIE2 |= 0x20;
EA = 1;
SFRPAGE = CAN0_PAGE;
CAN0CN=0X41;
CAN0ADR=BITREG;
CAN0DAT=0X34c0;//调波特率
start_CAN();
}
void delay1ms(uint time){//延迟1ms*time,这不是一个精确值
uint i;
uint j;
for (i=0;i<time;i++){
for(j=0;j<300;j++);
}
}
void float2ch(float f,uchar *p){//用指针扩展性比较强,但是运行时间比较长
tcffloat.tempval=f;
*p++=tcffloat.bytek.low0;
*p++=tcffloat.bytek.low1;
*p++=tcffloat.bytek.hi0;
*p=tcffloat.bytek.hi1;
}
void long2ch(long l,uchar *p){
tcflong.tempval=l;
*p++=tcflong.bytek.low0;
*p++=tcflong.bytek.low1;
*p++=tcflong.bytek.hi0;
*p=tcflong.bytek.hi1;
}
float ch2float(uchar *p){
tcffloat.bytek.low0=*p++;
tcffloat.bytek.low1=*p++;
tcffloat.bytek.hi0=*p++;
tcffloat.bytek.hi1=*p;
return tcffloat.tempval;
}
long ch2long(uchar *p){
tcflong.bytek.low0=*p++;
tcflong.bytek.low1=*p++;
tcflong.bytek.hi0=*p++;
tcflong.bytek.hi1=*p;
return tcflong.tempval;
}
void config(){
//看门狗禁止
WDTCN = 0x07;
WDTCN = 0xDE;
WDTCN = 0xAD;
SFRPAGE = 0x0F;
//交叉开关使能,但没有进行外围设备配置
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x40;
XBR3 = 0x00;
//管脚输出配置,P0口为开漏输出,其中P0.6接上拉电阻,P0为数字输入口
SFRPAGE = 0x0F;
P0MDOUT = 0x00;
P1MDIN = 0xFF;
//晶振配置,采用内部晶振8分频
SFRPAGE = 0x0F;
CLKSEL = 0x00;
OSCXCN = 0x00;
OSCICN = 0x84;
}
main(){
xdata float ff;
xdata long ll;
xdata float fff[2];
xdata long lll[2];
uchar i;
xdata uchar ss1[LEN];
xdata uchar ss2[LEN];
config();
for(i=0;i<LEN;i++){
rr1[i]=0;
rr2[i]=0;
}
canini();
float2ch(3.14159,&ss1[0]);
long2ch(1234567L,&ss1[4]);
float2ch(1.23456,&ss2[0]);
long2ch(987654L,&ss2[4]);
transmit (MSGNUMC,ss1,LEN);
transmit (MSGNUMD,ss2,LEN);
isnewdata_A=0;
isnewdata_B=0;
while(1){
if(isnewdata_A){
isnewdata_A=0;
ff=ch2float(rr1);
ll=ch2long(&rr1[4]);
fff[0]=ff;
lll[0]=ll;
}
if(isnewdata_B){
isnewdata_B=0;
ff=ch2float(rr2);
ll=ch2long(&rr2[4]);
fff[1]=ff;
lll[1]=ll;
}
}
}
void ISRname (void) interrupt 19
{ uint temp;
temppage=SFRPAGE;
SFRPAGE = CAN0_PAGE;
status = CAN0STA;
if((status&0x10) != 0){
CAN0STA&=0xef;
CAN0ADR=INTPEND1;
temp=CAN0DAT;
if((temp&NUMAPEND)!=0){
receive_data (MSGNUMA,rr1,LEN);
isnewdata_A=1;
}
if((temp&NUMBPEND)!=0){
receive_data (MSGNUMB,rr2,LEN);
isnewdata_B=1;
}
}
SFRPAGE=temppage;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -