#include
#defineucharunsignedchar
#defineuintunsignedint
ucharint_flag;
uintqian,bai,shi,ge;
uinTI;
floatdistance;
//AlphanumericLCDModulefuncTIons
#asm
.equ__lcd_port=0x18;PORTB
#endasm
#include
//ExternalInterrupt0servicerouTIne
interrupt[EXT_INT0]voidext_int0_isr(void)
{#asm(“sei”)
if(int_flag)
{
distance=i*17.3;
}
elseif(int_flag==0)
{
i=0;
int_flag=1;
TCCR0=0x0A;
}
}
//TImer0outputcompareinterruptserviceroutine
interrupt[TIM0_COMP]voidtimer0_comp_isr(void)
{
i++;
}
//Declareyourglobalvariableshere
voidmain(void)
{
//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=0x01;
//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=0x04;
DDRD=0x00;
//Timer/Counter0initialization
//Clocksource:SystemClock
//Clockvalue:2000.000kHz
//Mode:CTCtop=OCR0
//OC0output:Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0xc8;
//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:On
//INT0Mode:Anychange
//INT1:Off
//INT2:Off
GICR|=0x40;
MCUCR=0x01;
MCUCSR=0x00;
GIFR=0x40;
//Timer(s)/Counter(s)Interrupt(s)initialization
TIMSK=0x02;
//AnalogComparatorinitialization
//AnalogComparator:Off
//AnalogComparatorInputCapturebyTimer/Counter1:Off
ACSR=0x80;
SFIOR=0x00;
//LCDmoduleinitialization
lcd_init(16);
//Globalenableinterrupts
#asm(“sei”)
PORTA.0=1;
delay_us(15);
PORTA.0=0;
while(1)
{
qian=(int)distance/1000;
bai=(int)distance00/100;
shi=(int)distance0/10;
ge=(int)distance;
lcd_gotoxy(0,0);
lcd_putchar(qian+48);
lcd_putchar(bai+48);
lcd_putchar(shi+48);
lcd_putchar(ge+48);
delay_ms(200);
PORTA.0=1;
delay_us(15);
PORTA.0=0;
int_flag=0;
};
}