How to change the baud rate of HC-12 module using Arduino and AT89S51?

This post is based on these tutorials:

And contains two parts:

  • Change baud rate using Arduino
  • Change baud rate using AT89S51

Download User Manual From Here:
http://avrproject.ru/112/rf_hc12/2016-01-14_122335_HC-12_v2.3B.pdf

Or:
https://1drv.ms/b/s!AhkqucDhsneehqBEhbDACgc3lC_9Vw


Change baud rate using Arduino

Arduino HC-12
2 TxD
3 RxD
GND GND
GND SET
5V VCC
HC_12_Change_Baud_Rate_Auduino.png
Download the circuit diagram here:
https://1drv.ms/i/s!AhkqucDhsneehqBKNsP0u9egQg87vg

Note that Arduino is connected to the computer via USB cable.
And according to user manual:

When the second method (pin 5 “SET” tied to ground before power is applied), the module always enters command mode with the serial port configured for 9600bps, 8 data bits, no parity, 1 stop bit, irrespective of any previously configured settings.

So, when entering Command Mode, we should communicate with the HC-12 module using baud rate 9600 bps.
And the code for Arduino:


//HC-12 AT Commander
//Autor Tom Heylen tomtomheylen.com
//The HC-12 commander mode runs at baud rate 9600

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {
Serial.begin(9600);
Serial.println("Enter AT commands:");
mySerial.begin(9600);
}

void loop(){
if (mySerial.available()){
Serial.write(mySerial.read());
}
if (Serial.available()){
mySerial.write(Serial.read());
}
}

Or download it from:
https://1drv.ms/u/s!AhkqucDhsneehqBF_7uGzCJlZEJupw
Since 0→RX and 1←TX are not available when the Arduino is connected to the computer, we need to include software serial library, to make pin 2 and pin 3 act as serial pins.

After you upload the code to Arduino, you can start to enter AT commands.

  1. Change Baud Rate:
    Enter “AT+Bxxxx” to change baud rate to xxxx bps.
    Example: Enter “AT+B19200” and the module will work at baud rate 19200 bps and return “OK+B19200”.
    The available values of xxxx are: 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200.
    The default baud rate is 9600 bps.
  2. Change Communication Channel:
    Enter “AT+Cyyy” to change communication channel to yyy.
    Example: Enter “AT+C021” and the module will change to channel 021 and return “OK+C021”.
    The available values of yyy are: 001, 002, 003, …, 127.
    The default channel is 001.
  3. Factory Reset:
    Enter “AT+DEFAULT” and the module will reset to default settings and return “OK+DEFAULT”.

For more useful Commands, please refer to the links provides above or the user manual.


Change baud rate using AT89S51

WARNING, I HAVEN’T DONE THIS EXPERIMENT MYSELF, IT MAY BE WRONG.

Note that, since the HC-12 commander mode runs at fixed baud rate of 9600 bps, we need to choose specific frequency of crystal to make sure AT89S51 can meet at this baud rate. The 11.0592MHz is the most common one.
Search “8051 send at command” for more information.

This post is based on this tutorial:

With some modification. (Change GSM Module to HC-12 Module, Without LCD, delete some code)

Circuit Diagram:
HC_12_Change_Baud_Rate_AT89S51.png
Download the circuit diagram here:
https://1drv.ms/i/s!AhkqucDhsneehqBLETigM1iR_vnflQ

Code:


#include<reg52.h>
int k;
unsigned char str[26];
void GSM_init() // serial port initialization
{
TMOD=0x20; // Timer 1 selected, Mode 2(8-bit auto-reload mode)
TH1=0xfd; // 9600 baudrate
SCON=0x50; // Mode 1(8-bit UART), receiving enabled
TR1=1; // Start timer
}
void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned m,n ;
for(m=0;m<time;m++)
for(n=0;n<1275;n++);
}

void GSM_write(unsigned char ch) // Function to send commands to GSM
{
SBUF=ch; // Put byte in SBUF to send to GSM
while(TI==0); //wait until the byte trasmission
TI=0; //clear TI to send next byte.
}

main()
{
k=0;
GSM_init();
msdelay(200);
GSM_write('A'); // Sending 'A' to GSM module
msdelay(1);
GSM_write('T'); // Sending 'T' to GSM module
msdelay(1);
//I add the code below. Set the HC-12 Moudule's baud rate to 9600 bps.
GSM_write('+'); // Sending '+' to GSM module
msdelay(1);
GSM_write('B'); // Sending 'B' to GSM module
msdelay(1);
GSM_write('9'); // Sending '9' to GSM module
msdelay(1);
GSM_write('6'); // Sending '6' to GSM module
msdelay(1);
GSM_write('0'); // Sending '0' to GSM module
msdelay(1);
GSM_write('0'); // Sending '0' to GSM module
msdelay(1);
GSM_write(0x0d); // Sending carriage return to GSM module
msdelay(50);
while(1)
{
//do nothing
}
}

Using Keil uVision or other software to convert the code to .hex file.

And using Arduino or professional 8051 programmers to program the .hex file to HC-12 Module.

Advertisements

Published by

getabyte

An Electrical Engineering student who likes to learn new things.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s