• AVR Freaks

Helpful ReplyHot!Creating 2 independent square waves using timers?

Page: 1234 > Showing page 1 of 4
Author
didierleplae
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
2020/02/16 14:04:17 (permalink)
0

Creating 2 independent square waves using timers?

I am trying to write code for PIC16F18313 in which two pins would output two independent square waves (LO_FREQ and HI_FREQ). I need the ratio of the two waves to remain at around 1:5.4 but also need the ability to adjust the frequency of both waves with one pot. 
I also need a 3rd pin to output the XOR result of the two waves combined together.
I figured I would need to use timers to create the two independent waves. At the moment I am using TMR0 for the LO_FREQ and TMR2 for the HI_FREQ.
To start with I am able to get the LO_FREQ to work but not getting anything out of the HI_FREQ pin.
 
Here is the code in my TMR0 ISR:

void TMR0_ISR(void)
{
// clear the TMR0 interrupt flag
PIR0bits.TMR0IF = 0;
if(TMR0_InterruptHandler)
{
TMR0_InterruptHandler();
}
// add your TMR0 interrupt custom code

TMR0Count++;
if (TMR0Count == 4){
LO_FREQ_Toggle();
TMR0Count = 0;
if (LO_FREQ_LAT == 1 && HI_FREQ_LAT == 0){
XOR_OUT_LAT = 1;
}
else if (LO_FREQ_LAT == 0 && HI_FREQ_LAT == 1){
XOR_OUT_LAT = 1;
}
else {XOR_OUT_LAT = 0;}
}
}

 
And my TMR2 ISR:

void TMR2_ISR(void)
{
// clear the TMR2 interrupt flag
PIR1bits.TMR2IF = 0;
if(TMR2_InterruptHandler)
{
TMR2_InterruptHandler();
}

TMR2Count++;
if (TMR2Count == 3){
HI_FREQ_Toggle();
TMR2Count = 0;
}

}

 
Also my main.c, although there isn't much going on here except enabling interrupts:

#include "mcc_generated_files/mcc.h"

/*
Main application
*/
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
while (1)
{
//if (LO_FREQ_PORT == 1 && HI_FREQ_PORT == 0){
// XOR_OUT_LAT = 1;
//}
//else if (LO_FREQ_PORT == 0 && HI_FREQ_PORT == 1){
// XOR_OUT_LAT = 1;
//}
//else {XOR_OUT_LAT = 0;}





}
}
/**
End of File
*/

#1
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11824
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/16 14:09:17 (permalink)
+1 (1)
I would think two PWMs and a CLC could do all of this with very little code and no interrupts.
#2
didierleplae
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/16 14:11:28 (permalink)
0
I should add that the centered frequencies should be @ (LO_FREQ = 731Hz and HI_FREQ = 3946Hz) and ideally the pot would have a range of about 2.5 octaves in either direction.
#3
didierleplae
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/16 14:13:11 (permalink)
0
With PWMs wouldn’t the duty cycle change or how would this work?
I don’t even know what the CLC is
#4
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11824
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/16 14:14:47 (permalink)
+1 (1)
The PWMs will be at whatever duty cycle you want.  They don't autonomously change duty cycle.  The CLC is documented in your data sheet.
#5
didierleplae
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/16 23:26:42 (permalink)
0
OK. So, I get that the PWM could be used to create a square wave at 50% duty cycle. I also see that the CLC could be used to create the XOR gate. However all of the PIC16F18313 PWMs, as far as I can tell, have their frequency determined by the same TMR2. So, how can I have two independent frequencies created by 2 PWMs? Also, how could the frequency be controlled with a pot?
#6
ric
Super Member
  • Total Posts : 27071
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/17 05:58:53 (permalink)
0
Yes, you have two CCP modules, both clocked by TMR2, and two dedicated PWM modules, also clocked by TMR2.
Most recent PICs have more even numbered timers (e.g. TMR4, TMR6) any of which can be used by those modules. Unfortunately you seem to have a very limited PIC which can only use TMR2.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
Howard Long
Super Member
  • Total Posts : 796
  • Reward points : 0
  • Joined: 2005/04/04 08:50:32
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/17 06:48:26 (permalink)
+1 (1)
This device has an independent NCO too.
 
