This is one of my early attempts, more like educational example.
Code is based on previous Joop's work.
I just made adaptation for Barracuda PI metal detector.
I will use this occasion too; to place big THANKS and tribute to previous Joop's work!
Code is pretty much self explanatory. Also Joop added essential comments in it.
I think there is no need me to add anything above.
Code is based on previous Joop's work.
I just made adaptation for Barracuda PI metal detector.
I will use this occasion too; to place big THANKS and tribute to previous Joop's work!
Code is pretty much self explanatory. Also Joop added essential comments in it.
I think there is no need me to add anything above.
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LCDBitmap.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
LCDBitmap bitmap(&lcd, 12, 0);
#define DIGITAL_OUT PORTB
#define TX_PIN_A 5 // 13
#define SAMPLE_PIN_B 4 // 12
#define SAMPLE_PIN_C 3 // 11
unsigned int Sample1Delay = 65295;
static int value;
static int Mikrosekunde;
void setup ()
{
Init ();
InitTimer2 ();
InitTimer1 ();
TCNT2 = 200;
//*********************
Serial.begin(1200);
lcd.begin(16,2);
lcd.clear ();
lcd.setCursor(2, 0);
lcd.print("BARRACUDA");
//**************************************************
bitmap.begin();
bitmap.home();
for (byte x=0; x<=4; x++)
{
bitmap.line(x, BITMAP_H-1, x+BITMAP_H-1, 0, ON, NO_UPDATE);
bitmap.line(x, 0, x+BITMAP_H-1, BITMAP_H-1, ON, NO_UPDATE);
}
bitmap.update();
}
void VoidDelay (unsigned int n)
{
n += 12;
TCNT1 = n;
bitSet (TIFR1, TOV1);
while (!(TIFR1 & (1<<TOV1))) {;}
}
void loop ()
{
lcd.setCursor(0, 1);
lcd.print("D=");
lcd.print(Mikrosekunde);
lcd.print("uS ");
delay(50);
}
void InitTimer1 (void)
{
TCCR1A = 0;
TCCR1B = 0;
bitClear (TIMSK1, TOIE1);
TCNT1 = 0;
TCCR1B = 0x01;
}
void InitTimer2 (void)
{
TCCR2A = 0;
TCCR2B = 0;
bitSet (TIMSK2, TOIE2); // Enable Timer2 interrupt, TIMSKx, Timer/Counter Interrupt Mask Register
TCCR2B = 0x05; // Prescaler = /128 (8 us resolution)
}
void Init (void)
{
DDRB = B00111111;
}
void ReadDelayPot (void)
{
value = analogRead(A0);
Sample1Delay = 65535 - 16 - value;
Mikrosekunde = (65535 -Sample1Delay)/16;
}
ISR (TIMER2_OVF_vect)
{
TCNT2 = 47;
//******************************** MAIN PULSE - TX ******************************
DIGITAL_OUT |= (1<<TX_PIN_A);
VoidDelay (63935); // 65535-63935=1600, 1600/16=100uS
DIGITAL_OUT &= ~(1<<TX_PIN_A);
//*******************************************************************************
//******************************** MAIN DELAY ***********************************
VoidDelay (Sample1Delay); // Main Delay pauza
//*******************************************************************************
//****************************** MAIN DELAY PULSE *******************************
DIGITAL_OUT |= (1<<SAMPLE_PIN_B);
VoidDelay (64850); // 65535-64850=685, 685/16=42,8125 uS
DIGITAL_OUT &= ~(1<<SAMPLE_PIN_B);
//*******************************************************************************
//******************************** EF DELAY *************************************
VoidDelay (65135); // 65535-65135=400, 400/16=25uS
//*******************************************************************************
//****************************** EF DELAY PULSE *********************************
DIGITAL_OUT |= (1<<SAMPLE_PIN_C);
VoidDelay( 64850); // 65535-64850=685, 685/16=42,8125 uS
DIGITAL_OUT &= ~(1<<SAMPLE_PIN_C);
//*******************************************************************************
//******************************** EF+TX DELAY **********************************
VoidDelay( 64850); // 65535-64850=685, 685/16=42,8125 uS
//*******************************************************************************
ReadDelayPot();
}

Comment