找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1811|回复: 5
打印 上一主题 下一主题
收起左侧

做51单片机18B20 数码管一直闪烁 不知道该怎么弄 求大佬

[复制链接]
跳转到指定楼层
楼主
ID:811924 发表于 2020-9-7 16:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:213173 发表于 2020-9-7 19:42 | 只看该作者
参见类似现象的回帖http://www.51hei.com/bbs/dpj-194698-1.html
回复

使用道具 举报

板凳
ID:420836 发表于 2020-9-8 01:29 | 只看该作者
楼上的链接很有帮助。 该帖子为这个问题提供了正确的原因。
回复

使用道具 举报

地板
ID:691028 发表于 2020-9-10 13:09 | 只看该作者
你的Ds18b20ReadTemp()占用了太多时间导致LED刷新率太低。
你需要一个定时器做刷新。
回复

使用道具 举报

5#
ID:806634 发表于 2020-10-9 17:35 | 只看该作者
先把数码管是共阴极还是共阳极搞清楚
回复

使用道具 举报

6#
ID:708637 发表于 2020-10-9 18:00 | 只看该作者
1、消隐问题注意
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表