FWIW, if CLCs are lacking, you can also use the DSM to XOR two signals.
 
There are a couple of key unknowns in the story here: how accurate do the frequencies need to be (in step terms), and how much jitter is allowed?
 
These thread possibly of interest depending on the OP's application:
 
https://www.microchip.com/forums/tm.aspx?m=1092339
https://www.microchip.com/forums/tm.aspx?m=1081581
#8
NorthGuy
Super Member
  • Total Posts : 6099
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/17 08:06:40 (permalink)
+1 (1)
didierleplae
However all of the PIC16F18313 PWMs, as far as I can tell, have their frequency determined by the same TMR2. So, how can I have two independent frequencies created by 2 PWMs?



Compare mode uses TMR1. PWM mode uses TMR2. Either can be used to generate a square wave.
 
didierleplae
Also, how could the frequency be controlled with a pot?

 
You read the pot with ADC and configure the registers accordingly.
 
You also can use a rotary encoder instead of the pot.
#9
didierleplae
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/17 11:56:05 (permalink)
0
Howard Long
There are a couple of key unknowns in the story here: how accurate do the frequencies need to be (in step terms), and how much jitter is allowed? 

As far as accuracy in steps, ideally the pot would feel smooth, not perceptible steps. But I’ll take what I can get,
I’m not sure what to say about jitter. I am using these square waves for audio, as the basis for a percussion sound. So hopefully it would be audible jitter. That said, the sound would have a short decay so not sure if ut would be perceptible
#10
didierleplae
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/17 11:57:23 (permalink)
0
Out of curiosity, is there a reason the original way I was doing it with timers, doesn’t work?
I’m just interested to understand, because I thought that should work.
#11
pcbbc
Super Member
  • Total Posts : 1691
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 00:57:46 (permalink)
+2 (2)
We can’t say without seeing the rest of the code MCC has generated behind your back. Or the code in HI_FREQ_Toggle. Nothing in the code you have posted as far as I can see.
 
When you can’t find the problem yourself it’s usually not where you are looking. Therefore you are not qualified to decide which bits of code to show us and which bits are “unimportant“ to the problem.
 
MCC is a very quick way to get started and produce a sample project, it’s also a very quick way to put all the code we (and you for that matter) need to see to diagnose a problem in a multitude of disparate places such that it is impossible to get a complete picture of what it has done.
 
Could be anything from the timer not firing to the pin not being defined as output. What steps have you taken to diagnose these? Stepping through in debugger or simulator would be the obvious start.
 
One things for certain in the code you have posted, you are missing the XOR logic in the high frequency interrupt. Although that does not give the bug you reported of no high frequency output at all.

Plus this would seem a lot easier...
XOR_OUT_LAT = LO_FREQ_LAT ^ HI_FREQ_LAT;

Or perhaps...
if (LO_FREQ_LAT ^ HI_FREQ_LAT) XOR_OUT_LAT = 1; else XOR_OUT_LAT = 0;

Or even...
if (LO_FREQ_LAT == HI_FREQ_LAT) XOR_OUT_LAT = 0; else XOR_OUT_LAT = 1;

I expect they will all produce less code as well.
#12
didierleplae
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 01:08:51 (permalink)
0
pcbbc
MCC is a very quick way to get started and produce a sample project, it’s also a very quick way to put all the code we (and you for that matter) need to see to diagnose a problem in a multitude of disparate places such that it is impossible to get a complete picture of what it has done.

Yes, I agree... I guess that is what prevents me from uploading my whole project for all to see. There are about 9 .c files and 8 .h files. I posted the ones I thought were relevant. Is there a way to simply post the whole project here?
 
 
pcbbc
Plus this would seem a lot easier...
XOR_OUT_LAT = LO_FREQ_LAT ^ HI_FREQ_LAT;

 

Totally agree with this. 
#13
Howard Long
Super Member
  • Total Posts : 796
  • Reward points : 0
  • Joined: 2005/04/04 08:50:32
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 07:37:38 (permalink)
+2 (2)
I'm afraid I cann't agree regarding MCC, it causes far more problems than it fixes, particularly in the medium to long term. In the short term, you're in the lap of the gods as to whether it creates bug free code or not.
 
