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

📄 tianxiang2.c

📁 单片机单片机单单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机单片机片机单片机单片机单片机
💻 C
字号:
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>

#define uchar unsigned char 
#define uint unsigned int 

sbit ds=P2^2;
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;

uint temp;
float f_temp;

unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f,
0xBf,0x86,0xDb,0xCf,0xE6, 0xEd,0xEd,0x87,0xFf,0xEf};    
//共阴极数码管显示数字0.,1.,2.,3.,4.,5.,6.,7.,8.,9.    

void delay(uint z)
{	
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);	
}

void dsreset()
{
	uint i;
	ds=0;
	i=103;
	while(i>0) i--;
	ds=1;
	i=4;
	while(i>0) i--;
}   

bit tempreadbit()
{
	uint i;
	bit dat;
	ds=0;i++;
	ds=1;i++;i++;
	dat=ds;
	i=8;
	while(i>0) i--;
	return (dat);
}

uchar tempread()
{
	uchar i,j,dat;
	dat=0;
	for(i=1;i<=8;i++)
	{
		j=tempreadbit();
		dat=(j<<7)|(dat>>1);
	}
	return (dat);
}

void tempwritebyte(uchar dat)
{
	uint i;
	uchar j;
	bit testb;
	for(j=1;j<=8;j++)
	{
		testb=dat&0x01;
		dat=dat>>1;
		if(testb)
		{
			ds=0;
			i++;i++;
			ds=1;
			i=8;while(i>0) i--;
		}
		else
		{
			ds=0;
			i=8;
			while(i>0) i--;
			ds=1;
			i++;i++;
		}
	}
}

void tempchange()
{
	dsreset();
	delay(1);
	tempwritebyte(0xcc);
	tempwritebyte(0x44);
}

uint get_temp()
{
	uchar a,b;
	dsreset();
	delay(1);
	tempwritebyte(0xcc);
	tempwritebyte(0xbe);
	a=tempread();
	b=tempread();
	temp=b;
	temp<<=8;
	temp=temp|a;
	f_temp=temp*0.0625;
	temp=f_temp*10+0.5;
	f_temp=f_temp+0.05;
	return temp;
}

void display(uchar num,uchar dat)
{
	uchar i;
	dula=0;
	P0=table[dat];
	dula=1;
	dula=0;
	wela=0;
	i=0xFF;
	i=i&(~((0x01)<<(num)));
	P0=i;
	wela=1;
	wela=0;
	delay(1);
}

void dis_temp(uint t)
{
	uchar i;
	i=t/100;
	display(0,i);
	i=t%100/10;
	display(1,i);
	i=t%100%10;
	display(2,i);
}
void main()
{	
	uchar ii;
	while(1)
	{
		tempchange();
		for(ii=0;ii<5;ii++)
		{
			dis_temp(get_temp());
		}
	}
}

⌨️ 快捷键说明

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