All pastes #444018 Raw Edit

Untitled

public c v1 · immutable
·published 2007-04-17 04:32 UTC
rendered paste body
#define F_CPU 8000000UL#include <avr/signal.h>#include <avr/interrupt.h>#include <util/delay.h>int x=0;uint8_t led[4];//define times for start and end of signaluint16_t rising, falling;//this function sleeps s seconds (if F_CPU is set to frequency of processor)void sleep_sec(uint8_t s){  uint8_t i;  while (s--) for (i = 0; i < 100; i++) _delay_ms(10);}//this function sleeps s micro seconds (if F_CPU is set to frequency of processor)void sleep_usec(uint8_t s){  //uint8_t i;  while (s--) _delay_us(1);}//Timer1 capture interrupt service subroutineISR(TIMER1_CAPT_vect){	/*This subroutine checks was it start of pulse (rising edge)	or was it end (fallingedge)and performs required operations*/	if ((bit_is_set(PIND, 6))){ //if high level	//save start time	rising=ICR1;	led[0]=ICR1H;	led[1]=ICR1L;		//set to trigger on falling edge	TCCR1B = _BV(ICNC1)|_BV(CS10);	//DDRB = 0b11000000;	//PORTB = 0b10000000;}else{	//save falling time	falling=ICR1;	led[2]=ICR1H;	led[3]=ICR1L;    //rising edge triggers next	TCCR1B = _BV(ICNC1)|_BV(CS10)|_BV(ICES1);		DDRB = 0b00010000;	PORTB = 0b00000000;	sleep_sec(1);	int leda = 0b00000000;	//int led2 = 0b00000000;	leda = falling - rising; //TIME BETWEEN EDGES	leda /=8; //USING 8MHZ CHIP	leda /=58; //converting to cm	DDRB = 0xFF;	PORTB = (uint8_t)leda;	sleep_sec(1);	PORTB = led[0];	sleep_sec(7);	PORTB = led[1];	sleep_sec(7);	PORTB = led[2];	sleep_sec(7);	PORTB = led[3];	sleep_sec(7);  	}	}void pulse(){	DDRD = 0b00000001;	PORTD= 0b00000001;          //about 12us delay	asm volatile(	"ldi r16, 31\n\t"	"loop: dec r16\n\t"	"brne loop\n\t"	);	PORTD= 0b00000000;}int main(){	TIMSK = _BV(TICIE1);//|_BV(TOIE1);  	TCCR1B = _BV(CS10)|_BV(ICES1);//|_BV(ICNC1)        sei();	pulse();	  	while(1);	}