Encode

2
E:\Electronica\Proyectos pendientes electronica\DDS\AD9833\877ddscode1\encode.mbas viernes, 17 de julio de 2015 12:13 program encode ' Copyright 2012 by IZ1TDS - feel free to use and modify - Please credit if ' redistribute modified code ' For understanding refer to Analog Devices App Note AN 1070 ' PIC 16F877a 4 Mhz XTAL (XT mode) - AD9834 single output on out 0 dim LCD_EN as sbit at RB1_bit LCD_RS as sbit at RD7_bit LCD_D7 as sbit at RB5_bit LCD_D6 as sbit at RB4_bit LCD_D5 as sbit at RB3_bit LCD_D4 as sbit at RB2_bit dim LCD_EN_Direction as sbit at TRISB1_bit LCD_RS_Direction as sbit at TRISD7_bit LCD_D7_Direction as sbit at TRISB5_bit LCD_D6_Direction as sbit at TRISB4_bit LCD_D5_Direction as sbit at TRISB3_bit LCD_D4_Direction as sbit at TRISB2_bit dim counter as longword ' dim frequo as longword dim sword, cword as longword dim ach as float dim freqs as string[16] dim LSW,MSW as word dim i as integer const fixx as longword = 268435456 ' 2 ^ 28 (because 28 bit control word) const mckl as longword = 50000000 ' master clock value - change if needed sub procedure DDSend(dim value as word) ' Software SPI routine 16bit transfer SetBit(PORTD, 5) ' SCLK SetBit(PORTD, 4) ' CS SetBit(PORTD, 1) ' DATA ClearBit(PORTD, 4) for i = 0 to 15 if ((value and $8000) = 0) then ClearBit(PORTD, 1) else SetBit(PORTD, 1) end if value = value << 1 ClearBit(PORTD, 5) SetBit(PORTD, 5) next i SetBit(PORTD, 4) SetBit(PORTD, 1) end sub sub procedure DDSUpdate(dim freq as longword) ach = (freq / mckl) * fixx ' calculate frequency word for output 0 cword = ach '' round to integer value LSW = cword ''' control word LSW - refer to app note LSW = $4000 or LSW sword = cword >> 14 MSW = sword MSW = $4000 or MSW -1-

description

Codigo picBasic

Transcript of Encode

Page 1: Encode

E:\Electronica\Proyectos pendientes electronica\DDS\AD9833\877ddscode1\encode.mbas viernes, 17 de julio de 2015 12:13

program encode' Copyright 2012 by IZ1TDS - feel free to use and modify - Please credit if' redistribute modified code' For understanding refer to Analog Devices App Note AN 1070' PIC 16F877a 4 Mhz XTAL (XT mode) - AD9834 single output on out 0dim LCD_EN as sbit at RB1_bit LCD_RS as sbit at RD7_bit LCD_D7 as sbit at RB5_bit LCD_D6 as sbit at RB4_bit LCD_D5 as sbit at RB3_bit LCD_D4 as sbit at RB2_bitdim LCD_EN_Direction as sbit at TRISB1_bit LCD_RS_Direction as sbit at TRISD7_bit LCD_D7_Direction as sbit at TRISB5_bit LCD_D6_Direction as sbit at TRISB4_bit LCD_D5_Direction as sbit at TRISB3_bit LCD_D4_Direction as sbit at TRISB2_bitdim counter as longword'dim frequo as longworddim sword, cword as longworddim ach as floatdim freqs as string[16]dim LSW,MSW as worddim i as integer

const fixx as longword = 268435456 ' 2 ^ 28 (because 28 bit control word)const mckl as longword = 50000000 ' master clock value - change if needed

sub procedure DDSend(dim value as word) ' Software SPI routine 16bit transferSetBit(PORTD, 5) ' SCLKSetBit(PORTD, 4) ' CSSetBit(PORTD, 1) ' DATAClearBit(PORTD, 4) for i = 0 to 15 if ((value and $8000) = 0) then ClearBit(PORTD, 1) else SetBit(PORTD, 1) end if value = value << 1 ClearBit(PORTD, 5) SetBit(PORTD, 5) next i SetBit(PORTD, 4) SetBit(PORTD, 1)end sub

sub procedure DDSUpdate(dim freq as longword)

ach = (freq / mckl) * fixx ' calculate frequency word for output 0cword = ach '' round to integer valueLSW = cword ''' control word LSW - refer to app noteLSW = $4000 or LSWsword = cword >> 14MSW = sword

MSW = $4000 or MSW

-1-

Page 2: Encode

E:\Electronica\Proyectos pendientes electronica\DDS\AD9833\877ddscode1\encode.mbas viernes, 17 de julio de 2015 12:13

DDSend ($2100) ''' enter reset modeDDSend (LSW)DDSend (MSW)DDSend ($C000) ''' Phase 0DDSend ($2000) ''' leave reset modeend sub

sub procedure interrupt

if PORTD.6 = 0 thencounter = counter + 500 ' step 500 Hz change if you wantelsecounter = counter - 500end if

DDSUpdate(counter)

INTCON.INTF = 0 ' reset interruptend sub''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''main:Lcd_Init()Lcd_Cmd(_LCD_CLEAR)Lcd_Cmd(_LCD_CURSOR_OFF)Lcd_Out_Cp("IZ1TDS DDS 28bit")delay_ms(50)TRISD = 0delay_ms(10)SetBit(PORTD, 1) ' SPI DataSetBit(PORTD, 4) ' SPI Chip SelectSetBit(PORTD, 5) ' SPI Clockdelay_ms(10) ClearBit(PORTD, 0) ' delay_us(1) SetBit(PORTD, 0) ' RESET delay_us(1) ClearBit(PORTD, 0) 'delay_ms(5000)TRISB = 0TRISB.0 = 1TRISD.6 = 1 INTCON.GIE = 1 ' set global int's INTCON.INTE = 1 ' set interrupt OPTION_REG.INTEDG = 0 ' set rising front delay_ms(5000) counter = 1000000 ''' start frequency change to convenient valuewhile truedelay_ms(1000)

Lcd_Cmd(_LCD_CLEAR)longwordtostr(counter,freqs)Lcd_Out_cp(freqs)

wendend.

-2-