PIC10F322 Applications – Basic Astable Using NCO (Numerically Controlled Oscillator)

The code (basic_astable_nco.c) and headers can be found in my GitHub here – https://github.com/JamieStarling/MCU_FUN/tree/master/Microchip/PIC10F322/

The last post I mentioned about the NCO – (Numerically Controlled Oscillator). In short without too much hair loss you can get a astable output on PORTA.2 up to 500Kz. This frequency is also selectable. 

Here is the output at 500Khz, 

NCO 500Khz

So, how do we change it.. Well in the code below – there is a line..

setupNCO1(NCO_CLOCK_SOURCE_FOSC ,0xFFFF,NCO_FREQUNCY_MODE_FIXED);

The – 0XFFFF sets the Increment value of the adder.  With every pulse that comes into the NCO that value is added to the accumulator – which is 20bits in size. After two pulses the accumulator overflows – triggering the change on the output pin. 

So, how do we figure out other values – Here is the math  {Desired Freq}x 2 x 2^20 / {CPU Frequency}

Lets say, we want. 330Khz:

330,000 x 2 = 660,000 x 1048576 = 692,060,160,000 / 16000000 = 43,253.76 – Round it to 43,254 or (0xA8F6)

We replace – 0xFFFF with 0xA8F6 – to get… very very close to 330Khz.  There is some jitter but that is expected. 

NCO 333Khz

Anyway – the code is below – or you can find it all in my Github page. 

/*
 * File:   astable_nco.c
 * Author: Jamie
 *
 * Created on August 15, 2020, 10:39 AM
 */


#include "includes/10F322_deviceconfig.h"
#include "includes/nco.h"
#include "includes/gpio.h"
#include "includes/osc.h"

void main(void)
{
    cpuFreqSelect(MHZ16);
    pinMode(2,OUTPUT);    
    setupNCO1(NCO_CLOCK_SOURCE_FOSC ,0xFFFF,NCO_FREQUNCY_MODE_FIXED); //500Khz Output
    
    

    while(1){}
   
}