--------------------------- *.H  ---------------------


#include <16f628.h>



#FUSES NOWDT                     //No Watch Dog Timer

#FUSES INTRC_IO                  //Internal RC Osc, no CLKOUT

#FUSES PUT                       //Power Up Timer

#FUSES NOPROTECT                 //Code not protected from reading

#FUSES BROWNOUT                  //Reset when brownout detected

#FUSES MCLR                      //Master Clear pin enabled

#FUSES NOLVP                     //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#FUSES NOCPD                     //No EE protection



#use delay(clock=4000000)

#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)



#define  LMX_LE   PIN_A0

#define  LMX_DA   PIN_A1

#define  LMX_CL   PIN_A2

#define  GREEN    PIN_A3

#define  RED      PIN_A4

#define  LOCK     PIN_B3

#define  JMP2     PIN_A6

#define  JMP3     PIN_A7

#define  LMX_LD   PIN_B0

#define  RX       PIN_B1

#define  TX       PIN_B2

#define  SPARE    PIN_B3

#define  JMP0     PIN_B4

#define  JMP1     PIN_B5



#define N19_GO 128 // CHARGE PUMP CURRENT

#define N18_B 64

#define N17_B 32

#define N16_B 16

#define N15_B 8

#define N14_B 4

#define N13_B 2

#define N12_B 1

#define N11_B  128

#define N10_B 64

#define N9_B 32

#define N8_B 16

#define N7_B 8

#define N6_B 4  // B from 3 to 8191

#define N5_A 2 // X for LM2306

#define N4_A 1 // X for LMX2306

#define N3_A 128 // A from 0 to 7 for LMXX2306

#define N2_A 64

#define N1_A 32



#define C2 16

#define C1 8



#define R19_LD 128 // LOCK DETECTOR PRECISION

#define R18_T 64

#define R17_T 32

#define R16_T 16

#define R15_T 8

#define R14 4

#define R13 2

#define R12 1

#define R11 128

#define R10 64

#define R9 32

#define R8 16

#define R7 8

#define R6 4

#define R5 2

#define R4 1

#define R3 128

#define R2 64

#define R1 32 // R from 3 to 16383



#define F19_T 128 // test

#define F18_PD 64 // powerdown

#define F17_T 32 // test

#define F16_T 16 // test

#define F15_T 8 // test

#define F14_FLTCV 4 // fast lock timeout counter value

#define F13_FLTCV 2

#define F12_FLTCV 1

#define F11_FLTCV 128

#define F10_TCE 64 // timeout counter enable

#define F9_FLC 32 // fast lock control

#define F8_FLE 16 // fast lock enable

#define F7_CP 8  // charge pump tri-state

#define F6_PDPOL 4 // phase detector polarity, set for dF/dv+

#define F5_FOLD 2 // lock detector output state

#define F4_FOLD 1

#define F3_FOLD 128

#define F2_PD 64 // powerdown

#define F1_CR 32 // counter reset



#define GREEN_ON output_low(PIN_A3)

#define GREEN_OFF output_high(PIN_A3)

#define RED_ON output_low(PIN_A4)

#define RED_OFF output_high(PIN_A4)

#define LOCK_ON output_high(PIN_B3)

#define LOCK_OFF output_low(PIN_b3)






--------------------------- *.C  ---------------------

#include "xlock_1.h"




void

LMX_write_byte (unsigned char data, unsigned char number)

// write the the <number> of <data> to the lmx, msb first

{

    while (number > 0) {

    output_low(LMX_CL);

    delay_us(1);

    output_bit(LMX_DA, (data & 128));

    delay_us(1);

    output_high(LMX_CL);

    delay_us(1);

    output_low(LMX_CL);

    data = data << 1;

    number--;}

}



 void

 LMX_write (unsigned char data1, unsigned char data2, unsigned char data3)

 // write the 21 MSBs

 {

     output_high(LMX_LE);

     delay_us(1);

     output_low(LMX_LE);

     delay_us(1);

    LMX_write_byte(data1, 8);

    LMX_write_byte(data2, 8);

    LMX_write_byte(data3, 5);

    delay_us(1);

    output_high(LMX_LE);

 }



void

sayhi(void)

{

    GREEN_OFF; delay_ms (100);

    GREEN_ON;  delay_ms (100);

}



void

blink (unsigned char number)

{

   if (number == 0) {

   RED_OFF; LOCK_OFF; delay_ms (250);

    RED_ON; LOCK_ON; delay_ms (750);

    RED_OFF; LOCK_OFF; delay_ms (250);

   }

   while (number > 0) {

   RED_OFF; LOCK_OFF; delay_ms (250);

    RED_ON; LOCK_ON; delay_ms (250);

    RED_OFF; LOCK_OFF; delay_ms (250);

    number--;}

}







void

main(void)

