Go Down

Topic: Serial Monitor not working properly (Read 242 times) previous topic - next topic

jonnygainz

Feb 05, 2019, 01:30 pm Last Edit: Feb 06, 2019, 12:26 pm by jonnygainz
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?


jonnygainz

#1
Feb 05, 2019, 01:47 pm Last Edit: Feb 05, 2019, 01:48 pm by jonnygainz
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.

MartinL

#2
Feb 05, 2019, 03:42 pm Last Edit: Feb 05, 2019, 03:47 pm by MartinL
Hi jonnygainz,

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.

jonnygainz

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.

MartinL

Hi jonnygainz,

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.

jonnygainz

How do I fix it? Increase the rate?

MartinL

#6
Feb 06, 2019, 11:02 am Last Edit: Feb 06, 2019, 11:03 am by MartinL
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():

Code: [Select]
SerialUSB.begin(115200);    // Start the native USB communications (ignores specified bit rate)
while(!SerialUSB);          // Wait for the console to be opened

jonnygainz

#7
Feb 06, 2019, 12:28 pm Last Edit: Feb 06, 2019, 12:30 pm by jonnygainz
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

MartinL

#8
Feb 06, 2019, 12:48 pm Last Edit: Feb 06, 2019, 01:42 pm by MartinL
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.

jonnygainz

So this is achieved by:

Code: [Select]
PWM->PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1);

Right?

MartinL

Yes, that's right.

The CPRD register also needs to be doubled as well, in order to maintain the same period.

Go Up