This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

能否帮我找下下面程序的错误,读不出来,用TM4C123G读DS18B20,时序能不能检查下谢谢了



#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_timer.h"
#include "inc/hw_ints.h"
#include "inc/hw_gpio.h"
#include "inc/hw_i2c.h"
#include "inc/hw_sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/ssi.h"
#include "driverlib/i2c.h"
#include "driverlib/udma.h"
#include "driverlib/fpu.h"
#include "driverlib/rom.h"
#include "driverlib/uart.h"
#include "inc/hw_uart.h"
#include "driverlib/adc.h"
#include "lcddriver/uc1701.h"
#include "lcddriver/hw_uc1701.h"
#include "driverlib/comp.h"
//延时毫秒
void delay_ms(int n)
{
    SysCtlDelay(n*(SysCtlClockGet()/3000));
}
//延时微秒
void delay_us(int n)
{
    SysCtlDelay(n*(SysCtlClockGet()/3000000));
}
//复位
void DS18B20_Rst(void)
{
    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_3);//设置相应的引脚为输出
    GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);//输出低电平
    delay_us(750);//输出低电平时间大于480us
    GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);//输出高电平
    delay_us(16);//延时
}
//检测是否有应答
//返回0,存在
uint8_t DS18B20_Check(void)
{
    uint8_t retry=0;
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE,GPIO_PIN_3);
    while (GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_3)&&retry<200)
    {
         retry++;
         delay_us(1);
    };
        if(retry>=200)return 1;
        else retry=0;
        while (!GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_3)&&retry<240)
        {
            retry++;
            delay_us(1);
        };
        if(retry>=240)return 1;
        return 0;
}
//读一位数据
//返回值是1/0
uint8_t DS18B20_Read_Bit(void)
{
    uint8_t data;
    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_3);//输出
    GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);//低电平
    delay_us(2);
    GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE,GPIO_PIN_3);//输入
    delay_us(12);
    if(GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_3))
        data=1;
    else data=0;
    delay_us(50);
    return data;
}
//读一个字节
uint8_t DS18B20_Read_Byte(void)
{
    uint8_t dat;
    uint8_t j,i;
        for (i=1;i<=8;i++)
        {
            j=DS18B20_Read_Bit();
            dat=(j<<7)|(dat>>1);
        }
        return dat;
}
void DS18B20_Write_Byte(uint8_t dat)
{
    uint8_t j;
    uint8_t testb;
    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_3);//输出
        for (j=1;j<=8;j++)
        {
            testb=dat&0x01;
            dat=dat>>1;
            if (testb)
            {
                GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);//1
                delay_us(2);
                GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
                delay_us(60);
            }
            else
            {
                GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);//0
                delay_us(60);
                GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
                delay_us(2);
            }
        }
}
//开始转换
void DS18B20_Start(void)
{
    DS18B20_Rst();
    DS18B20_Check();
    DS18B20_Write_Byte(0xcc);// skip rom
    DS18B20_Write_Byte(0x44);// convert
}
//初始化DS18B20的IO口 DQ 同时检测DS的存在
//返回1:不存在
//返回0:存在
uint8_t DS18B20_Init(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    GPIODirModeSet(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_DIR_MODE_OUT);
    GPIOPadConfigSet(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD);
    GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
    DS18B20_Rst();
    return DS18B20_Check();
}
short DS18B20_Get_Temp(void)
{
    uint16_t temp;
    uint16_t TL,TH;
    TL=0;
    TH=0;
    short tem;
    DS18B20_Start();                    // ds1820 start convert
    DS18B20_Rst();
    DS18B20_Check();
    DS18B20_Write_Byte(0xcc);// skip rom
    DS18B20_Write_Byte(0xbe);// convert
    TL=DS18B20_Read_Byte(); // LSB
    TH=DS18B20_Read_Byte(); // MSB
    if(TH>7)
    {
        TH=~TH;
        TL=~TL;
        temp=0;//温度为负
    }else temp=1;//温度为正
    tem=TH; //获得高八位
    tem<<=8;
    tem+=TL;//获得低 八位
    tem=(float)tem*625;//转换
    if(temp)return tem; //返回温度值
    else return -tem;
}
int main(void)
{
    SysCtlClockSet(SYSCTL_SYSDIV_3 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                       SYSCTL_OSC_MAIN); //设置时钟
    short temperature;
    UC1701Init(60000);
    UC1701Clear();
    while(DS18B20_Init())   //DS18B20初始化
    {
        UC1701CharDispaly(0, 0, "DS18B20 Error");
        delay_ms(200);
        delay_ms(200);
    }
    UC1701CharDispaly(0, 0, "DS18B20 OK");
    while(1)
    {
              temperature=DS18B20_Get_Temp();
              UC1701DisplayN(1,5,temperature);
              delay_ms(10);
        /*qian =  temperature /1000;
        bai = temperature % 1000 / 100;
        shi = temperature % 1000 % 100 / 10;
        ge = temperature % 10;
        UC1701CharDispaly(0,0,"THE TEMPERATURE:");
        UC1701DisplayN(1,1,qian);
        UC1701DisplayN(1,2,bai);
        UC1701CharDispaly(1,3,".");
        UC1701DisplayN(1,4,shi);
        UC1701DisplayN(1,5,ge);
        delay_ms(100);*/
    }
}