RF12 module need to convert some C to Basic


Closed Thread
Results 1 to 40 of 44

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Wooooohooooo

    Ok so it is working... As far as I have set all the initialize registers as per the datasheet example with the exception of the 10MHz clock line, and a 4.5V low battery detect...
    I can now see the clock line change to 10MHz

    Working code is below...

    Code:
    define osc 4
    
    ' Define LCD registers and bits
    DEFINE  LCD_DREG        PORTD            'LCD addressing
    DEFINE  LCD_DBIT        4                'L1 = $80         
    DEFINE  LCD_RSREG       PORTD            'L2 = $C0 
    DEFINE  LCD_RSBIT       1                'L3 = $94
    DEFINE  LCD_EREG        PORTD            'L4 = $D4
    DEFINE  LCD_EBIT        3
    
    lcdrw           var portd.2
    nSEL            var portb.4
    SDI             var portb.5
    SCK             var portb.6
    SDO             var portb.7
    dout            var word
    n		        VAR	BYTE		         
    nn		        VAR	byte
    cmd             var bit[16]
    
    TRISB = %00000100
    
    low lcdrw
    nSEL = 1
    SDI = 1
    SCK = 0
    
    pause 500
    
    lcdout $fe,1
    
    Start:
    
    lcdout $fe,$80,"RFM12B Initialize"
    
    pause 500
    
    gosub Init
    
    goto start
    
    
    
    
    
    'Subroutines
    
    WriteCMD:
    
    for n = 15 to 0 step -1
      CMD[n] = dout.15
      dout = dout << 1
    next n
    
    SCK = 0
    nSEL = 0
    
    for nn = 15 to 0 step -1
      if cmd[nn] = 1 then
        gosub write1
      else
        gosub write0
      endif
    next nn
    
    sck = 0
    nSel = 1
    pause 500
    return
    
    
    Write0:
    SDI = 0 : SCK = 0 : pauseus 16 : SCK = 1 : pauseus 1 : return
    
    
    Write1:
    SDI = 1 : SCK = 0 : pauseus 16 : SCK = 1 : pauseus 1 : return
    
    
    
    
    Init:
    
    dout = $80D8                      'enable register, 433MHz, 12.5pf
    gosub Writecmd
    dout = $8208                      'Turn on crystal, !PA
    gosub Writecmd
    dout = $A640
    gosub Writecmd
    dout = $C647                      
    gosub Writecmd
    dout = $94C0                      'VDI, FAST, 134kHz
    gosub Writecmd
    dout = $C2AC
    gosub Writecmd
    dout = $CA80                      
    gosub Writecmd
    dout = $CA83                      'FIFO8, SYNC
    gosub Writecmd
    dout = $C49B
    gosub Writecmd
    dout = $9850                      '!mp, 9810=30kHz, Max Out
    gosub Writecmd
    dout = $E000                      'Not Used
    gosub Writecmd
    dout = $C80E                      'Not Used
    gosub Writecmd
    dout = $C0F7                      '10MHZ, 4.5V
    gosub Writecmd
    
    Return

    This has been very painful for me - having not touched RF before, so I'm hopeing that someone else gets some use out of my hair-pulling...

    There is much more to do, I haven't even got round to sending data yet, and I haven't worked out how you are meant to run the pic on the 1MHz clock, then set it to 10Mhz, and have it all work fine. At the moment I have it running on an external 4Mhz crystal.

    When I get a good working TX and RX bit of code I will start a new post with as much info as I can work out...

    Thanks again to all the people that helped me get this far!

    Cheers

    Dave

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Note to yourself, even though in this case it doesn't matter (because the default OSC is 4), the DEFINE's MUST be in CAPS, either the DEFINE itself, or the parameters, I don't remember which. In my case, I always just put both in caps. Don't have to figure out which one then.
    Code:
    DEFINE OSC 4
    DEFINE  LCD_DREG        PORTD            'LCD addressing
    DEFINE  LCD_DBIT        4                'L1 = $80         
    DEFINE  LCD_RSREG       PORTD            'L2 = $C0 
    DEFINE  LCD_RSBIT       1                'L3 = $94
    DEFINE  LCD_EREG        PORTD            'L4 = $D4
    DEFINE  LCD_EBIT        3
    DEFINE SHIFTPAUSE_US 16
    include "modedefs.bas"
    lcdrw var portd.2:nsel var portb.4:sdi var portb.5:sck var portb.6:sdo var portb.7
    dout var word:trisb=4:lcdrw=0:nsel=1:sdi=1:sck=0:pause 500:lcdout $fe,1
    Start:
    lcdout $fe,$80,"RFM12B Initialize":pause 500:gosub Init:goto start
    WriteCMD:
    nsel = 0 : shiftout sdi , sck , 5 , dout\16 : nsel = 1 : return
    Init:
    for temp = 0 to 12
    lookup temp , [ $80D8 , $8208 , $A640 , $C647 , $94C0 , $C2AC , $CA80 , $CA83 , $C49B , $9850 , $E000 , $C80E , $C0F7 ] , dout
    gosub writecmd
    next temp
    Return
    This should do exactly the same thing, but uses the shiftout command. Assuming your original program worked as you wanted it to work....

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,139


    Did you find this post helpful? Yes | No

    Default

    Here come the colon again!

    Well, Dave, actually you are running the Oscillator of the PIC at 4MHz, but the core is running at 1MHz (/4).

    If you want the pic running at 10MHz, then you should capitalize the define on top of the program as Skimask stated like this:

    DEFINE OSC 10

    After that, compiler will take care of the rest timing.

    Ioannis

  4. #4
    Join Date
    Dec 2007
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Osc

    Ioannis...

    I had defined the osc to 10, the problem was that at startup, the RFM12 only puts out a 1Mhz clock signal, and at that speed, it would not initialize the RFM12... Not exactly sure why.. I did some mucking around last night, and have now got it to start on 1Mhz, write to the LCD first, then initialize the RFM12. I'm not sure why but it did not want to do this the other way around.

    I'm glad I am now making progress..

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,139


    Did you find this post helpful? Yes | No

    Default

    Let me understand what you are doing. You are clocking the PIC from a 1MHz RFM12 output?

    Ioannis

  6. #6
    Join Date
    Dec 2007
    Posts
    60


    Did you find this post helpful? Yes | No

    Default clock

    on first turn-on, the RFM12 outputs a 1Mhz osc for the pic, which I am putting in on OSC1.
    After I write the command ... dout = $C0F7 : gosub Writecmd ... this changes that osc freq from 1MHz to 10MHz... so my pic will run at 10MHz. I have figured out the problem to just be that my crystal setting was on XT, not HS.

    I am onto the next part of converting C to Basic if anyone dare to assist....
    Code:
    void WriteFSKbyte( uchar DATA )
    {
      uchar RGIT=0;
      uint temp=0xB800;
      temp|=DATA;
    Loop: SCK = 0;
      nSEL=0;
      SDI=0;
      SCK=1;
      if(SDO)         //polling SDO
      {
        RGIT=1;
      }
      else
      {
        RGIT=0;
        WriteCMD(temp);
      }
    }
    So far I have...

    Code:
    WriteFSKbyte:
    temp = $B800
    
    Loop:
    SCK = 0
    nSEL = 0
    SDI = 0
    SCk = 1
    if SDO = 1 then
      rgit = 1
    else
      rgit = 0
    endif
    SCK = 0
    SDI = 1
    nSEL = 1
    if rgit = 0 then
      goto loop
    endif
    rgit = 0
    dout = temp
    gosub writecmd
    return
    The part I am struggling with right now is...

    Code:
      uchar RGIT=0;
      uint temp=0xB800;
      temp|=DATA;
    Does this take bit 0 of DATA and stick it to temp somehow??

    Someone with some C knowledge please help me!

    Thanks!

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,139


    Did you find this post helpful? Yes | No

    Default

    Why are you compilcating things?

    Just put a crystal or resonator on the pic and have it working on a precise time base. What you are doing now is screwing the timing of the commands and may be locks the pic at the time the freq. is changing. Also at 4MHz and lower the PIC should use XT settings and above that HS settings.

    I strongly recommend the PIC using its own clock.

    Ioannis

Similar Threads

  1. Version Control
    By btaylor in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 16th October 2011, 17:12
  2. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  3. Replies: 1
    Last Post: - 27th July 2008, 06:14
  4. using AND as an IF statement
    By dw_pic in forum mel PIC BASIC
    Replies: 27
    Last Post: - 8th June 2006, 18:05
  5. convert Basic Stamp2 (BS2) => Pic
    By bs2rdu in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 15th September 2005, 19:55

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts