Menu

HSerSend interruption

Help
JANIS
2018-01-08
2018-01-08
  • JANIS

    JANIS - 2018-01-08

    Hello!
    To understand the problem, I will tell you about my project. There are two devices - one device has a 3x7 segment display that indicates the temperature of the DS18B20 or running messages. The display mode depends on the temp value of the Hserreceive from the other device. The other device will consist of a fan control with HPWM, a photo sensor of the analog input, a timer, led by HPWM ... depending on the mode of operation, the second device sends a display byte through Hsersend, which will set the display mode. I created a test code for both devices. The main problem is that the byte tempvalue that is sent from the second device to the display through the serial port is unstable. The test code is intended to test how HserSend works. The test sends 3 values ​​(0, 1, 2) each at a time. Everything works at least normal, unless the program code contains the HPWM instruction ... Inserting the program code into HPWM, Hsersend begins to byte broadcast intermittently. Due to this, the display starts to distort the running message. I have no communication experience. I can not continue to build the program code :( Help ludz. I hope you understand my story.
    I add both program codes.

    second device program code (tx)

     #chip 16F886, 4
    #config osc=int
    
     ;Setup LCD Parameters
      #define LCD_IO 4
      #define LCD_NO_RW
     '' #define LCD_Speed fast
    ; ----- Define Hardware settings
      #define LCD_RS PORTc.4
      #define LCD_Enable PORTc.5
      #define LCD_DB4 PORTb.3
      #define LCD_DB5 PORTb.2
      #define LCD_DB6 PORTb.1
      #define LCD_DB7 PORTb.0
    
    dir portc.2 out
    tempvalue=0
    
    #define USART_BAUD_RATE 9600
    Dir PORTc.6 out  ' Tx 'Set USART transmit pin to output
    dim aa as word
    cycle:
    
    ''  test programmm
    aa++
    
    if aa=>4000 then aa=0
    if aa<1000 then tempvalue=1
    if aa>1000 & aa<2000then tempvalue=0
    if aa>2000 then tempvalue=2
    
       HSerSend tempvalue
    
    locate 0, 0
    
    Print "Hello World."
    
    HPWM 1, 40, 26
    
    wait 10 ms
    goto cycle
    

    Display DEVICE CODE (Rx)

      #chip 16F886, 4
    
      #config osc = int
        #define 7Seg_Commonanode
        'support for pfet or pnp high side drivers - speciali prieks draiveriem!
      ''#define 7Seg_HighSide
        #define DISP_SEG_A PORTB.0
        #define DISP_SEG_B PORTB.1
        #define DISP_SEG_C PORTB.2
        #define DISP_SEG_D PORTB.3
        #define DISP_SEG_E PORTB.4
        #define DISP_SEG_F PORTB.5
        #define DISP_SEG_G PORTB.6
        #define DISP_SEG_DOT PORTC.1
    
        #define Disp_Sel_1 PortA.1
        #define Disp_Sel_2 PortA.2
        #define Disp_Sel_3 PortA.3
    dir portb out
        dir porta.1 out
        dir porta.2 out
        dir porta.3 out
        dir portc.1 out
        ''------------
        ''  temp sensor settings
    #include <DS18B20.h>
    ' DS18B20 port settings
    #define DQ PortC.4
    ''  display settings
    InitTimer0 Osc, PS0_16''  0_16 pie 4mhz
    On Interrupt Timer0Overflow Call showdigit
    
    dig =1
    
     ''======================
       'USART settings
      #define USART_BAUD_RATE 9600  'sets up comport 1 for 9600 baud
    
    '               'Set USART receive pin to input
     Dir PORTC.7 In
     ''============
      time=0
      time_1=0
    char=0
    dim message as string
       signneg=0
       signpos=0
    
      main:
    
      ''=================================
    HSerReceive tempvalue
    filt++
    if filt=>50 Then
    tempvalue1=tempvaluereceived
    filt=0
    end if
    
      ''====temperature reading
      if tempvalue=0 then
    
      time=0
      time_1=0
    
       ReadDigitalTemp
    
      If RxData.7 = 1 Then
      sign=1
          signneg++  ''  filter
        if signneg=>10 then
        signneg=10
        signpos=0
        end if
        DSint = 0 - DSint 
        ''Negative numbers 
        Else
           signpos++''  filter
        if signpos>10 then
        signpos=10
        signneg=0
        end if
    
        sign=0
        DSint =DSint
        end if
    
    end if
    Num3 = DSInt/10
    Num2 = DSInt%10
    
          ''================================
    if tempvalue=2 then
    Message = "SECURITY "
    char=9
    speed=40
    end if
    
    if tempvalue=1 then
    Message = "FAN ON "
    char=7
    speed=40
    end if
    
    if  tempvalue=255 then
    Message = "CO FAULT "
    char=9
    speed=30
    end if
    
    ''=================
    if tempvalue>0 then
    time++
         if time=>speed then
           time_1++
         if time_1=>char then time_1=0
          time=0
           end if
    DisplayChar 1, Message(time_1), 1
    Wait 3 ms
      DisplayChar 2, Message(time_1+1), 1
      Wait 3 ms
      DisplayChar 3, Message(time_1+2), 1
      Wait 4 ms
    end if
       goto main
    ''==================================
    
    sub  showdigit
    
    if tempvalue = 0   then
        dig++
        If dig > 3 then dig =1
    
        Select Case dig
    case 1
     If signneg=10 then  Displaychar 1, 45, 1 '' 45 - minus character ASCII
    if signpos=10 then Displaychar 1, "C", 1
    'display, character, normal dot 1=yes , invert dot 1=off)
     ''two undocumented ascii characters for the 7 segment display,
    ''space and minus (32 and 450). Otherwise the DisplayChar function must be a letter or number.
        Case 2
         if sign=1 then
        DisplayValue 2, Num3, 1                                              
        Else
         DisplayValue 2, Num3, 0
         end if
        Case 3
     DisplayValue 3, Num2, 1
        Case Else
            dig = 1
        End Select
    
      end if
    end sub
    
     
  • JANIS

    JANIS - 2018-01-08

    Also, the lcd display function interferes with serial port operation. for example cls and other lcd instructions. but my project will also have a display provided.

     
  • mmotte

    mmotte - 2018-01-08

    JANIS,

    You have chosen to experience one of the most popular problems. Communicating between two PICs.

    use "Divide and Conquer" troubleshooting. You have a display on both ends of the comm line. use extra print/display statements on both PICs to see what is happening. Also slow things down( longer wait) so they become more apparent where the problem lies.

    On the Tx Pic, i don't think the HPWM statement should interfere.
    I would use your LCD to print both the "aa" value and the the "tempvalue" variables to the LCD.

    On the Rx PIC i do see a conflict. When reading temperature you are using the 3x7seg display to display using the interrupt "showdigit" to jump between digits at intervals of timer0. For the messages you are not using the interrupt but driving them directly with DisplayChar which is overriding the other display. We need to make the "showdigit" into a "showchar" intterrupt sub so they are not in conflict.
    OR shut the interrupt off when not using it. I like the first option of making it work with both char and numbers.

    Another problem is "HSerReceive tempvalue". HSerReceive sends back a default value(255) if there is no data available. You need to check for new data if you want to receive only you data(0,1,2).

    'If there is no new data, HSerReceive will return default value.
            comport = 1
            HSerReceive Rxvalue
            If Rxvalue <> 255    Then    ‘ don’t change your value if it is default
                 tempvalue = Rxvalue
            End If
    

    Even better way is:

    If USARTHasData then
        HSerReceive tempvalue
        End If
    

    I hope this helps. i will look at rewriting the "showdigit" intterrupt routine to do both as time allows.

    GL
    Mike

     
  • Anobium

    Anobium - 2018-01-08

    Why not try I2C between the two? Works ok.

     
  • JANIS

    JANIS - 2018-01-08

    Thanks, mmotte! I will try!

    Anobium, as I said, I have no experience with communications. this is the first project. The devices will be connected by cable of approx. 1.5m ....

     
  • JANIS

    JANIS - 2018-01-08

    I tried
    If USARTHasData then HSerReceive tempvalue End if
    Its work!

    I2C is a short distance. How much is the maximum distance?
    I have a question, where is the description in Help about the USARTHasData command?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.