Deneysel Elektronik üşenmedim denedim :D

Katılım
18 Ocak 2008
Mesajlar
1,088
Puanları
156
Yaş
40
Değerli ustalarım selamun aleyküm hayırlı çalışmalar Allah hepimizi korona virüs illetinden korusun biran önce de kurtarsın inşallah...

10 şubatta ameliyat oldum nisan 20 ye kadar da raporluyum arada kafama estikçe bazı deneyler yapmaya çalışıyorum...

MikroC kullanarak spi ile ilgili bir deney yaptım donanımsal olan spi yi kullandım adres bilgisi göndermeden sadece göndermek istediğim data bilgisini gönderdim.

Ve bu sinyal bilgisini de bir tane lojik analizör ile kaydettim. SPI yapılandırmasını şu şekilde yaptım.

SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

https://www.kontrolkalemi.com/forum/konu/spi-haberleşmede-sinyal-şekilleri-hakkında.133009/ bu linkte anlattığım gibi
_SPI_MASTER_OSC_DIV64 : Osilatör frekansı bölme oranı spi master dediği için picin osilatör frekansının 64 e bölünmesi

_SPI_DATA_SAMPLE_MIDDLE : Aralığın ortasında örneklenmiş giriş verileri clock sinyali verilerin tam ortasında aktif olması durumu

_SPI_CLK_IDLE_LOW : Sinyal iletişimi yokken saat sinyali boşta iken haberleşme yokken low seviyesinde olduğunu bildiriyor

_SPI_LOW_2_HIGH : Haberleşmenin clock hattının yükselen kenarında olduğunu yani düşükten yükselen kenara geçerken belirtiyor

Bizzat uyumlu çalıştığını gördüm

eFPjZI.jpg


Şekilde en üstteki sinyal cs hattı ortadaki clcock hattı alttaki ise data hattıdır.

while(1){

Chip_select=0

spi1_write(0b1010101010101111);

Chip_select=1;
dongu:
goto dongu;

Şimdi burada dikkat edilirse ben 16f877A nın spi hattından 16 bitlik veri yollamaya çalıştım neden böyle 1010 şeklinde derseniz analizörden okuması kolay olsun diye zaten low byte nin düşük 4 biti 1111 olduğu için datada da kendini kabak gibi belli etmekte :)

Peki ben bu denemeyi niçin yaptım ? Şunun için yaptım : Elimde bir tane ad9833 modülü var ben bu modülle değişik frekanslarda ve değişik sinyal şekillerinde sinyaller elde etmek istiyorum.

Modülün datasheetine baktığım zaman kontrol registeri 1 byte den büyük olduğu için ben de 8 bitlik bir mikro denetleyici ile normal şartlarda 1 byteden fazla değer yollayabiliyor muyum diye merak ettim ve bu denemeyi yaptım. Zaten adres bilgisi göndermeden direk data yollamamdaki amaç da bu oldu elbette daha önce adres bilgisi göndererek yaptığım analizler de var komutları ile birlikte ama önemli olan bu analiz.

Sonuçta ben yukarıdaki komutlar ile sadece bir byte veri gönderebildiğimi gördüm. o da 2 bytelik verinin düşük byte'ını gönderdiğini tespit ettim.

Ve Son tespitim ise spi yapılandırılması ile alakalı oldu yani benim hem yukarıdaki linkte hem de açıklama kısmında yazdığım gibi spi mod olayını gerçekleştirebildim.

Zaman buldukça bu spi ile ilgili daha değişik çalışmalar yaparak daha ince detaylarını da öğrenmeye çalışacağım.

Peki şimdi ben mikroc ile 8 bit değil de 16 bit veriyi yollamam için ne yapmam gerekir sizce ?
 

Ekli dosyalar

  • 16 bit gönderme3.jpg
    16 bit gönderme3.jpg
    142.5 KB · Görüntüleme: 11
Son düzenleme:
...spi1_write(0b1010101010101111); ...

...8 bitlik bir mikro denetleyici ile normal şartlarda 1 byteden fazla değer yollayabiliyor muyum diye merak ettim ve bu denemeyi yaptım. ...

Denemen güzel ama gerek yoktu,
spi1_write fonksiyonunun aldığı parametreye baksan zaten 1 byte gönderebileceğini görürdün.
https://download.mikroe.com/documents/compilers/mikroc/pic/help/spi_library.htm#spi1_write
SPIx_Write
Prototypevoid SPIx_Write(unsigned short data_);
ReturnsNothing.
DescriptionWrites byte via the SPI bus.
Parameters :
  • wrdata: data to be sent
RequiresYou need PIC MCU with hardware integrated SPI.
SPI must be initialized and communication established before using this function. See SPIx_Init_Advanced or SPIx_Init.
Example// write a byte to the SPI bus
char buffer;
...
SPI1_Write(buffer);

Çözüm;
tabiki bir çok yöntem var,
göndereceğin datayı bir dizi içine yazıp
dizinin son elemanına varılıncaya kadar dataları gönderen bir fonksiyon oluşturabilirsin.

Hiç uğraşmayıp ardarda spi_write komutlarını sıralayabilirsin :)

İşlemciye yük olmasın diyorsan timer kesmesi kurup
belli aralıklarla spi datayı göndermiş mi diye kontrol edip
varsa sıradaki veriyi yollayabilirsin.
SSPSTAT register ının BF (0. bit) i "1" ise bir bayt veri yollanmış veya alınmış demektir,
sıradaki veri yollanabilir veya alınabilir,
tabi bu kısımları MikroC kütüphanesi kendisi hallediyordur aslında.

Güzel bir mcu kullanıp
spi daki veri alışverişini DMA ya yükleyebilirsin.

DsPic30/33 Pic24 kullanıp 16 bit spi ayarlayıp kullanabilirsin:
http://download.mikroe.com/documents/compilers/mikroc/dspic/help/spi_library.htm

Bu arada AD9833 ün MikroC kütüphanesi var mı hazırda?
 
Bu arada AD9833 ün MikroC kütüphanesi var mı hazırda?


Kendi orjinalinde yok diye biliyorum...

Ama göndereceğim veriyi dizi içine alıp yollama olayı hoşuma gitti teşekkür ederim. Mikroc nin 8 bit yolladığını zaten biliyordum ama bir de deniyip göreyim istedim çünkü 8 bitlik bir veri örneğin 0zFC olsun bu veriyi ben derleyiciye 8 bit değil de 16 bit olarak yazabiliyorum örneğin 0xFCBA yada daha yüksek bit mesela 0X15B48A dediğiniz zaman da derleyici hata vermiyor...

Ama hata vermediği gibi gönderip göndermediğini görmek istedim göndermiyormuş :D
 
