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 first board to the RX pin of the second board using a wire, i see that the LEDs toggle as expected.
Schematic Below
Figure 3 - Communication between the two PIC microcontrollers using wires. This setup works successfully, and as expected.
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.
- I've ensured that the PIC boards TX/RX is working fine by connecting them directly using wires and it works as expected.
- 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 advise please?