⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tmpsensorm.nc

📁 c语言下的DS18B20数字温度传感器的驱动
💻 NC
字号:
module TmpsensorM {
  provides interface Tmps;
}

implementation {
	const unsigned char table[]="0123456789ABCDEF";

	void delay(uint16_t count)
        {
                uint8_t i;
                while(count)
                   {
                           i=100;
                           while(i>0) i--;
                           count--;
                   }
        }
        
	void ack()
	{
		uint8_t acke;
		TOSH_MAKE_S_OUTPUT();
		TOSH_CLR_S_PIN();
		delay(250);
		TOSH_MAKE_S_OUTPUT();
		TOSH_SET_S_PIN();
		delay(10);
		
		TOSH_MAKE_S_INPUT();
		//delay(10);
		acke=TOSH_READ_S_PIN();
		
		do{ acke=TOSH_READ_S_PIN();}while(acke);
                delay(50);
	}
	
	void write(unsigned char cmd)
	{
		unsigned char i,j;
		TOSH_MAKE_S_OUTPUT();
		for(j=0;j<=7;j++)
		{
			i=cmd&0x01;
			if(i==0x01)
			{
				TOSH_CLR_S_PIN();
				delay(1);
				TOSH_SET_S_PIN();
				delay(20);
			}
			else
			{
				TOSH_CLR_S_PIN();
				delay(20);
				TOSH_SET_S_PIN();
				delay(1);
			}
			cmd=cmd>>1;
		}
	}
	
	unsigned char read()
	{
		unsigned char i,byte,temp;
		byte=0x00;
		for(i=0;i<8;i++)
		{
			TOSH_MAKE_S_OUTPUT();
			TOSH_CLR_S_PIN();
			delay(3);
			TOSH_SET_S_PIN();
			TOSH_MAKE_S_INPUT();
			//delay(1);
			temp=TOSH_READ_S_PIN();//less than 500ns above 2.5V
			temp=temp&0x01;
			if(temp!=0x00)
			byte|=0x80;
			if(i<7)
			byte=byte>>1;
			delay(50);
			TOSH_MAKE_S_OUTPUT();
			delay(5);
		}
		return byte;
	}
	
	void match(unsigned char *p)
	{
		unsigned char i;
		ack();
		write(0x55);
		for(i=0;i<8;i++)
		write(p[i]);
	}
	
	unsigned char crccheck(unsigned char *p,unsigned char len)
	{
		unsigned char bit0,cbit,r,temp,i,j,byte;
		temp=0;
		for(j=0;j<len;j++)
		{
			byte=p[j];
			for(i=0;i<8;i++)
			{
				cbit=temp&0x01;
				bit0=byte&0x01;
				temp=temp>>1;
				r=cbit^bit0;
				if(r==1)
				temp=temp^0x8c;
				byte=byte>>1;
			}
		}
		return temp;
	}
	
	/*void asctocon(unsigned char *p,unsigned char len)
	{
		unsigned char i,temp;
		for(i=0;i<len;i++)
		{
			temp=p[len-1-i]&0xf0;
			temp=temp>>4;
			lcd_putc(table[temp]);
			temp=p[len-1-i]&0x0f;
			lcd_putc(table[temp]);
		}
	}*/
	
	command unsigned char Tmps.Tmp()
	{
		unsigned char i,crc,check;
		unsigned char ds[9];
		unsigned char romcode[8];
		unsigned char *asctop;
		unsigned char temp,temp0;
		//unsigned int temp;
		
		//MCUCR=0x00;
		//lcd_init();
		//lcd_clear();
		//lcd_gotoxy(0,0);
		//lcd_puts("SERIAL NUMBER IS");
				
		ack();
		write(0x33);
		for(i=0;i<8;i++)
		romcode[i]=read();
		//asctop=&romcod[0];
		//crc=crccheck(asctop,8);
		//if(crc==0x00)
		//{
		//	lcd_gotoxy(0,1);
		//	asctop=&romcode[0];
		//	asctocon(asctop,8);
		//}
		//delay(2000);
		//while(1)
		//{
			asctop=&romcode[0];
			match(asctop);
			write(0x44);
			delay(400);
			asctop=&romcode[0];
			match(asctop);
			write(0xbe);
			for(i=0;i<9;i++)
			ds[i]=read();
			//lcd_clear();
			//lcd_puts("Thermometer");
			asctop=&ds[0];
			crc=crccheck(asctop,9);
			if(crc==0x00)
			{
				temp0=ds[0];
				temp=ds[1];
				check=temp&0xf8;
				if(check==0x00)
				{
					temp0=temp0&0xf8;
					temp0=temp0>>3;
					temp=temp<<5;
					temp=temp|temp0;
					//temp=temp-0x01;
					//temp=ds[0];
				}
				else
				{
					temp=0x00;
				}
				//asctop=&ds[0];
				//asctocon(asctop,2);
			}
			/*if(crc==0x00)
			{
				temp=ds[1];
				temp=temp<<8;
				temp+=ds[0];
				asctop=&ds[0];
				asctocon(asctop,2);
			}
			lcd_gotoxy(0,1);
			if((temp&0xf800)!=0)
			{
				temp=0-temp;
				lcd_puts("-");
			}
			else
			        lcd_puts("+");
			crc=temp/16;
			for(i=0;i<3;i++)
			{
				ds[2-i]=table[crc%10];
				crc=crc/10;
			}
			ds[3]='.';
			crc=temp%16;
			crc=crc*10;
			ds[4]=table[crc/16];
			ds[5]=0xdf;
			ds[6]='C';*/
		//}
		return(temp);
	}
}
  	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -