Trying to convert a bascom based program to Adruino C

Hey guys,

i am building a Auto Tuner for a ham radio.
a buddy of mine made a piece of code for his tuner in bascom. but my project is in Adruino C.

he made it use the TIMER1 but i cannot get is to work :confused:

the code he made is:

$regfile = "m328pdef.dat" ;
$crystal = 16000000
$hwstack = 40
$swstack = 40
$framesize = 32

$prog &HFF , &HEE , &HD9 , &HFF


Config Clockdiv = 1

Config Pind.3 = Output
Config Pind.7 = Input

Config Aci = On , Compare = On
Set Acsr.6

Config Timer0 = Timer , Prescale = 1
Tccr0a = &B10100011
Reset Tccr0b.3
Ocr0a = 240
Ocr0b = 127

Config Timer1 = Timer , Prescale = 1 , Compare A = Disconnect , Compare B = Disconnect , Clear Timer = 1 , Capture Edge = Falling , Noise Cancel = 0

Compare1a = 15999
On Compare1a 1m_seconde_int

On Icp1 Capture_int
Cls
Cursor Off

Dim A As Single
Dim B As Single

Dim Frequentie As Single
Dim Frequentie_hf As Single

Dim Periode_teller As Word
Dim Cyclus_timer As Word
Dim Cyclus_teller As Word
Dim Cyclus1 As Word
Dim Cyclus2 As Word
Dim T1 As Word
Dim T2 As Word


Declare Sub Meet_frequentie
Declare Sub Hf_frequentie

Enable Compare1a
Enable Interrupts

Do
   Meet_frequentie
   Hf_frequentie
   Waitms 100
Loop
End

Meet_frequentie:
   Cyclus_teller = 0
   Periode_teller = 0
   Cyclus_timer = 500
   Do
   Loop Until Cyclus_timer = 0

   If Periode_teller > 0 Then
     A = 16000
     A = A * Cyclus_teller
     A = A - T1
     A = A + T2
     B = 16001127
     B = B * Periode_teller
     Frequentie = B / A
   Else
     Frequentie = 0
   End If
Return

Hf_frequentie:
    Frequentie_hf = Frequentie
    Shift Frequentie_hf , Left , 15
    Frequentie_hf = Frequentie_hf / 1000000
Return

1m_seconde_int:
   If Cyclus_timer > 0 Then
   Toggle Portd.3
     Incr Cyclus_teller
     If Cyclus_teller = 1 Then
        Set Tifr1 . 5
        Enable Icp1
     End If
     If Cyclus_teller > Cyclus_timer Then
       Disable Icp1
       If Periode_teller > 0 Then Decr Periode_teller
       Cyclus_teller = Cyclus2 - Cyclus1
       Cyclus_timer = 0
       Reset Portd.3
     End If
   End If
Return

Capture_int:
   T2 = Capture1
   Cyclus2 = Cyclus_teller
   If Periode_teller = 0 Then
     T1 = T2
     Cyclus1 = Cyclus2
   End If
   Incr Periode_teller
   If Periode_teller > 65500 Then
     Disable Icp1
     Cyclus_timer = 0
     Periode_teller = 0
   End If
Return

i tried something like

int ControlPin = 11;

byte FrequencyByte;
byte FrequencyByteHF;

volatile long A;
volatile long B;
volatile unsigned int PeriodCounter = 0;
volatile unsigned int CycleTimer = 0;
volatile unsigned int CycleCounter = 0;
volatile unsigned int Cycle1 = 0;
volatile unsigned int Cycle2 = 0;
volatile unsigned int T1 = 0;
volatile unsigned int T2 = 0;

void setup()
{
    pinMode(ControlPin, OUTPUT);
    noInterrupts();
    interrupts();
}

ISR(TIMER1_COMPA_vect)
{
    if(CycleTimer > 0)
    {
        digitalWrite(ControlPin, digitalRead(ControlPin) ^ 1);
        CycleCounter++;
        if(CycleCounter == 1)
        {
             bitSet(TIFR1, 5);
             bitSet(TIMSK1, 5);
        }
        if(CycleCounter > CycleTimer)
        {
            bitSet(TIMSK1, 0);
            if(PeriodCounter > 0)
            {
                PeriodCounter--;
            }
            CycleCounter = Cycle2 - Cycle1;
            CycleTimer = 0;
            digitalWrite(ControlPin, LOW);
        }
    }
}

ISR(TIMER1_CAPT_vect)
{
    T2 = ICR1;
    Cycle2 = CycleCounter;
    if(PeriodCounter == 0)
    {
        T1 = T2;
        Cycle1 = Cycle2;
    }
    PeriodCounter++;
    if(PeriodCounter > 65500)
    {
        bitSet(TIMSK1, 0);
        CycleTimer = 0;
        PeriodCounter = 0;
    }
}

void loop() {}


void MeasureFrequency()
{
    CycleCounter = 0;
    PeriodCounter = 0;
    CycleTimer = 500;
    do
    {
    }
    while(CycleTimer != 0);
    
    if(PeriodCounter > 0)
    {
        A = 16000;
        A = A * CycleCounter;
        A = A - T1;
        A = A + T2;
        B = 16001127;
        B = B * PeriodCounter;
        FrequencyByte = B / A;
    }
    else
    {
        FrequencyByte = 0;
    }
}

void FrequencyHF()
{
    FrequencyByteHF = FrequencyByte;
    FrequencyByteHF<<15;
    FrequencyByteHF = FrequencyByteHF / 1000000;
}

jaapjolman: i tried something like

And what happened? Did it compile without errors? If not post the error message

If it compiiled what did it actually do when you ran it? And what do you really want it to do?

...R

It compiled oké but when it ran it froze the board.

the thing it needs to do is calculate the frequency that is on analogpin 7, we tried to do this by using timer1.

the main problem is that i don't really understand the timer1 and how to use it. my friend did and made it in bascom but i rather use adruino C because it is more common on this platform

Were from the netherlands so the comment is in dutch.

this is my buddies full code is the .txt file
my full code is the adruino file
i also attatched some pictures of the hardware

harm_antenne_tuner_outdoor.ino (8.91 KB)

Frequentie_Teller_1_fl_point (1) [44691856].txt (11.4 KB)

What sort of signal are you measuring - what is the frequency and what is the voltage range.

Trying to figure out your program would take me too long (I'm lazy). If you can provide an explanation of how each section is intended to work it may help. Sorry I don't understand Dutch.

...R

the signal is try to measure is a frequency of a hamradio the hardware is like

Ham Radio -> Coaxial Cable -> Frequency Bridge -> Coaxial Cable -> SWR Bridge -> Coaxial Cable -> Antenna.

the frequency bridge is connected to analog pin 7 and the swr bridge is connected to analog pin 6.

the frequency if i understand it correcly are pulses so my buddie made a function that measures the amount of pulses it sends in 500 cycles, the value it holds is the raw frequency then the "Meet_frequentie" function converts it to the right value so we get the real frequency of the ham radio.

Sorry, but your Reply #5 conveys no information to me. I know nothing about Ham radio.

What frequency (in Hz) is being presented to the Arduino and what is the peak-to-peak voltage?

that measures the amount of pulses it sends in 500 cycles

The answer to that is 500.

...R