/************************************************************************************************************** * Copyright(c) 2004 Linux-Union, Inc. All rights reserved. * *Created by: Tomy Li *Create Date: 2004.4.15 *Reason: First create *Reversion: 0.02 ************************************************************************************************************** *Module name: DS1820 function *Description: * DS1820's function * 1. Use 6MHz or 12MHz(11.0592MHz) crystal,if use other crystal, all time sequence must be modified * 2. Read and write DS1820 IC * 3. Use DQ to control DS1820 *----------------------------------------------------------------------------------------------------------------- *Comments: * Optimizition level: 8(with speed) *--------------------------------------------------------------------------------------------------------------- *History: * 1. Modify all constant variable to upper case, replace some constant to predefined symbol. 2004-09-19 */ #define byte unsigned char #define uint unsigned int //Define DS1820's command #define SKIP_ROM 0XCC //Skip 64bits serial number #define READ_SCRATCH_PAD 0XBE //Read 9 bytes register data #define START_CONVERSION 0X44 //Start temperature conversion #define MATCH_ROM 0X55 //The match ROM command followed by a 64¨Cbit ROM code sequence allows the bus //master to address a specific slave device on a multi-drop or single-drop bus. #define READ_ROM 0X33 //This command can only be used when there is one slave on the bus. It allows the //bus master to read the slave¡¯s 64-bit ROM code without using the Search ROM procedure. #define SERACH_ROM 0XF0 //Allows the master to determine the number of slaves and their device types. #define ALARM_SERACH 0XEC //This command allows the master device to determine if any DS18S20s experienced an alarm // condition during the most recent temperature conversion. #define COPY_SCRATCH_PAD 0X48 //This command copies the contents of the scratchpad TH and TL registers (bytes 2 and 3) //to EEPROM. #define WRITE_SCRATCH_PAD 0X4E //This command allows the master to write 2 bytes of data to the DS18S20¡¯s scratchpad. #define RECALL_EEPROM 0XB8 //This command recalls the alarm trigger values (TH and TL) from EEPROM and places the //data in bytes 2 and 3. #define READ_POWER 0XB4 //The master can use this command to determine if any DS18S20s on the bus are using //parasite power. //------------------------------------------------------------------------ #define LEVEL_HIGH 1 #define LEVEL_LOW 0 /* #define true 1 #define false 0 // #define UI8_T unsigned char #define UI16_T unsigned int #define I8_T char #define BOOL bit */ //sbit DQ =DS1820_DQ; /***************************************************** Function: Dly10us Description: Delay specified number of 10us Parameters: us, number need to delay Return value: None ******************************************************/ void Dly10us(UI8_T us) { while(us--); //5 mechine cycle, 10us } /********************************************************************************** Function: write_1820 Description: write data ds1820 Parameter: cmd, command of ds1820 Return value: None ************************************************************************************/ void Write_1820(UI8_T val) { UI8_T i; for (i=8; i>0; i--) // writes byte, one bit at a time { DQ = LEVEL_LOW; // pull DQ low to start timeslot DQ = val&0x01; Dly10us(6); // hold value for remainder of timeslot. 60us DQ =LEVEL_HIGH; val=val/2; //val shift right 1 bit } } /********************************************************************************** Function: read_1820 Description: read one byte data from ds1820 Parameter: void Return value: read data ************************************************************************************/ UI8_T Read_1820(void) { UI8_T i; UI8_T value = 0; for (i=8;i>0;i--) { value>>=1; DQ = LEVEL_LOW; // pull DQ low to start timeslot DQ = LEVEL_HIGH; // then return high DQ=LEVEL_HIGH; DQ=LEVEL_HIGH; DQ=LEVEL_HIGH; DQ=LEVEL_HIGH; if(DQ) value|=0x80; //read bit at 13us. This is very important Dly10us(5); // wait for rest of timeslot } return(value); } /********************************************************************************** Function: rst_1820 Description: reset ds1820 Parameter: None Return value: If present, return 0, otherwise return 1 ************************************************************************************/ BOOL Rst_1820(void) { BOOL presence; DQ = LEVEL_LOW; //pull DQ line low Dly10us(48); // remain it low for 480us min(max 960us) DQ =LEVEL_HIGH; // allow line to return high Dly10us(6); // wait for presence 15-60us presence = DQ; // get presence signal Dly10us(42); // wait for end of timeslot about 420us(480-60) return(presence); // presence signal returned }