0
\$\begingroup\$

I'm trying to do a wireless communication between two PIC microcontrollers (PIC16F886) using the TX/RX lines using HC-12 RF transceiver modules (datasheet link below).

https://statics3.seeedstudio.com/assets/file/bazaar/product/HC-12_english_datasheets.pdf

I've built two identical boards, with 2 LEDs. When I connect the TX pin of the first board to the RX pin of the second board using a wire, I see that the LEDs toggle as expected.

HC12 Transmitter and Receiver Board

Schematic Below

enter image description here

Figure 3 - Communication between the two PIC microcontrollers using wires. This setup works successfully, and as expected.

PIC to PIC communication using Wires between TX and RX pins

So, now after ensuring that the PIC hardware is working as expected, I've connected two HC12 RF modules as per the description and data sheet. I'm able to configure these HC12 modules by connecting them to the desktop using a USB to Serial module and execute AT commands successfully.

Now after configuring two HC12 modules identically, and plugging them to the PIC boards which I had prepared and tested earlier, nothing happens.

  1. I've ensured that the PIC board's TX/RX is working fine by connecting them directly using wires, and it works as expected.
  2. I'm able to configure the HC12 modules using AT commands in my desktop - this ensures that HC12 modules are in good condition.

But, the wireless communication never works. What am I doing wrong?

Here is the code for TX and RX

TX - Transmitter

#define _XTAL_FREQ 4000000

#define GREEN_LED RB4
#define TRIS_GREEN_LED TRISB4;

#define HC12_SET RB5
#define TRIS_HC12_SET TRISB5

#define RED_LED RB1
#define TRIS_RED_LED TRISB1;

#include <xc.h>

void UART_send_char(char bt);
void UART_send_string(char* st_pt);
char UART_get_char();

int Baud_rate = 9600;
char get_value;

void main(void) 
{
    ANSEL = 0x00;
    ANSELH = 0x00;

    TRISC6 = 0; // TX Pin set as output
    TRISC7 = 1; // RX Pin set as input

    TRISB4 = 0;
    TRISB5 = 0;
    TRISB1 = 0;

    /**Initialize SPBRG register for required 
    baud rate and set BRGH for fast baud_rate**/
    SPBRG = ((_XTAL_FREQ/16)/Baud_rate) - 1;
    BRGH  = 1;  // for high baud_rate
    SYNC  = 0;    // Asynchronous
    SPEN  = 1;    // Enable serial port pins

    TXEN  = 1;    // enable transmission
    TX9   = 0;    // 8-bit reception selected

    HC12_SET = 1;

    int x = 0;

    while(1) //Infinite loop
    {
        if(x == 0)
        {
            UART_send_char('0');
            x = 1;
            RED_LED = 1;
            GREEN_LED = 0;
        }
        else
        {
            UART_send_char('1');
            x = 0;
            RED_LED = 0;
            GREEN_LED = 1;
        }
        __delay_ms(1000);
    }
}


//**Function to send one byte of date to UART**//
void UART_send_char(char bt)  
{
    while(!TXIF);  // hold the program till TX buffer is free
    TXREG = bt; //Load the transmitter buffer with the received value
}
//_____________End of function________________//

//**Function to convert string to byte**//
void UART_send_string(char* st_pt)
{
    while(*st_pt) //if there is a char
        UART_send_char(*st_pt++); //process it as a byte data
}
//___________End of function______________//


//**Function to get one byte of date from UART**//
char UART_get_char()   
{
    if(OERR) // check for Error 
    {
        CREN = 0; //If error -> Reset 
        CREN = 1; //If error -> Reset 
    }

    while(!RCIF);  // hold the program till RX buffer is free

    return RCREG; //receive the value and send it to main function
}
//_____________End of function________________//

RX - Receiver

#define _XTAL_FREQ 4000000

#define GREEN_LED RB4
#define TRIS_GREEN_LED TRISB4;

#define HC12_SET RB5
#define TRIS_HC12_SET TRISB5

#define RED_LED RB1
#define TRIS_RED_LED TRISB1;

#include <xc.h>

