標(biāo)題: 求助,根據(jù)at90s8515寫的溫度檢測(cè),一直檢測(cè)出了的是-1.5,不知道哪里出了問(wèn)題 [打印本頁(yè)]

作者: 簡(jiǎn)單_生活    時(shí)間: 2018-10-19 10:32
標(biāo)題: 求助,根據(jù)at90s8515寫的溫度檢測(cè),一直檢測(cè)出了的是-1.5,不知道哪里出了問(wèn)題
程序如下,芯片頻率是12M
#include<90s8515.h>
#include<delay.h>

unsigned int temp;
unsigned char dis[4]={0,0,0,0};
unsigned char led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0xff,0xbf};//共陽(yáng)極數(shù)顯管顯示0,1,2,3,4,5,6,7,8,9,不顯示,負(fù)號(hào)
unsigned char led1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//帶小數(shù)點(diǎn)顯示

unsigned char init(void)    //ds18b20初始化
{
  unsigned char flag;  
  DDRD=1;//端口設(shè)為輸出,發(fā)送低電平   
  delay_us(550);//延時(shí)550us
  DDRD=0;//端口設(shè)為輸入,釋放總線,等待ds18b20響應(yīng)
  delay_us(68);//延時(shí)68us       
while(PIND);
while(!PIND);
if(PIND)
{flag=0;}
else
{flag=1;}       
  delay_us(500);       
  return flag;                                                                    
  }   

void  write(unsigned char dat)    //寫字節(jié)
{       
        unsigned char i=0;
        for(i=8;i>0;i--)
        {        
                 DDRD=1;//拉低總線
         delay_us(2);
         if(dat&1)
         {DDRD=1;}
         else
         {DDRD=0;}  
         dat>>=1;
         delay_us(60);//主機(jī)延時(shí)60us,等待ds18b20進(jìn)行采樣
         DDRD=0;//釋放總線  
         //delay_us(2);
         }
}

unsigned char read(void)   //讀字節(jié)
{
        unsigned  char i=0;
        unsigned char dat=0;  
        for(i=8;i>0;i--)
        {         DDRD=1; //定義為輸出,拉低總線
        delay_us(1);
        DDRD=0;//定義為輸入,釋放總線
        delay_us(9);  
        dat>>=1;
        if(PIND)
                dat|=0x80; //按位或然后賦值給dat,讀取數(shù)據(jù)最高位為1
        delay_us(65);
                }       

                return(dat);
}


void Display(unsigned int temp)     //顯示函數(shù)
{
    if(temp<=0x0800)//進(jìn)行溫度轉(zhuǎn)換
    {
    temp>>=4;//二進(jìn)制轉(zhuǎn)換成十進(jìn)制                                                
    temp*=10;
    dis[0]=temp/1000;
    dis[1]=temp%1000/100;
    dis[2]=temp%1000%100/10;
    dis[3]=temp%1000%100%10;
    }
    else
    {
     temp=~temp;
     temp+=1;
     temp>>=4;
     dis[0]=0x0c;
     dis[1]=temp%100/10;
     dis[2]=temp%100%10;
     dis[3]=0;
    }  
    DDRC=0X0F;
    DDRA=0XFF;

    PORTC=0x01;
    PORTA=led[dis[0]];
    delay_us(400);

    PORTC=0x02;
    PORTA=led1[dis[1]];
    delay_us(400);

    PORTC=0x04;
    PORTA=led[dis[2]];
    delay_us(400);

    PORTC=0x08;
    PORTA=led[dis[3]];
    delay_us(400);
}

void main()
{   
    unsigned char th,tl;
    while(1)
    {
    init();
    write(0xcc);//跳過(guò)ROM的操作
    write(0x44);//進(jìn)行溫度變換
    delay_ms(10);     

    init();
    write(0xcc);//跳過(guò)ROM的操作
    write(0xbe);//發(fā)出讀取RAM的命令  

    th=read();
    tl=read();
     temp=th<<8;
     temp|=tl;
     Display(temp);
   }
}


作者: zhanghoub    時(shí)間: 2018-10-19 12:09
temp=th<<8;會(huì)不會(huì)有問(wèn)題呀!th是8位變量,左移8位后給temp會(huì)不會(huì)出錯(cuò),應(yīng)該temp=th;temp=temp<<8;
作者: 簡(jiǎn)單_生活    時(shí)間: 2018-10-19 12:22
已經(jīng)解決了
作者: 簡(jiǎn)單_生活    時(shí)間: 2018-10-19 13:00
已經(jīng)解決了,就是不知道為什么數(shù)顯管不顯示“-”號(hào)
作者: wxyz    時(shí)間: 2018-11-7 13:02
樓主,請(qǐng)問(wèn)你的問(wèn)題是怎么解決的?
另外,懇求能不能上傳一份完整的程序,包括頭文件。
作者: 1095754445    時(shí)間: 2019-12-16 14:54
你是不是顯示程序沒寫對(duì)。。。我就經(jīng)常數(shù)碼管程序?qū)戝e(cuò)細(xì)節(jié),你仔細(xì)檢查一下啊




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1