郑州大学信息工程学院DSP大作业参考.docx
《郑州大学信息工程学院DSP大作业参考.docx》由会员分享,可在线阅读,更多相关《郑州大学信息工程学院DSP大作业参考.docx(26页珍藏版)》请在冰豆网上搜索。
郑州大学信息工程学院DSP大作业参考
1.C语言事项矩阵相乘:
#include
voidmatrix(int**b,int**c,int**a,intnx,intny,intnk);
intmain()
{
inti,j,k,tmp;
intb_row,b_col;
intc_row,c_col;
inta_row,a_col;
int**b,**c,**a;
printf("输入B矩阵的行数列数以及C矩阵的列数\n");
scanf("%d%d",&b_row,&b_col,&c_col);
c_row=b_col;
a_row=b_row;
a_col=c_col;
a=(int**)malloc(sizeof(int*)*a_row);
for(j=0;ja[j]=(int*)malloc(sizeof(int)*a_col);}b=(int**)malloc(sizeof(int*)*b_row);for(j=0;jb[j]=(int*)malloc(sizeof(int)*b_col);}c=(int**)malloc(sizeof(int*)*c_row);for(j=0;jc[j]=(int*)malloc(sizeof(int)*c_col);}if(!c[c_row-1]){printf("noenoughtmemory\n");exit(0);}printf("输入B阵元素[%d][%d]\n",b_row,b_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);b[j][i]=tmp;}printf("输入C阵元素[%d][%d]\n",c_row,c_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);c[j][i]=tmp;}matrix(b,c,a,a_col,a_row,b_col);for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
a[j]=(int*)malloc(sizeof(int)*a_col);
}
b=(int**)malloc(sizeof(int*)*b_row);
for(j=0;jb[j]=(int*)malloc(sizeof(int)*b_col);}c=(int**)malloc(sizeof(int*)*c_row);for(j=0;jc[j]=(int*)malloc(sizeof(int)*c_col);}if(!c[c_row-1]){printf("noenoughtmemory\n");exit(0);}printf("输入B阵元素[%d][%d]\n",b_row,b_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);b[j][i]=tmp;}printf("输入C阵元素[%d][%d]\n",c_row,c_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);c[j][i]=tmp;}matrix(b,c,a,a_col,a_row,b_col);for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
b[j]=(int*)malloc(sizeof(int)*b_col);
c=(int**)malloc(sizeof(int*)*c_row);
for(j=0;jc[j]=(int*)malloc(sizeof(int)*c_col);}if(!c[c_row-1]){printf("noenoughtmemory\n");exit(0);}printf("输入B阵元素[%d][%d]\n",b_row,b_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);b[j][i]=tmp;}printf("输入C阵元素[%d][%d]\n",c_row,c_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);c[j][i]=tmp;}matrix(b,c,a,a_col,a_row,b_col);for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
c[j]=(int*)malloc(sizeof(int)*c_col);
if(!
c[c_row-1]){
printf("noenoughtmemory\n");exit(0);
printf("输入B阵元素[%d][%d]\n",b_row,b_col);
for(j=0;jfor(i=0;iscanf("%d",&tmp);b[j][i]=tmp;}printf("输入C阵元素[%d][%d]\n",c_row,c_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);c[j][i]=tmp;}matrix(b,c,a,a_col,a_row,b_col);for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
for(i=0;iscanf("%d",&tmp);b[j][i]=tmp;}printf("输入C阵元素[%d][%d]\n",c_row,c_col);for(j=0;jfor(i=0;iscanf("%d",&tmp);c[j][i]=tmp;}matrix(b,c,a,a_col,a_row,b_col);for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
scanf("%d",&tmp);
b[j][i]=tmp;
printf("输入C阵元素[%d][%d]\n",c_row,c_col);
for(j=0;jfor(i=0;iscanf("%d",&tmp);c[j][i]=tmp;}matrix(b,c,a,a_col,a_row,b_col);for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
for(i=0;iscanf("%d",&tmp);c[j][i]=tmp;}matrix(b,c,a,a_col,a_row,b_col);for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
c[j][i]=tmp;
matrix(b,c,a,a_col,a_row,b_col);
for(j=0;j{for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
for(i=0;iprintf("\n");system("PAUSE");}return0;}voidmatrix(int**b,int**c,int**a,intnx,intny,intnk){inti,j,k;for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
printf("\n");
system("PAUSE");
return0;
voidmatrix(int**b,int**c,int**a,intnx,intny,intnk)
inti,j,k;
for(j=0;jfor(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
for(j=0;jfor(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
for(i=0;ifor(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
for(k=0;k}};}2.C语言实现FFT:#include#include#includetypedefstd::complexcomplex;intN=16,N1=4;//N为总输入数,N1为总级数intr=0;complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出constdoublepi=3.141592653589793;intreverse_order(intN,inta)//倒序算法N为总级数(带入N1){intc[20],res=0;for(inti=N-1;i>=0;i--){if(a%2==0)c[i]=0;elsec[i]=1;a=a/2;res+=c[i]*pow(2,i);}returnres;}voidbutterfly(intlayer){complexw;intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数intc2=pow(2,layer-1);//本级蝶形算法包含的次数intc3=pow(2,layer);for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
};
2.C语言实现FFT:
typedefstd:
:
complexcomplex;
intN=16,N1=4;//N为总输入数,N1为总级数
intr=0;
complexa[64],b[64];//a[64]为每一级蝶形算法输入,b[64]为每一级蝶形算法的输出
constdoublepi=3.141592653589793;
intreverse_order(intN,inta)//倒序算法N为总级数(带入N1)
intc[20],res=0;
for(inti=N-1;i>=0;i--)
if(a%2==0)
c[i]=0;
else
c[i]=1;
a=a/2;
res+=c[i]*pow(2,i);
returnres;
voidbutterfly(intlayer)
complexw;
intc1=pow(2,N1-layer);//本级包含基本蝶形算法的次数
intc2=pow(2,layer-1);//本级蝶形算法包含的次数
intc3=pow(2,layer);
for(inti=0;i{for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
for(intj=0;j{r=j*pow(2,N1-layer);w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;}}for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
r=j*pow(2,N1-layer);
w=complex(cos(2*pi*r/N),-sin(2*pi*r/N));
b[j+i*c3]=a[j+i*c3]+a[j+c2+i*c3]*w;
b[j+c2+i*c3]=a[j+i*c3]-a[j+c2+i*c3]*w;
for(intk=0;k{a[k]=b[k];}}voidfft(){inti;for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
a[k]=b[k];
voidfft()
inti;
for(i=0;i{butterfly(i+1);}}voidmain(){inti;doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入doublenum2[64];doubleres;for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
butterfly(i+1);
voidmain()
doublenum1[64]={1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0};//输入
doublenum2[64];
doubleres;
for(i=0;i{num2[i]=num1[reverse_order(N1,i)];}for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
num2[i]=num1[reverse_order(N1,i)];
for(i=0;i{a[i]=complex(num2[i],0);}fft();for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
a[i]=complex(num2[i],0);
fft();
for(i=0;i{res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());printf("%f\t",X);}}3.看门狗软件实现方法:#include#includeunsignedinti,num;unsignedchartemp;voiddelay(unsignedintt);intmain(){num=0;P1=~(0x00);TMOD=0x10;TL1=0xB0;TH1=0x3C;ET1=1;EA=1; TF1=0; TR1=1; temp=0x01;for(i=0;i<8;i++){ P0=~temp; delay(100); temp<<=1; } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; while(1) { temp=0x80; for(i=0;i<8;i++) { P0=~temp; delay(100); temp>>=1; } } TR1=0; TL1=0xB0; TH1=0x3C; TF1=0; TR1=1; num=0; return0; } voiddelay(unsignedintt) { registerunsignedintbt; for(;t;t--) for(bt=0;bt<255;bt++); } voidint_T1()interrupt3using3 { num++; if(num==100) { P1=~(0x01); } }看门狗硬件实现电路:作业:1.结合实例说明“实时性”与“仿真”的概念。“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。2.汇编语言实现浮点数相乘:includeio32.inc.datamsg1byte'请输入第一个数字',13,10,0msg2byte'请输入第二个数字',13,10,0readbuf1byte256dup(0)readbuf2byte256dup(0)numbyte00h.codestart:xorecx,ecxxorebx,ebxmoveax,offsetmsg1calldispmsgmoveax,offsetreadbuf1callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf1cmpbyteptr[esi],'-'jzagain3again1:cmpbyteptr[esi],'.'jnzagain2pushecxincesidecedxagain2:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain1jmpdone1again3:incesiincnumdecedxjmpagain1done1:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpushebxpushedxxorecx,ecxxorebx,ebxmoveax,offsetmsg2calldispmsgmoveax,offsetreadbuf2callreadmsgmovedx,eaxpushedxmovesi,offsetreadbuf2cmpbyteptr[esi],'-'jzagain6again4:cmpbyteptr[esi],'.'jnzagain5pushecxincesidecedxagain5:incecxmoval,[esi]subal,30himulebx,10movzxeax,aladdebx,eaxdecedxincesicmpedx,00Hjnzagain4jmpdone2again6:incesiincnumdecedxjmpagain4done2:pushebxpopebxpopecxpopedxsubedx,ecxdecedxpopecxpopeaxaddecx,edximuleax,ebx movebx,1done3:imulebx,10dececxcmpecx,0jnzdone3movedx,0divebxcalldispuidmoveax,'.'calldispcmoveax,edxcalldispuidexit0endstart3.实现F2812的硬件看门狗设计:#include"DSP281x_Device.h"#include"DSP281x_Examples.h"interruptvoidwakeint_isr(void);Uint32WakeCount;Uint32LoopCount;voidmain(void){InitSysCtrl();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;EALLOW;PieVectTable.WAKEINT=&wakeint_isr;EDIS;WakeCount=0;LoopCount=0;EALLOW;SysCtrlRegs.SCSR=BIT1;EDIS;PieCtrlRegs.PIECRTL.bit.ENPIE=1;PieCtrlRegs.PIEIER1.bit.INTx8=1;IER|=M_INT1;EINT;KickDog();EALLOW;SysCtrlRegs.WDCR=0x0028;EDIS;for(;;){LoopCount++;}}interruptvoidwakeint_isr(void){WakeCount++;PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;}4. pc与单片机的串口通信 #include #include void delay1ms() { unsigned int i; for(i=500000;i>0;i--); //延时 } Initial_com() { TMOD=0x20; //定时器T1 PCON &= 0xef; SCON=0x50; TH1=0xfd; //波特率设置为9600 TL1=0xfd; TR1=1; //开定时器T1运行控制位 } main() { Initial_com(); while(1) { SBUF='c'; while(!TI); delay1ms(); delay1ms(); TI=0; } }主程序代码: #include unsigned char butter; void send(); void receive(); void main() { PCON=0x80; SCON=0Xd0; TMOD=0X20; TH1=0Xfd; TL1=0Xfd; TR1=1; TI=1; while(1); receive(); send(); } void send() { if(TI==1) { SBUF=1; TI=0; } } void receive() { if(RI==1) { ACC=SBUF; butter=ACC; RI=0; } }451串口通信#includemain(){unsignedchardat;TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;while(1){if(RI)//接收{RI=0;dat=SBUF;}SBUF=dat;//发送while(!TI);TI=0;}}2、中断方式:#includeunsignedchardat;bitflag;voiduart_isr()interrupt4{if(RI)//接收{RI=0;dat=SBUF;flag=1;}if(TI)TI=0;}main(){TMOD=0x20;TH1=TL1=0xfd;SCON=0x50;TR1=1;EA=1;ES=1;flag=0;while(1){if(flag){SBUF=dat;flag=0;}}5.实现F2812片内外设eCan的功能:#include"DSP281x_Device.h"//头文件#include"DSP281x_Examples.h"//函数声明voidmailbox_check(int32T1,int32T2,int32T3);voidmailbox_read(int16i);//全局变量Uint32ErrorCount;Uint32MessageReceivedCount;Uint32TestMbox1=0;Uint32TestMbox2=0;Uint32TestMbox3=0;//主函数voidmain(void){Uint16j;structECAN_REGSECanaShadow;//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)InitSysCtrl();//步骤2.初始化GPIO//InitGpio();//本例中略过//步骤3.清除所有中断并初始化PIE向量表DINT;//禁止CPU所有中断//初始化PIE控制寄存器组.//InitPieCtrl();//本例中略过//禁止CPU所有中断并清除所有中断标志位IER=0x0000;IFR=0x0000;//InitPieVectTable();//本例中略过//步骤4.初始化所有外设//InitPeripherals();//本例中不需要//步骤5.用户程序段MessageReceivedCount=0;ErrorCount=0;InitPieCtrl();//初始化PIE中断InitPieVectTable;//初始化PIE中断矢量表InitECan();//初始化eCAN//可以一次向邮箱写16位或32位数据//向发送邮箱MBOX0~15写入MSGID(ID号)ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;//向接收邮箱MBOX16~31写入MSGID(ID号)ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组ECanaRegs.CANMD.all=0xFFFF0000;//使能所有的邮箱ECanaRegs.CANME.all=0x
res=sqrt(a[i].real()*a[i].real()+a[i].imag()*a[i].imag());
printf("%f\t",X);
3.看门狗软件实现方法:
unsignedinti,num;
unsignedchartemp;
voiddelay(unsignedintt);
num=0;
P1=~(0x00);
TMOD=0x10;
TL1=0xB0;
TH1=0x3C;
ET1=1;
EA=1;
TF1=0;
TR1=1;
temp=0x01;
for(i=0;i<8;i++)
P0=~temp;
delay(100);
temp<<=1;
TR1=0;
while
(1)
temp=0x80;
temp>>=1;
voiddelay(unsignedintt)
registerunsignedintbt;
for(;t;t--)
for(bt=0;bt<255;bt++);
voidint_T1()interrupt3using3
num++;
if(num==100)
P1=~(0x01);
看门狗硬件实现电路:
作业:
1.结合实例说明“实时性”与“仿真”的概念。
“实时性”是指信号的输入、运算和输出都要在一定的时间内完成,并根据生产过程工况及现场情况变化及时进行处理。
而实时系统指在事件或数据产生的同时,能够在规定的时间内给予响应,以足够快的速度处理,及时地将处理结果送往目的地的一种处理系统。
例如时钟信号能够准确的定时,各处的时钟能够达到一致,在不同的场合需要达到ns级、μs级。
“仿真”是应用电子计算机对系统的结构、功能和行为以及参与系统控制的人的思维过程和行为进行动态性比较逼真的模仿。
2.汇编语言实现浮点数相乘:
includeio32.inc
.data
msg1byte'请输入第一个数字',13,10,0
msg2byte'请输入第二个数字',13,10,0
readbuf1byte256dup(0)
readbuf2byte256dup(0)
numbyte00h
.code
start:
xorecx,ecx
xorebx,ebx
moveax,offsetmsg1
calldispmsg
moveax,offsetreadbuf1
callreadmsg
movedx,eax
pushedx
movesi,offsetreadbuf1
cmpbyteptr[esi],'-'
jzagain3
again1:
cmpbyteptr[esi],'.'
jnzagain2
pushecx
incesi
decedx
again2:
incecx
moval,[esi]
subal,30h
imulebx,10
movzxeax,al
addebx,eax
cmpedx,00H
jnzagain1
jmpdone1
again3:
incnum
jmpagain1
done1:
pushebx
popebx
popecx
popedx
subedx,ecx
moveax,offsetmsg2
moveax,offsetreadbuf2
movesi,offsetreadbuf2
jzagain6
again4:
jnzagain5
again5:
jnzagain4
jmpdone2
again6:
jmpagain4
done2:
popeax
addecx,edx
imuleax,ebx
movebx,1
done3:
dececx
cmpecx,0
jnzdone3
movedx,0
divebx
calldispuid
moveax,'.'
calldispc
moveax,edx
exit0
endstart
3.实现F2812的硬件看门狗设计:
#include"DSP281x_Device.h"
#include"DSP281x_Examples.h"
interruptvoidwakeint_isr(void);
Uint32WakeCount;
Uint32LoopCount;
voidmain(void)
InitSysCtrl();
DINT;
InitPieCtrl();
IER=0x0000;
IFR=0x0000;
EALLOW;
PieVectTable.WAKEINT=&wakeint_isr;
EDIS;
WakeCount=0;
LoopCount=0;
SysCtrlRegs.SCSR=BIT1;
PieCtrlRegs.PIECRTL.bit.ENPIE=1;
PieCtrlRegs.PIEIER1.bit.INTx8=1;
IER|=M_INT1;
EINT;
KickDog();
SysCtrlRegs.WDCR=0x0028;
for(;;)
LoopCount++;
interruptvoidwakeint_isr(void)
WakeCount++;
PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;
4. pc与单片机的串口通信
void delay1ms()
unsigned int i;
for(i=500000;i>0;i--); //延时
Initial_com()
TMOD=0x20; //定时器T1 PCON &= 0xef;
SCON=0x50;
TH1=0xfd; //波特率设置为9600
TL1=0xfd;
TR1=1; //开定时器T1运行控制位
} main()
Initial_com();
SBUF='c';
while(!
TI);
delay1ms();
TI=0;
主程序代码:
unsigned char butter;
void send();
void receive();
void main()
PCON=0x80;
SCON=0Xd0;
TMOD=0X20;
TH1=0Xfd;
TL1=0Xfd;
TI=1;
(1);
receive();
send();
void send()
if(TI==1)
{ SBUF=1;
void receive()
if(RI==1)
ACC=SBUF;
butter=ACC;
RI=0;
451串口通信
main()
unsignedchardat;
TMOD=0x20;
TH1=TL1=0xfd;
if(RI)//接收
dat=SBUF;
SBUF=dat;//发送
2、中断方式:
bitflag;
voiduart_isr()interrupt4
flag=1;
if(TI)TI=0;
ES=1;
flag=0;
if(flag)
SBUF=dat;
5.实现F2812片内外设eCan的功能:
#include"DSP281x_Device.h"//头文件
//函数声明
voidmailbox_check(int32T1,int32T2,int32T3);
voidmailbox_read(int16i);
//全局变量
Uint32ErrorCount;
Uint32MessageReceivedCount;
Uint32TestMbox1=0;
Uint32TestMbox2=0;
Uint32TestMbox3=0;
//主函数
Uint16j;
structECAN_REGSECanaShadow;
//步骤1.初始化系统控制(PLL,看门狗,使能外设时钟)
//步骤2.初始化GPIO
//InitGpio();//本例中略过
//步骤3.清除所有中断并初始化PIE向量表
DINT;//禁止CPU所有中断
//初始化PIE控制寄存器组.
//InitPieCtrl();//本例中略过
//禁止CPU所有中断并清除所有中断标志位
//InitPieVectTable();//本例中略过
//步骤4.初始化所有外设
//InitPeripherals();//本例中不需要
//步骤5.用户程序段
MessageReceivedCount=0;
ErrorCount=0;
InitPieCtrl();//初始化PIE中断
InitPieVectTable;//初始化PIE中断矢量表
InitECan();//初始化eCAN
//可以一次向邮箱写16位或32位数据
//向发送邮箱MBOX0~15写入MSGID(ID号)
ECanaMboxes.MBOX0.MSGID.all=0x9555AAA0;
ECanaMboxes.MBOX1.MSGID.all=0x9555AAA1;
ECanaMboxes.MBOX2.MSGID.all=0x9555AAA2;
ECanaMboxes.MBOX3.MSGID.all=0x9555AAA3;
ECanaMboxes.MBOX4.MSGID.all=0x9555AAA4;
ECanaMboxes.MBOX5.MSGID.all=0x9555AAA5;
ECanaMboxes.MBOX6.MSGID.all=0x9555AAA6;
ECanaMboxes.MBOX7.MSGID.all=0x9555AAA7;
ECanaMboxes.MBOX8.MSGID.all=0x9555AAA8;
ECanaMboxes.MBOX9.MSGID.all=0x9555AAA9;
ECanaMboxes.MBOX10.MSGID.all=0x9555AAAA;
ECanaMboxes.MBOX11.MSGID.all=0x9555AAAB;
ECanaMboxes.MBOX12.MSGID.all=0x9555AAAC;
ECanaMboxes.MBOX13.MSGID.all=0x9555AAAD;
ECanaMboxes.MBOX14.MSGID.all=0x9555AAAE;
ECanaMboxes.MBOX15.MSGID.all=0x9555AAAF;
//向接收邮箱MBOX16~31写入MSGID(ID号)
ECanaMboxes.MBOX16.MSGID.all=0x9555AAA0;
ECanaMboxes.MBOX17.MSGID.all=0x9555AAA1;
ECanaMboxes.MBOX18.MSGID.all=0x9555AAA2;
ECanaMboxes.MBOX19.MSGID.all=0x9555AAA3;
ECanaMboxes.MBOX20.MSGID.all=0x9555AAA4;
ECanaMboxes.MBOX21.MSGID.all=0x9555AAA5;
ECanaMboxes.MBOX22.MSGID.all=0x9555AAA6;
ECanaMboxes.MBOX23.MSGID.all=0x9555AAA7;
ECanaMboxes.MBOX24.MSGID.all=0x9555AAA8;
ECanaMboxes.MBOX25.MSGID.all=0x9555AAA9;
ECanaMboxes.MBOX26.MSGID.all=0x9555AAAA;
ECanaMboxes.MBOX27.MSGID.all=0x9555AAAB;
ECanaMboxes.MBOX28.MSGID.all=0x9555AAAC;
ECanaMboxes.MBOX29.MSGID.all=0x9555AAAD;
ECanaMboxes.MBOX30.MSGID.all=0x9555AAAE;
ECanaMboxes.MBOX31.MSGID.all=0x9555AAAF;
//配置邮箱0~15作为发送邮箱,16~31作为接收邮箱
//因为在这里写操作是针对整个寄存器而不是某一位,所以不需要映射寄存器组
ECanaRegs.CANMD.all=0xFFFF0000;
//使能所有的邮箱
ECanaRegs.CANME.all=0x
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1