温度传感器ds18b20-AVR程序代码
扫描二维码
随时随地手机看文章
#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
};
}