当前位置:首页 > 单片机 > 单片机
[导读]#include#include//AlphanumericLCDModulefunctions#asm.equ__lcd_port=0x18;PORTB#endasm#include#defineucharunsignedchar#defineuintunsignedint#defineBIT(x)11;DQ_OUT;DQ_CLR;delay_us(10);DQ_SET;DQ_IN;if(DQ_

#include

#include

//AlphanumericLCDModulefunctions

#asm

.equ__lcd_port=0x18;PORTB

#endasm

#include

#defineucharunsignedchar

#defineuintunsignedint

#defineBIT(x)1<<(x)

#defineDQ_INDDRC&=~BIT(2)

#defineDQ_OUTDDRC|=BIT(2)

#defineDQ_SETPORTC|=BIT(2)

#defineDQ_CLRPORTC&=~BIT(2)

#defineDQ_RPINC&BIT(2)

uintwan,qian,bai,shi;

uchards18b20_reset(void)

{

uchari;

DQ_OUT;

DQ_CLR;

delay_us(500);

DQ_SET;

delay_us(100);

DQ_IN;

i=DQ_R;

delay_us(500);

returni;

}

voidds18b20_write_byte(ucharvalue)

{

uchari;

for(i=0;i<8;i++){

DQ_OUT;

DQ_CLR;

delay_us(10);

if(value&BIT(0))

{

DQ_SET;

}

delay_us(100);

DQ_SET;

value=value>>1;

}

}

uchards18b20_read_byte(void)

{

uchari,value;

for(i=0;i<8;i++)

{

value=value>>1;

DQ_OUT;

DQ_CLR;

delay_us(10);

DQ_SET;

DQ_IN;

if(DQ_R)

{

value|=0x80;

}

delay_us(50);

}

returnvalue;

}

//Declareyourglobalvariableshere

voidmain(void)

{uchari,j;

doubletemp;

//Declareyourlocalvariableshere

//Input/OutputPortsinitialization

//PortAinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTA=0x00;

DDRA=0x00;

//PortBinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTB=0x00;

DDRB=0x00;

//PortCinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTC=0x00;

DDRC=0x00;

//PortDinitialization

//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In

//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T

PORTD=0x00;

DDRD=0x00;

//Timer/Counter0initialization

//Clocksource:SystemClock

//Clockvalue:Timer0Stopped

//Mode:Normaltop=FFh

//OC0output:Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

//Timer/Counter1initialization

//Clocksource:SystemClock

//Clockvalue:Timer1Stopped

//Mode:Normaltop=FFFFh

//OC1Aoutput:Discon.

//OC1Boutput:Discon.

//NoiseCanceler:Off

//InputCaptureonFallingEdge

//Timer1OverflowInterrupt:Off

//InputCaptureInterrupt:Off

//CompareAMatchInterrupt:Off

//CompareBMatchInterrupt:Off

TCCR1A=0x00;

TCCR1B=0x00;

TCNT1H=0x00;

TCNT1L=0x00;

ICR1H=0x00;

ICR1L=0x00;

OCR1AH=0x00;

OCR1AL=0x00;

OCR1BH=0x00;

OCR1BL=0x00;

//Timer/Counter2initialization

//Clocksource:SystemClock

//Clockvalue:Timer2Stopped

//Mode:Normaltop=FFh

//OC2output:Disconnected

ASSR=0x00;

TCCR2=0x00;

TCNT2=0x00;

OCR2=0x00;

//ExternalInterrupt(s)initialization

//INT0:Off

//INT1:Off

//INT2:Off

MCUCR=0x00;

MCUCSR=0x00;

//Timer(s)/Counter(s)Interrupt(s)initialization

TIMSK=0x00;

//AnalogComparatorinitialization

//AnalogComparator:Off

//AnalogComparatorInputCapturebyTimer/Counter1:Off

ACSR=0x80;

SFIOR=0x00;

//LCDmoduleinitialization

lcd_init(16);

while(1)

{ds18b20_reset();

ds18b20_write_byte(0xcc);

ds18b20_write_byte(0x44);

delay_us(20);

ds18b20_reset();

ds18b20_write_byte(0xcc);

ds18b20_write_byte(0xbe);

i=ds18b20_read_byte();

j=ds18b20_read_byte();

temp=(j*256.0+i)*6.25;

wan=(int)temp/1000;

qian=(int)temp00/100;

bai=(int)temp0/10;

shi=(int)temp;

lcd_gotoxy(0,0);

lcd_putchar(wan+48);

lcd_putchar(qian+48);

lcd_putchar('.');

lcd_putchar(bai+48);

lcd_putchar(shi+48);

delay_ms(100);

//Placeyourcodehere

};

}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

在本教程中,我们将学习如何使用 DHT21 湿度和温度传感器与 Arduino 进行配合,并将测量值显示在 OLED 显示屏上。

关键字: OLED 显示屏 DHT21 温度传感器 Arduino

温度传感器则如同分布在电池表面的 “小卫士”,紧密关注着电池的温度变化。

关键字: 温度传感器

物联网与智能控制蓬勃发展,温度传感器作为感知环境温度变化的关键元件,广泛应用于工业生产、智能家居、医疗设备等众多领域。而单片机凭借其强大的控制能力和灵活的编程特性,成为处理温度传感器数据的理想平台。下面将深入剖析单片机程...

关键字: 单片机 温度传感器

全新温度传感器具备先进功能与封装尺寸灵活性,非常适合需要精密温度测量或温度补偿的应用。

关键字: 温度传感器 可充电电池组 工业探头

2025年09月11日,比利时泰森德洛·哈姆——全球微电子工程公司Melexis宣布,受性能和应用因素(包括目标物体的大小和距离)影响,选择合适的非接触式远红外(FIR)传感器颇具挑战性。人工评估不仅复杂耗时,还可能浪费...

关键字: 温度传感器 视场

在嵌入式系统开发中,STM32系列微控制器的内部温度传感器因其低成本、高集成度特性,广泛应用于设备自检、环境监测等场景。然而,受芯片工艺差异和电源噪声影响,其原始数据存在±1.5℃的固有误差。本文从硬件配置、校准算法、软...

关键字: STM32 温度传感器
关闭