Go Down

Topic: To generate 2MHz frequency from aurdino due  (Read 517 times) previous topic - next topic


hello everyone

I am getting errors while compiling,
please do help me out with it


Code: [Select]
const int freqOutputPin =3;
const int prescale  = 1;
const int ocr2bval  = 3;

const float period    = 2.0 * prescale * (ocr2bval+1) / (F_CPU/1.0e6);

const float freq      = 1.0e6 / period;

void setup()
    pinMode(freqOutputPin, OUTPUT);

    TCCR2A = ((1 << WGM21) | (1 << COM2A0));

    TCCR2B = (1 << CS20);  // No prescale

    TIMSK2 = 0;

    OCR2B = ocr2bval;

    Serial.print("Period    = ");
    Serial.println(" microseconds");
    Serial.print("Frequency = ");
    Serial.println(" Hz");

void loop()


You seem to have code that was written for an AVR micro like an UNO or a Mega and want it to run as is on a SAM board like a Due.  Ain't gonna happen.  You need to scrap this whole code and look for something specific to the Due.
|| | ||| | || | ||  ~Woodstock


yea i know
but i am very new to Arudino due

so can you help me out how to move on with it?


but i am very new to Arudino due
How does that make you incapable of using a basic search engine?  Google "generate square wave from Due" or something like that.  I'm positive that this has been solved several times over.  Come back when you have some specific question that is better than, " I've got nothing, I've done nothing, you explain the whole entire thing to me here." 
|| | ||| | || | ||  ~Woodstock


yea i understand

but can you tell me the code how to write
i ain't getting it
thank you


No.  This isn't a free code writing service.  This is where you get help fixing the code you write.  If you want someone to write it for you then there are folks that will do that for hire in the Gigs and Collaborations section. Just be very very specific about the requirements and how much you are willing to pay. 
|| | ||| | || | ||  ~Woodstock


Nov 06, 2017, 06:55 pm Last Edit: Nov 06, 2017, 07:00 pm by MartinL
Hi mark07,

The following code uses the Due's PWM controller to output a 2MHz square wave on pin DAC1:

Code: [Select]
// Output a 2MHz PWM waveform at a resolution of 5-bits on pin DAC1 (PWML0)
void setup() {
  // PWM Set-up on pin: DAC1
  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 = 42;                                   // Set the PWM frequency 84MHz/2MHz = 42
  REG_PWM_CDTY0 = 21;                                   // Set the PWM duty cycle 50%
  REG_PWM_ENA = PWM_ENA_CHID0;                          // Enable the PWM channel

void loop() {}

If you need to alter the duty cycle to a value other than 50% during operation then change the REG_PWM_CDTYUPD0 (duty cycle update register for channel 0) to a value between 0 and 42, (the value in the period register (REG_PWM_CPRD0).

The PWM controller has 8 channels, so it's also possible assign other pins if necessary, see https://forum.arduino.cc/index.php?topic=394558.0.

Go Up