PDA

View Full Version : Hserout/Hserin via bluetooth



louislouis
- 31st January 2017, 20:57
Hello boys,

I need a little alignment about serial communication via HC-5 bluetooth modules. I try to send via bluetooth 8bit ADC value from one MCU to another MCU and this value feed to MCP41010 digital pot.
I succesfully setuped and paired the bluetooth modules but I have trouble with receiving data. If I feed the data on receiver side to serial monitor on PC I saw changing correct values from 0 to 255 as I changing the pot value on transmitter side.
But in "datain" variable (on LCD) I dont have a correct value like 0 to 255. Value from 0 to 9 is correct but next is "random" numbers.
What is the right way to receive these value to datain variable?

Thanks.

The codes for
receiver:


; device PIC 16F688
#CONFIG
cfg = _INTRC_OSC_NOCLKOUT
cfg&= _WDT_ON
cfg&= _PWRTE_OFF
cfg&= _MCLRE_OFF
cfg&= _CP_OFF
cfg&= _CPD_OFF
cfg&= _BOD_ON
cfg&= _IESO_ON
cfg&= _FCMEN_ON
__CONFIG cfg
#ENDCONFIG

DEFINE OSC 8 ; Use a 8 MHZ internal clock
OSCCON = %01110000

DEFINE HSER_RCSTA 90h ; Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ; Enable transmit, BRGH = 1
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_CLROERR 1 ; Clear overflow automatically

Define LCD_DREG PORTC
Define LCD_DBIT 0
Define LCD_RSREG PORTA
define LCD_RSBIT 4
define LCD_EREG PORTA
define LCD_EBIT 5
define LCD_BITS 4
define LCD_LINES 2
define LCD_COMMANDUS 2000
define LCD_DATAUS 50

ANSEL = 0 ; Set all digital
CMCON0 = 7
TRISA = %000000 ; Set all output
TRISC = %000001 ; Set RC.5 input

SCK var PORTA.0 ; Alias PORTA.0 to SCK (serial data clock)
SDO var PORTA.1 ; Alias PORTA.1to SDO (serial data out)
CS var PORTA.2 ; Alias PORTA.2 to C_EN (chip enable)

datain var byte

PAUSE 500 : LCDOUT $FE,1 : PAUSE 250 ; Initialize LCD
HIGH CS ;Chip enable high to start MCP41010 digital pot
high sck ; Mode 1,1 SCK idles high
pause 10

Pot0 con %00010001 ; digital pot command setting

Main:
LCDOUT $FE,1
HSerin 500,main,[dec datain] ; read from bluetooth. Value between 0 - 255
pause 30
lcdout $FE,$80,dec datain ; lcd out only for information
pause 30

low CS ; Make digital pot chip active
pause 1
shiftout sdo,sck,5,[pot0,datain] ; Mode 1,1 / Mode 5 - clock idles high, datain value shift out
high CS
pause 1

goto main


transmitter:


; device PIC 16F688
#CONFIG
cfg = _INTRC_OSC_NOCLKOUT
cfg&= _WDT_ON
cfg&= _PWRTE_OFF
cfg&= _MCLRE_OFF
cfg&= _CP_OFF
cfg&= _CPD_OFF
cfg&= _BOD_ON
cfg&= _IESO_ON
cfg&= _FCMEN_ON
__CONFIG cfg
#ENDCONFIG

DEFINE OSC 8 ; Use a 8 MHZ internal clock
OSCCON = %01110000

Define ADC_BITS 8 ; Set number of bits in result
Define ADC_CLOCK 3 ; Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ; Set sampling time in uS

DEFINE HSER_RCSTA 90h ; Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ; Enable transmit, BRGH = 1
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_CLROERR 1 ; Clear overflow automatically

adval var byte

ANSEL = %10000000 ; Set AN7 analog input, rest digital
CMCON0 = 7
TRISA = %000000 ; Set all output
TRISC = %001000 ; Set RC4 output TX pin

let adval = 0

main:
ADCIN 7, adval
pause 20
hserout [dec adval,10] ; hserout adval to bluetooth HC-05
pause 20
GOTO Main

Dave
- 1st February 2017, 11:39
In your receiver code, how are you determining the entire data string has been sent?

louislouis
- 1st February 2017, 12:25
That's what I don't know.

If I try this on transmitter side:



main:
adcin 7, adval
pause 20
hserout [hex adval,13,10]
pause 20
goto main


and on receiver side:



main:
lcdout $fe,1
hserin 500, main, [hex datain]
pause 20
lcdout $fe,$80, dec datain
pause 20
goto main


It works, but the result on LCD is unstable, the value sometimes jumping between 0 -255 randomly.
I think this is not the right way how to send and receive serial data via BT .

Can I use hserout [dec adval,13,10] on transmitter side?

What about on receiver side hserin 500, main, [hex datain] not work,
maybe hserin 500, main, [str datain\3] ?

louislouis
- 1st February 2017, 17:26
I don't know how to read the sended decimal value in to the datain variable. On receiver side right on output from HC-05 I have correct serial decimal value 0 - 255, verified by serial monitor on PC.

Scampy
- 1st February 2017, 18:13
Sending data via an HC bluetooth module is no different to sending it via normal serial, just that there is no physical cable between the two devices (such as a PC and the PIC).

You could try using an FTDI usb to serial module and get the code working via hardwired setup and then use the HC module which should function exactly the same, if it doesn't then this would suggest that there may be a fault with the hardware.

I use these in a couple of my projects to send decimal values from word variables to an application running on a PC



HSEROUT [dec4 CH1_MAX]
HSEROUT [dec4 CH2_MAX]

etc

then
HSERIN 1000, RX_Bombed, [dec4 CH1_MAX]
to read it back



The RX Bombed is a time out



RX_Bombed:
TimeoutCount = TimeOutCount + 1
end select
Goto main



I then have this bit of code at the start of my comms subroutines



coms:

HSERIN [nTest]
SELECT CASE nTest
CASE "Q" ; if Q then send data to PC
Goto Term_TX
CASE "S" ; if S then receive data from PC
goto Term_RX
return



In the main program loop I have this to check the buffer and jump to the comms subroutine



FOR TempWD = 0 TO 500
IF RCIF=1 THEN GOSUB coms ; Check to see if PC application connected
PAUSE 1
next TempWD


Hope this helps

Sorry for got to add



RCIF VAR PIR1.5 ' USART receive flag

louislouis
- 1st February 2017, 18:41
Thank You for answer, maybe I found the problem. If I comment the "lcdout $fe,$80, dec datain" and "lcdout $fe,1" lines in my code the value in datain var come correct an stable.
Then I put the datain value to other variable like:


main:
hserin 500, main, [dec3 datain]
pause 10
let lcd_out = datain
lcdout $fe,$80, dec3 lcd_out
pause 20
goto main


And it works how I wish, luckily :-)