'$sim

' SP3SWJ 3 września 2006  program dziala poprawnie na Atmega32
' program miniVNA dostosowany do VNA3



$regfile = "m8def.dat"
$crystal = 11059200


$hwstack = 32
$swstack = 10
$framesize = 40


$baud = 115200


 Dim Z As Long

 Dim Ddsword As Long
 Dim Dds As String * 15
 Dim Control As String * 15                                 ' niepotrzebnie długa  definicja
 Dim Ddsstep As Long
 Dim Ddsstep1 As String * 15
 Dim Empty As Byte

'AD9851  config bits W32 - W40
 Dim Dds_mode As Byte                                       ' 1x=0    6x=1  tryp pracy generatora DDS AD9851
 Dds_mode = 1                                               '0000 0001

 Dim Adcphase As Word
 Dim Adcmagnitude As Word

 Dim Sample As Long
 Dim Sample1 As String * 15

'====================================================



Config Portb.0 = Output                                     ' sterowanie przełącznikiem
Relay Alias Portb.0                                         'wprost do przekaxnika - on bierze 32 mA

'---------

Config Portd.7 = Output                                     ' DDS  FQ Update
Dds_fqud Alias Portd.7                                      '13 noga procka - 8 noga DDSA

Config Portd.5 = Output                                     'DDS DDS_CLOCK
Dds_clock Alias Portd.5                                     '11 noga procka - 7  noga DDSa

Config Portd.6 = Output                                     'DDS DATA
Dds_data Alias Portd.6                                      '12 noga procka - 25 noga DDSa


'ADC-0   Magnitude    23 noga procka  do  wyjscia 13 nogi AD8302
'ADC-1   Phase        24 noga procka  do  wyjscia 9 nogi AD8302
'ADC_reference        21 noga procka  do  11 nogi AD8302



'===========================================================

Empty = 0

Reset Relay
Waitms 50
Set Relay
Waitms 50
Reset Relay
Waitms 50
Set Relay
Waitms 50
Reset Relay


Reset Dds_fqud
Reset Dds_data
Reset Dds_clock

For Z = 0 To 100                                            'pauza
Next Z


    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2
    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2
    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2
    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2

  Set Dds_fqud
  nop
  nop
  nop
  nop
  Reset Dds_fqud
  nop
  nop


    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2
    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2
    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2
    Shiftout , Dds_data , Dds_clock , Empty , 3 , 8 , 2

  Set Dds_fqud
  nop
  nop
  nop
  nop
  Reset Dds_fqud
  nop
  nop






'Print "VNA sterring using miniVNA SW - upgrade by SP3SWJ"


'Print "DDS AD9851 init END"

   Ddsword = 24000000

        Shiftout , Dds_data , Dds_clock , Ddsword , 3 , 32 , 2       'zawsze wysyłane 32 bity    LSB first
        Shiftout , Dds_data , Dds_clock , Dds_mode , 3 , 8 , 2       'zawsze   dane wysyłane do FTW

        Set Dds_fqud
  nop
  nop
  nop
  nop
  Reset Dds_fqud
  nop
  nop


'Print "DDS AD9851 1kHz test tone send"
'dds test tone 1kHz


      Config Adc = Single , Prescaler = Auto


  'Program start

'Print "Program start"
'Print "NO ECHO 4 parameters > CONTROL  DDS_FTW  ITERATION DDS_STEP"


'=================================================================================  LOOP
  Start Adc

  Do

'Print " "
'Print "======================="
'Print "CONTROL 1 WOBLER  0 VNA"                             'xxxxxxxxxx
Input Control Noecho

If Val(control) = 1 Then                                    'VNA=0  or WOBLER=1
Set Relay
Else
Reset Relay
End If


'Print "DDS FTW    (24=1Hz) 24000 1kHz="                     'xxxxxxxxxxxxxxxxx

Input Dds Noecho

'Print "Sample no="                                          'xxxxxxxxxxxxx

Input Sample1 Noecho

'Print "DDSstep FTW="                                        'xxxxxxxxxx

Input Ddsstep1 Noecho

  nop
  nop
  nop
  nop
  nop
  nop
  nop


   Ddsword = Val(dds)
   Sample = Val(sample1)
   Ddsstep = Val(ddsstep1)


'----------------------------------------------------------------------
   For Z = 1 To Sample

'swj   DDS  nigdy nie generuje częstotoliwości START
'zawsze zaczyna od   START  + STEP

     Ddsword = Ddsword + Ddsstep

        Shiftout , Dds_data , Dds_clock , Ddsword , 3 , 32 , 2       'zawsze wysyłane 32 bity    LSB first
        Shiftout , Dds_data , Dds_clock , Dds_mode , 3 , 8 , 2       'zawsze   dane wysyłane do FTW


        'SHIFTOUT pin , pclock , var , option [, bits , delay ]
                                         ' Option can be :
                                         '3 – LSB shifted out first when DDS_CLOCK goes high

'Print "DDS FTW write do DDS =>" ; Ddsword                   'xxxxxxxxx


  Set Dds_fqud
  nop
  nop
  nop
  nop
  Reset Dds_fqud

  nop
  nop
  nop
  nop
  nop
  nop


  Adcphase = Getadc(1)
  Adcmagnitude = Getadc(0)

  nop
  nop
  nop
  nop
  nop
  nop

'Print "Adcphase=" ; Adcphase
Printbin Adcphase

  nop
  nop
  nop
  nop
  nop

'Print "Adcmagnitude=" ; Adcmagnitude
Printbin Adcmagnitude



Next Z
                                                'Adcphase    'Adcmagnitude
Loop