MSP430与DS18B20驱动程序
**描 述: 利用但总线DS18B20测温程序,并在LCD显示,取三位有效小数位。整数部分两位。共5位显示 ********************************************************************************************************/ #include <msp430x44x.h> #define SEGE 0X80 #define SEGH 0X40 #define SEGF 0X20 #define SEGC 0X10 #define SEGG 0X08 #define SEGD 0X04 #define SEGB 0X02 #define SEGA 0X01 const unsigned char digit[10] = { SEGA|SEGB|SEGC|SEGD|SEGE|SEGF, /* "0" LCD segments a+b+c+d+e+f */ 0x12, /* "1" */ 0x8F, /* "2" */ 0x1F, /* "3" */ 0x3A, /* "4" */ 0x3D, /* "5" */ 0xBD, /* "6" */ 0x13, /* "7" */ 0xBF, /* "8" */ 0x3F /* "9" */ }; #define DQ1 P4OUT|=BIT4 #define DQ0 P4OUT&=~BIT4 float Temper=0.0; int temperature=0; unsigned char Error = 0; //---------------------------------- //功能:us 级别延时 // n=10,则延时10*5+6=56uS //---------------------------------- void DelayNus(unsigned int n) { while(n--){}; } //----------------------------------- //功能:写18B20 //----------------------------------- void Write_18B20(unsigned char n) { unsigned char i; for(i=0;i<8;i++) { DQ0; DelayNus(1);//延时13us 左右 if((n&0X01)==0X01) DQ1; else DQ0; n=n>>1; DelayNus(9);//延时50us 以上 DQ1; } } //------------------------------------ //功能:读取18B20 //------------------------------------ unsigned char Read_18B20(void) { unsigned char i; unsigned char temp; for(i=0;i<8;i++) { temp=temp>>1; DQ0; _NOP();//延时1us DQ1; _NOP();_NOP();//延时5us _NOP();_NOP();_NOP(); P4DIR&=~BIT4; if((P4IN&BIT4)==0) { temp=temp&0x7F; }else { temp=temp|0x80; } DelayNus(7);//延时40us P4DIR|=BIT4; DQ1; } return temp; } //----------------------------------- void Init (void) { DQ0; DelayNus(50);//延时500us DQ1; DelayNus(17);//延时90us P4DIR&=~BIT4; if((P4IN&BIT4)==BIT4) //0001 1111b=1f { Error =1; //失败1 P4DIR|=BIT4; }else { Error = 0;//初始化成功
DelayNus(80); P4DIR|=BIT4; DQ1; } } //---------------------------------- void Skip(void) { Write_18B20(0xcc); } //---------------------------------- void Convert (void) { Write_18B20(0x44); } //---------------------------------- void ReadDo (void) { Write_18B20(0xbe); } //---------------------------------- void ReadTemp (void) { char temp_low,temp_high; //温度值 temp_low=Read_18B20(); //读低位 temp_high=Read_18B20(); //读高位 temperature=(temp_high&0x0f); temperature<<=8; temperature|=temp_low; Temper=temperature*0.0625; } void GetTemp(void){ Init(); Skip(); Convert(); DelayNus(60000); DelayNus(60000); DelayNus(60000);//延时1s以上 Init(); Skip(); ReadDo(); ReadTemp(); } void InitLcd(void){ LCDCTL = LCDON + LCD4MUX + LCDSG0_1; // LCD on, 4-Mux, segments S0-S15 BTCTL = BT_fLCD_DIV128; // LCD clock freq is ACLK/128 P5SEL = 0xFC; // Select P5.2-7 as Com and Rxx } void display_number(unsigned long value, int start, int width) { int i; for (i = 0; i < width; i++) { LCDMEM[7 + i - start] = digit[value%10]; // remainder = character in table to display value /= 10; } } void Lcd_Clr(void) { volatile unsigned char i; for (i=0; i<7; i++) { LCDMEM = 0; } } void main(void) { WDTCTL = WDTPW + WDTHOLD; // 停狗 P4DIR |=BIT4; DQ1; InitLcd(); Lcd_Clr(); while(1) { GetTemp(); display_number((unsigned long int)(Temper*1000),7,5); LCDMEM[3] |= SEGH; } }
|