标题:
单片机程序功能:检测DS18B20并显示数码管上;问题初始化失败
[打印本页]
作者:
sssssdffs
时间:
2019-3-4 20:38
标题:
单片机程序功能:检测DS18B20并显示数码管上;问题初始化失败
程序功能:检测DS18B20并显示数码管上;奈何数码管一直显示A959;
晶振12 ;问题好像初始化不成功求大佬解答
#include<STC8.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define FOSC 12000000L
sbit w1=P1^0;
sbit w2=P1^1;
sbit w3=P1^2;
sbit w4=P1^3;//数码管位选
sbit k1=P1^4;
sbit k2=P1^5;
sbit k3=P1^6;
sbit k4=P1^7;
sbit DS=P5^4;数据口
sbit g=P5^5;
//define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative
uchar code SM[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code SM5[]={
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,0x87,
0xff,0xef,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void nb(uint z) //??
{
uint x,y;
x=z;
for(x;x>0;x--)
for(y=110;y>0;y--);
}
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void display(uint temp) //ÏÔʾ³ÌÐò
{
uchar A1,A2,A2t,A3;
A1=temp%10;
A2=temp/10%10;
A3=temp/100%10;
w1=0;
P3=SM[A1];
nb(50);
w1=1;
w2=0;
P3=SM[A2];
nb(50);
w2=1;
w3=0;
P3=SM[A3];
nb(50);
w3=1;
w4=0;
P3=0x77;nb(50);
w4=1;
}
void main()
{
uchar a;
tmpchange();
display(tmp());
while(1)
{tmpchange();
display(tmp());
}
}
作者:
LYZ5555
时间:
2019-3-20 15:06
DS18B20对时序的要求是很高的,有问题基本上都是时序的问题。当然软模的话可以排除硬件损坏的问题。另外注意连线的问题,才3个脚不容易错的。
时序的问题,关键的是延时的问题
作者:
我对你情有独钟
时间:
2019-4-1 13:52
可以用仿真,调一下程序
作者:
liushangming
时间:
2019-4-1 16:42
给你一个参考程序,带proteus仿真
DS18B20温度计.zip
2019-4-1 16:41 上传
点击文件名下载附件
90.8 KB, 下载次数: 23
可参考源码仿真
作者:
saya0769
时间:
2019-4-1 19:22
你是stc8系列的单片机。速度是很快的。检查一下延时吧。初始化的时候。延时大致是这样的。
void dsreset(void) //send reset and initialization command
{
uint i;
DS = 1;
de_us(10); // 10us
DS=0;
de_us(500); // 500us
DS=1;
de_us(60); // 60us
de_us(500); // 500us
}
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1