Microchip PIC18F4550 + DS18B20 數字溫度計電路程式,程式是利用 DS18B20 內置溫度處理電路,18F4550 送出一個 T [44H] 轉換命令,DS18B20 啟動溫度測量和 A-to-D 轉換,轉換後,溫度數據會存儲在 2-byte 的暫存器 (scratchpad memory) 內的溫度寄存器 (temperature register) 和 DS18S20 返回到空閒狀態,Master 會發出“讀取時段”(Read Time Slots) ,並將溫度資料接收,程式會讀取接收資料數據,然後在 LC1602 顯示屏顯示溫度。
PIC18F4550 + DS18B20 數字溫度計程式流程圖 |
▼主程式
void main(void) { char LCD_name[] ="BWS DS18B20 2012"; char LCD_name2[]=" "; int i; unsigned int timeout=0; char data; // Variables for Temperature unsigned short TempHL, TempH, TempL; char Buf[] =" "; unsigned int Temp_whole, Temp_fraction, Temp_value; signed int Temp_pinF, Temp_pinC; unsigned short C_Neg=0, F_Neg=0; char Temp_sign; //ADCON0 = 0x00; // Disable the AD converter //ADCON1 = 0x0F; // Set all ports to digital TRISA = 0b00000001; // Setup RA0=Input RA1=Output TRISB = 0b00100000; // Setup RB0=Input // Initialise LCD OpenXLCD(FOUR_BIT&LINES_5X7); // Init the LCD Display while(BusyXLCD()); WriteCmdXLCD(BLINK_OFF & CURSOR_OFF ); while(BusyXLCD()); stdout = _H_USER; // Redirect I/O to LCD LcdSetLine1(); // Put cursor on start of line 1 putsXLCD(LCD_name); // Display text while(1) { LATAbits.LATA1 = 1; // RA0=1 LED=ON Scope=1 if (reset_ow()==0){ write_byte_ow(0xCC); write_byte_ow(0x44); Delay10KTCYx(180); // Delay:750ms 10,000x2x(4x0.05us)= 4ms reset_ow(); write_byte_ow(0xCC); write_byte_ow(0xBE); TempL = read_byte_ow(); // Read LSB TempH = read_byte_ow(); // Read MSB Temp_value = (TempH << 8) + TempL; Temp_sign = '+'; C_Neg = Temp_value & 0x8000; // Test MSB if (C_Neg) { // If Negative Temp_value = (Temp_value ^ 0xFFFF) +1; // 2's comp Temp_sign = '-'; // Sign = Negative } Temp_pinC = (6 * Temp_value) + Temp_value/4; // Multiple by 6.25 Temp_whole = Temp_pinC / 100; // Separate Digi & Fraction Temp_fraction = Temp_pinC % 100; // Fraction sprintf(Buf,"%04u.%02uC",Temp_whole,Temp_fraction); // Put Temp into Buf Buf[0] = Temp_sign; // Buf0 = Sign LcdSetLine2(); // Put cursor on start of line 2 putsXLCD(Buf); // Display Content } else { LcdSetLine2(); // Put cursor on start of line 2 putsXLCD(LCD_name2); // Display Content } LATAbits.LATA1 = 0; // RA0=0 LED=OFF Scope=50ms Delay10KTCYx(240); // 10,000 x 1 x (4 x 0.05us) = 2ms // Scope = 2ms } // End While } |
18F4550 + DS18B20數字溫度計 |
LCD 1602 顯示溫度 +30.12℃ |
2012 年 6 月15日 天氣報告
氣溫:28.2 度 @ 22:20
相對濕度:百分之84%
天氣:大致多雲
沒有留言:
張貼留言