以下是一份基于新唐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;
}
```
注意: 这个程序仅供参考。在实际应用中,需要根据具体硬件平台和需求适当进行修改。
以下是一份基于新唐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;
}
```
注意: 这个程序仅供参考。在实际应用中,需要根据具体硬件平台和需求适当进行修改。
举报