void UART_send_char(char bt);
void UART_send_string(char* st_pt);
char UART_get_char();

int Baud_rate = 9600;
char get_value;

void main(void) 
{
    ANSEL = 0x00;
    ANSELH = 0x00;

    TRISC6 = 0; // TX Pin set as output
    TRISC7 = 1; // RX Pin set as input

    TRISB4 = 0;
    TRISB5 = 0;
    TRISB1 = 0;

    /**Initialize SPBRG register for required 
    baud rate and set BRGH for fast baud_rate**/
    SPBRG = ((_XTAL_FREQ/16)/Baud_rate) - 1;
    BRGH  = 1;  // for high baud_rate
    SYNC  = 0;    // Asynchronous
    SPEN  = 1;    // Enable serial port pins

    CREN  = 1;    // enable reception
    RX9   = 0;    // 8-bit reception mode selected

    HC12_SET = 1;

    while(1) //Infinite loop
    {
        get_value = UART_get_char(); 

        if (get_value == '1') //If the user sends "1"
        {
            RED_LED = 0;
            GREEN_LED = 1;
        }
        else if (get_value == '0') //If the user sends "0"
        {
           RED_LED = 1;
           GREEN_LED = 0;
        }
    }
}


//**Function to send one byte of date to UART**//
void UART_send_char(char bt)  
{
    while(!TXIF);  // hold the program till TX buffer is free
    TXREG = bt; //Load the transmitter buffer with the received value
}
//_____________End of function________________//

//**Function to convert string to byte**//
void UART_send_string(char* st_pt)
{
    while(*st_pt) //if there is a char
        UART_send_char(*st_pt++); //process it as a byte data
}
//___________End of function______________//


//**Function to get one byte of date from UART**//
char UART_get_char()   
{
    if(OERR) // check for Error 
    {
        CREN = 0; //If error -> Reset 
        CREN = 1; //If error -> Reset 
    }

    while(!RCIF);  // hold the program till RX buffer is free

    return RCREG; //receive the value and send it to main function
}
//_____________End of function________________//

What am I missing? Where am I going wrong? Any advice please?

\$\endgroup\$
13
  • \$\begingroup\$ Is Modem cross connected? Rx to Tx \$\endgroup\$ Jan 10, 2020 at 3:40
  • 1
    \$\begingroup\$ Please show schematic of the PIC + HC-12 etc. \$\endgroup\$ Jan 10, 2020 at 7:21
  • 1
    \$\begingroup\$ What antennas are you using? Can you connect one module to the PC as sender or receiver, or another usb-serial cable to test communication between modules on the PC? BTW - hackaday.com/2018/05/05/… \$\endgroup\$ Jan 10, 2020 at 7:45
  • 1
    \$\begingroup\$ Can the PICs communicate with the PC at the expected baudrate? If they both have wrong baudrate, they'll be able to communicate with each other but not with anyone else. \$\endgroup\$
    – Lundin
    Jan 10, 2020 at 13:48
  • \$\begingroup\$ Also, give the poor MCUs at least one decouping cap on the supply pin. Sitting close to a RF part also means you should consider smaller decoupling caps on any wire that may act as antenna. \$\endgroup\$
    – Lundin
    Jan 10, 2020 at 13:48

2 Answers 2

1
\$\begingroup\$

HC-12: I remember that AT-commands require a CR and/or LF at the end of the sent string. E.g. UART_send_string("Test\r\n");

\$\endgroup\$
0
\$\begingroup\$

Power and ground connections to the PIC are not shown in your schematic. Although, power and ground are shown on the HC12. Please add the missing connections to the schematic. Using two serial ports on your PC you should test that the HC12's can communicate together via those two ports.

\$\endgroup\$
1
  • \$\begingroup\$ Hope you have not seen my post completely. I've connected all the wire correctly. I'm also able to communicate the HC12 with PC. but two HC12 devices are not communicating together. The power and ground wires are connected. It is not shown in the diagram, and it doesn't show up in the proteus software. The software works like that. \$\endgroup\$ Mar 27, 2021 at 5:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.