PDA

View Full Version : UART communication problem



picmilan
- 26th September 2016, 08:02
Hello,

I am trying to use following code in a PIC18F14K50 to change configuration of a rn42 Bluetooth Module and it goes to command mode but then it gets stuck:


As an example I want to change name of bluetooth module and then reboot it.



DEFINE LOADER_USED 1
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 115200
DEFINE HSER_SPBRG 12
DEFINE HSER_CLOERR 1

DEFINE OSC 24

ADCON0 = 011100
ADCON1 = 000000
ADCON2 = 110101

ANSEL = 001000
ANSELH = 000011

TRISC.4 = 0
TRISC.0 = 0
TRISC.1 = 1
TRISB.5 = 1
TRISB.7 = 0

tx1 VAR PORTB.7
rx1 VAR PORTB.5

LED VAR PORTC.4

Mybutton VAR PORTC.1

mainloop:
If Mybutton = 0 Then
HIGH LED ' Turn on LED
Hserout ["$$$"] 'enter command mode
Pause 100
Hserout ["SN,MyBT"] 'Change name to MyBT
Pause 100
Hserout ["R,1"] 'Reboot
Pause 500
Else
LOW LED ' Turn off LED
Pause 500
Endif
Goto mainloop ' Do it forever



I receive the data on serial monitor when I use a UART-USB converter so I know the strings are being sent.

However,the module starts blinking after $$$ but it doesn't perform the rest of commands.The similar Arduino code works without any problem.

Any idea why it doesn't work?

HenrikOlsson
- 26th September 2016, 08:22
Hi,
Never used the RN42 but shouldn't there be some sort of line termination characters after each command, like a LF or CR?
How else would it distinguish between for example MyBT and R, perhaps you wanted to change the name to MyBTR....


EDIT: Yeah, the command reference (http://ww1.microchip.com/downloads/en/DeviceDoc/bluetooth_cr_UG-v1.0r.pdf) seems to indicate CR is what it expects.
/Henrik.

aerostar
- 26th September 2016, 18:23
You also have a typo, you have DEFINE HSER_CLOERR 1 it should be DEFINE HSER_CLROERR 1

picmilan
- 27th September 2016, 03:51
Hi,
Never used the RN42 but shouldn't there be some sort of line termination characters after each command, like a LF or CR?
How else would it distinguish between for example MyBT and R, perhaps you wanted to change the name to MyBTR....


EDIT: Yeah, the command reference (http://ww1.microchip.com/downloads/en/DeviceDoc/bluetooth_cr_UG-v1.0r.pdf) seems to indicate CR is what it expects.
/Henrik.

AWESOME! That was the problem and it's working now.