If the OP needs imperceptible step sizes, then and jitter is less of a problem, I'd suggest that NCO is probably the way to go, either in software or hardware: with the OP's chip, at least one would need to be in software as there's only on on chip hardware NCO. This needn't be a problem. Running the chip at 8MIPs, you should easily be able to run two software NCOs at a sufficient sample rate for audio. I'd use a single hardware timer, and update both NCOs in the timer driven ISR, as well as setting the XOR bit at the beginning of the ISR, precomputed from the the previous interrupt to minimise jitter. The ADC can be read in the super loop, and update the NCO increment value there.
post edited by Howard Long - 2020/02/18 07:40:56
#14
mpgmike
Super Member
  • Total Posts : 421
  • Reward points : 0
  • Joined: 2014/01/23 17:27:06
  • Location: NJ
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 07:46:05 (permalink) ☄ Helpfulby diegocw 2020/02/22 08:58:27
+1 (1)
I know you're wanting to use the 8-pin PIC16F18313, but the new Q43 has 3 NCO modules, along with 8 CLC, DSM, (from memory) 8 PWM/CCP, and 8 Timers.  Since you're wanting decay, you could use 1 NCO for HIGH, a second NCO for LOW, pipe them into a CLC along with a high-frequency PWM.  Use the PWM DC% for "volume", where you start at 50% and "decay" down to 0%.  Q43 availability is spotty.  Digi-Key has 5 PIC18F47Q43 in inventory.  I tried to order a sampling of PIC18F25Q43 from Microchip Direct and was told shipment wouldn't be for awhile.  I'm playing with a 47Q43 right now using all 3 NCO modules.  I LIKE IT!!

I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
#15
pcbbc
Super Member
  • Total Posts : 1691
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 07:56:52 (permalink)
+2 (2)
Howard LongI'm afraid I can't agree regarding MCC, it causes far more problems than it fixes, particularly in the medium to long term. In the short term, you're in the lap of the gods as to whether it creates bug free code or not.

Actually I think we agree on that.  I should have thought more before typing "MCC is..." and written "MCC seems like..." instead.
 
It's little more that a poor short cut for reading and understanding the datasheet IMO.  Sooner or later you've going to have to do that anyway, so might as well start now and bypass the intermediate step of buggy spaghetti production.
 
This project is a screen-ful of C at most.  The perfect place to start.
Meanwhile you could probably make a sizeable pamphlet out of the code MCC produced.
#16
Mysil
Super Member
  • Total Posts : 3670
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 08:19:13 (permalink)
+1 (1)
Hi,
About question in message #13 about how to post a project:
In MPLAB X there is a Package facility:
In the 'Projects' panel, upper left part of MPLAB window,
point to root of project, the small IC package icon, and Right-click.
This should open a long Pop-Up menu, click 'Package'
It should produce a .zip file, containing all files needed to restore and debug your project.
For this to work, project must be possible to build, but doesn't have to function as you intend.
The .zip file may be attached to a message, use 'Open Full Version' in message editor.
 
    Mysil
#17
1and0
Access is Denied
  • Total Posts : 10783
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 08:31:17 (permalink)
+1 (1)
didierleplae
I am using these square waves for audio, as the basis for a percussion sound. So hopefully it would be audible jitter. That said, the sound would have a short decay so not sure if ut would be perceptible

Square wave audio is harsh on the ears. ;)
 
#18
mbrowning
USNA79
  • Total Posts : 1746
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 08:35:00 (permalink)
+1 (1)
Digi-Key has 5 PIC18F47Q43 in inventory.

Note that these are engineering samples, not production parts (last I looked).
Digikey said 5 parts in stock. I ordered 5. The day they shipped I looked again and Digikey said 5 in stock. I think they have as many as you want, as long as you want no more than 5 at a time. The next person to order can get 5.
#19
pcbbc
Super Member
  • Total Posts : 1691
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Creating 2 independent square waves using timers? 2020/02/18 08:35:10 (permalink)
+2 (2)
1and0Square wave audio is harsh on the ears. ;)

Some would say that's a feature of percussion also...
#20
Page: 1234 > Showing page 1 of 4
Jump to:
© 2020 APG vNext Commercial Version 4.5