Kod:
Device = 18F8722

    On_Hardware_Interrupt GoTo INTHAND
    
    Xtal 10
    
    Declare Reminders Off
    @ CONFIG_REQ = 0 ; Override Compiler's configuration settings
    Asm-
    Config OSC = HSPLL    ;HS oscillator, PLL disable (Clock ddsFRQ = 4 x FOSC1)
    Config FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
    Config IESO = OFF    ;Two-Speed Start-up disabled
    Config PWRT = On    ;PWRT enabled
    Config BOREN = SBORDIS    ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
    Config BORV = 3    ;Minimum setting
    Config WDT = OFF    ;WDT disabled (control is placed on the SWDTEN bit)
    Config WDTPS = 32768    ;1:32768
    Config MODE = MC    ;Microcontroller mode
    Config ADDRBW = ADDR20BIT    ;20-bit Address Bus
    Config DATABW = DATA16BIT    ;16-bit External Bus mode
    Config Wait = OFF    ;Wait selections are unavailable for table reads and table writes
    Config CCP2MX = PORTC    ;ECCP2 input/output is multiplexed with RC1
    Config ECCPMX = PORTE    ;ECCP1/3 (P1B/P1C/P3B/P3C) are multiplexed onto RE6, RE5, RE4 and RE3 respectively
    Config LPT1OSC = OFF    ;Timer1 configured for higher power operation
    Config MCLRE = OFF    ;MCLR pin disabled; RG5 input pin enabled
    Config STVREN = On    ;Stack full/underflow will cause Reset
    Config LVP = OFF    ;Single-Supply ICSP disabled
    Config BBSIZ = BB2K    ;1K word (2 Kbytes) Boot Block size
    Config XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    Config Debug = OFF    ;Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
    Config CP0 = On    ;Block 0 (000800, 001000 or 002000-003FFFh) code-protected
    Config CP1 = On    ;Block 1 (004000-007FFFh) code-protected
    Config CP2 = On    ;Block 2 (008000-00BFFFh) code-protected
    Config CP3 = On    ;Block 3 (00C000-00FFFFh) code-protected
    Config CP4 = On    ;Block 4 (010000-013FFFh) code-protected
    Config CP5 = On    ;Block 5 (014000-017FFFh) code-protected
    Config CP6 = On    ;Block 6 (01BFFF-018000h) code-protected
    Config CP7 = On    ;Block 7 (01C000-01FFFFh) code-protected
    Config CPB = OFF    ;Boot Block (000000-0007FFh) not code-protected
    Config CPD = OFF    ;Data EEPROM not code-protected
    Config WRT0 = OFF    ;Block 0 (000800, 001000 or 002000-003FFFh) not write-protected
    Config WRT1 = OFF    ;Block 1 (004000-007FFFh) not write-protected
    Config WRT2 = OFF    ;Block 2 (008000-00BFFFh) not write-protected
    Config WRT3 = OFF    ;Block 3 (00C000-00FFFFh) not write-protected
    Config WRT4 = OFF    ;Block 4 (010000-013FFFh) not write-protected
    Config WRT5 = OFF    ;Block 5 (014000-017FFFh) not write-protected
    Config WRT6 = OFF    ;Block 6 (01BFFF-018000h) not write-protected
    Config WRT7 = OFF    ;Block 7 (01C000-01FFFFh) not write-protected
    Config WRTC = OFF    ;Configuration registers (300000-3000FFh) not write-protected
    Config WRTB = OFF    ;Boot Block (000000-007FFF, 000FFF or 001FFFh) not write-protected
    Config WRTD = OFF    ;Data EEPROM not write-protected
    Config EBTR0 = OFF    ;Block 0 (000800, 001000 or 002000-003FFFh) not protected from table reads executed in other blocks
    Config EBTR1 = OFF    ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
    Config EBTR2 = OFF    ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
    Config EBTR3 = OFF    ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
    Config EBTR4 = OFF    ;Block 4 (010000-013FFFh) not protected from table reads executed in other blocks
    Config EBTR5 = OFF    ;Block 5 (014000-017FFFh) not protected from table reads executed in other blocks
    Config EBTR6 = OFF    ;Block 6 (018000-01BFFFh) not protected from table reads executed in other blocks
    Config EBTR7 = OFF    ;Block 7 (01C000-01FFFFh) not protected from table reads executed in other blocks
    Config EBTRB = OFF    ;Boot Block (000000-007FFF, 000FFF or 001FFFh) not protected from table reads executed in other blocks
    Endasm-
    Declare Reminders On
    
    '******TMR0 INTERRUPT SET SECTION******************
    T0CON =%10000011 'TMR0 DISABLE
    TMR0H = $B     ' preset for Timer0 MSB register
    TMR0L = $DC     ' preset for Timer0 LSB register   100ms overflow 40MHZ
    '************************************
    
    '**********TMR1 INTERRUPT SET SECTION**************
    T1CON    =%11000100   'TMR1 OVERFLOW INTERRUPT
    TMR1H    =$D8          'preset for timer1 MSB register
    TMR1L    =$F0          'preset for timer1 LSB register 1msec UART2 receive data WDT FOR 40 MHZ
    T1CON.0  =0            'TMR1 stop
    PIR1.0   =0            'tmr1 flag clear
    PIE1.0   =0            'tmr1 interrupt disable
'    IPR1.0   =1            'tmr1 overflow interrupt high priority
    '************************************
    
'    '*********** TMR3 INTERRUPT SET SECTION **************
'    T3CON   =%11010101
'    TMR3H   = $B        ' preset for timer3 MSB register
'    TMR3L   = $DC        ' preset for timer3 LSB register  50mSEC 10MHZ FOSC/4
'    PIE2.1  =0           'TMR3 OVERFLOW INTERRUPT DISABLE
'    PIR2.1  =0           'FLAG CLEAR
'    '****************************************************
    
    
    '******ENHANCED CAPTURE COMPARE PWM SECTION
    ECCP1CON=%00000000
    ECCP2CON=%00000000
    ECCP3CON=%00000000
    
    CCP1CON=%00000000
    CCP2CON=%00000000
    CCP3CON=%00000000
    
    CCPR1H=0
    CCPR1L=0
    CCPR2H=0
    CCPR2L=0
    '*******************************************
    
    
    
    '**********UART1 RX SECTION**********
    PIE1.5=0 'UART1 RECEIVER INT DISABLE
    PIR1.5=0 'UART RECEIVE INTERRUPT FLAG BIT
'    IPR1.5=1 'UART1 RECEIVE INTERRUPT HIGH PRIORITY
    '************************************
    
    '*********UART2 RX SECTION***********
    PIE3.5=0  'UART2 RECEIVER INT DISABLE
    PIR3.5=0  'UART2 RECEIVE INTERRUPT FLAG BIT
'    IPR3.5=1  'UART2 RECEIVE INTERRUPT HIGH PRIORITY
    '************************************
    
    '*******ADC REGISTER SECTION****************
    ADCON1=%00001100 'A0,A1,A2 ANALOG,OTHER PIN DIGITAL CONFIG
    ADCON2=%10110000 'FOSC/2 16 TAD, RIGHT JUSTIFIED CONFIG
    '*******************************************
    
    '************ BAUDRATE GENERATOR 8/16 BIT SETTING **
    BAUDCON1.3=1 'BRG16 ENABLE EUASRT1
    BAUDCON2.3=1 'BRG16 ENABLE EUSART2
    '***************************************************
    
    Declare PLL_Req                   TRUE
    'Declare All_Digital               TRUE
    Declare FSR_CONTEXT_SAVE          TRUE
    Declare Bootloader                0           
    Declare Float_Display_Type       = large
'    Declare SDA_Pin                 =PORTJ.0
'    Declare SCL_Pin                 =PORTJ.1
'    Declare Slow_Bus                =1
    Declare Adin_Tad frc
    Declare Adin_Stime 100
    Declare Adin_Res   10
        
