//#include "suryaa.h"
#include <avr/interrupt.h>
static long distance=0;
void set_trigger_pin(void);//timer 3
void pin_configure(void);
void set_echo_pin(void);//timer4
void set_trigger_pin(void)
{
volatile char*tccr3a,*tccr3b,*ocr3al,*ocr3ah,*tcnt3h,*tcnt3l;
tccr3a=(volatile char*)0x90;
tccr3b=(volatile char*)0x91;
ocr3al=(volatile char*)0x98;
ocr3ah=(volatile char*)0x99;
tcnt3l=(volatile char*)0x94;
tcnt3h=(volatile char*)0x95;
*tcnt3l=0x00;
*tcnt3h=0x00;
*tccr3a = 0x40; // Set WGM02 bits for CTC mode and toggle OC0A on compare match
*tccr3b = 0x4D; // Set CS00 and CS02 bits for (prescaler = 1024,for 1 tick 64 mic.s)
*ocr3ah=0x01;//set compare match value(365)=365*64=23360m.s delay
*ocr3al =0x6D; // Set compare match value()
Serial.println("trigger pin set successful");
}
void pin_configure(void)
{
volatile char *dirL,*inL,*dire,*oute;
dirL=(volatile char *)0x10A;//set portL first bit as input for echo pin.
inL=(volatile char *)0x109;
dire=(volatile char*)0x2D;
oute=(volatile char*)0x2E;
*dirL=0x00;
*inL=0x00;
*dire=0xFF; //set ddr of pin 5;for 0c0a
*oute=0x00;
Serial.println("pin configuration is succesfull");
}
void set_echo_pin(void)
{
volatile char *tccr4b,*timsk4,*tcnt4h,*tcnt4l;
tccr4b=(volatile char*)0xA1;
timsk4=(volatile char*)0x72;
//tcnt4h=(volatile char*)0xA5;
//tcnt4l=(volatile char*)0xA4;
// *tcnt4h=0x00;
// *tcnt4l=0x00;
*tccr4b=0x42;//setting prescalar(8,so for 1 tick 0.5 mic.s delay)and +ve edge detect
*timsk4=0x20;//setting the innterupt mask flag for icp4.
Serial.println("echo pin is set succesfull");
}
ISR(TIMER4_CAPT_vect)
{
volatile char*icr4l,*icr4h,*tcnt4h,*tcnt4l;
volatile uint8_t x,y;
icr4l=(volatile char*)0xA6;
icr4h=(volatile char*)0xA7;
//tcnt4h=(volatile char*)0xA5;
// tcnt4l=(volatile char*)0xA4;
volatile long duration=0;
*tcnt4h=0x00;
*tcnt4l=0x00;
x=*icr4l; //assigning lower register value to variable x;
y=*icr4h; //assigning higher regiater value to variable y;
duration=((y<<8)|x)*0.5; //combining both register and make it as 16 bit.
//Serial.println(duration);
distance=duration*0.034/2;
Serial.print(distance);
Serial.print(" cm");
Serial.println();
}
void setup()
{
Serial.begin(9600);
set_trigger_pin();
pin_configure();
set_echo_pin();
sei();//to enable gloabal intterupt;
Serial.println("overall setup() is successful");
}
void loop()
{
volatile long x,i;
volatile char*inl=0x109;
x=*inl;
for(i=0;i<100000;i++);
Serial.print("the value at the echo pin:");
Serial.print(x);
Serial.print(" state");
Serial.println();
for(i=0;i<100000;i++);
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
led1:A
led1:C
r1:1
r1:2
led2:A
led2:C
r2:1
r2:2
led3:A
led3:C
r3:1
r3:2