基于MSP430G2553的DS18B20测温程序.docx

上传人:牧羊曲112 文档编号:3384963 上传时间:2023-03-12 格式:DOCX 页数:19 大小:39.77KB
返回 下载 相关 举报
基于MSP430G2553的DS18B20测温程序.docx_第1页
第1页 / 共19页
基于MSP430G2553的DS18B20测温程序.docx_第2页
第2页 / 共19页
基于MSP430G2553的DS18B20测温程序.docx_第3页
第3页 / 共19页
基于MSP430G2553的DS18B20测温程序.docx_第4页
第4页 / 共19页
基于MSP430G2553的DS18B20测温程序.docx_第5页
第5页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《基于MSP430G2553的DS18B20测温程序.docx》由会员分享,可在线阅读,更多相关《基于MSP430G2553的DS18B20测温程序.docx(19页珍藏版)》请在三一办公上搜索。

1、基于MSP430G2553的DS18B20测温程序/* * 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: n

2、one */ void main(void) System_Init; UartInit; _EINT; if (DS18B20_Init) P1OUT |= BIT0; else P1OUT |= 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_bu

3、f0 = 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 par

4、ameters: 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

5、_Clock(void) BCSCTL1 = CALBC1_8MHZ; / Set DCO 1M 8M 12M 16M DCOCTL = CALDCO_8MHZ; BCSCTL2 = SELM_1 + DIVM_1 + DIVS_3; / MCLK = 8M;SMCLK = 1M BCSCTL2 &= SELS; if (CALBC1_8MHZ = 0xFF | CALDCO_8MHZ = 0xFF) while(1); /hang-up /* * Function name: Config_IO * Descriptions: IO Port Configuration * input pa

6、rameters: none * output parameters: none * Returned value: none */ void Config_IO(void) / set all IO port outputs P1DIR = 0xFF; / All P1.x outputs P1OUT = 0x00; / All P1.x reset P2DIR = 0xFF; / All P2.x outputs P2OUT = 0x00; / All P2.x reset /* * Function name: System_Init * Descriptions: WatchDog T

7、imer 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

8、); #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=TXD P1SEL2 = BIT1 + BIT2 ;

9、/ P1.1 = RXD, P1.2=TXD UCA0CTL1 |= UCSSEL_1; / CLK = ACLK UCA0BR0 = 0x03; / 32kHz/9600 = 3.41 UCA0BR1 = 0x00; / UCA0MCTL = UCBRS1 + UCBRS0; / Modulation UCBRSx = 3 UCA0CTL1 &= UCSWRST; / *Initialize USCI state machine* IE2 |= UCA0RXIE; / Enable USCI_A0 RX interrupt /* * Function name: Uart_SendHexIn

10、t * 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 interrupt UCA0TXBUF = _data; /* * Function name: Uart_SendStr * Descriptions: Serial

11、 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;

12、 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)=

13、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 RcvB

14、uf = 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 */ #define DS18B20_Delay(us) _

15、delay_cycles(8*us) void DS18B20_WriteByte(unsigned char); unsigned char DS18B20_ReadByte(void); void DS18B20_ReadBytes(unsigned char, unsigned char*); void DS18B20_ReadID (unsigned char *id); void DS18B20_Config(void); unsigned int DS18B20_ReadTemp(void); unsigned char DS18B20_CRC(unsigned char, uns

16、igned 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

17、, 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,

18、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, 1

19、45, 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, 13

20、7, 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; DS18B2

21、0_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! else return_val = 0; /detect 1820 fail! DS18B20_Delay(100); DS18B20_WRITE; DS18B20_Delay(5); DS18B20_DQ_SET; r

22、eturn 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);

23、 /* * 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_HI

24、GH) 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;

25、 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 ROM DS18B20_Read

26、Bytes(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 ROM DS18B20_WriteByte(0x4e); /write scratchpad DS18B20_WriteByte(0x19); /upper limit DS18B20_WriteByte(0x1a); /lower limit DS18B20_WriteByte(0x7f); /set 11 bit (0.125) DS18B20_Init; DS18B20_WriteByte(0xcc); /skip ROM DS18B20_WriteByte(0x48); /save set value DS18B20_Init; DS18B20_WriteByte(0xcc); /skip ROM DS18B2

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号