标题:
做51单片机18B20 数码管一直闪烁 不知道该怎么弄 求大佬
[打印本页]
作者:
zzk15963
时间:
2020-9-7 16:42
标题:
做51单片机18B20 数码管一直闪烁 不知道该怎么弄 求大佬
#include "reg52.h"
#include"18B20ZHONGJI.H"
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
char num=0;
u8 DisplayData[8];
u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/*******************************************************************************
* ?? ?? ?? : delay
* ???????? : ?????????i=1?????????10us
*******************************************************************************/
void delay(u16 i)
{
while(i--);
}
/*******************************************************************************
* ?? ?? ?? : datapros()
* ???????? : ????????????????
* ?? ?? : temp
* ?? ?? : ??
*******************************************************************************/
void datapros(int temp)
{
float tp;
if(temp< 0) //???????????
{
DisplayData[0] = 0x40; // -
//????????????????????????????1?????????????
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//??????С?????*100??+0.5?????????????C??????????????????????С????
//????????????????????????0.5????+0.5??????0.5??????1???С??0.5???
//?????0.5????????С??????档
}
else
{
DisplayData[0] = 0x00;
tp=temp;//????????????С???????????????????????????
//?????????????????????????????????????????
temp=tp*0.0625*100+0.5;
//??????С?????*100??+0.5?????????????C??????????????????????С????
//????????????????????????0.5????+0.5??????0.5??????1???С??0.5???
//?????0.5????????С??????档
}
void DigDisplay()
{
u8 i;
for(i=0;i<6;i++)
{
switch(i) //λ?????????????????
{
case(0):
LSA=1;LSB=1;LSC=1; break;//?????0λ
case(1):
LSA=0;LSB=1;LSC=1; break;//?????1λ
case(2):
LSA=1;LSB=0;LSC=1; break;//?????2λ
case(3):
LSA=0;LSB=0;LSC=1; break;//?????3λ
case(4):
LSA=1;LSB=1;LSC=0; break;//?????4λ
case(5):
LSA=0;LSB=1;LSC=0; break;//?????5λ
delay(100);
}
P0=DisplayData[i];//????????
delay(1); //????????????
P0=0xFF;//????
}
}
void main()
{
while(1)
{
datapros(Ds18b20ReadTemp()); //??????????
DigDisplay();//????????????
}
}#include"18b20zhongji.h"
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
uchar Ds18b20Init()
{
uchar i;
DSPORT = 0; //??????????480us~960us
i = 70;
while(i--);//???642us
DSPORT = 1; //???????????????DS18B20???????????15us~60us??????????
i = 0;
while(DSPORT) //???DS18B20????????
{
Delay1ms(1);
i++;
if(i>5)//???>5MS
{
return 0;//????????
}
}
return 1;//????????
}
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0; //?д???λ?????????????????1us
i++;
DSPORT = dat & 0x01; //???д???????????????λ???
i=6;
while(i--); //???68us?????????????60us
DSPORT = 1; //???????????????1us?????????????????д?????????
dat >>= 1;
}
}
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DSPORT = 0;//???????????1us
i++;
DSPORT = 1;//??????????
i++;
i++;//???6us??????????
bi = DSPORT; //?????????????λ??????
/*??byte?????λ?????????????7λ???bi????????????????λ??0??*/
byte = (byte >> 1) | (bi << 7);
i = 4; //??????????48us??????????????
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //????ROM????????
Ds18b20WriteByte(0x44); //??????????
//Delay1ms(100); //??????????????????????????????????????????
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //????ROM????????
Ds18b20WriteByte(0xbe); //?????????????
}
int Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp(); //??д?????????
Ds18b20ReadTempCom(); //??????????????????????
tml = Ds18b20ReadByte(); //?????????16λ??????????
tmh = Ds18b20ReadByte(); //????????
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
#ifndef __18b20zhongji_H_
#define __18b20zhongji_H_
#include<reg52.h>
//---?????????---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
//--????????IO??--//
sbit DSPORT=P3^7;
//--??????????--//
void Delay1ms(uint );
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar com);
uchar Ds18b20ReadByte();
void Ds18b20ChangTemp();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
#endif
作者:
wulin
时间:
2020-9-7 19:42
参见类似现象的回帖
http://www.51hei.com/bbs/dpj-194698-1.html
作者:
TTQ001
时间:
2020-9-8 01:29
楼上的链接很有帮助。 该帖子为这个问题提供了正确的原因。
作者:
0x00000000
时间:
2020-9-10 13:09
你的Ds18b20ReadTemp()占用了太多时间导致LED刷新率太低。
你需要一个定时器做刷新。
作者:
wocjisji
时间:
2020-10-9 17:35
先把数码管是共阴极还是共阳极搞清楚
作者:
小炒肉111
时间:
2020-10-9 18:00
1、消隐问题注意
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1