80C51单片机通过模拟SPI口驱动UZZ9001读写操作
2015-06-24 来源:51hei
#include
进入单片机查看更多内容>>
#include#include #include sbit CS = P2^0; sbit CLK = P2^1; sbit DATA= P2^2; /*宏定义8155的A口,B口和控制口*/ #define PA8155 XBYTE[0x7101] #define PB8155 XBYTE[0x7102] #define COM8155 XBYTE[0x7100] //显示[0,1,2,3,4,5,6,7,8,9,A,b,c,d,e,f]的代码; unsigned char code Table[16]={0X0FC,0X60,0X0DA,0X0F2,0X66,0X0B6,0X0BE,0X0E0,0X0FE,0X0F6,0xee,0x3e,0x9c,0x7a,0xde,0x8e}; unsigned char Buf[4]={0,0,0,0}; //对应四个数码管显示数据的缓冲区 void delay(unsigned int time) //延时 { while(time--); } /************************************************* 函数名称:数码管显示 功能:将四个缓冲区单元的内容分别显示到四个数码管上面 参数:unsigned charp *Bufptr 返回值:无 **************************************************/ void DISP_LED(unsigned char *Bufptr) { unsigned char disi; unsigned char BitCT=0X10; for(disi=0;disi<4;disi++) { PA8155=BitCT; //选中个位数码管 PB8155=Table[*Bufptr++]+(disi==1); //取段码 BitCT=_crol_(BitCT,1); //左移一位 delay(500); } } unsigned int uzz9001(void) { unsigned int i,temp=0; CLK=1; CS=1; delay(5); CS=0; delay(1); i=16; while(i--) { temp<<=1; CLK=0; delay(5); CLK=1; if(DATA==1) temp |=0x0001; else temp &=0xfffe; delay(5); } CS=1; return(temp); } float angle(void) { unsigned int i,temp0,temp1,temp2=0; float jiaodu; for(i=0;i<8;i++) { temp0=uzz9001(); temp1=temp0&0x3f00; temp1>>=1; temp0&=0x007f; temp0=temp0+temp1; temp2+=temp0; } jiaodu=temp2*180.000/65536.000; return(jiaodu); } void main() { unsigned char count; unsigned int jiaodu; COM8155=0x03; TMOD=0X01; TH0=0X3c; //初始化,定时50MS TL0=0Xb0; count=0; TR0=1; while(1) { DISP_LED(Buf); if(TF0==1) { TF0=0; count++; if(count==10) //20*50MS=1S { count=0; jiaodu=(unsigned int) (angle()*10.0); DISP_LED(Buf); Buf[3]=jiaodu/1000; Buf[2]=jiaodu%1000/100; Buf[1]=jiaodu%100/10; Buf[0]=jiaodu%10; } } } }
相关文章