View Full Version : Pic18 and RN42 bluetooth communication problem using UART
picmilan
- 30th May 2016, 08:15
Hello everyone,
I have a RN42 bluetooth module in HID profile that I am trying to get it working with PIC18F24J50.
I have a code in arduino that is working and basically moves the cursor.I want to do the same in Basic but it doesn't work.
Code in Arduino :
int led = 13;
void mouseAction(void);
void setup() {
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
Serial.begin(115200);
delay(500);
digitalWrite(led, LOW);
}
void loop() {
digitalWrite(led, LOW);
mouseAction(0,0,10,0);
}
void mouseAction(int button,int x, int y,int wheel)
{
Serial.write(0xFD);
Serial.write(0x05);
Serial.write(0x02);
Serial.write(button)
Serial.write(x);
Serial.write(y);
Serial.write(wheel);
delay(20);
}
And this is the Basic code:
DEFINE LOADER_USED 1
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 115200
DEFINE HSER_SPBRG 1 ' 115200 Bauds
DEFINE HSER_CLOERR 1
DEFINE OSC 4
SPBRG = 1
RCSTA = 010000
TXSTA = 100100
TRISC.0 = 0
TRISC.6 = 0
TRISC.7 = 1
LED VAR PORTC.0
mainloop:
high LED
'TXREG = $FD
'TXREG = $05
'TXREG = $02
'TXREG = 0
'TXREG = 0
'TXREG = 10
'TXREG = 0
Hserout [$FD]
Hserout [$05]
Hserout [$02]
Hserout [0]
Hserout [0]
Hserout [10]
Hserout [0]
Pause 1000
low LED
Pause 1000
Goto mainloop
It doesn't work using 4Mhz internal oscillator and I wonder if that is problem and a 24 external oscillator will fix it or the is problem with my code?
I am a bit new to UART using Pic18 and I would appreciate anyway help.Thanks!
richard
- 30th May 2016, 09:24
It doesn't work using 4Mhz internal oscillator and I wonder if that is problem and a 24 external oscillator will fix it or the is problem with my code?
@ 4mHz The baudrate error is 8.5 % .Quite likely to cause trouble , the internal osc is quite capable of 8 12 16 24 or 48 MHz operation any one of which would be better than 4 .
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 115200
DEFINE HSER_SPBRG 1 ' 115200 Bauds
DEFINE HSER_CLOERR 1
DEFINE OSC 4
SPBRG = 1 why ? DEFINE HSER_SPBRG 1
RCSTA = 010000 ? even with %010000 its still wrong and conflicts with DEFINE HSER_RCSTA 90h
TXSTA = 100100 ? even with %100100 its redundant
picmilan
- 31st May 2016, 03:28
I changed the oscillator value but it still doesn't work.
I think it could be because of missing line in arduino code to enter command mode!
Serial.print("$$$")
Any suggestion on doing the same thing in Basic? How do I do it? using Hserout? Not sure about sending ASCII string.
Would appreciate any help.
richard
- 31st May 2016, 06:43
Serial.write(button)
Serial.write(x);
Serial.write(y);
Serial.write(wheel);
wheel ,x,y and button are all ints ie 16 bit signed values ie 8 bytes sent
Hserout [0]
Hserout [0]
Hserout [10]
Hserout [0]
are all bytes ie 8 bit unsigned values ie 4 bytes sent
I changed the oscillator value but it still doesn't work.
post your code including the fuse config
picmilan
- 31st May 2016, 08:35
How should I enter ints 16 bit signed values? like following?
Hserout [0]
Hserout [0]
Hserout [0]
Hserout [0]
Hserout [10]
Hserout [0]
Hserout [0]
Hserout [0]
New Code with config:
DEFINE LOADER_USED 1
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 115200
DEFINE HSER_SPBRG 7 ' 115200 Bauds
DEFINE HSER_CLOERR 1
DEFINE OSC 16
TRISC.0 = 0
TRISC.6 = 0
TRISC.7 = 1
LED VAR PORTC.0
Hserout ["$$$"] 'command mode Serial.print("$$$");
mainloop:
high LED
Hserout [$FD]
Hserout [$05]
Hserout [$02]
Hserout [0]
Hserout [0]
Hserout [10]
Hserout [0]
Pause 1000
low LED
Pause 1000
Goto mainloop
Thanks again for the help! I am stuck!
richard
- 31st May 2016, 10:33
How should I enter ints 16 bit signed values? like following?
to send 10
in little_endian format
Hserout [10,0]
or big_endian format
Hserout [0,10]
not sure how arduinos go about it they could be big or little endian
if your values can be negative then you need to pay close attention to your calcs pbp uses unsigned math
New Code with config:
there is no config there
pbp default config for pic18f24j50 is
#CONFIG
CONFIG WDTEN = ON
CONFIG PLLDIV = 5
CONFIG STVREN = ON
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CPUDIV = OSC1
CONFIG CP0 = OFF
CONFIG OSC = HSPLL
CONFIG T1DIG = ON
CONFIG LPT1OSC = OFF
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG WDTPS = 512
CONFIG DSWDTOSC = INTOSCREF
CONFIG RTCOSC = T1OSCREF
CONFIG DSBOREN = ON
CONFIG DSWDTEN = ON
CONFIG DSWDTPS = G2
CONFIG IOL1WAY = ON
CONFIG MSSP7B_EN = MSK7
CONFIG WPFP = PAGE_0
CONFIG WPEND = PAGE_WPFP
CONFIG WPCFG = OFF
CONFIG WPDIS = OFF
#ENDCONFIG
which expects an external xtal
DEFINE OSC 16
is just a compiler directive used for compile time timing calculations , it does nothing else
to use the internal osc the config words need to be set appropriately
such as
CONFIG OSC = INTOSC
or
CONFIG OSC = INTOSCPLL
and
CONFIG PLLDIV needs to match
along with
CONFIG CPUDIV which need to match
read the osc section of the data sheet
picmilan
- 31st May 2016, 10:46
Thanks alot! I will try that.
Do I need to set OSCCON and OSCTUNE registers as well if I am using internal oscillator? I am a bit confused about Frequency Tuning bit of OSCTUNE register.How you get to 16MHz from 96 MHz PLL?
richard
- 31st May 2016, 11:12
for 16mhz
#CONFIG
CONFIG WDTEN = ON
CONFIG PLLDIV = 2
CONFIG STVREN = ON
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CPUDIV = OSC3_PLL3
CONFIG CP0 = OFF
CONFIG OSC = INTOSCPLL
CONFIG T1DIG = ON
CONFIG LPT1OSC = OFF
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG WDTPS = 512
CONFIG DSWDTOSC = INTOSCREF
CONFIG RTCOSC = T1OSCREF
CONFIG DSBOREN = ON
CONFIG DSWDTEN = ON
CONFIG DSWDTPS = G2
CONFIG IOL1WAY = ON
CONFIG MSSP7B_EN = MSK7
CONFIG WPFP = PAGE_0
CONFIG WPEND = PAGE_WPFP
CONFIG WPCFG = OFF
CONFIG WPDIS = OFF
#ENDCONFIG
OSCTUNE.6=1 ; pll enable
8mhz -----/2 --= 4---pll--= 96 -/2 = 48 ------/3-- = 16
inc osc pll pre pll cpu div
picmilan
- 1st June 2016, 05:48
I got it running using external oscillator but bluetooth doesn't pickup the commands..
Any idea? even
Hserout [dec 10] doesn't work.
picmilan
- 1st June 2016, 07:04
Actually It's working using what you suggested.
Hserout [10,0]
Thanks alot!
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.