pic square wave dds generator

Status
Not open for further replies.

hugo

New Member
This small proj. might be useful for some people...
Basically it's a square wave dds generator 1-255 Hz with a pic micro,a crystal & caps, 8 pos dip switch.
The desired output frequency is determined by the switches ( binary like).
My inspiration came from mr. RB site :
https://www.romanblack.com/onesec/High_Acc_Timing.htm#decfreq
The source code mikrocpro v5.8.0 compiler :

Code:
// square wave dds generator  1-255 Hz
// 16F628 @ 16 MHz
// mikroc pro v5.8.0
// 8 pos DIP switch connected to PORTB
// -----------------------------------------------------------
// |ON  RB7  RB6  RB5  RB4   RB3   RB2  RB1  RB0  |
// |     |SW1|SW2|SW3|SW4|SW5|SW6|SW7|SW8|
// |      GND  GND  GND GND  GND  GND  GND GND |
// ------------------------------------------------------------
// desired output frequency is determined by the switches ( binary like)
// 1Hz -> 00000001 |SW1-7 OFF, SW8 ON|
// 7Hz -> 00000111 |SW1-5 OFF, SW6 ON, SW7 ON, SW8 ON|
// 255Hz -> 11111111 |SW1-8 ON

#define HZ 16000000   // 16MHz HS xtal = 16000000 
#define TOGGLE (HZ * 125)

// RAM vars

unsigned char sw_old,sw_new ;

volatile unsigned long accum;
volatile unsigned long freqHz;

//-----------------------------------------------------------------------------
void interrupt(void)
{

  PIR1.TMR2IF = 0;      // clear TMR2 roll flag
  accum += freqHz;      // decimal DDS addition into accumulator
  if(accum >= TOGGLE)
  {
    PORTA = ~ PORTA ;      // toggle output port to make frequency
    accum -= TOGGLE;    // decimal bresenham subtraction, keep remainder
  }
}
//-----------------------------------------------------------------------------

void main(void)
{

  // setup PIC 16F628
  CMCON = 0x07;   // comparators OFF

  PORTA = 0b00000000;   // porta is freq output
  TRISA = 0b00000000;

  TRISB = 0b11111111;     // PORTB all inputs
  
  OPTION_REG = 0 ;        // weak pull-ups on

  // setup TMR2 to roll every 200 ticks
  T2CON = 0b00000100;   // TMR2 ON, 1:1 prescale
  PR2 = (100-1);        // TMR2 int occurs every 100 ticks

  INTCON = 0b11000000;  // GIE = ON, PEIE = ON
  PIE1.TMR2IE = 0;      // TMR2 interrupt OFF

  sw_old = ~PORTB ;     // read switches
  freqHz = ( unsigned long ) sw_old * 100000 ;  // set the desired fq
  accum = 0;             // clear var
  PORTA = 0;
  PIE1.TMR2IE = 1;       // TMR2 interrupt ON


 for(;;)
  {
      sw_new = ~PORTB ;          // read switches
      
      if(sw_new != sw_old )      // new fq ?
       {
        PIE1.TMR2IE = 0;      // TMR2 interrupt OFF
        freqHz = ( unsigned long ) sw_new * 100000 ;  // set the desired fq
        sw_old = ~PORTB ;      // save the status of the switches
        accum = 0;             // clear var
        PORTA = 0;             //
        PIE1.TMR2IE = 1;       // TMR2 interrupt ON
       }
  }
}
 
I am looking at DDS. It looks like I can make a digital PLL using a little PIC.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…