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
}
}
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.
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".
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?
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".