'    Declare CCP2_Pin PORTC.1
'    Declare CCP1_Pin PORTC.2     
          
          
    '********** HSERIAL CONFIGURATION SECTION *****************     
    Declare Hserial_RCSTA=$90
    Declare Hserial_TXSTA=$24
    Declare Hserial_SPBRG=86 '115200 Bauds   PC TEST
    
    Declare Hserial2_RCSTA=$90
    Declare Hserial2_TXSTA=$24
    Declare Hserial2_SPBRG=86 '115200 Bauds   TO FSK BOARD

    Declare Hserial_Clear=On
    '**********************************************************
    
    
    '*************** INPUT DECLARATIONS ***********************
    Input       PORTC.7      'UART USB RXD
    Input       PORTA.0      'SYS VOLT MEASUREMENT CHANNEL
    Input       PORTA.1      'ON BOARD SENSOR TEMPERATURE MEASUREMENT
    Input       PORTA.2      'ON BOARD SENSOR HUMIDITY MEASUREMENT
    Input       PORTG.2      'UART2 FSK RXD
    Input       PORTE.7      'MAX31865 SDI
    Input       PORTD.0      'MAX31865 DRDY
    Input       PORTJ.1      'CURRENT SENSED (LOW ACTIVE)
    Input       PORTB.0      'ADC DRDY (LOW ACTIVE)
    Input       PORTB.2      'TEST BUTTON (LOW ACTIVE)
    Input       PORTJ.6      'ADC SDI
    '**********************************************************
    
    
    '*************** OUTPUT DECLARATIONS **********************
    Output      PORTJ.5      'ADG IN2
    Output      PORTJ.4      'ADG IN1
    Output      PORTC.6      'UART USB TXD             
    Output      PORTG.1      'UART2 FSK TXD
    Output      PORTH.1      'MAX31865 SCK
    Output      PORTH.0      'MAX31865 SDO
    Output      PORTE.6      'MAX31865 CS
    Output      PORTD.3      'AD9833 SDO
    Output      PORTD.4      'AD9833 SCK
    Output      PORTD.5      'AD9833 FSYNC
    Output      PORTD.6      'CURRENT ON (OPTO COUPLER HIGH ACTIVE)
    Output      PORTD.7      'MR1 (OPTO MOSFET LED ANODE)
    Output      PORTJ.0      'MR2 (OPTO MOSFET LED ANODE)
    Output      PORTJ.2      'HB LED
    Output      PORTB.4      'ADC SDO
    Output      PORTB.5      'ADC PDWN (LOW ACTIVE)
    Output      PORTC.5      'ADC RST (LOW ACTIVE)
    Output      PORTC.4      'ADC START
    Output      PORTC.3      'ADC CS (LOW ACTIVE)
    Output      PORTC.2      'LOW PASS CUT OFF FREQ OUT (HPMW)
    Output      PORTJ.7      'ADC SCK
    '**********************************************************
    
    
    '************** INPUT ALIAS ******************************
    Symbol        MAX_SDI        =PORTE.7
    Symbol        MAX_DRDY       =PORTD.0
    Symbol        CRNT_SNS       =PORTJ.1
    Symbol        ADC_DRDY       =PORTB.0
    Symbol        TST_BTN        =PORTB.2
    Symbol        ADC_SDI        =PORTJ.6
    '*********************************************************
    
    
    '************** OUTPUT ALIAS ******************************
    Symbol        spSW2          =PORTJ.5
    Symbol        spSW1          =PORTJ.4
    Symbol        MAX_SCK        =PORTH.1
    Symbol        MAX_SDO        =PORTH.0
    Symbol        MAX_CS         =PORTE.6
    Symbol        DDS_SDO        =PORTD.3
    Symbol        DDS_SCK        =PORTD.4
    Symbol        DDS_CE         =PORTD.5
    Symbol        CrnT           =PORTD.6
    Symbol        MR1            =PORTD.7
    Symbol        MR2            =PORTJ.0
    Symbol        HB             =PORTJ.2
    Symbol        ADC_SDO        =PORTB.4
    Symbol        ADC_PWDN       =PORTB.5
    Symbol        ADC_RST        =PORTC.5
    Symbol        ADC_STRT       =PORTC.4
    Symbol        ADC_CS         =PORTC.3
    Symbol        ADC_SCK        =PORTJ.7
    '**********************************************************
    
    
    '***************** INIT OUTPUTS ********
    spSW2     =0
    spSW1     =0
    MAX_SCK   =0
    MAX_SDO   =0
    MAX_CS    =1
    DDS_SDO   =0
    DDS_SCK   =1
    DDS_CE    =1
    CrnT      =0
    MR1       =0
    MR2       =0
    HB        =0
    ADC_SDO   =0
    ADC_RST   =1
    ADC_PWDN  =1
    ADC_STRT  =0
    ADC_CS    =1
    ADC_SCK   =0
    '***************************************
    
    
    '*********** DDS VARIABLES *************
    Dim ddsFRQ As Dword 'Desired ddsFRQ
    Dim DDS_OP As Float 'DDS Output
    Dim FTW As Dword 'ddsFRQ Tunning word
    Dim FTWLSB As FTW.Word0 'First word LSB of the ddsFRQ tuning word
    Dim FTWMSB As FTW.Word1 'Second word MSB of the ddsFRQ tuning word
    Dim FTWLSB1 As Word '
    Dim DDS_Chip As Byte 'Selects DDS chip DDS1 = 1, DDS2 = 2
    Dim DDS_Waveform As Byte 'Select Waveforms Type Sine = 1, Triangle = 2, Square = 3 (TTL)
    Dim DDS_Control_Register As Word
    Dim Freq_Reg As Bit 'Selects ddsFRQ Register REG0 or REG1
    '**************************************
    
    Dim Sayac        As Dword
    Dim CHRES0       As Dword
    Dim CHRES1       As Dword
    Dim CHRES2       As Dword
    Dim CHRES3       As Dword
    Dim CHRES4       As Dword
    Dim CHRES5       As Dword
    Dim CHRES6       As Dword
    Dim CHRES7       As Dword
    
    Dim TOT0         As Dword
    Dim TOT1         As Dword
    Dim TOT2         As Dword
    Dim TOT3         As Dword
    Dim TOT4         As Dword
    Dim TOT5         As Dword
    Dim TOT6         As Dword
    Dim rtdTOT       As Dword

    Dim DUMMY1 As Byte


    Dim VLT0         As Float        'VLT0
    Dim VLT1         As Float        'VLT1
    Dim VLT2         As Float        'VLT2
    Dim VLT3         As Float        'VLT3
    Dim VLT4         As Float        'VLT4
    Dim VLT5         As Float        'VLT5
    Dim VLT6         As Float        'VLT6
    'Dim VLT7 As Float        'VLT7

    Dim comFLT       As Float
    Dim Kh           As Float
    Dim Kl           As Float
    Dim minRES       As Float
    Dim maxRES       As Float
    Dim PmaxSP       As Float
    Dim NmaxSP       As Float
    Dim resVAL8      As Float
    Dim resVAL16     As Float
    Dim resVAL32     As Float
    Dim resVAL64     As Float
    Dim SPR          As Float
    Dim SP           As Float
    Dim shuntRES     As Float
    Dim SourceVLT    As Float
    Dim measCRNT     As Float
    
    Dim sensCRNT     As Float
    Dim spOFFSET     As Float
    Dim onbRH        As Float
    Dim onbTMP       As Float
    Dim Temp0        As Float
    Dim rtd_R        As Float

    Dim prbVLT       As Float
    Dim humVLT       As Float
    Dim tmpVLT       As Float


    '****** PT100 LINEARISATION VARIABLES ***
    Dim c0    As Float
    Dim c1    As Float
    Dim c2    As Float
    Dim c3    As Float
    Dim c4    As Float
    Dim c5    As Float
    Dim c6    As Float
    Dim c7    As Float
    Dim num   As Float
    Dim denom As Float
    Dim T     As Float
    '****************************************
    
  
    Dim          comTGL100         As Bit
    Dim          comTGL500         As Bit
    Dim          comTGL1000        As Bit
    Dim          SrcvD             As Bit
    Dim          SptrLCK           As Bit
    Dim          btnLCK            As Bit 
    Dim          chSIGN0           As Bit
    Dim          chSIGN1           As Bit
    Dim          chSIGN2           As Bit
    Dim          chSIGN3           As Bit
    Dim          chSIGN4           As Bit
    Dim          chSIGN5           As Bit
    Dim          chSIGN6           As Bit
    Dim          chSIGN7           As Bit
    
    Dim          DummY             As Byte
    Dim          gpINX             As Byte
    Dim          rxLENGTH          As Byte
    Dim          txLENGTH          As Byte
    Dim          RecBYTE           As Byte
    Dim          sPTR              As Byte
    Dim          comINX1           As Byte
    Dim          rtdTOUT           As Byte
    Dim          onbTMPABS         As Byte
    Dim          onbRHABS          As Byte
    Dim          prbVLTABS         As Byte
    Dim          measMODE          As Byte
    Dim          tmrTICK100        As Byte
    Dim          tmrTICK500        As Byte
    Dim          tmrTICK1000       As Byte
    Dim          testSAY           As Byte 'TEST SİL
    Dim          AvrG              As Byte
    Dim          SETavrg           As Byte
    Dim          DIGBYTE           As Byte
    Dim          RxByte            As Byte
    Dim          I_SPIIndex        As Byte
    Dim          I_SpiDataLen      As Byte
    Dim          i_spiBUFFER[30]   As Byte
    Dim          Ah                As Byte
    Dim          Al                As Byte
    Dim          comBYTE           As Byte
    Dim          rtdAVRG           As Byte
    Dim          setRTDAVRG        As Byte
    Dim          FltrTOL           As Byte
    Dim          rxBUFF[100]       As Byte
    Dim          txBUFF[100]       As Byte
    Dim          rxCOMMAND         As Byte
    Dim          txCSUM            As Byte
    Dim          rxCSUM            As Byte
    Dim          txCALC            As Byte
    Dim          rxCALC            As Byte
    Dim          maxBYTE           As Byte
    Dim          setID             As Byte
  
    
 
    Dim          comWORD           As Word
    Dim          rtdRAW            As Word
    Dim          RTD_H_TRSH        As Word
    Dim          RTD_L_TRSH        As Word
    Dim          newCODE           As Word
    Dim          oldCODE           As Word
    Dim          measSAY           As Word
    Dim          RAW0              As Word
    Dim          RAW1              As Word
    Dim          RAW2              As Word
    Dim          ddsCONTREG        As Word
    Dim          onbTOT0           As Word
    Dim          onbTOT1           As Word
    Dim          onbTOT2           As Word
    Dim          cableRES          As Word
    
    Dim          resVAL8ABS        As Word
    Dim          resVAL16ABS       As Word
    Dim          resVAL32ABS       As Word
    Dim          resVAL64ABS       As Word
    Dim          sprABS            As Word
    Dim          spABS             As SWord
    Dim          Temp0ABS          As SWord
    
 

    Symbol RREG_CMD =%00100000
    Symbol WREG_CMD =%01000000

    Clear
    
    
    
    EData $00 'DDS FREQ BYTE 3    BYTE NUMB 0   (DWORD)
    EData $00 'DDS FREQ BYTE 2    BYTE NUMB 1
    EData $03 'DDS FREQ BYTE 1    BYTE NUMB 2 
    EData $E8 'DDS FREQ BYTE 0    BYTE NUMB 3       1KHZ
    
    EData 3   'DDS WAVE FORM 1=SINE 2=TRIANGLE 3=SQUARE  BYTE NUMB 4   (BYTE)
    
    EData $00 'MINIMUM RESISTANCE BYTE3   BYTE NUMB 5   (FLOAT)
    EData $00 'MINIMUM RESISTANCE BYTE2   BYTE NUMB 6
    EData $00 'MINIMUM RESISTANCE BYTE1   BYTE NUMB 7
    EData $00 'MINIMUM RESISTANCE BYTE0   BYTE NUMB 8   0 OHM
    
    EData $00 'MAXIMUM RESISTANCE BYTE3   BYTE NUMB 9   (FLOAT)
    EData $60 'MAXIMUM RESISTANCE BYTE2   BYTE NUMB 10
    EData $6A 'MAXIMUM RESISTANCE BYTE1   BYTE NUMB 11
    EData $8C 'MAXIMUM RESISTANCE BYTE0   BYTE NUMB 12   15000 OHM
    
    EData $84 'Kh BYTE3   BYTE NUMB 13   (FLOAT)
    EData $EB 'Kh Byte2   BYTE NUMB 14
    EData $51 'Kh Byte1   BYTE NUMB 15
    EData $7E 'Kh Byte0   BYTE NUMB 16
    
    EData $00 'Kl BYTE3   BYTE NUMB 17   (FLOAT)
    EData $00 'Kl Byte2   BYTE NUMB 18
    EData $00 'Kl Byte1   BYTE NUMB 19
    EData $7F 'Kl Byte0   BYTE NUMB 20
    
    EData $00 'CABLE RESISTANCE BYTE3   BYTE NUMB 21   (FLOAT)
    EData $00 'CABLE RESISTANCE Byte2   BYTE NUMB 22
    EData $00 'CABLE RESISTANCE Byte1   BYTE NUMB 23
    EData $00 'CABLE RESISTANCE Byte0   BYTE NUMB 24
    
    EData $6E 'SENS CURRENT BYTE3   BYTE NUMB 25   (FLOAT)
    EData $12 'SENS CURRENT Byte2   BYTE NUMB 26
    EData $03 'SENS CURRENT Byte1   BYTE NUMB 27
    EData $74 'SENS CURRENT Byte0   BYTE NUMB 28   0.0005 A
  
    EData $00 'SP OFFSET BYTE3   BYTE NUMB 29   (FLOAT)  w
    EData $00 'SP OFFSET Byte2   BYTE NUMB 30
    EData $00 'SP OFFSET Byte1   BYTE NUMB 31
    EData $00 'SP OFFSET Byte0   BYTE NUMB 32
  
  
    '*************** PROGRAMCI DEĞERLERİ  ***********
    Symbol RREF                 =400 'RTD CONSTANT CURRENT REFERANCE RESISTOR   PT100=400R PT1000=4000R
    shuntRES                    =10     'SHUNT RESISTOR OHM
    Freq_Reg                    =0      'Point to which register
    SETavrg                     =28     '24 bit adc average count
    PmaxSP                      =15     'MAXIMUM SP VOLTAGE POSITIVE
    NmaxSP                      =-15    'MAXIMUM SP VOLTAGE NEGATIVE
    setRTDAVRG                  =10
    FltrTOL                     =3      'FLICKER FILTER TOLERANCE
    RTD_H_TRSH                  =297 'RTD HIGH TRESHOLD
    RTD_L_TRSH                  =18  'RTD LOW TRESHOLD
    maxBYTE                     =99  'SERIAL COMM MAX BYTE
    setID                       ="E" 'ELOG SET ID
    '************************************************


    '***************** PT100 LINEARISATION VALUES ****
    c0= -245.19
    c1=  2.5293
    c2= -0.066046
    c3=  0.0040422
    c4= -0.0000020697
    c5= -0.025422
    c6=  0.0016883
    c7= -0.0000013601
