MKL25Z, UART1 configuration. What is wrong?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

MKL25Z, UART1 configuration. What is wrong?

跳至解决方案
2,204 次查看
madagaskar
Contributor II

Hello everyone!

I try  to connect PC with FRDM-KL25Z board via USB-UART adapter. Unfortunately when I configured UART1 using “Kinetis L Peripheral Quick Reference” and “Reference Manual” it didn’t work correctly… I can receives and transfers data but during the sending, all messages are badly received by FRDM-KL25Z and PC.

I tried to program whole configuration by myself but with no effect. Probably I have some easy mistake but I have no idea where… (maybe it's problem with baud?)

On PC I use PUTTY to connect with KL25Z and I’ m sure that it isn’t problem with USB-UART adapter because he worked great when I communicated with my beaglebone black.

Maybe anybody have had the same problem and know what should I improve?

Below UART1 settings and program code.

UART1 Settings:

  • baud: 2400
  • 8-bit character length
  • One stop bit
  • Parity disabled

Program Code:

#include "derivative.h" /* include peripheral declarations */

#include "stdio.h"

#define CLOCK_UART1        (SIM_SCGC4 |= SIM_SCGC4_UART1_MASK)

#define CLOCKC             (SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK)

#define UART_TX            (PORTC_PCR4 |= PORT_PCR_MUX(3))

#define UART_RX            (PORTC_PCR3 |= PORT_PCR_MUX(3))

int main(void)

{

      

       uint8_t data;

      

       CLOCKC;

      

       UART_RX;

       UART_TX;

      

       //clock init

       CLOCKB;

       CLOCK_UART1;

      

       //transfer and receive disable

UART1_C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);

//BAUD: 2400        48 000 000/(16*2400) = 0b10011100010

       UART1_BDH = UART_BDH_SBR(0x4);

       UART1_BDL = UART_BDL_SBR(0xE2);                                

      

//transfer and receive enable

       UART1_C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);                

       for(;;){

            

       if(UART1_S1 & UART_S1_RDRF_MASK)

       {

          //receive data  

          data = UART1_D;

            

             while(!(UART1_S1 & UART_S1_TDRE_MASK) && !(UART1_S1 & UART_S1_TC_MASK))

             {}

             //transfer data    

             UART1_D = data;

             puts("sent!");

       }

                   

       }

return 0;

}

标记 (4)
1 解答
1,182 次查看
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Pawel:

Besides of my colleague Paul's suggestion, I cannot see some call to a MCU clock configuration function, while baud rate according to your code is calculated from a 48 MHz clock. Are you configuring the MCU core/bus clock during initialization routines?

Regards!

Jorge González

在原帖中查看解决方案

0 项奖励
6 回复数
1,183 次查看
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Pawel:

Besides of my colleague Paul's suggestion, I cannot see some call to a MCU clock configuration function, while baud rate according to your code is calculated from a 48 MHz clock. Are you configuring the MCU core/bus clock during initialization routines?

Regards!

Jorge González

0 项奖励
1,182 次查看
Paul_Tian
NXP Employee
NXP Employee

Hi, Pawel Szulc

Yes, you should confirm your clock setting first, then change your code as I mentioned. Waiting for your testing result. Please also help to tell us more. You cannot receive data, receive a wrong data or not send data, please give us more.

Best Regards

Paul

0 项奖励
1,182 次查看
madagaskar
Contributor II

I receive a wrong data and I had no idea why. But now it works! Thanks Zhe Tian and Jorge_Gonzalez.:) You have right, It was problem with bus clock configuration. When I corrected my program, UART start working without any problems.

1,182 次查看
ricardoontivero
Contributor I

hello Pawel, How was the code finally? I understand its missing first clock setting, but how you did it?   I've been workin with UART0 with no problems but im trying communicate with another microncontroller by UART1 /2.
Thanks!

0 项奖励
1,182 次查看
madagaskar
Contributor II

Hello Ricardo Ontiveros Torres,

I'm sorry that I reply so late... It was problem with bus clock because i didn't initialize him (baud rate = bus clock/(16×BR)). So to fix it I use "Kinetis L Peripheral Module Quick Reference" (you can download it from freescale home website) and chapter 4: Clocking System. There are everything explained step by step.:)  If you will have any problem please let me know.

Best Regards

Paweł

0 项奖励
1,182 次查看
Paul_Tian
NXP Employee
NXP Employee

Hi, Pawel Szulc

I checked your code. Would you please have a try on following change?

"while(!(UART1_S1 & UART_S1_TDRE_MASK) && !(UART1_S1 & UART_S1_TC_MASK))

to be

while(!(UART1_S1 & UART_S1_TDRE_MASK));

Hope my reply can help you.

Best Regards

Paul