How? PIC calculate/output simultaneously

Status
Not open for further replies.

Thread Starter

richiechen

Joined Jan 1, 2012
93
Hey everyone, I am using a PIC18F.

I want to make the PIC output a 1kHz sinewave thorough a DAC all the time.
The PIC has to receive data from ADC and calculate at the same time.


Can PIC do the two tasks at the same time? Or I have to use two PICs...


Thanks!
 

spinnaker

Joined Oct 29, 2009
7,830
It depends on your meaning of "same time". If you mean true multi tasking the then no the Pic cannot do it.

But there is no reason you can't do what you want if you don't expect the DAC and ADC code to execute simultaneously.

I don't have a of experience yet in PWM but if the PIC has true PWN output then I would think it would output constantly no matter what else you do in your code.
 

Thread Starter

richiechen

Joined Jan 1, 2012
93
It depends on your meaning of "same time". If you mean true multi tasking the then no the Pic cannot do it.

But there is no reason you can't do what you want if you don't expect the DAC and ADC code to execute simultaneously.

I don't have a of experience yet in PWM but if the PIC has true PWN output then I would think it would output constantly no matter what else you do in your code.
Thanks Spinnaker!
You reminds me about the PWM.....It could work and generate the sine wave I want after going through a low-pass filter.

Quite a good idea!
 

MrChips

Joined Oct 2, 2009
30,706
You can create a look-up table that contains a sine-wave and output this to a DAC using interrupts.

At the same time the MCU can process ADC data with or without using interrupts.
 

Thread Starter

richiechen

Joined Jan 1, 2012
93
You can create a look-up table that contains a sine-wave and output this to a DAC using interrupts.

At the same time the MCU can process ADC data with or without using interrupts.
That could be a good idea!

I also need to read data through ADC actually...And one cycle of reading is 4ms(80data), during which the program should not be interrupted.

Can ADC work at the same time when an interrupt is called to export value to DAC?
 

John P

Joined Oct 14, 2008
2,025
What do you mean by saying you plan to drive the DAC all the time?

You should understand that it's a digital output that must change in steps, and so of course your sine wave will actually have little discrete levels, not a smooth curve. So how can it be driven all the time? What you need to specify is how frequently the changes in output occur. Once that's established, you can figure out how much other work the processor can do in between changes.

And you'd be crazy to start an A/D conversion, then have the processor do nothing until the result is available. You start the conversion, then either check a flag periodically to see if it's done, or simply do other things that you know will take enough time for the conversion to complete, then come back and get the result.

Maybe you need to reconsider this, given that the sine wave is 1KHz. How many times can you interrupt the PIC per second, and how many samples do you need to generate for the sine wave? If it's (let's say) 40,000 samples a second, which is a pretty fast rate for the PIC, that's still only 40 DAC levels per cycle, and you won't get a very good sine shape out of that. Is there some way that a PIC can do this, or must you rethink the whole scheme?
 
Last edited:

MrChips

Joined Oct 2, 2009
30,706
I also need to read data through ADC actually...And one cycle of reading is 4ms(80data)
Can you explain what you mean by 4ms(80data)?

during which the program should not be interrupted.
It is the other way around. Interrupt service routines (ISR) should be as short as possible. It is possible to service both DAC and ADC in under 1us on a reasonably fast MCU. The ISR takes priority and will never be interrupted.

The beauty with interrupts is your MCU can be doing intensive data processing and called to handle interrupts on demand.

What you are attempting to achieve is standard programming practice and should not be a problem.

You have to outline fully what you are attempting to do.
 

MrChips

Joined Oct 2, 2009
30,706
And you'd be crazy to start an A/D conversion, then have the processor do nothing until the result is available. You start the conversion, then either check a flag periodically to see if it's done, or simply do other things that you know will take enough time for the conversion to complete, then come back and get the result.
There is no need to check a flag periodically. If you know the ADC conversion time you can read the ADC in the same ISR as the DAC is being serviced.

Maybe you need to reconsider this, given that the sine wave is 1KHz. How many times can you interrupt the PIC per second, and how many samples do you need to generate for the sine wave? If it's (let's say) 40,000 samples a second, which is a pretty fast rate for the PIC, that's still only 40 DAC levels per cycle, and you won't get a very good sine shape out of that. Is there some way that a PIC can do this, or must you rethink the whole scheme?
40 samples per cycle is not bad at all. Output rate of 40,000 samples/second translates to an interrupt every 25us. If the ISR spends only 2us that leaves 90% of the cycle for other data processing. (I am not familiar with the processing speeds of the particular PIC chosen.)
 

John P

Joined Oct 14, 2008
2,025
40 samples per cycle might or might not be enough, because he hasn't said what he needs. It's a great tradition here to ask for help while leaving out the most important information! At least the PIC18 types don't require a routine to save/restore the most important registers, which does help keep the interrupts fast.

I'm a strong believer in having just one interrupt source active, where that one is a fast repetitive clock. Everything else can be counted down from that, or handled via flags being monitored. Doing it that way means that you never need to worry about which interrupt is being delayed by another, so you know that your clock rate is what you set it for. Also you don't have to sort out which one occurred during the precious interrupt period.
 

ErnieM

Joined Apr 24, 2011
8,377
40 samples per cycle might or might not be enough, because he hasn't said what he needs. It's a great tradition here to ask for help while leaving out the most important information!
Agreed.