'    ******************************************


      
    DelayMS 500                 

    GoTo OVERSUB
    
    
    Include "MODEDEFS.BAS"   
    
  
INTHAND:

    Context Save
    
        


        '*************** UART2 INTERRUPT SECTION **************
        '*********************************************************
        If PIR3.5=1 Then 'UART2 RECEIVE INTERRUPT
      
      
            If RCSTA2.1=1 Then RCSTA2.4=0:RCSTA2.4=1 'overrun error clear
      
            If SptrLCK=0 Then sPTR=0:SptrLCK=1
          
            TMR1H    =$D8          'preset for timer1 MSB register
            TMR1L    =$F0          'preset for timer1 LSB register 1ms UART2 receive data WDT 
            T1CON.0=1  'TMR1 enable
            PIR1.0 =0  'TMR1 FLAG CLEAR

            
      
            SrcvD=0
          
            RecBYTE=RCREG2
            'PIR3.5=0
          
            rxBUFF[sPTR]=RecBYTE
            If sPTR < maxBYTE Then Inc sPTR
        
      
        EndIf
        '*************************************************************
        '*************************************************************
    
    
    
    
    
        '************************ TMR1 OVERFLOW INTERRUPT SECTION ************************************
        '************************************************************************
        If PIR1.0=1 Then 'tmr1 overflow interrupt flag
          
      
            PIR1.0  =0 'FLAG CLEAR
            T1CON.0 =0  'stop tmr1
            
            
            
            SrcvD=1
            rxLENGTH=(sPTR - 1) 'crc16 için paket sayısı al
            sPTR=0
            SptrLCK=0
    
            
        EndIf
       '***************************************************************************
       '***************************************************************************
    
    

    
        '********************* TMR0 OVERFLOW INTERRUPT 100mSEC*****************************
        If INTCON.2=1 Then
        
            '******************* 100 mSEC TIMER PROCESS SECTION *************
            comTGL100=~comTGL100  'COMMON TOGGLE BIT 100 mSEC
      
            If measSAY < 100 Then Inc measSAY
          
            
            '*************** 500 msec PROCESS ********************************
            If tmrTICK500 >= 4 Then '500msec
                tmrTICK500=0
                
                        

                

                
                comTGL500=~comTGL500 'COMMON TOGGLE BIT 500 mSEC   
                
            EndIf
            '*****************************************************************
            
            '***************** 1000 msec PROCESS *****************************
            If tmrTICK1000 >= 10 Then '1000msec
                tmrTICK1000=0
        
              
          
          
                comTGL1000=~comTGL1000  'COMMON TOGGLE BIT 1000 mSEC
                
            EndIf
            '************************************************************************
            
            
            

            
            Inc tmrTICK100
            Inc tmrTICK500
            Inc tmrTICK1000
      
            
            INTCON.2=0 'flag clear
        
            TMR0H   = $B         ' preset for timer0 MSB register
            TMR0L   = $DC        ' preset for timer0 LSB register  100mSEC 10MHZ FOSC/4
        
        EndIf
        '***************************************************************************
    
    Context Restore   
    
    