{

    unsigned char jump;

   unsigned char rc0;

   unsigned char rc1;

   unsigned char bc0;

   unsigned char bc1;

   unsigned char ac0;

   unsigned char n0;

   unsigned char n1;

   unsigned char n2;

   unsigned char rr0;

   unsigned char rr1;

   unsigned char rr2;

   unsigned char f0;

   unsigned char f1;

   unsigned char f2;



   n0=0; n1=0; n2=0; rr0=0; rr1=0; rr2=0; f0=0; f1=0; f2=0;



   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);

   setup_timer_1(T1_DISABLED);

   setup_timer_2(T2_DISABLED,0,1);

   setup_comparator(NC_NC_NC_NC);

   setup_vref(FALSE);



    sayhi();

    // OK, now everybody nows we are running, let s get going



    // set N R F

    // first, generic data

    // byte order x0 x1 x2

    // bit order is 19 to 1, c2, c1

    // byte boundaries 19-12 11-4 3-1 c2 c1

    n0 = N19_GO; // set charge pump current

    n2 = C1; // for n c1=1, c2=0 - for r both 0

//   f1 = F3_FOLD+F6_PDPOL; // digital phase lock and + df/dv vco

//   f2 = C2; // for f c1=0, c2=1

     f1 = F6_PDPOL; // digital phase lock and + df/dv vco

     f2 = C2 + F3_FOLD; // for f c1=0, c2=1







    // now set A,B and R

    // say 124.5 MHz , R counter = 40 , N counter = 498 (A counter 498-62*8=2, B counter 62)

    // translation to our char values R= RC1*256+RC0, AC=A, B=BC1*256+BC0;

    // other combinations should be easy based on the above, and below

    // 13cm

    // A    B    N    R    IF    QRG        XTAL

    // 0    68    544    45    144    2320    120.88889

    // 4    58    468    40    144    5760    117.00000

    // 2    53    426    40    144    10368    106.50000

    // 2    62    498    40    144    24048    124.50000

    // 4    61    492    40    432    24048    123.00000

    // 1    61    489    40    144    47088    122.25000

    // 5    122    981    80    0    47088    122.62500

    // rc1 = bc1 = 0; rc0 = 45; bc0 = 68; ac0 = 0;

    // rc1 = bc1 = 0; rc0 = 40; bc0 = 58; ac0 = 4;

    // rc1 = bc1 = 0; rc0 = 40; bc0 = 53; ac0 = 2;

    // rc1 = bc1 = 0; rc0 = 40; bc0 = 62; ac0 = 2;

    // rc1 = bc1 = 0; rc0 = 40; bc0 = 61; ac0 = 4;

    // rc1 = bc1 = 0; rc0 = 40; bc0 = 61; ac0 = 1;

    // rc1 = bc1 = 0; rc0 = 80; bc0 = 122; ac0 = 5;



    // read qrg selection jumpers and signal by # blinks

    jump = input(JMP0)*8+input(JMP1)*4+input(JMP2)*2+input(JMP3);

    blink(jump);



    switch (jump)

    {

        case 0: rc1 = bc1 = 0; rc0 = 45; bc0 = 68; ac0 = 0; break;//2320-144

        case 1: rc1 = bc1 = 0; rc0 = 40; bc0 = 58; ac0 = 4; break;//5760-144

        case 2: rc1 = bc1 = 0; rc0 = 40; bc0 = 53; ac0 = 2; break;//10368-144

        case 3: rc1 = bc1 = 0; rc0 = 40; bc0 = 62; ac0 = 2; break;//24048-144

        case 4: rc1 = bc1 = 0; rc0 = 40; bc0 = 61; ac0 = 4; break;//24048-432

        case 5: rc1 = bc1 = 0; rc0 = 40; bc0 = 61; ac0 = 1; break;//47088-144

        case 6: rc1 = bc1 = 0; rc0 = 80; bc0 = 122; ac0 = 5; break;//47088-0

        default: rc1 = bc1 = 0; rc0 = 40; bc0 = 62; ac0 = 2; //24048-144

    }





    // now set our register values

    // A

    n2 += (ac0 << 5);

    n1 += (ac0 >> 3); // this code only for lmx2316/26

    // B

    n1 += (bc0 << 2);

    n0 += (bc0 >> 6);

    n0 += (bc1 << 2);

    // R

    rr2 += (rc0 << 5);

    rr1 += (rc0 >> 3);

    rr1 += (rc1 << 5);

    rr0 += (rc1 >> 3);





    // write N R F

    // to LMX chip

    LMX_write(n0, n1, n2);

    LMX_write(rr0, rr1, rr2);

    LMX_write(f0, f1, f2);



    // I think we are done here, say goodbye

    sayhi();

 //    for (;;) continue;    // done. loop forever

      while (1>0) {sayhi(); if (input(LMX_LD)) {LOCK_OFF;} else {LOCK_ON;}} // test always do something

}