基于MSP430F149單片機(jī)驅(qū)動(dòng)DS2762讀寫操作C語(yǔ)言程序,能夠?qū)Π存I或著其他信號(hào)響應(yīng),讀出電流以及電壓量,并進(jìn)行相應(yīng)處理。 //#include <msp430x14x.h> #define uchar unsigned char #define uint unsigned int uint A,B,C,D,E,F,G,H,I,J;//此處對(duì)io430x14x中的宏定義C進(jìn)行了注釋,需注意 #define IO_OUT P3DIR |= BIT0; #define IO_INP P3DIR &= ~BIT0; uint data; /****************************************************************************** 對(duì)讀寫時(shí)間進(jìn)行規(guī)范化 初始調(diào)試采用standard=1的參數(shù) // Pause for exactly 'tick' number of ticks = 0.25us 實(shí)際測(cè)的A延時(shí)14Us B125US ******************************************************************************/ void SetSpeed(uint standard) { // Adjust tick values depending on speed if (standard) { // Standard Speed A = 6 * 4 / 2; B = 64 * 4 / 2; C = 60 * 4 / 2; D = 10 * 4 / 2; E = 9 * 4 / 2; F = 55 * 4 / 2; G = 0 / 2; H = 480 * 4 / 2; I = 70 * 4 / 2; J = 410 * 4 / 2; } else { // Overdrive Speed A = 1.5 * 4; B = 7.5 * 4; C = 7.5 * 4; D = 2.5 * 4; E = 0.75 * 4; F = 7 * 4; G = 2.5 * 4; H = 70 * 4; I = 8.5 * 4; J = 40 * 4; } } /****************************************************************************** 延時(shí)程序 注意,需要選用8M晶振,時(shí)鐘周期125ns ******************************************************************************/ void tickDelay(uint tick) // Implementation is platform specific { for(;tick>0;tick--); } /****************************************************************************** 主機(jī)復(fù)位脈沖 當(dāng)接收結(jié)果result為0時(shí),表明從機(jī)應(yīng)答 ******************************************************************************/ uchar OWTouchReset(void) { uchar result; IO_OUT; tickDelay(G); P30 = 0; // Drives DQ low tickDelay(H); P30 = 1; // Releases the bus tickDelay(I); IO_INP; result = P3IN & 0X01; // Sample for presence pulse from slave tickDelay(J); // Complete the reset sequence recovery return result; // Return sample presence pulse result } /****************************************************************************** 像寫DS2762寫入一位 Send a 1-Wire write bit. Provide 10us recovery time. ******************************************************************************/ void OWWriteBit(uchar bit) { if (bit) { // Write '1' bit P30 = 0; // Drives DQ low tickDelay(A); P30 = 1; // Releases the bus tickDelay(B); // Complete the time slot and 10us recovery } else { // Write '0' bit P30 = 0; // Drives DQ low tickDelay(C); P30 = 1; // Releases the bus tickDelay(D); } } /****************************************************************************** 從DS2762讀出一位 Read a bit from the 1-Wire bus and return it. Provide 10us recovery time. ******************************************************************************/ uchar OWReadBit(void) { uchar result; P30 = 0; // Drives DQ low tickDelay(A); P30 = 1; // Releases the bus tickDelay(E); result = P3IN & 0X01; // Sample the bit value from the slave tickDelay(F); // Complete the time slot and 10us recovery return result; } /****************************************************************************** 像寫DS2762寫入一個(gè)字節(jié) Send a 1-Wire write . Provide 10us recovery time. DS2762特性發(fā)送所有的命令和數(shù)據(jù)都是字節(jié)的低位在前,這與多數(shù)串行通信格式相反 ******************************************************************************/ void OWWriteByte(uchar data) { uchar loop; // Loop to write each bit in the byte, LS-bit first for (loop = 0; loop < 8; loop++) { OWWriteBit(data & 0x01); // shift the data byte for the next bit data >>= 1; } } /****************************************************************************** 從DS2762中讀一個(gè)字節(jié) ******************************************************************************/ uchar OWReadByte(void) { uchar loop, result=0; for (loop = 0; loop < 8; loop++) { // shift the result to get it ready for the next bit result >>= 1; // if result is one, then set MS bit if (OWReadBit()) result |= 0x80; } return result; } /****************************************************************************** 發(fā)送字符函數(shù) 按照論文圖時(shí)序進(jìn)行的操作 目前不清楚連續(xù)讀需要通過測(cè)試進(jìn)行優(yōu)化。 方案一:發(fā)出讀命令 發(fā)出一個(gè)地址 讀兩次 應(yīng)該是正確的 方案二:發(fā)出讀命令 發(fā)出兩個(gè)地址 讀兩次 ******************************************************************************/ uint readvoltage(void) { uchar j = 1; unsigned char volhigh,vollow; while(j) //檢查2762是否應(yīng)答 { j = OWTouchReset(); } OWWriteByte(0xcc); // OWWriteByte(0x69); OWWriteByte(0x0c); vollow = OWReadByte(); volhigh = OWReadByte(); return ((volhigh<<8)|(vollow)); //將兩個(gè)字節(jié)進(jìn)行合并 } /****************************************************************************** 時(shí)鐘初始化 注意,需要選用8M晶振,時(shí)鐘周期125ns ******************************************************************************/ void Init_clk(void) {unsigned char i; //時(shí)基模塊的時(shí)鐘設(shè)置 //單片機(jī)上電時(shí),MCLK主時(shí)鐘的源默認(rèn)選擇為DCO提供.F1系列DCO默認(rèn)800KHZ. //ACLK輔助時(shí)鐘默認(rèn)為XT1,XT1一般接32768HZ晶體. //SMCLK子時(shí)鐘默認(rèn)為DCO,同樣是800KHZ. //XT2需要人為開啟,并要檢測(cè)其開啟是否成功. BCSCTL1 &= ~(XT2OFF + XTS); //啟動(dòng)XT2高速時(shí)鐘模塊 BCSCTL2 |= SELM1; //MCLK主時(shí)鐘選XT2為時(shí)鐘源.TX2輸入不分頻. BCSCTL2 &= ~SELS; //SMCLK選為DCO為時(shí)鐘源.(參考) //剛才開啟了XT2,需要一定時(shí)間XT2才進(jìn)入穩(wěn)定狀態(tài).所以需要等待并檢測(cè)穩(wěn)定狀態(tài). //通常采用do...for語(yǔ)法,這是TI推薦的程序?qū)懛? do { IFG1 &=~OFIFG; //清OSCFault 標(biāo)志 for(i=0xff;i>0;i--) //延時(shí)等待其開啟穩(wěn)定 ; } while((IFG1 & OFIFG) !=0); //檢查OSCFault標(biāo)志位是否為0,若為0則表示XT2開啟穩(wěn)定. //否則一直等下去... } /****************************************************************************** 主函數(shù) 注意,需要選用8M晶振,時(shí)鐘周期125ns ******************************************************************************/ void main() { WDTCTL = WDTPW | WDTHOLD; // 停止看門狗 Init_clk(); SetSpeed(1); while(1) { data = readvoltage(); } }