اطلاعیه

Collapse
No announcement yet.

برنامه ی راه اندازی rfm12

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    برنامه ی راه اندازی rfm12

    بعد از کلی تلاش تونستم rfm12 رو راه بندازم . شماتیک و سورس کد بسکامشم برای دوستان میذارم .
    اما یه مشکل هست !
    این برنامه حالت تست رو داره و بیشتر جنبه ی اموزشی داره .به خاطر همین از استادان تقاضا میکنم زحمت تشریح کردن برنامه رو بکشند
    تا همه بتونند نحوه ی کار با این مازول رو درک کنند.

    #2
    پاسخ : برنامه ی راه اندازی rfm12

    $regfile = "m8def.dat"
    $hwstack = 32
    $swstack = 10
    $framesize = 40

    $crystal = 16000000
    $baud = 9600

    Const File = "TxRx 1.05 .bas"
    Const Description = "RFM12 monitor program"

    Print
    Print Description
    Print File
    Print Version()

    Rfm12_rst Alias Portc.5
    Config Rfm12_rst = Input

    Config Portb = &B0010_1101

    Test_pin Alias Portb.0

    Rfm12_cs Alias Portb.2
    Set Rfm12_cs

    Rfm12_sdi Alias Portb.3

    Rfm12_sdo Alias Pinb.4
    Set Portb.4

    Rfm12_sck Alias Portb.5

    Rx_led Alias Portd.5
    Config Pind.5 = Output

    Tx_led Alias Portd.6
    Config Pind.6 = Output

    Error_led Alias Portd.7
    Config Pind.7 = Output

    Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , _
    Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 1
    Spiinit

    Declare Sub Rfm12_spi_wrt
    Declare Sub Rfm12_tx(txarray As Byte)
    Declare Sub Rfm12_rx(rxarray As Byte)
    Declare Sub Rfm12_tx_ready
    Declare Sub Rfm12_rx_wait
    Declare Sub Rfm12_get_status_bit
    Declare Sub Saveflags
    Declare Sub Rfm12_clear_fifo
    Declare Sub Rfm12_rx_on
    Declare Sub Rfm12_init
    Declare Sub Printflags

    'From: RF12 Programing Guide
    'Min Freq 430.24
    'Max Freq 439.75
    'in 2.5KHz steps
    '
    'That makes 3800 possible frequencies
    'to choose from.

    'Fc = the center Freq. in MHz
    'Fr = the Freq. register value.
    'Fc = 430 + Fr * 0.0025 (MHz)
    'Fr = (Fc - 430) * 400
    Const Fc = 915.00
    Const Fr =(fc - 430) * 400
    Const Fcmnd = &HA000 + Fr

    'Payload can be up to 254 bytes.
    'I have tested up to 128 bytes only.
    'const Payload_Size = 128
    Const Payload_size = 32
    'const Payload_Size = 16
    Const Pls_plus1 = Payload_size + 1

    'make the test beacon length Payload_Size - 2
    Const Beacon = "111111789012345678901234567890"

    Dim Tempbit As Bit
    Dim Temp As Byte
    Dim Tempw As Word

    Dim Spi_tx_wrd As Word
    Dim Spi_tx_l As Byte At Spi_tx_wrd Overlay
    Dim Spi_tx_h As Byte At Spi_tx_wrd + 1 Overlay
    Dim Spi_rx_wrd As Word
    Dim Spi_rx_l As Byte At Spi_rx_wrd Overlay
    Dim Spi_rx_h As Byte At Spi_rx_wrd + 1 Overlay

    Dim Rx_status_bit As Bit
    Dim Rxbuffer(pls_plus1) As Byte
    Dim Rxstring As String * Payload_size At Rxbuffer(1) Overlay
    Rxbuffer(pls_plus1) = 0

    Dim Txindex As Byte
    Dim Txbuffer(pls_plus1) As Byte
    Dim Txstring As String * Payload_size At Txbuffer(1) Overlay
    Txbuffer(pls_plus1) = 0

    Dim Timeout As Word
    Dim Cmnd As String * 4
    Dim Flags As Byte

    Echoflag Alias Flags.0
    Beaconflag Alias Flags.1
    Sermsgflag Alias Flags.2
    Rxonflag Alias Flags.3
    Txonflag Alias Flags.4

    'I have read somewhere that eram address 0 has a bug
    'so I don't use it.
    Dim Dummy As Eram Word At 0
    'ProgCntr must be at eram address 2.
    'ProgCntr is incramented by the bootloader
    'each time the chip is flashed.
    Dim Progcntr As Eram Word At 2
    Dim Savedflags As Eram Byte


    Tempw = Progcntr
    Print "uC flashed " ; Tempw ; " times."
    Print

    'kill some time here while
    'rfm12 does a power on reset.
    Reset Rx_led
    Reset Tx_led
    Reset Error_led
    Waitms 500
    Set Rx_led
    Set Tx_led
    Set Error_led

    Rfm12_init
    Rfm12_rx_on

    Flags = Savedflags
    Print "Echo = " ; Echoflag
    Print "Beacon = " ; Beaconflag
    Cmnd = ""

    Do
    If Beaconflag = 1 Then
    'Tx a test beacon about once a second.
    Txstring = Beacon
    Print "Tx: " ; Txstring
    Rfm12_tx Txbuffer(1)
    'now loop waiting for a reply msg.
    For Temp = 1 To 255
    'poll RFM12 for Rx data.
    Rfm12_get_status_bit
    If Rx_status_bit = 1 Then
    'Rx RxBuffer
    Rfm12_rx Rxbuffer(1)
    Print "Rx: " ; Rxstring
    If Rxstring <> Txstring Then
    Reset Error_led
    Print "<>"
    End If
    Rxstring = ""
    Exit For
    End If
    If Temp = 255 Then
    Reset Error_led
    Print "no echo"
    End If
    'this delay gives the other module time to echo
    'back the beacon.
    'waitus 50 'at 38.3kbps, 32 byte payload
    'waitus 350 'at 19.2kbps, 128 bytes
    Waitus 150 'at 19.2kbps, 32 bytes
    Set Error_led
    Next Temp
    Else
    'poll RFM12 for Rx data.
    Rfm12_get_status_bit
    If Rx_status_bit = 1 Then
    'reset RX_LED
    'Rx RxBuffer
    Rfm12_rx Rxbuffer(1)
    Print "Rx: " ; Rxstring
    If Echoflag = 1 Then
    Print "Ec: " ; Rxstring
    'Tx RxBuffer.
    Rfm12_tx Rxbuffer(1)
    End If
    'set RX_LED
    End If
    End If

    Temp = Inkey()
    If Temp <> 0 Then

    If Temp = 27 Then
    'press "esc" to get a command prompt.
    Spi_tx_wrd = 0
    Rfm12_spi_wrt
    Print
    Print Description
    Print File
    Print Version()
    Print
    Tempw = Progcntr
    Print "uC flashed " ; Tempw ; " times."
    'print
    'print "Status word = " ; bin(SPI_Rx_H) ; "_" ; bin(SPI_Rx_L)
    Printflags
    Print
    Input "Command >" , Cmnd
    Print
    Rfm12_rx_on
    Elseif Temp = 13 Then
    Print
    Print "Tx: " ; Txstring
    'Tx TxBuffer.
    Rfm12_tx Txbuffer(1)
    Txstring = ""
    Txindex = 0
    Else
    'fill the TxBuffer until <cr> or full.
    If Txindex < Payload_size Then
    Print Chr(temp);
    Incr Txindex
    Txbuffer(txindex) = Temp
    Temp = Txindex + 1
    Txbuffer(temp) = 0
    End If
    End If
    End If


    If Cmnd <> "" Then
    Select Case Cmnd
    Case "u"
    'To start the bootloader without using the reset button.
    'Press "u <CR>"
    'then within 2+3=5 seconds press F4 to start the programer.
    Print
    Print "Press F4 to start upload."
    Waitms 3000
    'address of boot loader.
    'for mega168
    'goto &H1C00

    'for mega88
    Goto &HC00

    Case "c"
    'Input a hex command word value to send to RFM12.
    'For example enter: c0e0 <enter> will set the
    'ouput CLK to 10MHZ.
    'see the RFM12 Programing guide for commands.
    Print
    Input "Enter a Command Word in HEX >" , Cmnd
    Spi_tx_wrd = Hexval(cmnd)
    Rfm12_spi_wrt
    Print "RFM12 returned >" ; Hex(spi_rx_wrd)

    Case "b"
    Toggle Beaconflag
    If Beaconflag = 1 Then Echoflag = 0
    Saveflags
    Printflags

    Case "e"
    Toggle Echoflag
    If Echoflag = 1 Then Beaconflag = 0
    Saveflags
    Printflags

    Case Else
    Print "Unknown Command"

    End Select
    Print
    Cmnd = ""
    End If
    Loop

    Sub Printflags
    If Beaconflag = 1 Then
    Print "Beacon ON"
    Else
    Print "Beacon OFF"
    End If
    If Echoflag = 1 Then
    Print "Echo ON"
    Else
    Print "Echo OFF"
    End If
    End Sub

    Sub Saveflags
    'save to eram
    Flags = Flags And 3
    Savedflags = Flags
    End Sub

    Sub Rfm12_init
    Local I As Byte
    Print "Init values:"
    'Initialize RFM12
    Restore Init_data
    For I = 1 To 14
    Read Spi_tx_wrd
    Print Hex(spi_tx_wrd)
    'send init data to RFM12.
    Rfm12_spi_wrt
    'waitms 100
    Next I
    Print
    End Sub

    '******************************************** ***************************
    'The following data lines are RFM12 commands used to initialize the device.
    'read the RFM12 progamming guide and data sheets for a complete
    'description of these commands.
    '
    'Configuration settings:
    '
    '&h80D8 = Configure module for 433Mhz band.
    '
    'RF center Freq. setting
    'select freq with const "Fc" above.
    'Fcmnd is a constant that holds the Freq command.
    'see the "const" statments at the top of this code.
    'example: &hA000 + Fr = &hA7D0 for 435MHz
    '
    'To change the data rate the following values will need to be adjusted.
    '
    'bit rate
    '&hC611 = 19.2kbps works well at 8MHz
    '&hC608 = 38.3kbps works at 8MHz
    '
    'Rx band width
    '&h94A0 = Rx BW = 134KHz works at 8MHz
    '&h9480 = 200kHz works at 8MHz
    '
    'Tx deviation
    '&h9850 = 90kHz works at 8MHz
    '&h9870 = 120kHz works at 8MHz

    Init_data:
    Data &H80D8% , &H8298% , Fcmnd% , &HC611% , &H94A0% , &HC2AC% , &HCA81%
    Data &HCED4% , &HC483% , &H9850% , &HCC17% , &HE000% , &HC800% , &HC040%
    '******************************************** *****************************

    Sub Rfm12_clear_fifo
    'Clear FIFO
    Spi_tx_wrd = &HCA81
    Rfm12_spi_wrt
    Spi_tx_wrd = &HCA83
    Rfm12_spi_wrt
    End Sub

    Sub Rfm12_tx(txarray As Byte)
    Local I As Byte
    Reset Tx_led
    'turn on Tx
    Spi_tx_wrd = &H8239
    Rfm12_spi_wrt
    Txonflag = 1
    Waitus 150
    'Send Preamble.
    Restore Txpreamble_data
    For I = 1 To 5
    Read Spi_tx_wrd
    Rfm12_tx_ready
    If Timeout = 0 Then Exit For
    Rfm12_spi_wrt
    Next I
    'Send payload.
    For I = 1 To Payload_size
    Rfm12_tx_ready
    If Timeout = 0 Then Exit For
    Spi_tx_wrd = &HB800 + Txarray(i)
    Rfm12_spi_wrt
    Next I
    Rfm12_tx_ready
    Rfm12_rx_on
    Set Tx_led
    End Sub

    Txpreamble_data:
    Data &HB8AA% , &HB8AA% , &HB8AA% , &HB82D% , &HB8D4%

    Sub Rfm12_tx_ready
    Reset Rfm12_cs
    Timeout = 65500
    While Rfm12_sdo = 0
    Decr Timeout
    Waitus 6
    If Timeout = 0 Then
    Print "Tx Timeout"
    Exit While
    End If
    Wend
    End Sub

    Sub Rfm12_rx_on
    'Start Rx
    Spi_tx_wrd = &H8299
    Rfm12_spi_wrt
    Rfm12_clear_fifo
    Rxonflag = 1
    Waitus 80
    End Sub

    Sub Rfm12_rx(rxarray As Byte)
    Local I As Byte
    Timeout = 1
    Reset Rx_led
    Spi_tx_wrd = &HB000
    For I = 1 To Payload_size
    Rfm12_rx_wait
    If Timeout = 0 Then
    Set Rx_led
    Set Error_led
    Exit For
    End If
    Rfm12_spi_wrt
    Rxarray(i) = Spi_rx_l
    Next I
    Rxarray(i) = 0
    Rfm12_clear_fifo
    Set Rx_led
    End Sub

    Sub Rfm12_rx_wait
    Timeout = 35500
    Do
    Rfm12_get_status_bit
    'timeout if nothing happens.
    Decr Timeout
    If Timeout = 0 Then
    Reset Error_led
    Print "Rx Timeout"
    Exit Do
    End If
    Loop Until Rx_status_bit = 1
    End Sub

    Sub Rfm12_get_status_bit
    Reset Rfm12_sdi
    Reset Rfm12_cs
    Set Rfm12_sck
    Rx_status_bit = Rfm12_sdo
    Reset Rfm12_sck
    Set Rfm12_cs
    End Sub

    Sub Rfm12_spi_wrt
    'Send word (SPI_Tx_wrd) to RFM12.
    'Also receive word (SPI_Rx_wrd).
    'RFM12 Chip select
    Reset Rfm12_cs
    'send hi byte.
    Spi_rx_h = Spimove(spi_tx_h)
    'send lo byte.
    Spi_rx_l = Spimove(spi_tx_l)
    'deselect chip.
    Set Rfm12_cs
    End Sub

    دیدگاه


      #3
      پاسخ : برنامه ی راه اندازی rfm12

      سلام
      این برنامه برای فرستنده و گیرنده یکیه؟

      دیدگاه


        #4
        پاسخ : برنامه ی راه اندازی rfm12

        شماتیک رو هم می شه بذارین؟

        دیدگاه

        لطفا صبر کنید...
        X