READ_EEPROM:

    ddsFRQ.Byte3      =ERead 0
    ddsFRQ.Byte2      =ERead 1
    ddsFRQ.Byte1      =ERead 2
    ddsFRQ.Byte0      =ERead 3
    
    DDS_Waveform      =ERead 4
    
    minRES.Byte3      =ERead 5
    minRES.Byte2      =ERead 6
    minRES.Byte1      =ERead 7
    minRES.Byte0      =ERead 8
    
    maxRES.Byte3      =ERead 9
    maxRES.Byte2      =ERead 10
    maxRES.Byte1      =ERead 11
    maxRES.Byte0      =ERead 12
    
    Kh.Byte3          =ERead 13
    Kh.Byte2          =ERead 14
    Kh.Byte1          =ERead 15
    Kh.Byte0          =ERead 16
    
    Kl.Byte3          =ERead 17
    Kl.Byte2          =ERead 18
    Kl.Byte1          =ERead 19
    Kl.Byte0          =ERead 20
    
'    cableRES.Byte3    =ERead 21
'    cableRES.Byte2    =ERead 22
'    cableRES.Byte1    =ERead 23
'    cableRES.Byte0    =ERead 24
    
    sensCRNT.Byte3    =ERead 25
    sensCRNT.Byte2    =ERead 26
    sensCRNT.Byte1    =ERead 27
    sensCRNT.Byte0    =ERead 28
    
    spOFFSET.Byte3    =ERead 29
    spOFFSET.Byte2    =ERead 30
    spOFFSET.Byte1    =ERead 31
    spOFFSET.Byte0    =ERead 32
    
Return   
    
 
INIT_ADC:

 
    ADC_RST  =0
    DelayMS 1
    ADC_RST=1
    DelayMS 50

    i_spiBUFFER#0= $11       'Stop read data continuous mode. Register okuma yazma için bu mod kapatılmalı.(komutlar  PDF sayfa 35 de)
    I_SpiDataLen=1
    GoSub WRSpi
 
    '======= BAZI REGISTER LARI YAZMAYA BAŞLIYORUZ.(Register tablosu PDF sayfa 39 da)==========
    'registerleri yazarken kendi sub fonksiyonumuzu kullanıyoruz.
                    
                    'sub fonksiyon kullanımı şu şekilde ;
'    i_spiBUFFER#0= <---- komut ve adres
'    i_spiBUFFER#1= <---- kaç regiser yazılacak (0 ise 1 reg. yazılacak)
'    i_spiBUFFER#2= <---- registere yazılacak değer                       
'    I_SpiDataLen= <---- tüm komut kaç byte dan oluşuyor 
'    Call WRSpi                   
                    
                    
    i_spiBUFFER#0= $03   
    i_spiBUFFER#0=i_spiBUFFER#0 | WREG_CMD    '
    i_spiBUFFER#1=0   
    i_spiBUFFER#2=$c0                       '2.4V internal referans , op-amp kapalı
    I_SpiDataLen=3   
    GoSub WRSpi
    DelayMS 10                               
  
    i_spiBUFFER#0= $01   
    i_spiBUFFER#0=i_spiBUFFER#0 | WREG_CMD    '
    i_spiBUFFER#1=0   
    i_spiBUFFER#2=%10010110                        ' 24bit 1ksps             son 3 bit > 011= 24-bit 8k, 100=4k,101=2k,110=1k
    I_SpiDataLen=3                                 
    GoSub WRSpi
      
    
    i_spiBUFFER#0= $02   
    i_spiBUFFER#0=i_spiBUFFER#0 | WREG_CMD    '
    i_spiBUFFER#1=0   
    i_spiBUFFER#2=%11110000                        ' test voltajı ayarı Register 2
    I_SpiDataLen=3                                   
    GoSub WRSpi
            '  bit   
                '4   0 : Test signals are driven externally  1 : Test signals are generated internally
                '3   Reserved R/W 0h Reserved.
                '2   Sinyal amplitude 0 : 1 × –(VVREFP – VVREFN) / 2400     1 : 2 × –(VVREFP – VVREFN) / 2400
                '1:0  test sinyal frekans
                                        '00 : Pulsed at fCLK / 221
                                        '01 : Pulsed at fCLK / 220
                                        '10 : Not used
                                        '11 : At dc   
    
    

    'burda 8 adc nin config register ini tek seferde yazıyoruz.
    i_spiBUFFER#0= $05   
    i_spiBUFFER#0=i_spiBUFFER#0 | WREG_CMD    '
    i_spiBUFFER#1=7   
    i_spiBUFFER#2=%00010000  'GAIN 12 SP                      ' AD input config (sayfa 45 e bak) .Son 3 bit>>> 100=Temp sensor  ,011=VDD,000=normal
    i_spiBUFFER#3=%00010000
    i_spiBUFFER#4=%00010000
    i_spiBUFFER#5=%00010000
    i_spiBUFFER#6=%00010000
    i_spiBUFFER#7=%00010000
    i_spiBUFFER#8=%00010000
    i_spiBUFFER#9=%00010000
    I_SpiDataLen=10   
    GoSub WRSpi


Return 

 
 
INIT_INT:

    'PIE1.5  =1 'UART RX1 INTERRUPT ENABLE
    'PIR1.5  =0 'UART RX1 FLAG CLEAR

    PIE3.5  =1 'UART RX2 INTERRUPT ENABLE
    PIR3.5  =0 'UART RX2 FLAG CLEAR

    INTCON.5=1 'TMR0 INT ENABLE
    INTCON.2=0 'TMR0 FLAG CLEAR
    
    PIE1.0  =1 'TMR1 OVERFLOW INTERRUPT ENABLE
    PIR1.0  =0 'TMR1 OVERFLOW INTERRUPT FLAG CLEAR
    
 
    
    INTCON.6=1 'PIE
    INTCON.7=1 'GIE

Return   
    
 
 
WRSpi:


    ADC_CS=0
    For I_SPIIndex=0 To I_SpiDataLen - 1
        SHOut ADC_SDO,ADC_SCK,MsbFirst_L,[i_spiBUFFER[I_SPIIndex]]
    Next I_SPIIndex
    ADC_CS=1   

Return 
 
 
    
INIT_UART:

    HSerIn 1,TOUT,[DummY]
    TOUT:
    
    HSerIn2 1,TOUT2,[DummY]
    TOUT2:


Return   


INIT_DDS:

    DDS_CE = 0 'Activate DDS (Set Chip Enable Low)
    SHOut DDS_SDO,DDS_SCK,5, [$2100\16] 'Apply Reset
    SHOut DDS_SDO,DDS_SCK,5, [$2000\16] 'Remove reset
    DDS_CE = 1 'Disable DDS (Set Chip Enable High)


    DDS_Control_Register = $2100
  



Return


READ_PT100:

        MAX_CS=0
        Nop
        SHOut MAX_SDO,MAX_SCK,MsbFirst,[$80] '$80
        Nop
        SHOut MAX_SDO,MAX_SCK,MsbFirst,[%10110011] 'bias on,normally off,shot,3 wire,50 hz
        MAX_CS=1
        
    
        rtdTOUT=0
        Repeat
            DelayMS 1
            Inc rtdTOUT
            If rtdTOUT>=25 Then Break
        Until MAX_DRDY=0
                
                              
      
        MAX_CS=0
        Nop
        SHOut MAX_SDO,MAX_SCK,MsbFirst,[$01]
        Nop
        SHIn MAX_SDI,MAX_SCK,MsbPre,[rtdRAW.HighByte]
        MAX_CS=1
      

        MAX_CS=0
        Nop
        SHOut MAX_SDO,MAX_SCK,MsbFirst,[$02]
        Nop
        SHIn MAX_SDI,MAX_SCK,MsbPre,[rtdRAW.LowByte]
        MAX_CS=1
        
        
        MAX_CS=0
        Nop
        SHOut MAX_SDO,MAX_SCK,MsbFirst,[$00]
        Nop
        SHIn MAX_SDI,MAX_SCK,MsbPre,[DummY]
        MAX_CS=1
      
    
        MAX_CS=0
        Nop
        SHOut MAX_SDO,MAX_SCK,MsbFirst,[$07]
        Nop
        SHIn MAX_SDI,MAX_SCK,MsbPre,[DummY]
        MAX_CS=1
      
      
      
        rtdTOT=(rtdTOT + rtdRAW)
                            
        
        Inc rtdAVRG
        If rtdAVRG < setRTDAVRG Then Return
        rtdAVRG=0
      
 

        comWORD=(rtdTOT / setRTDAVRG)  'average
        rtdTOT=0
        
        
        '*************** FLICKER FILTER *******************
        newCODE=comWORD
        If (newCODE - oldCODE) > FltrTOL And (oldCODE - newCODE)> FltrTOL Then  'OR AND OLCAK
            oldCODE=newCODE
        EndIf
      
