String Old
String Old
h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define ON 1
#define OFF 0
//////////////////////////
#define LED_OUT PORTD.3
#define POT_INPUT 2
#define TRIG_IN PIND.2
#define SIGNAL_OUT PORTB.2
////////////////////////
char tmr_fn=0;
char TRIG_SENSOR=OFF;
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//////
void init_system_IO()
{
DDRD.3=OUTPUT_MODE_PORT;
DDRD.2=INPUT_MODE_PORT;
DDRB.2=OUTPUT_MODE_PORT;
}
for(i=0;i!=avg_;i++)
{
AVG_BUFFER=(unsigned long int)read_adc(ch_)+AVG_BUFFER;
delay_ms(1);
}
tmr_fn=0;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 65.536 ms
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) |
(0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) |
(0<<CS10);
TCNT1=tm;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
}
void STOP_TIMER()
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) |
(0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) |
(0<<CS10);
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
START_TIMER(0xffff-d);
while(tmr_fn==0){}
return tmr_fn;
}
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
//LED_OUT=!LED_OUT;
tmr_fn=1;
STOP_TIMER();
}
//LED_OUT=!LED_OUT;
TRIG_SENSOR=ON;
/////////////////////////////////////////////////////////// MAIN
PROGRAM ///////////////////////////////////////////////////////////////////////////
/////////////////////////////
void main(void)
{
#define MIN_PULSE 600
#define MAX_PULSE 16300
// Timer variables
unsigned long previousMillis = 0;
unsigned int printInterval = 500;
unsigned long startTime = 0;
unsigned long stopTime = 0;
unsigned int currentValue=0;
unsigned int x=0;
//////////////////////////////////////////
delay_ms(100);
init_system_IO();
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADFR) | (0<<ADIF) | (0<<ADIE) | (0<<ADPS2) |
(1<<ADPS1) | (1<<ADPS0);
SFIOR=(0<<ACME);
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) |
(0<<MPCM);
UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (1<<TXEN) | (0<<UCSZ2) |
(0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) |
(1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
UBRRL=0x33;
STOP_TIMER();
#asm("sei")
for(i=0;i!=8;i++){LED_OUT=!LED_OUT;delay_ms(50);}
LED_OUT=OFF;
while (1)
{
#asm("wdr")
///////////////////////////////////////////////////////////////////////////////////
//////
if(TRIG_SENSOR==ON)
{
TRIG_SENSOR=OFF;
#asm("wdr")
SIGNAL_OUT=1;
delay_us(600);
SIGNAL_OUT=0;
if(POT_VALUE>1000){LED_OUT=ON;}else{LED_OUT=OFF;}
/////////////////////////////
delay_us(7600);
SIGNAL_OUT=1;
delay_us(240);
SIGNAL_OUT=0;
///////////////////////////////////////////////////
POT_VALUE=(unsigned int)POT_VALUE*13;
POT_VALUE=(unsigned int)13600-POT_VALUE;
delay_us_timer(POT_VALUE);
///////////////////////////////////////////////////
SIGNAL_OUT=1;
delay_us(240);
SIGNAL_OUT=0;
}
///////////////////////////////////////////////////////////////////////////////////
//////