求大虾们给看看这个单片机C语言的程序 错在哪了
程序代码:
#include "reg52.h" #include "uart.h" sbit sdi= P1^0; sbit sck= P1^1; sbit nsel= P1^2; sbit sdo= P1^3; sbit key1=P2^0; sbit key2=P2^1; sbit key3=P2^2; sbit key4=P2^3; sbit l4=P2^7; sbit l1=P2^4; sbit l2=P2^5; sbit l3=P2^6; unsigned char workflag; unsigned char rxdata[16]; //16,接收数据存储器组 unsigned char txdata[16]; //16,发送数据存储器组 bit revframe; unsigned char spirevbyte(); unsigned int spirevtwobyte(); void spisendbyte(unsigned char x); void spisendcommand(unsigned int x); void txregframe(); void fiforxframe(); void ia442xinit(); void delayms(unsigned int n); unsigned char num=0; unsigned char comdata[16]; bit flag=0; unsigned uchar code table[]= { 0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e } void display(uchar d_num) { P0=table[d_num]; } void anjian() { uchar a,b,num; if(key1==0) { delay(10); if(key1==0) { num++; if(num==9) num=0; b=num; a=num; display(b); while(!key1); send(a); } } if(key2==0) { delay(10); if(key2==0) { if(num==0) num=8; else num--; b=num; a=num; display(b); while(!key2); send(a); } } if(key4==0) { if(key4==0) { b=0; num=0; a=10; display(b); while(!key4); send(a); } } if(key3==0) { if(key3==0) { send(a); while(!key3); } } } void init() { TMOD=0x20; TH1=0xfd; TL1=0xfd; TR1=1; REN=1; SM0=0; SM1=1; EA=1; ES=1; d=1; } void main() { unsigned char b; UART_Init(); P1=0xcf; txdata[0]=0x2d; txdata[1]=0xd4; txdata[2]=0xfa; txdata[3]=0x01; txdata[4]=0x55; ia442xinit(); spirevtwobyte(); spisendcommand(0x8288); //;电源管理 while(1) { spisendcommand(0xca83); //;FIFO COMMAND while(1) { sdi=0; nsel=0; init(); l4=1; l1=0,l2=1,l3=1; P0=table[0]; while(1) { anjian(); } if(sdo) { fiforxframe(); if(!revframe) break; revframe=0; send(rxdata[2]); break; } if(flag) { ES=0; txregframe(); //发送一帧数据 flag=0; RI=0; ES=1; break; } } } } void serial() interrupt 4 //串口中断 { if(RI) { txdata[4]=SBUF; flag=1; RI=0; } } void ia442xinit() //;ia442x初始化 { spisendcommand(0x80dd); //;配置命令,fd-915M,dd-433M spisendcommand(0x820c); //;电源管理 spisendcommand(0xa080); //;频率设置 spisendcommand(0xc623); //;波特率设置 spisendcommand(0x9483); //;接收控制 spisendcommand(0xc2ec); //;数据滤波 spisendcommand(0xca81); //;FIFO COMMAND spisendcommand(0xc4e7); //;AFC COMMAND spisendcommand(0x9840); //;TXCTRL spisendcommand(0xffff); //;WAKEUP spisendcommand(0xc80e); //;间歇工作 spisendcommand(0xc060); //;时钟输出 } void fiforxframe() //;ia442x接收一帧数据 { unsigned char i,n; spirevbyte(); spirevbyte(); rxdata[0]=spirevbyte(); nsel=1; if(rxdata[0]==0xfa) { sdi=0; nsel=0; while(!sdo); spirevbyte(); spirevbyte(); rxdata[1]=spirevbyte(); nsel=1; n=rxdata[1]&0x0f; for(i=0;i<n;i++) { sdi=0; nsel=0; while(!sdo); spirevbyte(); spirevbyte(); rxdata[i+2]=spirevbyte(); nsel=1; } revframe=1; } spisendcommand(0xca81); } void txregframe() //;ia442x发送一帧数据 { unsigned char i,n; unsigned int u; spisendcommand(0x8228); n=txdata[3]+4; for(i=0;i<n;i++) { nsel=0; while(!sdo); u=0xb8; u<<=8; u+=txdata[i]; spisendcommand(u); } nsel=0; while(!sdo); spisendcommand(0xb8aa); nsel=0; while(!sdo); spisendcommand(0x8288); spirevtwobyte(); } void spisendcommand(unsigned int x) //;写指令 { unsigned char comdata1,comdata2; unsigned int a; a=x; nsel=0; comdata2=a; comdata1=a>>8; spisendbyte(comdata1); spisendbyte(comdata2); nsel=1; } void spisendbyte(unsigned char x) //;写8位数据 { unsigned char i,bytedata; display(); x=P0; bytedata=x; for(i=0;i<8;i++) { sck=0; sdi=bytedata&0x80; bytedata=bytedata<<1; sck=1; } sck=0; sdi=0; } unsigned int spirevtwobyte() //;读状态 { unsigned int u; nsel=0; u=spirevbyte(); u<<=8; u+=spirevbyte(); nsel=1; return(u); } unsigned char spirevbyte() //;读8位数据 { unsigned char i,bytedata; sdi=0; sck=0; bytedata=0; for(i=0;i<8;i++) { sck=1; bytedata=bytedata<<1; if(sdo) bytedata+=1; sck=0; } return(bytedata); } void delayms(unsigned int n) //延时 { unsigned int i,j; for(j=0;j<n;j++) { for(i=0;i<112;i++) ; } }