Go Down

Topic: Changing Arduino Zero PWM Frequency (Read 73208 times) previous topic - next topic

MartinL

#45
Mar 11, 2016, 11:34 am Last Edit: Mar 11, 2016, 11:41 am by MartinL
Quote
The MUX settings should be the same, but are not because these last settings freezed my M0 twice.
Why should the M0 freezing have anything to do with the MUX settings?

Quote
Also you did not comment on the observations on the stencilled Word doc., where you can see that some parts of the sketch are not operative: this should surely point to relevant differences.
The code that I provided outputs 370Hz on D7 at a resolution of 11-bits, I've tested it on my Zero and it works. Why do you need to change the generic clock source to 8MHz, when 48MHz and the subsequent dividers are sufficient? Changing the generic clock to an 8MHz source ripples through to the timers, so you'll need to evaluate new values for the timer prescaler and PER register. That's why parts of the sketch in your Word file are inoperative.

Have you managed to get your M0 to output a 370Hz PWM signal on D7 using the code I provided?


glovisol

Dear Martin,

Thanks for your questions. Here is further info.

Further to my previous post, I took the previous (safely working) sketch and patiently made the modifications towards your last sketch, changing one thing at a time. I have come to the end and the M0 reloads with no problems, so I cannot explain why your original sketch made the M0 fail twice and this criticity of course worrie me.

Apart from this, please let me know if your sketch on your M0 works as described in your post, because on this M0, some parts REALLY DO NOTHING, as per my previous stencilled Word doc.

The parts that do nothing are:

===========================================
REG_GCLK_GENDIV = GCLK_GENDIV_DIV(3) |          // Divide the 48MHz clock source by divisor 3: 48MHz/3=16MHz
                   GCLK_GENDIV_ID(4);            // Select Generic Clock (GCLK) 4
 while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization

 REG_GCLK_GENCTRL = GCLK_GENCTRL_IDC |           // Set the duty cycle to 50/50 HIGH/LOW
                    GCLK_GENCTRL_GENEN |         // Enable GCLK4
                    GCLK_GENCTRL_SRC_DFLL48M |   // Set the 48MHz clock source
                    GCLK_GENCTRL_ID(4);          // Select GCLK4
 while (GCLK->STATUS.bit.SYNCBUSY);  

Here I can change DIV(3) to any number, but output freq. never changes. I can go from DFLL48M to OSC8M and output never changes.

==========================================

 REG_TCC0_PER = 2702;      // Set the frequency of the PWM on TCC0 to 370Hz
 while(TCC0->SYNCBUSY.bit.PER);

The PER register also is not operative and changing REG_TCC0_PER from 20000 to 1000 does not produce any change in output frequency. So the 2702 factor is useless here.

==========================================

All the remainder of the script is working well, so that, as received, the M0 has an output of 11,760 Hz.

Therefore: 11760/370 = 32 this is the missing scale factor.

To get to 370 Hz we must have a Prescaler factor of 8*32 = 256 and indeed, if I set the prescaler at 256 the M0 outputs the required 370 Hz.

kind regards,

glovisol

MartinL

#47
Mar 11, 2016, 12:02 pm Last Edit: Mar 11, 2016, 12:21 pm by MartinL
Quote
Apart from this, please let me know if your sketch on your M0 works as described in your post, because on this M0, some parts REALLY DO NOTHING, as per my previous stencilled Word doc.
The code that I supplied to output 370Hz PWM on D7 works. I've tested it on my Zero, my multimeter reads 370Hz.

All I can suggest is waiting until you get your Genuino Zero and the Arduino.cc IDE up and running. At least then we'll be singing from the same hymn sheet.

glovisol

Dear Martin,

I totally agree, it is pointless to lose time and risking failures while operating on two different machines. Will contact you again when my Genuino Zeros arrive.

Bt the way, where you from?

glovisol

glovisol

Dear Martin,

I ordered two Zero's in France last friday, march 11 and should be here any time now. Will advise.

Kind regards,

glovisol

MartinL

#50
Mar 14, 2016, 07:31 pm Last Edit: Mar 14, 2016, 07:32 pm by MartinL
Hi glovisol,

Quote
Bt the way, where you from?
I'm from England.

Quote
I ordered two Zero's in France last friday, march 11 and should be here any time now. Will advise.
Speak to you soon.

Kind regards,
MartinL

glovisol

Hi, MartinL,

The two Genuino zeros arrived this afternoon. One was defective just ot of the box: the native USB port was dead, didn't even light the ON Led and I immediately sent it back.

Downloaded IDE 1.6.7, but now I have to re-learn all the works, as this animal is a lot different from the M0! I still have to figure out how to print things into the serial monitor, as a start.

Will contact you again as soon as I can get this thing alive.

glovisol

glovisol

#52
Mar 17, 2016, 06:37 pm Last Edit: Mar 17, 2016, 06:47 pm by glovisol
Hi MartinL,

I finally sorted the Genuino Zero & loaded your sketch.

First the good news: I see 370 Hz on pin 7, but...here

 the bad news: the sketch starts only after pressing the reset button : it shhows the square wave @ 370 Hz for perhaps 3 seconds, then pin 7 goes high and stays there until I press the reset again.

All above loading on Programming Port (COM 2).  Loading on Native Port (COM14) I see the 370 Hz on pin 7 for about six seconds, then off. To see it again I must press the reset button.

Scratiching my head...

I have more news about the c.c. and org. IDES, but I leave these for later.

glovisol


glovisol

Hi MartinL

It works with Genuino Zero, but not, as we have seen last week, with Arduino M0!! This means that differences between the two IDES's and perhaps the two boards are more than currently believed.

I enclose the sketch which is the exact copy of your original. The only mystery is why the operating time is determined by the timex variable, but will investigate tomorrow. With timex = 100000 millis it works for a long time before going off. Frequency is confirmed 370 Hz and can go down to fraction of Hz manipulating the different parameters.

In the meantime my Picoscope 2000 decided to die, so more frustration. But I still had my faithful old friend, the Tektronix 2336 which tided me over.

Another important  issue: if you download IDE 1.7.8 (org.) the Atmel drivers of this IDE stay there even if you later load IDE 1.6.7 (c.c.). If you are not aware of this, all kind of strange things start happening with the Genuino (for instance it is impossible to load your sketch)....by luck I have many laptops and after getting crazy for two hours, loaded a fresh IDE 1.6.7 on a completely clean laptop and things started looking up. I reckon a good system cleanup is necessary when going from 1.7.8 to 1.6.7........

Hope to meet you on site  tomorrow.

Kind regards,

glovisol

glovisol

Hi MartinL,

Compliments, you really did a super job!

Herewith enclosed MINIMAL frequency change sketch that works with Genuino Zero, IDE 1.6.7. The problem with the sketch not staying "ON" yesterday was due to:

analogWrite(7, sensorValue_5);

which was conflicting with the CCBx register.

In any case the sketch outputs a fixed frequency, NOT a PWM which changes with an input pin parameter change, so some more work is necessary.

I still have a problem with sketches writing to the  Serial Monitor (Programmimg Port). Sometimes it works, sometimes it does not...

Cheers,

glovisol

MartinL

If you change the CCBx register value in your loop(), this will change the duty cycle of the PWM signal. The value of the CCBx register can be between 0 and whatever value you have in the PER register.

So for 370Hz the value in the PER is 2702, therefore if you set the CCBx register to 1351 you'll get a 50% duty cycle. 0 will give a 0V output for 0% duty cycle, while 2702 will give 3.3V for 100%. As the value of the CCBx is buffered, changes to the duty cycle won't cause any glitches on your output.

Code: [Select]
REG_TCC0_CCB3 = 1351;       // TCC0 CCB3 - 50% duty cycle on digtital pin 7 (D7)
while(TCC0->SYNCBUSY.bit.CCB3);

glovisol

Why not writing a Frequency Library out of this?

MartinL

I guess it would be possible to write a frequency library within certain constraints. The main issue though would be in creating something that satifies everyone's requirements in terms of PWM signal frequency and resolution, not to mention output pin multiplexing.

glovisol

Hi MartinL,

Here is the bottom line.

1. Complete sketch with Pin 7 working @ 400 Hz and outputting 0 - 100% PWM with Analogue input A5 receiving 0 - 2500 mV.

2. PC screens showing operation.

Your script is very powerful and gives accurate results. To prepare a library, the first step is planning a strategy for pin utilisation. I think that perhaps up to six pins can be used as outputs, to match the six available Analogue inputs. The real problem is that the output pins must output INDEPENDENT PWM levels to follow the individual A0 - A5 input voltages. In any case the output resolution shown by the enclosed sketch is simply amazing, when compared to what one can get out of Arduino Uno.

Have a nice weekend.

glovisol

glovisol

Hi MartinL,

Based on your work, now I have the sketch with 6 input / output channels working @ 400 Hz, on output pins: 2, 3, 4, 5, 6 & 7, from registers TCC0_CCB0 up to TCC1_CCB1. I anticipated at least two months of work, but with your help & know how I am in business after two weeks! I can post this sketch for the benefit of the community if you instruct me to do so.

Furthermore with this work we became aware that the board Arduino M0, working with its out of factory bootloader and IDE 1.7.8, does not respond to out of the book ATMEL standard commands  and I am wondering if it should not be our duty to warn other users of this problem. At the end of the day, I have two useless M0 boards in my lab, which were paid good money. Not only these boards did not and do not respond, but one became completely unoperative as a result of uploading software, something that should never happen and was revived with a special trick only.

We could further analyse these problems by using these boards and uploading the perfectly working shetches you have developed and see what happens.

Thanks again,

glovisol


Go Up