新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 80C51單片機通過模擬SPI口驅(qū)動UZZ9001讀寫操作

80C51單片機通過模擬SPI口驅(qū)動UZZ9001讀寫操作

作者: 時間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
#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};             //對應(yīng)四個數(shù)碼管顯示數(shù)據(jù)的緩沖區(qū)   void delay(unsigned int time)  //延時                                           {      while(time--);     }   /*************************************************  函數(shù)名稱:數(shù)碼管顯示  功能:將四個緩沖區(qū)單元的內(nèi)容分別顯示到四個數(shù)碼管上面  參數(shù):unsigned charp *Bufptr  返回值:無  **************************************************/   void DISP_LED(unsigned char *Bufptr)   {   unsigned char disi;   unsigned char BitCT=0X10;   for(disi=0;disi<4;disi++)   {   PA8155=BitCT;                                       //選中個位數(shù)碼管   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;   }   }   }   }   


評論


技術(shù)專區(qū)

關(guān)閉