1、/* main.c*/*include header file*/#include #include “system.h“#include “uart.h“#include “ds18b20.h“unsigned int temp;char tx_buf6 = 0,0,0,0,n,0;/* Function name: main* Descriptions: main function* input parameters: none* output parameters: none* Returned value: none*/void main(void)System_Init();Uart
2、Init();_EINT();if (DS18B20_Init()P1OUT |= BIT0;elseP1OUT |= BIT6;while (1)_delay_cycles(800000);_delay_cycles(800000);temp = DS18B20_GetTemp();/ x_buf0 = temp%10000/1000+0;/ tx_buf1 = temp%1000/100+0;/ tx_buf2 = temp%100/10+0;/ tx_buf3 = temp%10+0;tx_buf0 = temp%100/10+0;tx_buf1 = temp%10+0;Uart_Sen
3、dStr(tx_buf);/*Include header file*/#include #include “system.h“/*Function Declaration*/void Config_WDT(void);void Config_Clock(void);void Config_IO(void);/* Function name: Config_WDT* Descriptions: WatchDog Timer Configuration* input parameters: none* output parameters: none* Returned value: none*/
4、void Config_WDT(void)WDTCTL = WDTPW + WDTHOLD; / Stop watchdog timer/* Function name: Config_Clock* Descriptions: System Clock Configuration* input parameters: none* output parameters: none* Returned value: none*/void Config_Clock(void)BCSCTL1 = CALBC1_8MHZ; / Set DCO 1M 8M 12M 16MDCOCTL = CALDCO_8M
5、HZ;BCSCTL2 = SELM_1 + DIVM_1 + DIVS_3; / MCLK = 8M;SMCLK = 1MBCSCTL2 if (CALBC1_8MHZ = 0xFF | CALDCO_8MHZ = 0xFF)while(1); /hang-up/* Function name: Config_IO* Descriptions: IO Port Configuration* input parameters: none* output parameters: none* Returned value: none*/void Config_IO(void)/ set all IO
6、 port outputsP1DIR = 0xFF; / All P1.x outputsP1OUT = 0x00; / All P1.x resetP2DIR = 0xFF; / All P2.x outputsP2OUT = 0x00; / All P2.x reset/* Function name: System_Init* Descriptions: WatchDog Timer Configuration* input parameters: none* output parameters: none* Returned value: none*/void System_Init(
7、void)Config_WDT();Config_Clock();Config_IO();_delay_cycles(800000);_delay_cycles(800000);#ifndef SYSTEM_H_#define SYSTEM_H_/*Extern Function Declaration*/extern void System_Init(void);#endif /* SYSTEM_H_ */*/#include #include #include “uart.h“/* Function name: UartInit* Descriptions: Serial port ini
8、tialization* input parameters: none* output parameters: none* Returned value: none*/void UartInit(void)P1SEL = BIT1 + BIT2 ; / P1.1 = RXD, P1.2=TXDP1SEL2 = BIT1 + BIT2 ; / P1.1 = RXD, P1.2=TXDUCA0CTL1 |= UCSSEL_1; / CLK = ACLKUCA0BR0 = 0x03; / 32kHz/9600 = 3.41UCA0BR1 = 0x00; /UCA0MCTL = UCBRS1 + UC
9、BRS0; / Modulation UCBRSx = 3UCA0CTL1 / *Initialize USCI state machine*IE2 |= UCA0RXIE; / Enable USCI_A0 RX interrupt/* Function name: Uart_SendHexInt* Descriptions: Serial port send data by interrupt* input parameters: _data: data* output parameters: none* Returned value: none*/void Uart_SendHexInt
10、(unsigned char _data)IE2 |= UCA0TXIE; / enable USCI_A0 TX interruptUCA0TXBUF = _data;/* Function name: Uart_SendStr* Descriptions: Serial port send string* input parameters: *pBuffer: the buffer will be transmitted* output parameters: none* Returned value: none*/void Uart_SendStr(char *pBuffer)unsig
11、ned int q0;unsigned int n_Byte;n_Byte = strlen(pBuffer);for(q0 = 0;q0= 1;DS18B20_Delay(1);/* Function name: DS18B20_ReadByte* Descriptions: DS18B20 read one byte* input parameters: none* output parameters: none* Returned value: read byte*/unsigned char DS18B20_ReadByte(void)unsigned char i,dat=0;for
12、(i=0;i= 1;DS18B20_DQ_SET;DS18B20_READ;DS18B20_Delay(12);if(DS18B20_DQ_IS_HIGH)dat |= 0x80;DS18B20_Delay(5);return dat;/* Function name: DS18B20_ReadBytes* Descriptions: DS18B20 read bytes* input parameters: len: number of bytes* output parameters: *p: read buffer* Returned value: none*/void DS18B20_
13、ReadBytes(unsigned char len, unsigned char *p)unsigned char i;for(i=0;ilen;i+)*p = DS18B20_ReadByte();p+;/* Function name: DS18B20_ReadID* Descriptions: DS18B20 read ID* input parameters: none* output parameters: *id: ds18b20 ID* Returned value: none*/void DS18B20_ReadID(unsigned char *id)DS18B20_In
14、it();DS18B20_WriteByte(0x33); /read ROMDS18B20_ReadBytes(8,id);/* Function name: DS18B20_Config* Descriptions: DS18B20 internal config* input parameters: none* output parameters: none* Returned value: none*/void DS18B20_Config(void)DS18B20_Init();DS18B20_WriteByte(0xcc); /skip ROMDS18B20_WriteByte(0
15、x4e); /write scratchpadDS18B20_WriteByte(0x19); /upper limitDS18B20_WriteByte(0x1a); /lower limitDS18B20_WriteByte(0x7f); /set 11 bit (0.125)DS18B20_Init();DS18B20_WriteByte(0xcc); /skip ROMDS18B20_WriteByte(0x48); /save set valueDS18B20_Init();DS18B20_WriteByte(0xcc); /skip ROMDS18B20_WriteByte(0xb
16、8); /call-back set value/* Function name: DS18B20_GetTemp* Descriptions: DS18B20 get temperature* input parameters: none* output parameters: none* Returned value: temperature*/unsigned int DS18B20_GetTemp(void)DS18B20_ReadID(DS18B20_IDBuff);DS18B20_Config();DS18B20_Init ();DS18B20_WriteByte(0xcc); /
17、skip romDS18B20_WriteByte(0x44); /Temperature convertDS18B20_Init ();DS18B20_WriteByte(0xcc); /skip romDS18B20_WriteByte(0xbe); /read Temperaturereturn DS18B20_ReadTemp();/* Function name: DS18B20_ReadTemp* Descriptions: read temperature from ds18b20* input parameters: none* output parameters: none*
18、 Returned value: temperature buffer*/unsigned int DS18B20_ReadTemp(void)unsigned int Temperature;unsigned char temp_buff9;DS18B20_ReadBytes(9,temp_buff);if (DS18B20_CRC(9,temp_buff)=0) /CRC verify succeedTemperature = temp_buff1*0x100 + temp_buff0;/ Temperature *= 0.625;Temperature /= 16;return Temp
19、erature;/* Function name: DS18B20_CRC* Descriptions: CRC verification* input parameters: len: number of bytes* *temp_buff: verification array* output parameters: none* Returned value: verification result*/unsigned char DS18B20_CRC(unsigned char len, unsigned char *temp_buff)unsigned char i,crc_data=
20、0;for(i=0;ilen;i+) /table look-up verifycrc_data = CrcTablecrc_datatemp_buffi;return crc_data;#ifndef DS18B20_H_#define DS18B20_H_/*Port Definitions*/#define DS18B20_DQ_DIR P1DIR/#define DS18B20_DQ_OUT P1OUT/#define DS18B20_DQ_IN P1IN/#define DS18B20_DQ BIT5#define DS18B20_DQ_DIR P2DIR#define DS18B20_DQ_OUT P2OUT#define DS18B20_DQ_IN P2IN#define DS18B20_DQ BIT0/*Macro definition - Main clock frequency*/#define MCLKFRQ 8000000L/*Extern Function Declaration*/unsigned char DS18B20_Init(void);unsigned int DS18B20_GetTemp(void);#endif /* DS18B20_H_ */