This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF24LE1 repeat twice the action

Hellow everyone, I'm testing the UART communication between the MCU and the PC. There is communication in both directions, the problem is that even if you only press the keyboard once, the instruction (flashing a led) is repeated twice. Annex code and image of the terminal. Thank you very much for the help

//MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN 
void main(void)
{
  //Variables locales.....................
	uint8_t caracter;
	
	// Configure TXD pin as output.
  // P0.5, P0.3 and P1.0 are configured as outputs to make the example run on
  // either 24-pin, 32-pin or 48-pin nRF24LE1 variants.
  P0DIR = 0xD6;		//1101 0110 
  P1DIR = 0xFE;

	P0CON = 0x31;	//0011 0000 + 1 Digital Input, buffer ON, Pull Down resistor connected  
  P00=0;
	
	// Initializes the UART
  hal_uart_init(UART_BAUD_9K6);

  // Wait for XOSC to start to ensure proper UART baudrate
  while(hal_clk_get_16m_source() != HAL_CLK_XOSC16M)
  {}

  // Enable global interrupts
  EA = 1;

  // Print "Lo que quieras" at start-up
  putstring("\r\nRutina de Pruebas\r\n");
	putstring("\r\nPara la Comunicacion UART\r\n");

  for(;;)
  {
  putstring("\r\nMENU DE INICIO\r\n");
	putstring("\r\n1. Parpadeo lento LED\r\n");
	putstring("\r\n2. Parpadeo Rapido LED\r\n\n\n");
	caracter = hal_uart_getchar();  // original: getchar();
	printf("\r\nTu seleccion fue: %c \n\n\n\n", caracter); 
		// /*
		switch (caracter)
		{
			case '1':
				Parpadeo_LED();
				caracter = 0;
			break;
			case '2':
				Parpadeo_Rapido_LED();
				caracter = 0;
			break;
			default:
			putstring("\r\nSELECION NO VALIDA\r\n");
			LED = 1;
			delay_ms(1000);	
			LED = 0;
			caracter = 0;
			break;	
		}
	// */
  }
}//Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin 

  • Hi,

    Have you scoped the UART interface, to check what data is incoming on hal_uart_getchar()?

    Maybe you need some kind of debounce to ensure that bounce on the input doesn't trigger the same code several times.

    For new development I strongly recommend to check out the nRF52832. There have not been done any new maintenance for the nRF24-series for several years.

    Best regards,
    Kenneth

  • Kenneth, thank you very much for your prompt response. The program is already running well. I only eliminated the infinite cycle, because I have noticed that whenever an interruption is involved, (happened to me testing deep sleep) the pointer besides going to the direction of the interruption vector, the MCU enters in a general reset or something similar. Incidentally, I had to add a delay of 10 ms at end of every case (1 and 2, lines 88 y 93), as seen in the corrected program that I append, because if there are some rare careacteres in the terminal, when executing the printf function, I realized this because option 3 of the switch case a blinking that delays works as a delay and in that option the characters were shown well, if someone could explain why, I would appreciate it. I'm going to take a look at nRF52832. Thanks again.

    #include <stdio.h>
    #include <stdint.h>
    #include "nrf24le1.h"
    #include "hal_uart.h"
    #include "hal_clk.h"
    #include "hal_delay.h"
    
    #define	SW	 P01
    #define	LED	 P00
    
    
    // Cusomization of low level stdio function. Used by for example printf().
    #ifdef __ICC8051__
    int putchar(int c)
    #else /*presume C51 or other accepting compilator*/
    char putchar(char c)
    #endif
    {
      hal_uart_putchar(c);
      return c;
    }
    
    // Cusomization of low level stdio function. Used by for example gets().
    #ifdef __ICC8051__
    int getchar(void)
    #else /*presume C51 or other accepting compilator*/
    char getchar(void)
    #endif
    {
      return hal_uart_getchar();
    }
    
    //Variables Globales...................
    
    //Prototipo de funciones...............
    void Boton(void);
    void Parpadeo_LED(void);
    void Parpadeo_Rapido_LED(void);
    
    // Repeated putchar to print a string
    void putstring(char *s)
    {
      while(*s != 0)
    		putchar(*s++);	//Es igual a la anterior
    }
    
    
    //MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN 
    void main(void)
    {
      //Variables locales.....................
    	char caracter;
    	char edoLED;
    	/*
    	*Configure TXD pin as output.
      *P0.5, P0.3 and P1.0 are configured as outputs to make the example run on
      *either 24-pin, 32-pin or 48-pin nRF24LE1 variants.
      */
    	
    	P0DIR = 0xD6;		//1101 0110 
      P1DIR = 0xFE;
    
    	P0CON = 0x31;	//0011 0000 + 1 Digital Input, buffer ON, Pull Down resistor connected  
    
    	// Initializes the UART
      hal_uart_init(UART_BAUD_9K6);
    
      // Wait for XOSC to start to ensure proper UART baudrate
      while(hal_clk_get_16m_source() != HAL_CLK_XOSC16M)
      {}
    
      // Enable global interrupts
      EA = 1;
    			
    	//Desplegar MENU:		
    	putstring("\r\nMENU DE INICIO\r\n");
    	putstring("\r\n1. Encender LED\r\n");
    	putstring("\r2. Apagar LED\r\n");
    	
    	//Obtener respuesta y ejecutarla
    	caracter = hal_uart_getchar();   // original: getchar();	
    	printf("\r\nTu seleccion fue: %c \r\n\n\n", caracter); 
    	
    		switch (caracter)
    		{
    			case '1':	
    				LED = 1;
    				delay_ms(10);	//Si se quita este retraso manda caracteres raros 
    			break;
    			
    			case '2':	
    				LED = 0;
    				delay_ms(10);
    			break;
    			
    			default:
    			edoLED = LED;
    			printf("\r\nSELECION NO VALIDA\r\n");
    			Parpadeo_LED();
    			LED = edoLED;
    			break;	
    		}
    }//Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin MAIN Fin 
    
    
    
    //BOTON BOTON BOTON BOTON BOTON BOTON BOTON BOTON BOTON BOTON BOTON BOTON BOTON BOTON  
    void Boton(void)
    {
    	while (SW == 1){}
    	delay_ms(15);
    	while (SW == 0){}
    	delay_ms(15);	
    }// Fin BOTON Fin BOTON Fin BOTON Fin BOTON Fin BOTON Fin BOTON Fin BOTON Fin BOTON Fin 
    
    
    
    
    // PARPADEO_LED PARPADEO_LED PARPADEO_LED PARPADEO_LED PARPADEO_LED PARPADEO_LED  
    void Parpadeo_LED(void)
    {
      //Variables locales
      char i;
      for(i=0; i<3; i++)                           
            {
            LED=1;
            delay_ms(400);
            LED=0;
            delay_ms(400);
            }
    }// Fin PARPADEO_LED Fin PARPADEO_LED Fin PARPADEO_LED Fin PARPADEO_LED Fin PARPADEO_LED 
    
    
    
    
    //PARPADEO_RAPIDO_LED PARPADEO_RAPIDO_LED PARPADEO_RAPIDO_LED PARPADEO_RAPIDO_LED  
    void Parpadeo_Rapido_LED(void)
    {
      //Variables locales
      char i;
      for(i=0; i<5; i++)                           
            {
            LED=1;
    					delay_ms(60);	//Original: delay_us(100);
            LED=0;
            delay_ms(60);
            }
    }//Fin PARPADEO_RAPIDO_LED Fin PARPADEO_RAPIDO_LED Fin PARPADEO_RAPIDO_LED Fin  
    /** @} */

Related