From b0218782adc8e05c89d51aaceb674fbf45bd4626 Mon Sep 17 00:00:00 2001 From: James Talbert <jtalbert@iastate.edu> Date: Thu, 27 Sep 2018 11:44:05 -0500 Subject: [PATCH] Add frequency scanning to PWM loop tests --- .../Loopback_Error_Check/src/helloworld.c | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/quad/vivado_workspace/PWM_Combined_Tests/PWM_Combined_Tests.sdk/Loopback_Error_Check/src/helloworld.c b/quad/vivado_workspace/PWM_Combined_Tests/PWM_Combined_Tests.sdk/Loopback_Error_Check/src/helloworld.c index 2d20f1a91..ea39a04e3 100644 --- a/quad/vivado_workspace/PWM_Combined_Tests/PWM_Combined_Tests.sdk/Loopback_Error_Check/src/helloworld.c +++ b/quad/vivado_workspace/PWM_Combined_Tests/PWM_Combined_Tests.sdk/Loopback_Error_Check/src/helloworld.c @@ -51,6 +51,7 @@ #include "xparameters.h" #include "xuartps.h" #include "sleep.h" +#include "math.h" int main() { init_platform(); @@ -61,33 +62,43 @@ int main() { (volatile uint32_t*) (XPAR_PWM_RECORDER_0_S_AXI_BASEADDR); volatile uint32_t* pwmGenerate = (volatile uint32_t*) (XPAR_PWM_SIGNAL_OUT_0_S_AXI_BASEADDR); + while (1) { + print( + "Please connect JB0 and JB1 with an external jumper.\r\nWhen ready, press any button.\n\r"); + while (buttonsGPIO[0] == 0) + ; + print("Thank You :)\r\n"); - print( - "Please connect JB0 and JB1 with an external jumper.\r\nWhen ready, press any button.\n\r"); - while (buttonsGPIO[0] == 0) - ; - print("Thank You :)\r\n"); + const int min_period = 1000; + const int max_period = 5 * 1000 * 1000; + const float period_step_factor = 1.5; + int period; + for (period = min_period; period < max_period; period *= + period_step_factor) { - pwmGenerate[0] = 50 * 1000; - pwmGenerate[1] = 25 * 1000; + pwmGenerate[1] = period / 2; + pwmGenerate[0] = period; - int iteration; - const int num_iterations = 100; - for (iteration = 0; iteration < num_iterations; iteration++) { - pwmGenerate[1] = (iteration * pwmGenerate[0]) / num_iterations; - usleep(10000); + int duty_iteration; + const int num_duty_iterations = 100; + for (duty_iteration = 0; duty_iteration < num_duty_iterations; + duty_iteration++) { + pwmGenerate[1] = ((duty_iteration + 1) * pwmGenerate[0]) + / (num_duty_iterations + 1); - int error = pwmRecord[0] - pwmGenerate[1]; - int abserror = error; - if (abserror < 0) { - abserror = -error; + usleep(period / 25); + + int error = pwmRecord[0] - pwmGenerate[1]; + int abserror = (error < 0) ? -error : error; + if (abserror > pwmGenerate[0] / 1000) { + xil_printf("%5d Hz, %3d%% Duty Cycle: %d cycle error\r\n", + (50 * 1000 * 1000 / pwmGenerate[0]), + (100 * pwmGenerate[1]) / pwmGenerate[0], error); + } + + } + usleep(period / 5); } - xil_printf("%3d%% Duty Cycle: %c%3d.%08d%% error\r\n", - (100 * pwmGenerate[1]) / pwmGenerate[0], - (error < 0) ? '-' : ' ', - (100 * abserror) / ((int) pwmGenerate[0]), - ((10000000000 * abserror) / ((int) pwmGenerate[0])) - % 100000000); } cleanup_platform(); return 0; -- GitLab