'        Temp0=(oldcode / 32)- 256       'RTD SICAKLIK FORMUL    'test
'        Temp=(Temp + 10)'TMP0cal) 'ERROR CORRRECTION CAL VAL   'test
        rtd_R=(oldCODE * RREF) / 32767 'RTD DİRENÇ FORMUL
    
        
    
        '***************** LINEARISATION TEMP ********
        num=0
        denom=0
        num = rtd_R * (c1 + rtd_R * (c2 + rtd_R * (c3 + rtd_R * c4)))
        denom = 1.0 + rtd_R * (c5 + rtd_R * (c6 + rtd_R * c7))
        Temp0 = c0 + (num / denom)
'        Temp0=(Temp0 + TMP0cal) 'calib val
        '*********************************************
        
      
        Temp0ABS=Temp0   
      
Return



ONADCREAD: 'ON BOARD ADC READ SUB

    
    onbTOT0  =0
    onbTOT1  =0
    onbTOT2  =0
    
    For comINX1=0 To 19
    
        RAW0=ADIn 0 'SYS VOLT RAW
        RAW1=ADIn 1 'TEMP RAW
        RAW2=ADIn 2 'HUM RAW
    
        onbTOT0=(onbTOT0 + RAW0)
        onbTOT1=(onbTOT1 + RAW1)
        onbTOT2=(onbTOT2 + RAW2)
        
    Next comINX1
    
    onbTOT0=(onbTOT0 / 20)
    onbTOT1=(onbTOT1 / 20)
    onbTOT2=(onbTOT2 / 20)
    
    prbVLT  =(onbTOT0 * 4.99) / 1024
    tmpVLT  =(onbTOT1 * 4.99) / 1024
    humVLT  =(onbTOT2 * 4.99) / 1024

    prbVLTABS=Abs prbVLT

    onbRH=(-12.5 + (125 * (humVLT / 3.3) ) )   'rh
    onbRHABS=Abs onbRH

    onbTMP= (-66.875 + (218.75 *(tmpVLT / 3.3) ) )       't
    onbTMP=onbTMP - 5
    onbTMPABS=Abs onbTMP



Return


EXTADCREAD:


    GoSub MEASCLR
  
    For AvrG=0 To SETavrg
    
  
        While ADC_DRDY=1
        Wend
        
        
        
        i_spiBUFFER#0= 0x12       'okunan adc değerlerini gönder komutu
        I_SpiDataLen=1
        GoSub WRSpi
        
      
        ADC_CS=0
        For I_SPIIndex=0 To 26
            SHIn ADC_SDI,ADC_SCK,MsbPost_L,[i_spiBUFFER[I_SPIIndex]]
        Next I_SPIIndex
        ADC_CS=1 
    
    
    
        CHRES0.Byte3=0
        CHRES0.Byte2=i_spiBUFFER[3]
        CHRES0.Byte1=i_spiBUFFER[4]
        CHRES0.Byte0=i_spiBUFFER[5]
        
        CHRES1.Byte3=0
        CHRES1.Byte2=i_spiBUFFER[6]
        CHRES1.Byte1=i_spiBUFFER[7]
        CHRES1.Byte0=i_spiBUFFER[8]
        
        CHRES2.Byte3=0
        CHRES2.Byte2=i_spiBUFFER[9]
        CHRES2.Byte1=i_spiBUFFER[10]
        CHRES2.Byte0=i_spiBUFFER[11]
        
        CHRES3.Byte3=0
        CHRES3.Byte2=i_spiBUFFER[12]
        CHRES3.Byte1=i_spiBUFFER[13]
        CHRES3.Byte0=i_spiBUFFER[14]
        
        CHRES4.Byte3=0
        CHRES4.Byte2=i_spiBUFFER[15]
        CHRES4.Byte1=i_spiBUFFER[16]
        CHRES4.Byte0=i_spiBUFFER[17]
        
        CHRES5.Byte3=0
        CHRES5.Byte2=i_spiBUFFER[18]
        CHRES5.Byte1=i_spiBUFFER[19]
        CHRES5.Byte0=i_spiBUFFER[20]
        
        CHRES6.Byte3=0
        CHRES6.Byte2=i_spiBUFFER[21]
        CHRES6.Byte1=i_spiBUFFER[22]
        CHRES6.Byte0=i_spiBUFFER[23]
        
        chSIGN0=0
        chSIGN1=0
        chSIGN2=0
        chSIGN3=0
        chSIGN4=0
        chSIGN5=0
        chSIGN6=0
      
    
        If CHRES0.23=1 Then chSIGN0=1 : CHRES0=($FFFFFF - CHRES0) + 1
        If CHRES1.23=1 Then chSIGN1=1 : CHRES1=($FFFFFF - CHRES1) + 1
        If CHRES2.23=1 Then chSIGN2=1 : CHRES2=($FFFFFF - CHRES2) + 1
        If CHRES3.23=1 Then chSIGN3=1 : CHRES3=($FFFFFF - CHRES3) + 1
        If CHRES4.23=1 Then chSIGN4=1 : CHRES4=($FFFFFF - CHRES4) + 1
        If CHRES5.23=1 Then chSIGN5=1 : CHRES5=($FFFFFF - CHRES5) + 1
        If CHRES6.23=1 Then chSIGN6=1 : CHRES6=($FFFFFF - CHRES6) + 1


        TOT0=(TOT0 + CHRES0)
        TOT1=(TOT1 + CHRES1)
        TOT2=(TOT2 + CHRES2)
        TOT3=(TOT3 + CHRES3)
        TOT4=(TOT4 + CHRES4)
        TOT5=(TOT5 + CHRES5)
        TOT6=(TOT6 + CHRES6)
  
    Next AvrG   
    
    


    CHRES0=(TOT0 / SETavrg):TOT0=0
    CHRES1=(TOT1 / SETavrg):TOT0=1
    CHRES2=(TOT2 / SETavrg):TOT0=2
    CHRES3=(TOT3 / SETavrg):TOT0=3
    CHRES4=(TOT4 / SETavrg):TOT0=4
    CHRES5=(TOT5 / SETavrg):TOT0=5
    CHRES6=(TOT6 / SETavrg):TOT0=6

 
    GoSub CALCVAL

Return


MEASCLR:

    
    CHRES0=0
    CHRES1=0
    CHRES2=0
    CHRES3=0
    CHRES4=0
    CHRES5=0
    CHRES6=0
    
    TOT0=0
    TOT1=0
    TOT2=0
    TOT3=0
    TOT4=0
    TOT5=0
    TOT6=0
 
    
Return


