【正文】
將DQ拉低 delay_18B20(80)。 //精確延時 大于 480us delay_18B20(14)。 x=DQ。 //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗 delay_18B20(20)。}/***********ds18b20讀一個字節(jié)**************/ uchar ReadOneChar(){ uchar i=0。 uchar dat=0。 for (i=8。i0。i) { DQ=0。 // 給脈沖信號 dat=1。 DQ=1。 // 給脈沖信號 if(DQ) delay_18B20(4)。 } return(dat)。}/*************ds18b20寫一個字節(jié)****************/ void WriteOneChar(uchar dat){ uchar i=0。 for (i=8。i0。i) { DQ=0。 DQ=datamp。0x01。 delay_18B20(5)。 DQ=1。 dat=1。 }}/**************讀取ds18b20當(dāng)前溫度************/void ReadTemperature(){ uchar a=0。 uchar b=0。 Init_DS18B20()。 WriteOneChar(0xCC)。 // 跳過讀序號列號的操作 WriteOneChar(0x44)。 // 啟動溫度轉(zhuǎn)換 delay_18B20(100)。 // this message is wery important Init_DS18B20()。 WriteOneChar(0xCC)。 //跳過讀序號列號的操作 WriteOneChar(0xBE)。 //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度 delay_18B20(100)。 a=ReadOneChar()。 //讀取溫度值低位 b=ReadOneChar()。 //讀取溫度值高位 wen_du=((b*256+a)4)。 //當(dāng)前采集溫度值除16得實際溫度值}void display()//顯示溫度{ w1=0。P0=table[d1]。delay(10)。 //第1位 P0=0x00。w1=1。delay(1)。 w2=0。P0=table[16]。delay(10)。//第2位 P0=0x00。w2=1。delay(1)。 w3=0。P0=table[d2]。 delay(10)。//第3位 P0=0x00。w3=1。delay(1)。 w4=0。P0=table[d3]。delay(10)。//第4位 P0=0x00。w4=1。delay(1)。}void zi_keyscan()//自動模式按鍵掃描函數(shù){ if(key1==0) { delay(10)。 if(key1==0)flag=1。 while(key1==0)。//松手檢測 } while(flag==1) { d1=18。d2=shang/10。d3=shang%10。 display()。 if(key1==0) { delay(10)。 if(key1==0)flag=2。 while(key1==0)。//松手檢測 } if(key2==0) { if(key2==0) { shang+=5。 if(shang=100)shang=100。 }while(key2==0)。//松手檢測 } if(key3==0) { delay(10)。 if(key3==0) { if(shang=10)shang=10。 }while(key3==0)。//松手檢測 } } while(flag==2) { d1=17。d2=xia/10。d3=xia%10。 display()。 if(key1==0) { delay(10)。 if(key1==0)flag=0。 while(key1==0)。//松手檢測 } if(key2==0) { delay(10)。 if(key2==0) { if(xia=95)xia=95。 }while(key2==0)。//松手檢測 } if(key3==0) { delay(10)。 if(key3==0) { xia=1。 if(xia=0)xia=0。 }while(key3==0)。//松手檢測 } }}void zi_dong()//自動溫控模式{ uchar i。 d1=dang。d2=wen_du/10。d3=wen_du%10。 zi_keyscan()。//按鍵掃描函數(shù) display()。 if(wen_duxia){dj=0。dang=0。}//低于下限 停止 if((wen_du=xia)amp。amp。(wen_du=shang))//1擋 { dang=1。 for(i=0。i5。i++){dj=0。display()。zi_keyscan()。} for(i=0。i5。i++){dj=1。display()。zi_keyscan()。} } if(wen_dushang){dj=1。dang=2。}//高溫全速}void main(){ uchar j。 dj=0。 shang=30。 for(j=0。j80。j++) ReadTemperature()。 while(1) { ReadTemperature()。 for(j=0。j100。j++)zi_dong()。//自動溫控模式 }}外文資料譯文With the rapid development of electronic technology, puters have been deeply infiltrated into our lives, many elearning enthusiasts SCM knowledge, but the content of SCM abstract parison, the relative electronic enthusiasts already familiar with the analog circuits, digital Circuit, SCM in a number of new concepts, even these very basic concept of the general disdain the author, natural materials will not be very indepth on these concepts, but they are learning in the content must be understandable, the bination of the following I am learning, teaching experience, the most basic of these concepts to make a note of hope one’s help. Microcontroller Features(1) High integration, small size, high reliability SCM will each feature integrated in a crystal chip, highly integrated, pact nature is also the smallest. The chip itself is designed according to industrial measurement and control environmental requirements, and internal wiring is very short, its antiindustrial noise performance is better than the general mon CPU. Microcontroller program instructions, constants and tables in the ROM is not easy to cure such damage, many signal channel within a chip, so high reliability.(2) Strong control To meet the requirements of the control object, the microcontroller has a very rich instruction conditions: ability to branch transfer, I / O port logic operations and bit processing power, ideal for special control functions.(3) lowvoltage, low power, ease of production of portable products In order to meet widely used in portable systems, many microcontroller operating voltage of only ~ , the operating current of only hundreds of microamps. (4) easy to expandWith a puter chip ponents required for normal operation. There are many external chips for expansion and parallel with the threebus, serial input / output pins, it is easy to create various size of puter applications.(5) Excellent cost performance Single chip with high performance. To improve the speed and efficiency, have begun to use the RISC MCU and DSP technologies such lines. MCU 64KB addressing capacity constraints have been exceeded, and some have up to 1MB and 16MB, the onchip ROM capacity of up to 62MB, RAM capacity of up to 2MB. Since the widespread use of single chip, thus greatly sales, panies make business more petitive price is very low, its high cost performance.隨著電子技術(shù)的迅速發(fā)展,計算機已深入地滲透到我們的生活中,許多電子愛好者開始學(xué)習(xí)單片機知識,但單片機的內(nèi)容比較抽象,相對電子愛好者已熟悉的模擬電路、數(shù)字電路,單片機中有一些新的概念,這些概念非常基本以至于一般作者不屑去談,教材自然也不會很深入地講解這些概念,但這些內(nèi)容又是學(xué)習(xí)中必須要理解的,下面就結(jié)合本人的學(xué)習(xí)、教學(xué)經(jīng)驗,對這些最基本概念作一說明,希望對別人有所幫助。下面是單片機的主要特點。 (1)高集成度,體積小,高可靠性 單片機將各功能部件集成在一塊晶體芯片上,集成度很高,體積自然也是最小的。芯片本身是按工業(yè)測控環(huán)境要求設(shè)計的,內(nèi)部布線很短,其抗工業(yè)噪音性能優(yōu)于一般通用的CPU。單片機程序指令,常數(shù)及表格等固化在ROM中不易破壞,許多信號通道均在一個芯片內(nèi),故可靠性高。 (2)控制功能強,為了滿足對對象的控制要求,單片機的指令系統(tǒng)均有極豐富的條件:分支轉(zhuǎn)移能力,I/O口的邏輯操作及位處理能力,非常適用于專門的控制功能。 (3)低電壓,低功耗,便于生產(chǎn)便攜式產(chǎn)品 為了滿足廣泛使用于便攜式系統(tǒng),~,而工作電流僅為數(shù)百微安。 (4)易擴展 片內(nèi)具有計算機正常運行所必需的部件。芯片外部有許多供擴展用的三總線及并行、串行輸入/輸出管腳,很容易構(gòu)成各種規(guī)模的計算機應(yīng)用系統(tǒng)。 (5)優(yōu)異的性能價格比,單片機的性能極高。為了提高速度和運行效率,單片機已開始使用RISC流水線和DSP等技術(shù)。單片機的尋址能力也已突破64KB的限制,有的已可達(dá)到1MB和16MB,片內(nèi)的ROM容量可達(dá)62MB,RAM容量則可達(dá)2MB。由于單片機的廣泛使用,因而銷量極大,各大公司的商業(yè)競爭更使其價格十分低廉,其性能價格比極高