Since the only output mentioned so far is a 1KHz sine wave just how much information is the OP intending to stuff onto this?

And how the information may change this output is another point to ponder.
 

MrChips

Joined Oct 2, 2009
30,706
I'm a strong believer in having just one interrupt source active, where that one is a fast repetitive clock. Everything else can be counted down from that, or handled via flags being monitored. Doing it that way means that you never need to worry about which interrupt is being delayed by another, so you know that your clock rate is what you set it for. Also you don't have to sort out which one occurred during the precious interrupt period.
I agree too.

The intent was to have a single 40kHz timer interrupt (or something similar). 25us is lots of time to service an interrupt, read an ADC and output to a DAC. Even if that takes 10us, you still have 15us to perform other tasks.

The op has to tell us what he is doing. Isn't it a mystery why folks come here looking for answers but they want to keep things secret?
 

joeyd999

Joined Jun 6, 2011
5,234
I'm a strong believer in having just one interrupt source active, where that one is a fast repetitive clock. Everything else can be counted down from that, or handled via flags being monitored. Doing it that way means that you never need to worry about which interrupt is being delayed by another, so you know that your clock rate is what you set it for. Also you don't have to sort out which one occurred during the precious interrupt period.
Then you are seriously limiting what you can do in any one program.

I always reserve Timer 0 for a 'system clock' that sets the base timing for the code, and usually starts the A/D converter, if I use one.

The A/D interrupt then captures converted data.

If I have an LCD display (direct drive), then I will use the LCD int to update the display.

If I have an external A/D converter, then I will use an external int to capture that data.

If I am using the EUSART, the I will use that interrupt to send/receive data.

If I am using PWM, I will use that interrupt for updating the PWM output.

Etc, etc...

Interrupt latency is not all that big of a deal if you plan ahead and know what it is going to be. Interrupts can be arranged so the the most timing critical ones get serviced more promptly, and/or have higher priority. Much of the hardware is double buffered so that a bit of latency won't change the overall timing.

In my designs, everything in integrated into the CPU as much as possible. This means doing lots apparently simultaneously. Good timing and interrupt planning helps tremendously towards this end.
 

MrChips

Joined Oct 2, 2009
30,706
What you say is also true. A lot depends on what other tasks need to be done.
We have to wait for the op to respond. HK is GMT+8.
 

THE_RB

Joined Feb 11, 2008
5,438
Can you explain what you mean by 4ms(80data)?
...
I would guess that means a cycle over 40mS, that reads 80 samples?

Which would be 2 samples a mS, ie 2 ADC samples per 1kHz "sine".

And like everyone else I can only guess why he needs to generate a 1kHz sine and take 2 ADC samples during it... :)

Maybe it is to measure the response of a coil (like a guitar pickup) to a fixed freq?
 

Thread Starter

richiechen

Joined Jan 1, 2012
93
Hi everyone

Before I start to read every post,I have to say thanks and you guys touched me much.
It is so much fun to discuss problems with friends I have never met in face. And every person discusses seriously with interest.

Thanks for your kindly help. I will try to help other people when I could.


Richie
 

Thread Starter

richiechen

Joined Jan 1, 2012
93
Well I have read every post and learned much. :)

The ADC is reading data for 4ms per cycle, which are four complete periods of 1kHz sine wave. Each period of sine wave I plan to capture 20data. I need to capture data continuously during one cycle, which ensures that the captured data could present the sine wave correctly.

About the DAC, I plan to use 20data/period of the 1kHz sine wave, then the DAC will work on 20kHz.

Yes, both input and output of the system is 1kHz sinewave. I am trying to build a system analyzer. The system will be given 1kHz input and MCU will try to analyze the output of the system, which is also 1kHz sine wave, however, distorted maybe.


John is right, I do not need to control DAC "continuously".

Richie
 

MrChips

Joined Oct 2, 2009
30,706
Still not clear what you are trying to do.
What is the purpose of the "system analyzer"?
What is the purpose of the DAC?

Why do you need four cycles?

If you are sampling 20 points in 1ms, then your sampling rate is 20kHz or a period of 50us.
The most important thing is to take a reading at exactly 50us apart. Any error in this period is going to introduce errors. You need to set up a timer with a period of 50us to trigger the ADC. The total number of samples you take depends on the resolution you wish to achieve.
 

John P

Joined Oct 14, 2008
2,025
Reading the A/D and setting the DAC each at a 20KHz rate doesn't seem very difficult, and you can do it with a single interrupt. The easy way to do it is read the A/D, store the data, then start the A/D for its next conversion. Then output the figure you want to send to the DAC, and the program can exit the interrupt.

However, if you actually want to vary the frequency of one of those actions slightly, you have a major problem. Sometimes they will coincide, and sometimes they won't.

Joeyd, you and I don't agree, but this isn't the place to argue the issue.
 

joeyd999

Joined Jun 6, 2011
5,234
Joeyd, you and I don't agree, but this isn't the place to argue the issue.
There is nothing to agree or disagree about, and no point to argue. Either you are limiting the capability of you designs, or your designs are not involved/intricate/complex enough to fully exercise the silicon you choose to use.

And this is the perfect place to discuss it. The OP wanted to know if two things can be done on a PIC simultaneously. The correct answer is yes. In fact, generally quite a few more than two things.
 
Status
Not open for further replies.
Top