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

📄 dd.c

📁 spce061读写DS18B20(温度传感器)的程序
💻 C
字号:
#include <SPCE061V004.H>
#include <stdio.h>
#define ReleaseDQH   *P_IOB_Dir&=0xFFFE	
#define SetDQL   *P_IOB_Dir|=0x0001;*P_IOB_Data&=0xFFFE
#define DQ	 ((*P_IOB_Data)&0x0001)
#define nop __asm("Nop")

extern void LCD(char* c);
extern void initLCD(void);
extern void d2s(double in,char* out);
void time_delay(unsigned char time)
{
//	time=time-10;
//	time=time/30;
	while(time!=0)time--;
}

void ds_reset(void)
{
	unsigned char count=0;
	SetDQL;
	time_delay(33);
//	time_delay(240);
	ReleaseDQH;
	return;
}

void check_pre(void)
{
//	while(DQ);
//	while(~DQ);
	time_delay(25);
}

char tmrbit(void)
{
	char i=0;
	char dat;
	SetDQL;nop;
	ReleaseDQH;
	nop;
	nop;
	nop;


	dat=DQ;
	time_delay(3);
	return dat;
}

unsigned char rd_ds18()
{
	unsigned char i,j,dat=0;
	for(i=1;i<=8;i++)
	{
		j=tmrbit();
		dat=(j<<(i-1))|dat;
	}
	return dat;
}

void wr_ds18(char dat)
{
	signed char i=0;
	unsigned char j;
	char testb;

	for(j=1;j<=8;j++)
	{
		testb=dat&0x01;
		dat=dat>>1;
		if(testb)
		{
			SetDQL;
			nop;
			nop;
			ReleaseDQH;
			time_delay(3);
		}
		else
		{
			SetDQL;
			//time_delay(6);
			nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
			nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
			ReleaseDQH;
			nop;
			nop;
		}
	}
}

unsigned int get_temp(void)
{
	unsigned int temp=0;
	unsigned char a=0,b=0;
	ds_reset();
	check_pre();
	wr_ds18(0xcc);/****skiprom***/
	wr_ds18(0x44);/*startconvert*/
	while(DQ == 0);
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	ds_reset();
	check_pre();
	wr_ds18(0xcc);/****skiprom***/
	wr_ds18(0xbe);/****readram***/
	a=rd_ds18();
	b=rd_ds18();
	temp=((unsigned int)b)<<8;
	temp=temp+a;
//	temp=temp>>2;
	return temp;
}


int main()
`{

	unsigned int t;
	double x=0;
	char buf[32];
	*P_IOB_Dir=0x0000;
	*P_IOB_Attrib=0x0001;
	
/*
	ds_reset();
	check_pre();
	wr_ds18(0xcc);
	wr_ds18(0x44);
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	time_delay(0x3fff);
	*P_Watchdog_Clear = C_WDTCLR;
	
//	while(DQ == 0);
	
	ds_reset();
	check_pre();
	wr_ds18(0xcc);
	wr_ds18(0xbe);
	for(i=0;i<9;i++)x[i]=rd_ds18();
	temp=((unsigned int)b)<<8;
	temp=temp+a;
	*/
//	initLCD();
//	LCD("Hello,world!");
	while(1)
	{
		t=get_temp();
		x=0.0625*(double)(t&0x000F)+(double)(t>>4);
		
		*P_Watchdog_Clear = C_WDTCLR;
		d2s(x,buf);
//		sprintf(buf,"T=%g",x);
		initLCD();
		LCD("T=");
		LCD(buf);
		
		*P_Watchdog_Clear = C_WDTCLR;
	}
		
	return t;
}

⌨️ 快捷键说明

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