完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
以下是一份基于新唐M051系列MCU的DS18B20温度传感器程序:
``` #include #include "NuMicro.h" #define DELAY_TIME 15 int32_t temperature; uint8_t u8TempCount, u8TempData[9]; void OneWire_GPIO_Init(void) { GPIO_SetMode(PB, BIT5, GPIO_MODE_OUTPUT); PB5 = 1; } void OneWire_delay(uint32_t ms) { while(ms--) { CLK_SysTickDelay(2000); } } void OneWire_WriteBit(uint8_t u8Bit) { GPIO_SetMode(PB, BIT5, GPIO_MODE_OUTPUT); PB5 = 0; if(u8Bit) OneWire_delay(1); else OneWire_delay(DELAY_TIME); PB5 = 1; if(u8Bit) OneWire_delay(DELAY_TIME); else OneWire_delay(1); } uint8_t OneWire_ReadBit(void) { uint8_t u8Bit = 0; GPIO_SetMode(PB, BIT5, GPIO_MODE_OUTPUT); PB5 = 0; OneWire_delay(1); GPIO_SetMode(PB, BIT5, GPIO_MODE_INPUT); OneWire_delay(DELAY_TIME); if(PB5) u8Bit = 1; OneWire_delay(DELAY_TIME); return u8Bit; } void OneWire_Reset(void) { GPIO_SetMode(PB, BIT5, GPIO_MODE_OUTPUT); PB5 = 0; OneWire_delay(480); GPIO_SetMode(PB, BIT5, GPIO_MODE_INPUT); OneWire_delay(80); while(PB5 == 0); OneWire_delay(400); } void OneWire_WriteByte(uint8_t u8Data) { int i; for(i=0; i<8; i++) { OneWire_WriteBit(u8Data & 0x01); u8Data >>= 1; } } uint8_t OneWire_ReadByte(void) { int i; uint8_t u8Data = 0; for(i=0; i<8; i++) { u8Data >>= 1; if(OneWire_ReadBit()) u8Data |= 0x80; } return u8Data; } void DS18B20_Init(void) { OneWire_GPIO_Init(); OneWire_Reset(); OneWire_WriteByte(0xCC); // Skip ROM OneWire_WriteByte(0x4E); // Write Scratchpad OneWire_WriteByte(0x00); // Th register = 0 OneWire_WriteByte(0x00); // Tl register = 0 OneWire_WriteByte(0x1F); // Configuration Register OneWire_Reset(); OneWire_WriteByte(0xCC); // Skip ROM OneWire_WriteByte(0x44); // Convert T OneWire_Reset(); } void DS18B20_ReadROM(uint8_t *romdata) { int i; OneWire_Reset(); OneWire_WriteByte(0x33); // Read rom command for(i=0; i<8; i++) { romdata[i] = OneWire_ReadByte(); } } void DS18B20_ReadScratchpad(void) { int i; OneWire_Reset(); OneWire_WriteByte(0xCC); OneWire_WriteByte(0xBE); // Read Scratchpad for(i=0; i<9; i++) { u8TempData[i] = OneWire_ReadByte(); } } void DS18B20_GetRawTemperature(void) { uint32_t u32Temperature = 0; float fTemperature = 0; DS18B20_ReadScratchpad(); u32Temperature = u8TempData[1] << 8; u32Temperature |= u8TempData[0]; if(u32Temperature & 0x8000) { u32Temperature = ~u32Temperature + 1; fTemperature = -(float)u32Temperature / 16; } else { fTemperature = (float)u32Temperature / 16; } temperature = (int32_t)(fTemperature * 100); } int main() { SYS_UnlockReg(); /* Enable 12MHz oscillator */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk|CLK_PWRCON_OSC22M_EN_Msk|CLK_PWRCON_HXT_EN_Msk); /* Waiting for 12MHz clock ready */ CLK_SysTickDelay(2000); /* Switch HCLK clock source to HXT and set HCLK divider to HCLK/1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_CLKDIV0_HCLK(1)); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); /* Select UART module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV0_UART(1)); /* Reset UART module */ SYS_ResetModule(UART0_RST); /* Configure UART module */ UART_Open(UART0, 115200); /* Enable FMC ISP function */ FMC_Open(); /* Disable all interrupt */ __disable_irq(); /* Enable all interrupt */ __enable_irq(); printf("nnCPU @ %dHzn", SystemCoreClock); DS18B20_Init(); while(1) { DS18B20_GetRawTemperature(); printf("Temperature: %d.%02d Celsiusn", temperature/100, temperature%100); CLK_SysTickDelay(1000000); } return 0; } ``` 注意: 这个程序仅供参考。在实际应用中,需要根据具体硬件平台和需求适当进行修改。 |
|
|
|
只有小组成员才能发言,加入小组>>
3254 浏览 9 评论
2937 浏览 16 评论
3439 浏览 1 评论
8941 浏览 16 评论
4030 浏览 18 评论
1072浏览 3评论
557浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
550浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2285浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1843浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-5 13:25 , Processed in 1.072530 second(s), Total 49, Slave 39 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号