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.

tm4c123gxl和ds18b20,为什么温度一直是16.06



我参照51单片机温度传感器例程改过来的

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"

//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>UART Echo (uart_echo)</h1>
//!
//! This example application utilizes the UART to echo text. The first UART
//! (connected to the USB debug virtual serial port on the evaluation board)
//! will be configured in 115,200 baud, 8-n-1 mode. All characters received on
//! the UART are transmitted back to the UART.
//
//*****************************************************************************


//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//*****************************************************************************
//
// The UART interrupt handler.
//
//*****************************************************************************
void
UARTIntHandler(void)
{
uint32_t ui32Status;

//
// Get the interrrupt status.
//
ui32Status = ROM_UARTIntStatus(UART0_BASE, true);

//
// Clear the asserted interrupts.
//
ROM_UARTIntClear(UART0_BASE, ui32Status);

//
// Loop while there are characters in the receive FIFO.
//
while(ROM_UARTCharsAvail(UART0_BASE))
{
//
// Read the next character from the UART and write it back to the UART.
//
ROM_UARTCharPutNonBlocking(UART0_BASE,
ROM_UARTCharGetNonBlocking(UART0_BASE));

//
// Blink the LED to show a character transfer is occuring.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);

//
// Delay for 1 millisecond. Each SysCtlDelay is about 3 clocks.
//
SysCtlDelay(SysCtlClockGet() / (1000 * 3));

//
// Turn off the LED
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

}
}

//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void
UARTSend( uint8_t *pui8Buffer, uint32_t ui32Count)
{
//
// Loop while there are more characters to send.
//
while(ui32Count--)
{
//
// Write the next character to the UART.
//
ROM_UARTCharPutNonBlocking(UART0_BASE, *pui8Buffer++);
}
}
//ÑÓʱ
void DelaySec(uint32_t sec)
{
uint32_t i , j= 0;

for(i=0; i<sec; i++)
{
for(j=0; j<65535; j++)
{
}
}
}

void Delay1ms(unsigned int y)
{
// unsigned char x;
// for( ; y>0; y--)
// {
// for(x=110; x>0; x--);
// }
SysCtlDelay(y*(SysCtlClockGet() / (1000 * 3)));
}
void Delay1us(unsigned int y)
{
SysCtlDelay(y*(SysCtlClockGet() / (1000000 * 3)));
}
unsigned char Ds18b20Init()
{
unsigned char i;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0); //½«×ÜÏßÀ­µÍ480us~960us
// i = 70;
// while(i--);//ÑÓʱ642us
Delay1us(642);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 1); //È»ºóÀ­¸ß×ÜÏߣ¬Èç¹ûDS18B20×ö³ö·´Ó¦»á½«ÔÚ15us~60usºó×ÜÏßÀ­µÍ
//i = 0;
Delay1us(15);
while(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_6)) //µÈ´ýDS18B20À­µÍ×ÜÏß
{
i++;
if(i>5)//µÈ´ý>5MS
{

return 0;//³õʼ»¯Ê§°Ü
}
Delay1ms(1);
}
return 1;//³õʼ»¯³É¹¦
}
void Ds18b20WriteByte(unsigned char dat)
{

unsigned int i, j;

for(j=0; j<8; j++)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0); //ÿдÈëһλÊý¾Ý֮ǰÏȰÑ×ÜÏßÀ­µÍ1us
//i++;
Delay1us(1);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6,dat & 0x01);
//È»ºóдÈëÒ»¸öÊý¾Ý£¬´Ó×îµÍλ¿ªÊ¼
// i=6;
// while(i--);//ÑÓʱ68us£¬³ÖÐøÊ±¼ä×îÉÙ60us
Delay1us(68);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 1);//È»ºóÊÍ·Å×ÜÏߣ¬ÖÁÉÙ1us¸ø×ÜÏ߻ָ´Ê±¼ä²ÅÄܽÓ×ÅдÈëµÚ¶þ¸öÊýÖµ
Delay1us(1);
dat >>= 1;
}
}

unsigned char Ds18b20ReadByte()
{
unsigned char byte, bi;
unsigned int i, j;
for(j=8; j>0; j--)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0);//ÿдÈëһλÊý¾Ý֮ǰÏȰÑ×ÜÏßÀ­µÍ1us
//i++;
Delay1us(1);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 1); //È»ºóдÈëÒ»¸öÊý¾Ý£¬´Ó×îµÍλ¿ªÊ¼
// i++;
// i++; //ÑÓʱ6usµÈ´ýÊý¾ÝÎȶ¨
Delay1us(6);
bi =GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_6); //¶ÁÈ¡Êý¾Ý£¬´Ó×îµÍλ¿ªÊ¼¶ÁÈ¡
/*½«byte×óÒÆÒ»Î»£¬È»ºóÓëÉÏÓÒÒÆ7λºóµÄbi£¬×¢ÒâÒÆ¶¯Ö®ºóÒÆµôÄÇλ²¹0¡£*/
byte = (byte >> 1) | (bi << 7);
// i = 4; //¶ÁÈ¡ÍêÖ®ºóµÈ´ý48usÔÙ½Ó×ŶÁÈ¡ÏÂÒ»¸öÊý
// while(i--);
Delay1us(48);
}
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;
unsigned char tmh, tml;
Ds18b20ChangTemp(); //ÏÈдÈëת»»ÃüÁî
Ds18b20ReadTempCom(); //È»ºóµÈ´ýת»»Íêºó·¢ËͶÁȡζÈÃüÁî
tml = Ds18b20ReadByte(); //¶ÁȡζÈÖµ¹²16룬ÏȶÁµÍ×Ö½Ú
tmh = Ds18b20ReadByte(); //ÔÙ¶Á¸ß×Ö½Ú
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
void show_temp(int temp)
{
unsigned char datas[] = {0, 0, 0, 0, 0};
temp=temp*0.0625*100+0.5;
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
ROM_UARTCharPut(UART0_BASE,'0'+ datas[0]);
DelaySec(20);
ROM_UARTCharPut(UART0_BASE, '0'+datas[1]);
DelaySec(20);
ROM_UARTCharPut(UART0_BASE, '0'+datas[2]);
DelaySec(20);
ROM_UARTCharPut(UART0_BASE, '.');
ROM_UARTCharPut(UART0_BASE, '0'+datas[3]);
DelaySec(20);
ROM_UARTCharPut(UART0_BASE, '0'+datas[4]);
DelaySec(20);
ROM_UARTCharPut(UART0_BASE, '\n');
}


//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************
int
main(void)
{
//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
ROM_FPUEnable();
ROM_FPULazyStackingEnable();

//
// Set the clocking to run directly from the crystal.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);

//
// Enable the GPIO port that is used for the on-board LED.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable the GPIO pins for the LED (PF2).
//
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2);
ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_3);
ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_4);
ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_5);
ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_6);

GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 1);
//
// Enable the peripherals used by this example.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);


//
// Enable processor interrupts.
//
ROM_IntMasterEnable();

//
// Set GPIO A0 and A1 as UART pins.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

//
// Configure the UART for 115,200, 8-N-1 operation.
//
ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));

//
// Enable the UART interrupt.
//
ROM_IntEnable(INT_UART0);
ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

//
// Prompt for text to be entered.
//
UARTSend((uint8_t *)"TEST ", 16);

//
// Loop forever echoing data through the UART.
//
while(1)
{
int temp1=Ds18b20ReadTemp();
show_temp(temp1);
temp1=0;
DelaySec(300);

}
}