+++++++++++++++++++PROGRAMA+++++++++++++++++++++++++++++++
'-------------------------------------------------------------------------------
'name : CH_AD9912_03_mod.bas
'copyright : (c) 2009, Dipl.Ing. Dr. Christian Hirt, Austria
'purpose : AD9912 DDS Control TEST
'micro : ATmega32-16
'flash : 35% used
'programmer : homebrew (STK200/STK300 emulation)
'compiler : MCS BASCOM-AVR 1.11.9.3
'hardware : AVR + AD9912 Modul + LCD
'code build : 20032009 (20. März 2009)
'status : Only simulated
'run time : 2,57 ms with LCD
'-------------------------------------------------------------------------------
'$sim
$regfile = "m644pdef.dat" 'ATmega32
'$PROG &hFF , &hE1 , &hD9 , &h00 'Fuse bits. Take care !!
$crystal = 11059200
'stack and framesize not optimized!
$hwstack = 64
$framesize = 64
$swstack = 64
$baud = 9600
' ----- Config LCD 16 x 2 lines -------
Ddrc.3 = 1 'R/W LCD bei Bascom normal auf GND
R_w Alias Portc.3 'R/W LCD bei Bascom normal auf GND
R_w = 0 'R/W to GND bei Bascom normal auf GND
Config Lcd = 16 * 2
Config Lcdpin = Pin , Rs = Portc.2 , E = Porta.1
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7
Config Lcdbus = 4
Initlcd 'wegen R/W Leitung nötig
Cls
Cursor Off
'----- Config AVR SPI ----------
'Config Portb = Output
Ddrb = &B11111011 'portb = Output/Input
Ddra = &B00000110 'PA1,PA2 = Output
Spcr = &B01010001 'SPI Control Register
' Bit 0 = 1 SPR0 - 11059200Hz/16 = 691.2 kHz bis 2.7 MHz getestet
' Bit 1 = 0 SPR1 - 11059200Hz/16 = 691.2 kHz
' Bit 2 = 0 CPHA - Clock Phase
' Bit 3 = 0 CPOL - Clock Polarity
' Bit 4 = 1 MSTR - Select as Master
' Bit 5 = 0 DORD - MSB first
' Bit 6 = 1 SPE - SPI enabled
' Bit 7 = 0 SPIE - No SPI interupt if SPIF is set
Rst Alias Portb.0 'RESET
Ioupd Alias Porta.2 'IO-Update
Cs1 Alias Portb.4 'SPI-Chip1 Select
Cs2 Alias Portb.3 'SPI-Chip2 Select
'Sdio Alias Portb.5 'Harware SPI kein Config nötig
'Sclk Alias Portb.7 'Harware SPI kein Config nötig
Rst = 1 'Inverse = 0
Waitms 10
'----- AD9912 Instruction Word Bits -----
Const Iwb = &B0110_0000 [EMAIL="'@MSB"]'@MSB[/EMAIL] first
'Bit15 = R/W = 0 => Write
'Bit14 = W1 = 1 and Bit13 = W0 = 1 => Streaming Mode
'Bit0 - Bit12 => Selected Register Address (AD9912 Register Map)
'----- Declarations ----------------------
Dim D_ftw As Double
Dim Ref_freq As Double
Dim Freq As Double
Dim Freq_anz As Long 'Zusätlich für Frequenzanzeige LCD
Dim D_temp1 As Double , D_temp2 As Double
Dim Ftwo(6) As Byte
Dim L_j As Long
Dim Fstrng As String * 9
Dim S As String * 9
Dim S1 As String * 11
Dim Ftw As String * 16
Dim I As Byte
Dim A As Byte
Declare Sub Writedata(byval Dat As Byte)
Declare Sub Setaddr(byval Addr As Word)
Declare Sub Initad9912
Declare Sub Calcftwo
Declare Sub Writeftwo
Declare Sub Showfreqlcd
Declare Sub Showftwo_lcd
'Main begin -----------
Call Initad9912
Ref_freq = 1000000000 'Reference Frequency 1 GHz
Freq = 128500000 '--> Testfrequenz 300.0 MHz
Call Calcftwo
Call Writeftwo
Call Showfreqlcd
Call Showftwo_lcd
Print Ftwo(1)
Print Ftwo(6)
End ' Main End --------
'##########################################################################
Function Writedata(byval Dat As Byte)
Spdr = Dat 'AVR SPI Data Register
Bitwait Spsr.7 , Set 'wait until SPIF = 1
End Function
Function Setaddr(byval Addr As Word) 'Set 16-bit Instruction Word
Reset Cs1 'CS = 0
Reset Ioupd 'IOUPD = 0
A = High(addr)
Spdr = A Or Iwb 'Instruction Word Bits
Bitwait Spsr.7 , Set 'wait until SPIF = 1
A = Low(addr)
Spdr = A
Bitwait Spsr.7 , Set 'wait until SPIF = 1
End Function
Sub Initad9912
'AD9912 Chip Reset
Rst = 0 'Reset = 1 (Ansteuerung inverse)
Waitms 10
Rst = 1 'Reset = 0 (Ansteuerung inverse)
Waitms 10
Reset Ioupd 'IOUPD = 0
Set Cs1 'CS = 1
Call Setaddr(&H0000) 'Set Serial Config Register
Call Writedata(&B0001_1000) 'Default Hex 18 vorher keine DDS-Funktion!!
'SDO Active = D0,D7 = 0 => 3-wire mode with SDIO
'LSB First = D1,D6 = 0 => MSB first
'Soft Reset = D2,D5 = 0 => S1 to S4 tristated
'Long Instr = D3,D4 = 1 => Long instructions only for Read
Set Cs1 'CS = 1
Call Setaddr(&H0010) 'Set Power-Down and Enable
Call Writedata(&B1100_0000)
'HSTL Power down = D7 = 1
'Disable CMOS = D6 = 0
'PLL Power Down = D4 = 1
Set Cs1 'CS = 1
'wait 1
Call Setaddr(&H0022)
Call Writedata(&B0000_0000)
Set Cs1
Call Setaddr(&H0500)
Call Writedata(&B1000_1111)
Set Cs1
Call Setaddr(&H0505)
Call Writedata(&B1000_1111)
Set Cs1
End Sub
Sub Writeftwo 'Write FTWO into AD9912
Call Setaddr(&H01ab)
For A = 1 To 6
Spdr = Ftwo(a) 'SPI Data Register
Bitwait Spsr.7 , Set 'wait until SPIF = 1
Next
Set Cs1 'CS = 1
Set Ioupd 'IOUPD = 1
End Sub
'Calculate FTWO from Freq (Algorithm from Hecker & Steih)
Sub Calcftwo
D_ftw = Freq / Ref_freq
For I = 1 To 6
D_temp1 = D_ftw * 256.0
D_temp2 = Int(d_temp1)
D_ftw = D_temp1 - D_temp2
L_j = D_temp2
Ftwo(i) = L_j
Next I
End Sub
Sub Showfreqlcd 'show Freqency LCD upper line
Freq_anz = Freq 'Left,Mid,Right funktioniert nicht mit Double
Fstrng = Str(freq_anz)
S = Format(fstrng , "000000000")
S1 = Left(s , 3) + "." + Mid(s , 4 , 3) + "." + Right(s , 3)
Upperline
Cursor Off
Lcd "F " ; S1 ; " Hz"
End Sub
Sub Showftwo_lcd 'show FTWO (hex) LCD lower line
Ftw = "FTW "
For A = 1 To 6
Ftw = Ftw + Hex(ftwo(a))
Next
Lowerline
Cursor Off
Lcd Ftw
End Sub
'############################## End ############################################