Microchip PIC18F4550 + DS18B20 + RS232 數字溫度數據記錄儀 (Data Logger) 程式,程式是利用 DS18B20 內置溫度處理電路,18F4550 送出一個T [44H] 轉換命令,DS18B20 啟動溫度測量和 A-to-D 轉換,轉換後,溫度數據會存儲在 2-byte 的暫存器 (scratchpad memory) 內的溫度寄存器 (temperature register) 和 DS18S20 返回到空閒狀態,Master 會發出“讀取時段”(Read Time Slots) ,並將溫度資料接收,程式會讀取接收資料數據,然後在 LC1602 顯示屏顯示溫度及傳送到 RS232 串行 USART 電路,PC 電腦便會接收資料數據,並儲存資料數據。
PIC18F4550 + DS18B20 + RS232 數字溫度數據記錄儀程式流程圖 |
▼ 主程式
void main(void) { char LCD_name[] ="BWS DS18B20 v1.1"; char LCD_name2[]=" "; TRISA = 0b00000001; // Setup RA0=Input RA1=Output TRISB = 0b00100000; // Setup RB5=Input TRISC = 0b10111111; //RC7(RX): input mode RC6(TX): output mode // 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 // Init RS232 rs232Init(); RCONbits.IPEN = 0; // Interrupt Priority Control INTCONbits.GIE = 1; // All Interrupt INTCONbits.PEIE = 1; // Peripherial Interrupt Enable sprintf(glb_Buf,"%s\r\n",LCD_name); rs232SendString(glb_Buf); // Tx Header to RS232 Temp_read(); // Initiate the Temperature Reading while(1) { LATAbits.LATA1 = 1; // RA0=1 LED=ON Scope=1 Delay10KTCYx(240); // 10,000 x 1 x (4 x 0.05us) = 2ms // Scope = 2ms if (auto_mode == 1){ Temp_read(); } 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 } |
▼讀取溫度副程式
void Temp_read(void) // Temperature Reading Subroutine { // Variables for Temperature Sensor unsigned short TempHL, TempH, TempL; unsigned short C_Neg=0, F_Neg=0; unsigned int Temp_whole, Temp_fraction, Temp_value; signed int Temp_pinF, Temp_pinC; char Temp_sign; //if (reset_ow()==0){ while(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(glb_Buf,"%04u.%02uC ",Temp_whole,Temp_fraction); // Put Temp into Buf glb_Buf[0] = Temp_sign; // Buf0 = Sign LcdSetLine2(); // Put cursor on start of line 2 putsXLCD(glb_Buf); // Display Content if (rs232_tx == 1){ rs232SendString(glb_Buf); sprintf(glb_Buf,"\r\n"); rs232SendString(glb_Buf); } return ; } |
初始 DS18B20 的數據是 +85.00C |
DS18B20 讀取溫度數據是 +30.93C |
2012年 7月 3日 天氣報告
氣溫:28.2 度 @ 22:00
相對濕度:百分之81%
天氣:天色大致良好
沒有留言:
張貼留言