Using PWM interrupt to enable and disable DAC port

Hi all,

I am using an Arduino DUE in order to generate 100kHz PWM signal through DAC1.
I am trying to figure how I can use an external 400Hz PWM as interrupt for enable/disable the 100kHz PWM in DAC1.

here is my code so far.

Thanks in advanced,

Matan

double channel;
void setup() {
    Serial.begin(9600); // Serial for debug
    pinMode(2, INPUT);
}

void loop() {
    channel = pulseIn(2, HIGH);
    Serial.println(channel);
    if (channel>2000)
    {
      REG_PMC_PCER1 |= PMC_PCER1_PID36;                     // Enable PWM 
      REG_PIOB_ABSR |= PIO_ABSR_P16;                        // Set PWM pin perhipheral type A or B, in this case B
      REG_PIOB_PDR |= PIO_PDR_P16;                          // Set PWM pin to an output
      REG_PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1);      // Set the PWM clock rate to 84MHz (84MHz/1) 
      REG_PWM_CMR0 = PWM_CMR_CPRE_CLKA;                     // Enable single slope PWM and set the clock source as CLKA
      REG_PWM_CPRD0 = 840;                                  // Set the PWM frequency 84MHz/40kHz = 2100 
      REG_PWM_CDTY0 = 420;                                  // Set the PWM duty cycle 50% (2100/2=1050)
      REG_PWM_ENA = PWM_ENA_CHID0;                          // Enable the PWM channel     
    }
    else 
    {
        DACC->DACC_CHDR = DACC_CHDR_CH0 | DACC_CHDR_CH1; // Disable DACs 0 and 1
    }
}

Why ?

2 Likes

Hi ard_newbie,

I need a 100kHz PWM signal for my project. There is any easier or better way to generate this 100kHz PWM signal?

Thanks again,

Matan

Just throwing out thoughts.

So you want to make a 2 input AND gate.

So one pin in the MCU generates the 400Hz PWM, another pin reads the 400Hz PWM.

Another thing generates the 100kHz PWM.

Another thing, the AND gate, some code, only enables 100Khz as an output when the 400Hz PWM is HIGH.

1 Like

Hi idahowalker,

I think I misexpanied myself.. The 400Hz signal generted by an external device. Actually its an ESC signal from other controller.

My system looks has 3 given components (1, 2 and 4) and a link controller (3) Arsuino Due:

(1) remote control with few push buttons.

(2) External controller with 5 output ports, each generate 400Hz PWM at length that depends on how long the user push the button.

(3) Arduino Due. It task to use this push buttons at (1) that generate 400Hz PWM signals at (2) to operate the device described at (4). It is a link between the given components.

(4) A device that need 100kHz pwm signal and other 4 TTL signal in order to operate.

Hope I explained better the system,

Thanks again,

Matan

Reply #2 seems to have been overlooked. You are talking about digital signals. What has a DAC to do with this? A DAC produces analog signals.

1 Like

Hi aarg,

I find a code in the forum that use the Due's DAC to produced such high frequency PWM. After small adjustment, I figured how to generate 100kHz PWM.

Do you know any better way to do so?

Thanks again,

Matan

Is is a secret code? What small adjustment? Is your project working now?

1 Like

Hi aarg,

The code is attached in the original post. The only adjustment was because the example generate 40kHz PWM. I just change this 2 lines

The current code works but don't stops the PWM once the 400Hz signal arrives at the 1st time.
I wonder if there is a better way to generate a 100kHz signal?

Thanks again,
Matan

REG_PWM_CPRD0 = 840;                                  // Set the PWM frequency 84MHz/40kHz = 2100 
 REG_PWM_CDTY0 = 420;                                  // Set the PWM duty cycle 50% (2100/2=1050)

I'm repeatedly asking what any sane engineer would ask about this project, why is a DAC used to "produce PWM"? It's highly irregular because 100% of all PWM is digital. The "P" in "PWM" stands for "pulse" which is always digital in that context. I'm not saying it's wrong, however it definitely needs an answer.

Also your replies about what it's used for, go in circles. We're told that the answer to what the device is, is in "#4". But all that is there is another reference to "a device".

I understand,

I just copy an example from this forum and they use the DAC port...
I can handle this 100kHz PWM with regular digital output.

The end device (4) is laser module.

Thanks again,

Matan

Well, the coding for digital output is much simpler and has fewer limitations and more processor support for high speeds, than for analog. So I think you need to know why it was done that way originally - a good reason, or just someone flailing around until something worked...

What is the source of this code? Where did it come from? Which forum is "this forum"? It might help if there were some context or explanations of what is going on. Can you supply a link to the discussion or place where the code was hosted?

1 Like

Hi aarg,

Here is the source code link

Matan

That thread begins with a very confused post, which eventually gets sorted out. Read down a bit, you'll see that my intuition is correct. The assumption that a D/A should be used was founded on a good deal of confusion and ignorance of basic microprocessor operation. Then they start talking about timer driven PWM which is really what you need.

If you look back, your very first response highlighted that issue clearly (post #2). You chose to ignore it and here we are...

By the way, many processors implement "gated PWM" as outlined in reply #4, directly in hardware. So that is something you might want to investigate.

I would begin by Googling, "Arduino Due PWM". Also download the Due processor data sheet, and look closely at the Arduino analogWrite() API for the Due, in case that can be made to work "out of the box".

1 Like

Will do,

Thanks again

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.