Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18F27Q43 NCO + DAC To Create Variable-Amplitude Square Wave?

For The Popcorn

Well-Known Member
Most Helpful Member
I'm starting to wrap my head around the new features of the PIC18F27Q43 - this chip has amazing features for a couple bucks. There's a lot to learn here.

I want to use the NCO to generate a square wave of up to around 5kHz to trigger the DAC to give an adjustable amplitude and frequency square wave. I had the idea that this would be possible using just features of micro, but I'm not seeing how to do this.

I suppose one way to accomplish this is to use an analog switch triggered by the NCO to switch the DAC output.

The goal is an adjustable amplitude and frequency tone to drive a speaker as a replacement for a piezo beeper. I've found that an integrated square wave (low pass filter + gain) yields a pleasant tone.

Thanks for any suggestions.
 
I am not familiar with that PIC family ... The ones I have been using are the PIC16F15xxx which have NCO and DAC as well as CLC blocks.

The reason I mention CLC blocks (Configurable Logic Cells) is that you may need to use one to bridge the NCO output with the DAC output.

I.e. looking at the datasheet for the PIC18F27Q43 ... (Reference Table 3-1 in datasheet)
1) You would generate the NCO at the desired frequency as an input to the CLC block to serve as a "gate".
2) The DAC1OUT2 (RB7) would serve as an input to the CLC3 or CLC7
3) The Output of the CLC could be directed to an IO pin which in theory would show the NCO frequency at the DAC level.


Another possibility is to tie a resistor 1k or so going from the DAC output to the NCO output and configure the NCO output as an open drain ... See ODCONx.

Your output will be on the NCO output. When the NCO is LOW the output will be low, but when the NCO is HIGH, the DAC level will be present.
 
Ahhh. Good ideas. Thank you.

I may be able to use the NCO output to toggle the enable bit of the DAC output.
 
2) The DAC1OUT2 (RB7) would serve as an input to the CLC3 or CLC7
The DAC output is an analog voltage, and the CLC is a digital peripheral. There's no connection from the DACOUT to a CLC input.

Another possibility is to tie a resistor 1k or so going from the DAC output to the NCO output and configure the NCO output as an open drain ... See ODCONx.

Your output will be on the NCO output. When the NCO is LOW the output will be low, but when the NCO is HIGH, the DAC level will be present.
Now that might work. The DAC has a pretty high output impedance though (each of the R steps is ~20K), so it's not going to drive much.


I may be able to use the NCO output to toggle the enable bit of the DAC output.
Not easily. You'd have to use the NCO to generate interrupts and do it in code.
 
So it sounds like two options that won't depend on code are:

● Open drain NCO + DAC
Buffering the DAC with a voltage follower would probably be a good idea.

● NCO toggling an analog switch switching the DAC output. A voltage follower on the DAC output still probably a good idea.


The LM4861 amp chip I'm planning on using has an ENABLE input, so the NCO can probably be left running continuously, and the amp enable used to control the "beep".

Thanks for the comments.
 
I would think the Open drain NCO + DAC combo would give you about the same result as toggling an analog switch... either way you're modulating the DAC output with the NCO signal. You could probably even get rid of the 1K resistor since you'd always have at least 20K on the output of the DAC so you'd never really be shorting the output directly.

Worth a shot.

You probably are going to need a voltage buffer on the DAC output. Shame the Q43 doesn't have any opamps like the Q41/Q71 have, but it's a nice chip otherwise.
 
Last edited:
Thank you Sir. I'll rig up a test.
 
You could loop the NCO output back to the DAC positive reference input?
That should in effect turn whatever signal the DAC is producing on or off.

(I'm not sure if pin select will allow both to be on the same pin, or you would need an external connection?)
 
Hey, now that's an interesting idea that I think might work!
You can't map two outputs to the same pin, but usually inputs don't cause a problem.

On the 28-pin 27Q43, the DAC VREF+ input is on pin RA3, and the NCO1 output can be mapped via PPS to PORTA
(NCO2 and NCO3 can't be mapped to PORTA)

DAC1OUT1 is on RA2.
 
Hmmm. So the reference voltage is either ~5V or 0V, and scaled down from there. Interesting idea.

I can almost hear the DAC saying "make up your mind!"
 
You can't map two outputs to the same pin, but usually inputs don't cause a problem.

The worst case is just to connect the two port pins together if routing internally doesn't work. This sounds workable!
 
The dataSHEET is over 900 pages! There are definitely a few things to learn. If you haven't seen Microchip's HTML datasheets, take a look. They are so much nicer than scrolling up and down through a PDF.
 
For future this is pretty easy to do on a PSOC (sine or arbitrary waveform) :

1708349176832.png


This was a 4 channel requirement, done on 1 chip, with lots of other resources left
for other tasks.... Amplitude done under software control of WaveDAC.

Easy to do a bursted tone as well, example (again one chip) :



Regards, Dana.
 
Last edited:
you could loop the NCO output back to the DAC positive reference input?
So, I just tried this with an 18F27Q43, using PPS to map the NCO1 output into the DAC1 VREF+ and it works for low freq signals just fine. When the NCO freq is around 50KHz or so, things begin to distort and you get a DC offset shift.
 
When the NCO freq is around 50KHz or so, things begin to distort and you get a DC offset shift.

Excellent, thank you for giving it a try! 50kHz is 10x what I need, so this looks like a winner.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top