Good day, I’m trying to use the serial monitor to check the output of my program. The Serial monitor is working in some places in my code but not others. I will comment in the code where it works and where it doesn’t. When I hook up the due to an Oscilloscope I’m seeing it outputting correctly when all the serial monitor statements (Serial.print()) are commented out, but I want to use the serial monitor to see exactly the values that are being outputted. Can someone tell me what to do?
FastSin.h (1.41 KB)
Attached is a FastSin.h file, its used for the sine calculations. Just drop it in the project file if you're going to run the code.
The purpose of the PWM_Handler() interrupt service routine (ISR), is to schedule small tasks usually in conjuction with the PWM controller.
The added burden of calling Serial output within the ISR means that during this time other pending interrupts cannot be serviced and if the timer period is short enough it may interfere with the ISR of the next timer cycle. Also, I noticed that your timer is being clocked very fast at 48MHz, although your comment states that it's to be clocked at 2MHz.
It would be better to simply set a flag in your ISR and use this to run the run_SVPWM() function in the loop() portion of your code. You could also try using the native USB port instead (SerialUSB), as this operates much faster than the programming port.
Thanks MartinL, but that in itself is not the issue, the real issue was the version of the IDE, i was running the most recent version, but when i tried the 1.5.2 r2 version, it worked just fine. and yes in the comments i had 2MHz, but i actually set it for 42MHz, I wasn't getting enough resolution with a 2MHz clock.
The issue is that you've got a serial port running at 9600bps and a PWM_Handler() function that's being called 5833 times a second.
How do I fix it? Increase the rate?
The easiest thing to try, is to use the Due's native USB port instead of the programming port. The native USB port effectively ignores the specified bit rate and runs at a very high speed.
Just plug your USB cable into the Due's native USB port, select "Due (Native USB Port)" from the boards menu in Arduino IDE then use "SerialUSB" instead of "Serial" in your code. To get the program to wait until you've opened the console is also usual to add the line "while(!SerialUSB);" after the SerialUSB.begin():
SerialUSB.begin(115200); // Start the native USB communications (ignores specified bit rate)
while(!SerialUSB); // Wait for the console to be opened
One other thing, do you recommend that I use the full 84MHz clock signal to get maximum resolution? Or keep it at 42MHz? And if yes i could use the full 84MHz can i just set the divider to 1 and instead of 2 and it will work?
P.S - I removed my code because it's code for my final year project in University
Yes, it's possible achieve an extra bit of resolution by doubling the timer clock from 42MHz to 84MHz and the CPRD period register from 3600 to 7200.
Resolution = log(7200)/log(2) = 12.8 bit
Whether or not it's necessary to increase the resolution is really project dependent, but it will work. In either case the ISR will be called at the same rate, as the update period remains unchanged.
So this is achieved by:
PWM->PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1);
Yes, that's right.
The CPRD register also needs to be doubled as well, in order to maintain the same period.