Senin, 04 Juli 2011

contoh program ultrasonik ping paralax ngangge timer kados mekaten


#define PulseOut PORTA.0
#define PulseIn PINA.0
#define DirPulse DDRA.0

unsigned int counter;
float jarak;    
unsigned char pes1[16];
unsigned char pes2[16];
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here

counter=0;                 //Initial value
         DirPulse=1;                //Set as output
         PulseOut=1;
         delay_us(5);
         PulseOut=0;
         DirPulse=0;                //Set as input
         PulseOut=1;                //Pullup activated
         while (PulseIn==0) {}
         while (PulseIn==1)
         {
          counter++;
         }
         jarak=(counter*0.034442)/2;              // V=344.424m/s
         //lcd_gotoxy(0,0);
         //lcd_putsf ("halo");
         sprintf(pes1,"Counter=%5d uS",counter);
         sprintf(pes2,"S=%3.2f cm",jarak);      
         lcd_gotoxy(0,0);lcd_puts(pes1);
         lcd_gotoxy(0,1);lcd_puts(pes2);
         delay_ms(200);
         lcd_clear();
}
void main(void)
{
TCCR0=0x01;
TIMSK=0x01;
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
     

      }
}