I have make code to check error rate of baud rates in teensy-LC.
But, I got difficult result than PJRC site. (https://www.pjrc.com/teensy/td_uart.html)
ex) 230400bps error rate
my code : 0.16%
PJRC site : -6.99%
Is there a problem with my code?
Code:
#include <stdio.h>
#include <stdlib.h>
#define __MKL26Z64__ /* teensy-LC */
//#define __MK20DX256__ /* teensy 3.1 & 3.2 */
#if defined(__MKL26Z64__)
#define KINETISL
#define F_CPU 48000000
#define F_PLL 96000000
#define F_BUS 24000000
#elif defined(__MK20DX256__)
#define KINETISK
#define F_CPU 96000000
#define F_PLL 96000000
#define F_BUS 48000000
#endif
#if defined(KINETISK)
#define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
#define ACTUIAL(count) (F_CPU / 16 / (count/32))
#elif defined(KINETISL)
#define BAUD2DIV(baud) (((F_PLL / 2 / 16) + ((baud) >> 1)) / (baud))
#define ACTUIAL(count) (F_PLL / 2 / 16 / count)
#endif
int main()
{
int baud[] = {115200, 230400, 250000, 460800, 500000, 921600, 1000000, 2000000, 4608000};
printf("<F_CPU : %d>\n", F_CPU);
for(int i=0;i<(sizeof(baud)/sizeof(int));i++){
int count = BAUD2DIV(baud[i]);
#if defined(KINETISK)
if (count < 32) count = 32;
#elif defined(KINETISL)
if (count == 0) count = 1;
#endif
int actual = ACTUIAL(count);
float err = ((float)(actual - baud[i]) / (float)baud[i]) * (float)100.0;
printf("baud: %d, count: %d, actual: %d, error: %.2f\n", baud[i], count, actual, err);
}
return 0;
}
<output>
<F_CPU : 48000000>
baud: 115200, count: 26, actual: 115384, error: 0.16
baud: 230400, count: 13, actual: 230769, error: 0.16
baud: 250000, count: 12, actual: 250000, error: 0.00
baud: 460800, count: 7, actual: 428571, error: -6.99
baud: 500000, count: 6, actual: 500000, error: 0.00
baud: 921600, count: 3, actual: 1000000, error: 8.51
baud: 1000000, count: 3, actual: 1000000, error: 0.00
baud: 2000000, count: 2, actual: 1500000, error: -25.00
baud: 4608000, count: 1, actual: 3000000, error: -34.90
But, I got difficult result than PJRC site. (https://www.pjrc.com/teensy/td_uart.html)
ex) 230400bps error rate
my code : 0.16%
PJRC site : -6.99%
Is there a problem with my code?
Last edited: