基于MSP430G2553的DS18B20测温程序.doc_第1页
基于MSP430G2553的DS18B20测温程序.doc_第2页
基于MSP430G2553的DS18B20测温程序.doc_第3页
基于MSP430G2553的DS18B20测温程序.doc_第4页
基于MSP430G2553的DS18B20测温程序.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/* * main.c */*include header file*/#include #include system.h#include uart.h#include ds18b20.hunsigned 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();UartInit();_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_SendStr(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*/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_8MHZ;BCSCTL2 = SELM_1 + DIVM_1 + DIVS_3;/ MCLK = 8M;SMCLK = 1MBCSCTL2 &= SELS;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 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(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 initialization* 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 + UCBRS0; / Modulation UCBRSx = 3UCA0CTL1 &= UCSWRST; / *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(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)unsigned int q0;unsigned int n_Byte;n_Byte = strlen(pBuffer);for(q0 = 0;q0n_Byte;q0+)while(IFG2&UCA0TXIFG)=0);UCA0TXBUF = *pBuffer;pBuffer+;/*Interrupt Response Function*/* Function name: USCI0TX_ISR* Descriptions: Serial port transmit interrupt function* input parameters: none* output parameters: none* Returned value: none*/#pragma vector = USCIAB0TX_VECTOR_interrupt void USCI0TX_ISR(void)while(IFG2&UCA0TXIFG)=0);IE2 &= UCA0TXIE;/ close USCI_A0 TX interrupt/* Function name: USCI0RX_ISR* Descriptions: Serial port receive interrupt function* input parameters: none* output parameters: none* Returned value: none*/#pragma vector = USCIAB0RX_VECTOR_interrupt void USCI0RX_ISR(void)unsigned char RcvBuf = 0;RcvBuf = UCA0RXBUF;Uart_SendHexInt(RcvBuf);#ifndef UART_H_#define UART_H_/*Extern Function Declaration*/extern void UartInit(void);extern void Uart_SendHexInt(unsigned char);extern void Uart_SendStr(char*);#endif /* USART_H_ */*Function Declaration*/#defineDS18B20_Delay(us)_delay_cycles(8*us)voidDS18B20_WriteByte(unsigned char);unsigned charDS18B20_ReadByte(void);voidDS18B20_ReadBytes(unsigned char, unsigned char*);voidDS18B20_ReadID (unsigned char *id);voidDS18B20_Config(void);unsigned intDS18B20_ReadTemp(void);unsigned charDS18B20_CRC(unsigned char, unsigned char*);unsigned char CrcTable 256=0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53;unsigned char DS18B20_IDBuff8;/* Function name: DS18B20_Init* Descriptions:Initial DS18B20* input parameters: none* output parameters: none* Returned value: 0:detect ds18b20 fail!1:detect ds18b20 success!*/unsigned char DS18B20_Init(void)unsigned char return_val = 0;DS18B20_WRITE;DS18B20_Delay(5);DS18B20_DQ_CLR;DS18B20_Delay(500);DS18B20_DQ_SET;DS18B20_Delay(20);DS18B20_READ;DS18B20_Delay(5);if(DS18B20_DQ_IS_LOW)return_val = 1;/detect 1820 success!elsereturn_val = 0;/detect 1820 fail!DS18B20_Delay(100);DS18B20_WRITE;DS18B20_Delay(5);DS18B20_DQ_SET;return return_val;/* Function name: DS18B20_WriteByte* Descriptions:DS18B20 send one byte* input parameters: dat:byte* output parameters: none* Returned value: none*/void DS18B20_WriteByte(unsigned char dat)unsigned char i;DS18B20_WRITE;DS18B20_Delay(5);for (i=0;i= 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(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_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_Init();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(0x4e);/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(0xb8);/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); /skip rom DS18B20_WriteByte(0x44); /Temperature convert DS18B20_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* 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 verif

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论