CALCVAL:
    
 


    '********** MEASUREMENTS MODE ****************
    Select measMODE
      
        Case 0 'NORMAL RES
            
            measCRNT      =(CHRES3 * 0.48) / 1677721.5   'TOTAL CURRENT VOLTAGE CONVERSION
            measCRNT=(measCRNT / shuntRES) 'TOTAL CURRENT CALCULATE
            
            '********** VOLTAGE CALCULATE ****
            VLT2=(CHRES2 * 4.80) / 1677721.5   '8 INCH
            VLT1=(CHRES1 * 4.80) / 1677721.5   '16 INCH
            VLT6=(CHRES6 * 4.80) / 1677721.5   '32 INCH
            VLT5=(CHRES5 * 4.80) / 1677721.5   '64 INCH
            '*****************************
            
            '******** RESISTANCE CALCULATE *****
            If measCRNT >= sensCRNT Then 'akım çekiliyorsa direnç ölçüm aralığında
                
                resVAL8  =(VLT2 / measCRNT)
                resVAL16 =(VLT1 / measCRNT)
                resVAL32 =(VLT6 / measCRNT)
                resVAL64 =(VLT5 / measCRNT)
            
                '********** LINEARISATION  SECTION *****
                If resVAL8  > (maxRES / 2.3) Then resVAL8 =resVAL8  * Kh
                If resVAL16 > (maxRES / 2.3) Then resVAL16=resVAL16 * Kh
                If resVAL32 > (maxRES / 2.3) Then resVAL32=resVAL32 * Kh
                If resVAL64 > (maxRES / 2.3) Then resVAL64=resVAL64 * Kh
            
                If resVAL8  < (maxRES / 5) Then resVAL8 =resVAL8  * Kl
                If resVAL16 < (maxRES / 5) Then resVAL16=resVAL16 * Kl
                If resVAL32 < (maxRES / 5) Then resVAL32=resVAL32 * Kl
                If resVAL64 < (maxRES / 5) Then resVAL64=resVAL64 * Kl
                '****************************
            
                DIGBYTE.0=0 'NORMAL RESISTIVITY IN RANGE
          
            Else
          
                resVAL8  =maxRES
                resVAL16 =maxRES
                resVAL32 =maxRES
                resVAL64 =maxRES
                
                DIGBYTE.0=1 'NORMAL RESISTIVITY OVER RANGE
          
            EndIf
            '*******************
            
            
            
            '********** LIMIT SECTION ***
            If resVAL8  < minRES Then resVAL8  =minRES
            If resVAL16 < minRES Then resVAL16 =minRES
            If resVAL32 < minRES Then resVAL32 =minRES
            If resVAL64 < minRES Then resVAL64 =minRES
            
            If resVAL8  > maxRES Then resVAL8  =maxRES
            If resVAL16 > maxRES Then resVAL16 =maxRES
            If resVAL32 > maxRES Then resVAL32 =maxRES
            If resVAL64 > maxRES Then resVAL64 =maxRES
            '***********************       
    
            '********** ABS VAL *******
            resVAL8ABS   =Abs resVAL8
            resVAL16ABS  =Abs resVAL16
            resVAL32ABS  =Abs resVAL32
            resVAL64ABS  =Abs resVAL64
            '**************************
            
            
            
        Case 1 'SPR
        
            
            SourceVLT =(CHRES4 * 4.80) / 1677721.5   'SOURCE VOLTAGE CONVERSION
            measCRNT  =(CHRES3 * 0.48) / 1677721.5   'TOTAL CURRENT VOLTAGE CONVERSION
            measCRNT=(measCRNT / shuntRES) 'TOTAL CURRENT CALCULATE
            
            SPR=(SourceVLT / measCRNT) - shuntRES    'spr resistance calculate source voltage / current and extract shuntres + serial res
        
            '********** linerization ********
            If SPR      > (maxRES / 2.3) Then SPR=SPR * Kh
            If SPR      < (maxRES / 5)   Then SPR=SPR * Kl
            '********************************

        
            '******** CABLE RESISTANCE EXTRACT SECTION *****
            SPR=(SPR - cableRES)
            '***********************************************
            
            DIGBYTE.1=0 'SPR IN RANGE
            
            '********** limits ************
            If SPR      < minRES Then SPR      =minRES   
            If SPR      > maxRES Then SPR      =maxRES : DIGBYTE.1=1 'SPR OVER RANGE
            '******************************
            
            '********** ABS VAL *******
            sprABS=Abs SPR
            '**************************
            
            
        Case 2 'sp
            
            
            '********** VOLTAGE CALCULATE *******
            VLT0=(CHRES0 * 4.80) / 1677721.5   'SP VOLTAGE
            
            '******* SIGNED / UNSIGNED SECTION ****
            If chSIGN0=1 Then VLT0= -VLT0
            '**********************************
            
            
            '******** LINEARISATION *****
            SP=(VLT0 * 1.09833) '10MOHM / 1MOHM DIVIDER
            '*************************
            
            '********* OFFSET EXTRACT / SUBTRACT *****
            SP=(SP - spOFFSET)
            '******************************
            
            
            '********* LIMIT ******
            If SP > PmaxSP  Then SP= PmaxSP
            If SP < NmaxSP  Then SP= NmaxSP
            '**********************
            
          
            '********** ABS VAL ****
            spABS= (SP * 1000)
            '***********************
            
          
            
    EndSelect
    '********************************************
    
Return




 
        
    
    
    
    'GoSub MEASCLR
 
  
 


FTW_CALC: 'Calculates ddsFRQ tuning words


    DDS_OP = ddsFRQ/0.0149011 '0.03725290298 'Equation to produce DDS tuning word Master CLK / 2pwr28

    'Extract LSB Tuning Word
    FTW = DDS_OP 'DDS_OP (Float) = Freq tuning word (Dword) << bit shift doesn't work with float
    FTWLSB1 = FTWLSB 'Extract LSB and put it in LSB1

    'Extract MSB tuning word
    FTW = FTW << 2 'Shift Bits Left twice


    Select Freq_Reg

        Case 0
            FTWLSB1.15 = 0 'Set LSB bit 15 & 14 to 01 (Code to write to DDS Freq Reg 0)
            FTWLSB1.14 = 1
            FTWMSB.15 = 0 'Set MSB bit 15 & 14 to 01 (Code to write to DDS Freq Reg 0)
            FTWMSB.14 = 1
            DDS_Control_Register.11 = 0 ' Tells DDS to use ddsFRQ Register 0
        Case 1
            FTWLSB1.15 = 1 'Set LSB bit 15 & 14 to 10 (Code to write to DDS Freq Reg 1)
            FTWLSB1.14 = 0
            FTWMSB.15 = 1 'Set MSB bit 15 & 14 to 10 (Code to write to DDS Freq Reg 1)
            FTWMSB.14 = 0
            DDS_Control_Register.11 = 1 ' Tells DDS to use ddsFRQ Register 1
    EndSelect


    Select DDS_Waveform 'Sets the control register code to produce coressponding waveforms

        Case 1 'Sine
            DDS_Control_Register.5 = 0 : DDS_Control_Register.1 = 0
        Case 2 'Triangle
            DDS_Control_Register.5 = 0 : DDS_Control_Register.1 = 1
        Case 3 'Square
            DDS_Control_Register.5 = 1 : DDS_Control_Register.1 = 0 : DDS_Control_Register.3 = 1
    EndSelect




    DDS_CE = 0 'Activate DDS (Set Chip Enable Low)
    SHOut DDS_SDO,DDS_SCK,5, [FTWLSB1\16] 'Set ddsFRQ
    SHOut DDS_SDO,DDS_SCK,5, [FTWMSB\16]
    DDS_Control_Register.8 = 0 'Removes reset from DDS chip
    SHOut DDS_SDO,DDS_SCK,5, [DDS_Control_Register\16] 'Writes to Control Register removing reset
    DDS_CE = 1 'Disable DDS (Set Chip Enable High)


Return

PCTESTSEND:

    Inc testSAY
    If testSAY >= 50 Then
        testSAY=0   
        
        HSerOut ["8      ",Dec2 resVAL8, " Ohm ",13,10]
        HSerOut ["16     ",Dec2 resVAL16," Ohm ",13,10]
        HSerOut ["32     ",Dec2 resVAL32," Ohm ",13,10]
        HSerOut ["64     ",Dec2 resVAL64," Ohm ",13,10,13,10]
        HSerOut ["I      ",Dec5 measCRNT," A ",13,10]
        HSerOut ["V      ",Dec5 SourceVLT," V ",13,10]
        HSerOut ["SPR    ",Dec2 SPR," Ohm ",13,10]
        HSerOut ["SP     ",Dec5 SP," V ",13,10,13,10]
        HSerOut ["RH     ",Dec3 onbRHABS," % "," intTMP   ",Dec3 onbTMPABS," °C ","  extTMP  ",Dec1 Temp0,13,10,13,10]
        
    EndIf             

Return





