版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)/* * 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: n
2、one* Returned value:none*/void main(void)System_Init();UartInit();_EINT();if (DS18B20_Init()P1OUT |= BIT0;elseP1OUT |= BIT6;while (1)_delay_cycles();_delay_cycles();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 =
3、 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
4、 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;/
5、 Set DCO 1M 8M 12M 16MDCOCTL = CALDCO_8MHZ;BCSCTL2 = SELM_1 + DIVM_1 + DIVS_3;/ MCLK = 8M;SMCLK = 1MBCSCTL2 &= SELS;if (CALBC1_8MHZ = 0 xFF | CALDCO_8MHZ = 0 xFF)while(1);/hang-up/* Function name: Config_IO* Descriptions: IO Port Configuration* input parameters: none* output parameters: none* Return
6、ed value: none*/void Config_IO(void)/ set all IO port outputsP1DIR = 0 xFF; / All P1.x outputsP1OUT = 0 x00; / All P1.x resetP2DIR = 0 xFF; / All P2.x outputsP2OUT = 0 x00; / All P2.x reset/* Function name: System_Init* Descriptions: WatchDog Timer Configuration* input parameters: none* output param
7、eters: none* Returned value: none*/void System_Init(void)Config_WDT();Config_Clock();Config_IO();_delay_cycles();_delay_cycles();#ifndef SYSTEM_H_#define SYSTEM_H_/*Extern Function Declaration*/extern void System_Init(void);#endif /* SYSTEM_H_ */*/#include #include #include uart.h/* Function name: U
8、artInit* 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 = 0 x03; / 32kHz/9600 = 3.41
9、UCA0BR1 = 0 x00; /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 parameter
10、s: 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 v
11、alue: 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*
12、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 par
13、ameters: 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
14、_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
15、 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,
16、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,
17、 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,
18、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, 4
19、3, 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 ds18
20、b20 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!DS1
21、8B20_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;D
22、S18B20_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;DS18B2
23、0_Delay(12);if(DS18B20_DQ_IS_HIGH)dat |= 0 x80;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)
24、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(0 x33); /read ROMDS18B2
25、0_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(0 xcc);/skip ROMDS18B20_WriteByte(0 x4e);/write scratchpadDS18B20_WriteByte(0 x19);
26、/upper limitDS18B20_WriteByte(0 x1a);/lower limitDS18B20_WriteByte(0 x7f);/set 11 bit (0.125)DS18B20_Init();DS18B20_WriteByte(0 xcc);/skip ROMDS18B20_WriteByte(0 x48);/save set valueDS18B20_Init();DS18B20_WriteByte(0 xcc);/skip ROMDS18B20_WriteByte(0 xb8);/call-back set value/* Function name: DS18B2
27、0_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(0 xcc); /skip rom DS18B20_WriteByte(0 x44); /Temperatur
28、e convert DS18B20_Init ();DS18B20_WriteByte(0 xcc); /skip romDS18B20_WriteByte(0 xbe); /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
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版淘寶商家與消費者退款合同3篇
- 二零二五年度個人電子支付個人連帶責任保證合同4篇
- 二零二五年度基礎加固旋挖樁合同模板2篇
- 二零二五年度房屋抵押貸款合同(含還款計劃)4篇
- 二零二五年度創(chuàng)業(yè)培訓學習協(xié)議
- 二零二五版污泥運輸與污泥處理項目環(huán)境風險評估合同范本3篇
- 2025版職業(yè)培訓機構教師聘用合同3篇
- 河道預應力管樁施工方案
- 鐵路聲屏障施工方案
- 二零二五年度個人教育貸款擔保合同范例4篇
- 《個體防護裝備安全管理規(guī)范AQ 6111-2023》知識培訓
- 商品退換貨申請表模板
- 實習單位鑒定表(模板)
- 六西格瑪(6Sigma)詳解及實際案例分析
- 機械制造技術-成都工業(yè)學院中國大學mooc課后章節(jié)答案期末考試題庫2023年
- 數(shù)字媒體應用技術專業(yè)調研方案
- 2023年常州市新課結束考試九年級數(shù)學試卷(含答案)
- 正常分娩 分娩機制 助產學課件
- 廣東縣級農商銀行聯(lián)社高管候選人公開競聘筆試有關事項上岸提分題庫3套【500題帶答案含詳解】
- 中國成人住院患者高血糖管理目標專家共識課件
- 射頻技術在疼痛的應用課件
評論
0/150
提交評論