Control a Radio PLL with a PIC


Closed Thread
Results 1 to 40 of 54

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    37

    Default Control a Radio PLL with a PIC

    I am very new to the PIC world and to PBP. So if I ask what seems like a dumb question, please bare with me.
    I want to control an old Knight Kit 2 meter radio.
    It currently uses a thumbwheel to program the PLL in Binary.
    The count starts at 000000000. And ends at 011111111.
    I want to use an Encoder with 2 outputs. CW/CCW. Using a +5 volt reference.
    So each pulse is a logical 1.
    It will either increment the count by 1 or decrement the count by 1 depending on which way the encoder is turning.
    CCW will equal Down pulses on RA2, CW = Up on RA3.
    The PLL program pins require +5 volt logical 1 for selection.
    I also want to display the count from 0 to 255 in decimal on a 3 digit 7 segment LED display.
    PIC is an 18F6680.

    I tried doing this as asm but didn't get anywhere.
    It seemed like it should be straight forward, but I quickly discovered it wasn't.

    I thought about using Button to monitor the two input lines but read in the manual that this would not be good for what I want to do.

    I searched the forum looking for something similar but couldn't find anything related.
    Any suggestions or thoughts on how I should approach this in PBP?
    All comments welcome.
    Thanks

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Sounds like FUN! First let me say, I think if you search on encoder, you may have better results. I am pretty sure there is at least an article.
    Now having said that, I have some questions for you. Encoders usually have an A and a B channel. are these what you are calling the CW and CCW outputs? If so is this an encoder with extra drive board or something?

    Are you planning to didicate 8 lines to the PLL, 10 lines to the display and 2 lines to encoder? Or do you have a different hardware plan?

    This will be pretty straight forward, but we need to know what background you do or do not have so far for this.

    Are you set fast on the uP? if you are just starting out with PIC's you may want to consider a 16F device. Just my opinion.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    That's funny. I just ordered 2 pic18f4431 s on Thursday for this specific purpose. I found even using asm interrupts the 16f690 too slow to track the encoder without losing steps, (either that or it was switch bounce). So I figure to use a PIC with QEI Hardware inputs and a 40mhz clock. Failing that I will try using flip flops to buffer the inputs. In any event there is plenty to learn.
    Now off to Costco for a box of Corn Nuts, and 3 pounds of coffee . . .
    Edit:
    I also want to display the count from 0 to 255 in decimal on a 3 digit 7 segment LED display.
    PIC is an 18F6680.
    Or you can use a lookup table and display channels or frequency.
    Last edited by Archangel; - 11th June 2011 at 03:41. Reason: add
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Joe, How fast are you spinning the encoder? How many CPR? I have been using 500 line (2000 CPR) encoder for a gate position applacation for about 6-7 years now. No bounce because they are optical. never lose any pulses, but they are going pretty slow. (30RPM) 1000 cps. I am doing this with 16F676, no interrupts just checking the A-B states in my main. It is a fast main loop but never the less. Oh and running on 4Mhz internal OSC
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Quote Originally Posted by cncmachineguy View Post
    Joe, How fast are you spinning the encoder? How many CPR? I have been using 500 line (2000 CPR) encoder for a gate position applacation for about 6-7 years now. No bounce because they are optical. never lose any pulses, but they are going pretty slow. (30RPM) 1000 cps. I am doing this with 16F676, no interrupts just checking the A-B states in my main. It is a fast main loop but never the less. Oh and running on 4Mhz internal OSC
    Hi, I am using the "El Cheapo" mechanical encoders similar to a rotory switch, just 3 terminals A com B tried both kinds gray scale and quatdradture. I think it is losing counts during the serout routine, it should not but it does, again it could be noisy encoder too, I will know better when I get those PICs next week. I have some optical encoders here, but they have a bizillion steps per rev. These switch type have 24.
    Here is the code:
    Code:
    @ __config _HS_OSC & _WDT_ON & _PWRTE_ON & _MCLRE_OFF  & _CP_OFF & _FCMEN_OFF & _BOR_OFF
    
    '************************ DEFINES HERE *************************************
    DEFINE OSC 20               ' set to 20mhz
    INCLUDE "MODEDEFS.BAS"
    clear                       ' clear out variables
     DEFINE DEBUG_REG PORTC
     DEFINE DEBUG_BIT 6
     DEFINE DEBUG_BAUD 19200
     DEFINE DEBUG_MODE 0
    
    '********************** PIC16F690 setup ************************************
    ADCON0  = 0
    ADCON1  = 0
    ANSEL   = 0
    ANSELH  = 0
    CM1CON0 = 0
    CM2CON0 = 0
    
    TrisA = %00000000
    TrisB = %11000000 
    TrisC = %00000000
    
    '*********************** ASSEMBLY INTERUPT VARIABLES ***********************
    wsave   var     byte $20   system
    wsave1  var     byte $a0   system    ' Necessary for devices with RAM in bank1
    wsave2  var     byte $120  system    ' Necessary for devices with RAM in bank2
    ssave   var     byte bank0 system
    psave   var     byte bank0 system     
    
    '***************************************************************************
    enc_old         VAR BYTE
    enc_new         VAR BYTE
    enc_tmp         var byte
    enc_counter     VAR BYTE
    enc_counter_old VAR BYTE
    enc_scaler      var BYTE
    Display         Var Byte
    
    INTCON = %10001000      ' Enable PortB Change Interupts  
    IOCB   = %11000000
    pause 5500            ' wait for LCD to start 
    
    goto start            'skip over interupt handler
    '*********************** ASSEMBLY INTERUPT HANDLER *************************
    
    define  INTHAND myint
    Asm
           
    myint 
        
    
    
    
        ;====== BEGINNING OF THE ROTARY ENCODER CODE ========
        ;The Rotary Encoder is connected to PORTB  
        ;The A signal of the encoder connected to the PIN portB.7
        ;The B signal of the encoder connected to the PIN portB.6
        ;
        ;The 4 variables used are declared in the PicBasic code.
        ;
        ;    enc_new VAR BYTE
        ;    enc_old VAR BYTE
        ;   enc_tmp VAR BYTE
        ;    enc_counter VAR WORD
        ;
        ;================================================
            
           ;Read latest input from PORTB & put the value in enc_new.
             movf    PORTB,W
             movwf  _enc_new
    
             ;Strip off all but the 2 MSBs in enc_new.
             movlw    0xc0             ;Create bit mask (bits 7 & 6). b'11000000' ?
             andwf   _enc_new,F       ;Zero bits 5 thru 0.
    
            ;check to see if encoder has moved
            movf    _enc_old,W         ;move enc_old to W
            movwf   _enc_tmp           ;put W to enc_tmp
            movf    _enc_new,W         ;move enc_new to W for XOR
            xorwf   _enc_tmp,F         ;XOR enc_tmp to detect encoder movement
            btfsc   _enc_tmp,7         ;if bit is clear, encoder moved.
            goto    Continue           ;no movement exit isr
    
             ;Determine the direction of the Rotary encoder.  
             rlf     _enc_old,F          ;left shift it into _enc_old to align bit 6 of 
                                     ;_enc_old with bit 7 of enc_new.
    
             movf    _enc_new,W          ;Move the contents of enc_new to W in order to XOR.
             xorwf   _enc_old,F         ;XOR previous inputs (in _enc_old) with latest
                                     ;inputs (in W) to determine CW or CCW.
     
              btfsc   _enc_old,7          ;Test bit 7 of result (in _enc_old).  Skip next line
                                       ;if it is 0 (direction is CCW).
             goto    Up               ;Bit is 1 (direction is CW).  Go around Down
                                     ;and increment counter.
    
    Down
             ;Decrements enc_counter because the rotary encoder moved CCW.
            ;Decrements enc_counter (16 bit value), sets Z on exit.
                  
            decf    _enc_counter,F      ; Decrement low byte
            incfsz  _enc_counter,W      ; Check for underflow
            incf    _enc_counter+1,F    ; Update
            decf    _enc_counter+1,F    ; Fixup
            movf    _enc_counter,W
            iorwf   _enc_counter+1,W    ; Set Z bit
    
            
            ;Add here code for the CCW LED if needed.
             ;bsf     _led            ;turn on led
             
             goto    Continue          ;Branch around UP.
    
    Up
            ;Increments enc_counter because the rotary encoder moved CW.
            ;Increments enc_counter (16 bit value), sets Z on exit.
    
            incfsz  _enc_counter,W      ; Add one to low byte
            decf    _enc_counter+1,F    ; No carry (negates next step)
            incf    _enc_counter+1,F    ; Add one to high byte
            movwf   _enc_counter        ; Store updated low byte back.
            iorwf   _enc_counter+1,W    ; Set Z flag
    
            
           ;Add here code for the CW LED if needed.
           ;bsf     _led2                ; turn on led
        
    Continue         
    ;       Assign the latest encoder inputs (in enc_new) to _enc_old.
             movf     _enc_new,W
             movwf   _enc_old
    
    ;       Restore saved registers
            movf    psave, W
            movwf   PCLATH
            swapf   ssave, W
            movwf   STATUS
            swapf   wsave, F
            swapf   wsave, W
            bcf     INTCON, RABIF          ; Clear the Interupt Flag
            RETFIE                             ; Return from interrupt
    endasm
    
    '***************************************************************************
    '************************* PROGRAM STARTS HERE *****************************
    '***************************************************************************
    
    START:              ' Main Program starts here
    
    
    
    
    enc_counter     = 0    ' set default encoder value
    enc_counter_old = 0
       ' DEBUG 254,2,  "ENCODER             "  ' change display
       ' DEBUG 254,192,#Display,"                 "
       ' DEBUG 254,148,"                    "  ' clear display
       ' DEBUG 254,212,"                    "    
        
    MAIN:  'see if value has changed
        if  enc_counter <> enc_counter_old then enc_counter_old = enc_counter
        Display = (enc_counter/2)
        
    
       
                     
        DEBUG 254,2,"ENCODER             "          ' change display
        DEBUG 254,192,#Display,"                  "
        DEBUG 254,148,"                    "  ' clear display
        DEBUG 254,212,"                    "      ' display enc_counter value
        sound portC.7,[display,1]           
                    
    goto MAIN
    Got it here, more or less: http://www.picbasic.co.uk/forum/showthread.php?t=9700

    It actually works a bit better using serout instead of debug, at least as far as the display goes, but is faster using debug, in terms of how fast the loop cycles.
    I really have not invested that much time into it.
    Last edited by Archangel; - 11th June 2011 at 07:13.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    For the Rotary Encoder have a look at this approach.

    I wanted to do it also with interrupts, but I got interrupted many times!

    http://www.picbasic.co.uk/forum/cont...er#new_comment

    Ioannis

  7. #7
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Quote Originally Posted by Archangel View Post
    That's funny. I just ordered 2 pic18f4431 s on Thursday for this specific purpose. I found even using asm interrupts the 16f690 too slow to track the encoder without losing steps, (either that or it was switch bounce). So I figure to use a PIC with QEI Hardware inputs and a 40mhz clock. Failing that I will try using flip flops to buffer the inputs. In any event there is plenty to learn.
    Now off to Costco for a box of Corn Nuts, and 3 pounds of coffee . . .
    Edit:

    Or you can use a lookup table and display channels or frequency.
    Do you have an old Knight Kit radio as well?
    Display channels or frequency? This sounds great. How would I do that?
    My displays are only 3 digits.

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Exclamation Re: Control a Radio PLL with a PIC

    Quote Originally Posted by RayL113 View Post
    Do you have an old Knight Kit radio as well?
    Display channels or frequency? This sounds great. How would I do that?
    My displays are only 3 digits.
    Nope no Knight kit, I was thinking of a retrofit for old 23 ch CB radios make them useful, other freqs, 10M . . . set a couple of vars
    Code:
     temp var byte
    Code:
     i var byte
    Code:
     lookup i[010,020,030 . . .500],temp
    this aliases your count to the numbers in the lookup table . . . You could set a freq switch with static displays like 142, 143, 144 . . . and follow with the displays driven by the lookup table displaying the fine freqs. Or change up to a word var and you have up to 65535 values you could step through, or use 2 encoders (old Henry 2m used 2 switches ganged) sorry about the code formats, crazy forum software glitch killed formatting for me, so I had to do it this way.
    Last edited by Archangel; - 11th June 2011 at 08:53.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  9. #9
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Var, I take to mean variable. Remember I'm new to Pics and PBP. So a word var? Is this an extended table?

  10. #10
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Quote Originally Posted by cncmachineguy View Post
    Sounds like FUN! First let me say, I think if you search on encoder, you may have better results. I am pretty sure there is at least an article.
    Now having said that, I have some questions for you. Encoders usually have an A and a B channel. are these what you are calling the CW and CCW outputs? If so is this an encoder with extra drive board or something?

    Are you planning to didicate 8 lines to the PLL, 10 lines to the display and 2 lines to encoder? Or do you have a different hardware plan?

    This will be pretty straight forward, but we need to know what background you do or do not have so far for this.

    Are you set fast on the uP? if you are just starting out with PIC's you may want to consider a 16F device. Just my opinion.
    Thanks for the reply. I'll try to answer as best I can.
    The encoder acts like two momentary switches. It has 3 pins. Center is common and the two outside pins complete the switch. When turning in one direction only 1 pin has high pulses.
    Not Gray code.
    10 lines for the PLL and 11 for the display (decimal point)
    My background is machine language only. No C or Visual Basic.
    And yes. Set fast on the processor. Have already purchased 10 of these. Have a lot more projects I want to try.

  11. #11
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    It looks like you should have a comma:

    PEEK CHSEL, CHSELVAR

  12. #12
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    I have a concern for you, in this bit of code
    Code:
    LOOP
    PEEK CHSEL CHSELVAR
    If CHSEL > CHSELVAR Then UP
    Else 
    PEEK CHSEL CHSELVAR
    IF CHSEL < CHSELVAR Then DOWN
    Else
    GOTO Start
    I think your intention is to set chselvar equal to the contents of address chsel. then in the next line you test to see if they are different. So if I am following this correctly, you will have to move the channel selector at the exact moment between the peek and the IF. May work better to set a var like chselvarold then on the next loop see if chselvar = chselvarold. if = then goto start, if not decode what to do.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  13. #13
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Quote Originally Posted by cncmachineguy View Post
    I have a concern for you, in this bit of code
    Code:
    LOOP
    PEEK CHSEL CHSELVAR
    If CHSEL > CHSELVAR Then UP
    Else 
    PEEK CHSEL CHSELVAR
    IF CHSEL < CHSELVAR Then DOWN
    Else
    GOTO Start
    I think your intention is to set chselvar equal to the contents of address chsel. then in the next line you test to see if they are different. So if I am following this correctly, you will have to move the channel selector at the exact moment between the peek and the IF. May work better to set a var like chselvarold then on the next loop see if chselvar = chselvarold. if = then goto start, if not decode what to do.
    What about if I don't recheck with Peek and do this instead;
    CHLP:
    PEEK CHSEL, CHSELVAR 'Look at the 4 input pins from the channel selector
    If CHSEL > CHSELVAR Then UP 'if it's larger then run subroutine UP
    IF CHSEL < CHSELVAR Then DOWN 'if it's smaller then run subroutine DOWN
    GOTO Start 'if neither are true then goto start

    What do you think?

  14. #14
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    That worked great! Thanks

  15. #15
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Quote Originally Posted by scalerobotics View Post
    It looks like you should have a comma:

    PEEK CHSEL, CHSELVAR
    Adding the comma worked great! Thanks

  16. #16
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Got that working! Thanks for the tip,
    Now on to the display. 3 digit 7 segment display, common cathode.
    I want to take the port C pin states and look those up in a table.
    Then Send the table contents at that address to the 3 digit display.
    I've been reading post and looking at some lookup codes but to be honest, I don't know where to start.
    I think I know what I want in the table but getting it to the display is where I'm getting confused.
    Any suggestions?

  17. #17
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Rayl113, I assume the display is going to be multiplexed as all you are using is port c? Is it going to have latches for the digit data or is it going to be some kind of serial display?
    Dave Purola,
    N8NTA
    EN82fn

  18. #18
    Join Date
    Jun 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Control a Radio PLL with a PIC

    Quote Originally Posted by Dave View Post
    Rayl113, I assume the display is going to be multiplexed as all you are using is port c? Is it going to have latches for the digit data or is it going to be some kind of serial display?
    The display is laid out like this;
    RB0=A
    RB1=B
    RB2=C
    RB3=D
    RB4=E
    RC0=F
    RC1=G
    RC2=dp
    RC3=Digit-1
    RC4=Digit-2
    RC5=Digit-3
    RC6=Digit-4(future)
    RC7=Digit-5(future)

    I'm using NPN transistors to switch the digit commons to ground.
    It would probably be a good idea to latch in the display data but I'm not sure how to proceed.
    It's mostly learn by trial and error at this point.

Members who have read this thread : 1

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