Hserout/Hserin via bluetooth


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default Hserout/Hserin via bluetooth

    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:
    Code:
    ; 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:
    Code:
    ; 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
    Last edited by louislouis; - 31st January 2017 at 21:01.

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Hserout/Hserin via bluetooth

    In your receiver code, how are you determining the entire data string has been sent?
    Dave Purola,
    N8NTA
    EN82fn

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Hserout/Hserin via bluetooth

    That's what I don't know.

    If I try this on transmitter side:

    Code:
    main:
    adcin 7, adval
    pause 20
    hserout [hex adval,13,10]
    pause 20
    goto main
    and on receiver side:

    Code:
    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] ?

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Hserout/Hserin via bluetooth

    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.

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Hserout/Hserin via bluetooth

    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

    Code:
    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

    Code:
    RX_Bombed:
        TimeoutCount = TimeOutCount + 1 
        end select 
    Goto main
    I then have this bit of code at the start of my comms subroutines

    Code:
    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

    Code:
    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

    Code:
    RCIF                VAR PIR1.5                    ' USART receive flag
    Last edited by Scampy; - 1st February 2017 at 18:25.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Hserout/Hserin via bluetooth

    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:
    Code:
    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 :-)

Similar Threads

  1. HSERIN and HSEROUT Pins
    By Dick Ivers in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd March 2014, 21:08
  2. Problem with HSEROUT/HSERIN
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd July 2013, 09:43
  3. HSERIN and HSEROUT problems
    By amindzo in forum General
    Replies: 2
    Last Post: - 31st August 2006, 06:51
  4. Hserin/hserout ?
    By Scott in forum General
    Replies: 6
    Last Post: - 27th April 2005, 23:46
  5. Bluetooth wireless with PIC / HSERIN / DAC
    By rpatel in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th December 2004, 22:13

Members who have read this thread : 3

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts