#include #include #include #include char text[32]; unsigned int cnt; int dis; void main(void) { PORTA=0x00; DDRA=0x00; PORTB=0x00; DDRB=0xFF; PORTC=0x00; DDRC=0x00; PORTD=0x00; DDRD=0x00; TCCR0=0x00; TCNT0=0x00; OCR0=0x00; TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x02; TCNT2=0x00; OCR2=0x00; MCUCR=0x00; MCUCSR=0x00; TIMSK=0x00; UCSRB=0x00; ACSR=0x80; SFIOR=0x00; ADCSRA=0x00; SPCR=0x00; TWCR=0x00; // RS - PORTB Bit 0 // RD - PORTB Bit 1 // EN - PORTB Bit 2 // D4 - PORTB Bit 4 // D5 - PORTB Bit 5 // D6 - PORTB Bit 6 // D7 - PORTB Bit 7 lcd_init(16); while (1) { DDRC.0=1; PORTC.0=1; delay_us(20); PORTC.0=0; DDRC.0=0; while (PINC.0==0); TCNT2=0; cnt=0; while (PINC.0==1) { if (TCNT2>250) { TCNT2=0; cnt++; } } dis=( cnt*255 + TCNT2 ) /58 ; lcd_gotoxy(0,0); sprintf(text,"distance=%3d",dis); lcd_puts(text); delay_ms(30); } }