|
下載:
74hc595.rar
(2.27 KB, 下載次數(shù): 7)
2017-6-14 21:08 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
- #include <reg52.h>
- #include <intrins.h> /*used for _nop_() function*/
- sbit SDATA_595 =P1^3 ;/*串行數(shù)據(jù)輸入*/
- sbit SCLK_595 = P1^1;/*移位時(shí)鐘脈沖*/
- sbit RCK_595 =P1^2 ; /*輸出鎖存器控制脈沖*/
- #define AD_WR WR
- #define AD_RD RD
- #define AD_INTR INT0 /*51的相應(yīng)端口連接ADC0804的三個(gè)控制端口*/
- //#define Vin_Max 5
- ////////////////////////////////////////
- static unsigned char data num1=15; /*存放AD轉(zhuǎn)換數(shù)據(jù)的全局變量*/
- /****************************************************************/
- unsigned char code Dig_Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
- 0x88,0x82,0xc6,0xa1,0x84,0x8e};/*0到F的共陽段碼表*/
- //unsigned char data Dis_Tab[17]={0x00,0x01,0xff,0x02,0xff,0x04,0xff,0x08,0xff,0x10,0xff,0x20,0xff,0x40,0xbf,0x80,
- // 0xbf};
- unsigned char data Dis_Tab[17]={0x00,0x01,0xf7,0x02,0xf7,0x04,0xf7,0x08,0xff,0x10,0x7f,0x20,0xbf,0x40,0xbf,0x80,
- 0xbf};
- /****************************************************************/
- /* 更新顯示數(shù)組Dis_Tab[]函數(shù)Dis_Updata()*/
- void Dis_Updata()
- {
- /*數(shù)據(jù)訪問通過全局的num1*/
- //unsigned char data temp1;
- ///////////////////////////////////////////////
- float data temp2; //定義存放計(jì)算得到的電壓值
- unsigned long data temp3;
-
- // {
- // temp1=num1;
- // num1&=0x0F;/*屏蔽高四位*/
- // Dis_Tab[16]=Dig_Tab[num1]; /*A為代顯示數(shù)據(jù)*/
- /*Dig_Tab[]數(shù)組為段碼表,序號(hào)A值用來檢索對(duì)應(yīng)段碼,同時(shí)賦制值更新顯示表*/
- // num1=temp1;
- // /*此語句編譯時(shí)有問題,requires ANSI-style prototype ;missing function-prototype*/
- // /*asm ("SWAP num1;" );*/
- // /*用C實(shí)現(xiàn)高低四位交換程序如下,一個(gè)問題ACC是否為帶符號(hào)數(shù)*/
- // num1>>=4;
- // num1&=0x0F;
-
- // Dis_Tab[14]=Dig_Tab[num1];
- // }
- //////////////////添加/////////////////////////////////
- temp2=(num1*5)/255.0;
- //temp2=(num1*5)/0xFF+((num1*5)%0xFF); 此除法運(yùn)算有問題
-
- if(temp2<0)
- {
- Dis_Tab[8]=0xbf; //若小于零,顯示負(fù)號(hào)
- //temp2=(-temp2); //若是負(fù)數(shù),取正
- }
-
- temp2*=1000; //擴(kuò)大1000倍,分別提取各個(gè)位的數(shù)值
- temp3=temp2;
-
- // temp3=( unsigned long)temp2; //強(qiáng)制浮點(diǎn)數(shù)轉(zhuǎn)換為長整型
- Dis_Tab[10]=Dig_Tab[temp3/1000]&0x7f; //數(shù)制轉(zhuǎn)化,向高看齊,必須的用長整型,浮點(diǎn)數(shù)不可
- Dis_Tab[12]=Dig_Tab[temp3/100%10];
- Dis_Tab[14]=Dig_Tab[temp3%100/10];
- Dis_Tab[16]=Dig_Tab[temp3%10];
-
-
-
- }
- /****************************************************************/
- /*延時(shí)子程序void Delay(unsinged char data i)*/
- void Delay1(unsigned char i)
- {
- while(--i);
- }
- /****************************************************************/
- /* 精確延時(shí)time=2i+4,at 12MHZ AXIAL*/
- void Delay2(unsigned char data j)
- {
- while(--j);
-
- }
- /****************************************************************/
- /*Int0 AD轉(zhuǎn)換結(jié)束讀取數(shù)據(jù)程序*/
- void Int0_ISR(void) interrupt 0
- {
- AD_RD=0; /*開始讀數(shù)據(jù)*/
- num1=P0; /*讀人數(shù)據(jù)*/
-
- AD_RD=1;
-
- Dis_Updata();
-
-
- }
- /****************************************************************/
- /*A/D數(shù)據(jù)采集子程序ADC0804*/
- void AD_start(void)
- {
- AD_WR=0;
- _nop_();
- _nop_();
- AD_WR=1; /*發(fā)出寫信號(hào),啟動(dòng)AD轉(zhuǎn)換*/
-
- }
- /****************************************************************/
- /*T0中斷程序,定時(shí)啟動(dòng)AD轉(zhuǎn)換*/
- void T0_ISR(void) interrupt 1
- {
- TL0=0x00;
- TH0=0x00;
- ///////////////////////////////////////////
- AD_start(); /* 每隔0.256ms啟動(dòng)一次AD*/
-
- Dis_Updata();
- }
- /****************************************************************/
- /*串口、定時(shí)器、TNT0中斷 初始化函數(shù)*/
- void Init_Mcu51(void)
- {
- //TMOD|=0x22;/*T1波特率發(fā)生,MODE=2;T0為8位自動(dòng)重裝,*/
- TMOD|=0x21;/*T1波特率發(fā)生,MODE=2;T0為16位,*/
- TL1=0xF4;
- TH1=0xF4; /*2400bps @AXIAL=11.0592MHZ*/
- TL0=0x00;
- TH0=0x00; /*定時(shí)時(shí)間為0.256ms*/
- IT0=1; //下降沿觸發(fā)
-
- TR1=1; /*start T1*/
- TR0=1;
- SCON|=0x52;/*MODE=1,TI=1*/
- IE=0x83; /*開INT0,開T0,總中斷*/
-
- }
- /****************************************************************/
-
- /*寫數(shù)據(jù)到74HC595函數(shù)*/
- void WR_595(unsigned char data num2 )
- {
- unsigned char data count1;
-
- for(count1=0;count1<=7;count1++)
- {
- if((num2&0x80)==0x80) /*最高位為1,則向SDATA_595發(fā)送1*/
- SDATA_595=1;/*發(fā)出數(shù)據(jù)的最高位*/
- else
- SDATA_595=0;
- num2<<=1; /*右移位*/
- SCLK_595=0;
- _nop_();
- _nop_();
- SCLK_595=1; /*產(chǎn)生上生沿*/
-
- }
- }
- /****************************************************************/
- /*更新74HC595輸出數(shù)據(jù)函數(shù)void Out_595(void)*/
- void Out_595(void)
- {
- RCK_595=0;
- _nop_();
- _nop_();
- RCK_595=1; /*上升沿鎖存數(shù)據(jù)*/
- }
- /****************************************************************/
- /*LED驅(qū)動(dòng)程序void Led_Drive(void)*/
- void Led_Drive(void)
- {
- unsigned char data Dis_Count;
-
- while(1)
- { ++num1;
- for(Dis_Count=1;Dis_Count<=16;Dis_Count++)
- {
- WR_595(Dis_Tab[Dis_Count]);
- if(Dis_Count%2==0) /*判斷若傳完兩個(gè)字節(jié)數(shù)據(jù),則更新74HC595輸出數(shù)據(jù),并延時(shí)*/
- {
- Out_595();
- //Delay1(1);
-
- }
-
- }
- }
- }
- /****************************************************************/
- main()
- {
- //P0=0xff;
- //P1=0xff;
- Init_Mcu51();
- Led_Drive(); //自身是個(gè)無限循環(huán)顯示的程序
-
-
- }
復(fù)制代碼
|
|