MEASCONT:

  
    
    If measSAY>=10 Then  'CURRENT SETTLING TIME ILE ILGILI DEĞİŞTİRMEYİN   20 TIMER INT TE
        measSAY=0


        GoSub EXTADCREAD
        
        
        Inc measMODE
        If measMODE>=3 Then measMODE=0
        'measmode=1
    
        Select measMODE
                    
            Case 0 'NORMAL RES
            
                spSW1=0:spSW2=0 'SP SWITCHS OFF
            
                MR1=1:MR2=0 'PROBE RETURN SELECT
                
                GoSub INIT_DDS
                GoSub FTW_CALC
                
                CrnT=1      'CURRENT ON
              
            Case 1  'SPR
                
                spSW1=0:spSW2=0 'SP SWITCHS OFF   

                MR1=0:MR2=1 'SURFACE RETURN SELECT
                
                GoSub INIT_DDS
                GoSub FTW_CALC
                
                CrnT=1      'CURRENT ON
            
            Case 2 'SP MEASUREMENTS
        
                CrnT=0      'CURRENT OFF
                
                DDS_CE = 0 'Activate DDS (Set Chip Enable Low)
                SHOut DDS_SDO,DDS_SCK,5, [$2100\16] 'Apply Reset
                DDS_CE = 1 'Disable DDS (Set Chip Enable High)
                
                MR1=0:MR2=1 'SURFACE RETURN SELECT
            
                
                spSW1=1:spSW2=1 'SP SWITCHES ON
                
          
        EndSelect


    
    EndIf



Return



RXBUFFCLR:

    For gpINX=0 To maxBYTE
        rxBUFF[gpINX]=0
    Next gpINX
    
    sPTR=0

Return

RCVDCONT: 'SERIAL UART RECEIVE CONTROL SUB


    If SrcvD=0 Then Return 'paket alınmadıysa girme
  
  
    '***** paket alındı ****
 
    rxCSUM=rxBUFF[rxLENGTH - 1] 'GELEN PAKETİN CSUM INI AL
    
    rxCALC=0
    For gpINX=0 To (rxLENGTH - 2) 'CSUM VE END CHAR HARIC
        rxCALC=rxCALC + rxBUFF[gpINX]
    Next gpINX
    
    If rxCSUM <> rxCALC Then SrcvD=0:GoSub RXBUFFCLR:Return 'paket doğru değilse çık
    
    '*************** paket doğru alındıysa *******
    
    If rxBUFF[2] <> setID Then SrcvD=0:GoSub RXBUFFCLR:Return 'id uymazsa çık
  
  
    cableRES.HighByte       =rxBUFF[3]       
    cableRES.LowByte        =rxBUFF[4]
  
    '******* GÖNDERME OLURSA ***
    
 
    GoSub SENDPACK   
  
  
    SrcvD=0
    
  

Return




SENDPACK:

    txBUFF[0]   ="*" 'STX
    txBUFF[1]   ="F" 'FSK HEADER
    
    txBUFF[2]   ="E" 'ELOG ID
    
    
    txBUFF[3]   =resVAL8ABS.HighByte
    txBUFF[4]   =resVAL8ABS.LowByte
    
    txBUFF[5]   =resVAL16ABS.HighByte
    txBUFF[6]   =resVAL16ABS.LowByte
    
    txBUFF[7]   =resVAL32ABS.HighByte
    txBUFF[8]   =resVAL32ABS.LowByte
    
    txBUFF[9]   =resVAL64ABS.HighByte
    txBUFF[10]  =resVAL64ABS.LowByte
    
    txBUFF[11]  =sprABS.HighByte
    txBUFF[12]  =sprABS.LowByte
    
    txBUFF[13]  =spABS.HighByte
    txBUFF[14]  =spABS.LowByte
    
    txBUFF[15]  =DIGBYTE
    
    txBUFF[16]  =Temp0ABS.HighByte
    txBUFF[17]  =Temp0ABS.LowByte
    
    txBUFF[18]  =onbTMPABS
    
    txBUFF[19]  =onbRHABS
    
    txBUFF[20]  =prbVLTABS




'    txBUFF[3]   =resVAL8.Byte3
'    txBUFF[4]   =resVAL8.Byte2
'    txBUFF[5]   =resVAL8.Byte1
'    txBUFF[6]   =resVAL8.Byte0
    
'    txBUFF[7]   =resVAL16.Byte3
'    txBUFF[8]   =resVAL16.Byte2
'    txBUFF[9]   =resVAL16.Byte1
'    txBUFF[10]  =resVAL16.Byte0
    
'    txBUFF[11]  =resVAL32.Byte3
'    txBUFF[12]  =resVAL32.Byte2
'    txBUFF[13]  =resVAL32.Byte1
'    txBUFF[14]  =resVAL32.Byte0
    
'    txBUFF[15]  =resVAL64.Byte3
'    txBUFF[16]  =resVAL64.Byte2
'    txBUFF[17]  =resVAL64.Byte1
'    txBUFF[18]  =resVAL64.Byte0
    
'    txBUFF[19]  =SPR.Byte3
'    txBUFF[20]  =SPR.Byte2
'    txBUFF[21]  =SPR.Byte1
'    txBUFF[22]  =SPR.Byte0
    
'    txBUFF[23]  =SP.Byte3
'    txBUFF[24]  =SP.Byte2
'    txBUFF[25]  =SP.Byte1
'    txBUFF[26]  =SP.Byte0
    
'    txBUFF[27]  =DIGBYTE
    
'    txBUFF[28]  =measCRNT.Byte3
'    txBUFF[29]  =measCRNT.Byte2
'    txBUFF[30]  =measCRNT.Byte1
'    txBUFF[31]  =measCRNT.Byte0
    
'    txBUFF[32]  =SourceVLT.Byte3
'    txBUFF[33]  =SourceVLT.Byte2
'    txBUFF[34]  =SourceVLT.Byte1
'    txBUFF[35]  =SourceVLT.Byte0
    
'    txBUFF[36]  =Temp0.Byte3
'    txBUFF[37]  =Temp0.Byte2
'    txBUFF[38]  =Temp0.Byte1
'    txBUFF[39]  =Temp0.Byte0
    
'    txBUFF[40]  =onbTMPABS
    
'    txBUFF[41]  =onbRHABS
    
'    txBUFF[42]  =prbVLTABS
    
'    TXLENGTH=42   
    
    txLENGTH=20
    
    
    txCSUM=0
    For gpINX=0 To txLENGTH
        txCSUM=txCSUM + txBUFF[gpINX]
    Next gpINX
    
    txBUFF[txLENGTH + 1]=txCSUM
    txBUFF[txLENGTH + 2]="#" 'ETX 0
 
    
    txLENGTH=txLENGTH + 2
    
 
    For gpINX=0 To txLENGTH
        HSerOut2 [txBUFF[gpINX]]
    Next gpINX   


Return


OVERSUB:
  
    GoSub READ_EEPROM
    GoSub INIT_INT
    GoSub INIT_UART
  
   '********** 1000HZ %50 %50 DUTY SQUARE WAVE FOR LOW PASS FILTER **
    PR2     =%10011011
    T2CON   =%00000111     
    CCPR1L  =%01001101
    CCP1CON =%00111100
    '***********************************

  
    GoSub INIT_DDS   
    GoSub FTW_CALC
    
    
    GoSub INIT_ADC
    DelayMS 5
    ADC_STRT=1
  
    While 1=1
          
        GoSub RCVDCONT                   
        GoSub READ_PT100
        GoSub ONADCREAD
        GoSub MEASCONT
        
      
        
        
        'GoSub PCTESTSEND
 
        
        If TST_BTN=1 Then btnLCK=0
 
        If TST_BTN=0 And btnLCK=0 Then
            btnLCK=1
          
            GoSub SENDPACK
            
            
        EndIf
        
                
    
    Wend

    
    
'yüzeye gidecek paket
'8 INCH
'16 INCH
'32 INCH
'64 INCH
'SPR
'SP
'OVRSTS
'I
'V
'RH
'T
'PT100
'SYS VOLT

biraz uzunca bir kod. ama yardımcı olur. ad9833 dds entegresini bir projemde kullandım. onun kodunu komple paylaştım.

proton dilinde. C ye çevirebilirsiniz.

OVERSUB başlangıcından hemen sonra init_dds ve ftw_calc rutinleri içerisinde.

bu arada ben AD9833 için 4mhz osilator kullandım. siz 25mhz veya daha değişik ise ddsfrq için rutin içinde ona göre hesap yaparak yazın. entegre çok güzel çalışıyor. ben sinus ile toprak direnci ölçümünde kullandım.

kendinize function generator mü yapacaksınız?. öyle ise bir kaç tavsiyede bulunacağım.
 

Forum istatistikleri

Konular
129,880
Mesajlar
931,002
Kullanıcılar
452,758
Son üye
aprnck

Yeni konular

Çevrimiçi üyeler

Geri
Üst