PLz Help! Serin and Serout Confusion


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Jun 2014
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: PLz Help! Serin and Serout Confusion

    i really appreciate ur help but I did what u said and the problem still there and the pwm is fixed at 30% duty even when um changing the volt here's the code

    sender:
    Code:
    'Sender Code
    
    
    Define OSC 8
    
    char var byte
    
    ' Main program loop
    start:
    
    if (portb.0==0) then       'button pressed
    char = "A"                 'character A sent to the other pic
    Serout portc.6, 2,[char]
    
    pause 50
    elseif (portb.0!=0) then
    char = "B"
    Serout portc.6, 2,[char]
    
    pause 50
    
    endif
    
    Goto start
    End
    Reciever
    Code:
    DEFINE OSC 8
               'LCD CONFIGURATION
    DEFINE LCD_DREG PORTB      'Define PIC port used for LCD Data lines
    DEFINE LCD_DBIT 4          'Define first pin of portb connected to LCD DB4
    DEFINE LCD_RSREG PORTB     'Define PIC port used for RS line of LCD
    DEFINE LCD_RSBIT 3         'Define Portb pin used for RS connection Communication
    DEFINE LCD_EREG PORTB      'Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT 0          'Define PortB pin used for E connection
    DEFINE LCD_BITS 4          'Define the 4 bit communication mode to LCD
    DEFINE LCD_LINES 2         'Define using a 2 line LCD
    DEFINE LCD_COMMANDUS 2000  'Define delay between sending LCD commands
    DEFINE LCD_DATAUS 50       'Define delay time between data sent.
               'PWM CONFIGURATION
    DEFINE CCP1 REG PORTC      'Port to be used by HPWM1
    DEFINE CCP1_BIT 2          'Pin to be used by Hpwm1
    DEFINE CCP2 REG PORIIC     'Port to be used by HPWM2
    DEFINE CCP2_BIT 1          'Pin to be used by hwmp2
                'ADC CONFIGURATION
    Define ADC_BITS 10          '10 bit result
    define ADC_clock 3         '8mhz external clock
    define ADC_Samplus 50
    trisA= 255      'all PORTA pins are inputs
    adcon1= %10000010 ' Set PORTA analog and right justify
       trisc=0 
       portc=0
    
       Temp var word     'Temp out of Lm35
       Vp var word       'Voltage out of Pressure Sensor
       Vl var word       'Voltage out of Level Sensor
       char var byte
        
       main:
       Serin portc.7,2,char           'Receive data sent from the transmitting pic 
    if (char == "A" ) then            'if data is character A
       
       pause 50
       Adcin 0, Temp                            ' Read channel 0 to Temp (0-1023)
       Temp = (Temp */ 500)>>2                  ' Equates to: (Temp * 500)/1024
       LCDOut ,$fe,$80,"Temp= ",DEC2 Temp       'Display Temp On The First Line 
       
       Adcin 1, Vp                                                     'Read channel 1 to Vp (0-1023)
       Vp = (Vp */ 500)>>2                                             'Equates to: (Vp * 500)/1024
       LCDOut $fe,$c0 ,"Pressure = ",DEC (Vp/100),".", DEC2 Vp," volt" 'Display Voltage out of pressure sensor on second line
       
       Adcin 2, Vl                                                     'Read channel 2 to Vl (0-1023)
       Vl = (Vl */ 500)>>2                                             'Equates to: (Vl * 500)/1024
       LCDOut $fe,$94 ,"Level= ",DEC (Vl/100),".", DEC2 Vl," volt"     'Display Voltage out of Level sensor on third line    
    
       if portd.0==0 then               'Soil Moisture is lower than preset value 
        if  Vp<3 then                   'Voltage From Pressure Sensor is Less than preset value (3 volt)
           Hpwm 2, 179,5000             'Solenoid is on with 70% duty cycle at 5 khz 
           if Vl<3 then                 'If voltage of Level sensor is Less than preset value (3 volt) 
           hpwm 1,179,5000              'Pump is on with 70% duty cycle at 5 khz
           elseif Vl>=3 then            'If voltage of Level sensor is more than or equal preset value
           hpwm 1,77,5000               'Pump is on with 30% duty cycle at 5 khz
           ENDIF
        elseif Vp>=3 then                'Voltage From Pressure Sensor is more than or equal preset value (3 volt)
           hpwm 2,77,5000                'Solenoid is on with 30% duty cycle at 5 khz
           if Vl<3 then                  'If voltage of Level sensor is less than preset value (3 volt)
           hpwm 1,179,5000               'Pump is on with 70% duty cycle at 5 khz
           elseif Vl>=3 then             'If voltage of Level sensor is more than or equal preset value
           hpwm 1,77,5000                'Pump is on with 30% duty cycle at 5 khz
           endif
        endif
       elseif portd.0!=0 then            'Soil Moisture is high or equal to preset value
           hpwm 1,0,5000                 'Turn off Pump
           hpwm 2,0,5000                 'Turn off Solenoid
       endif
    elseif char=="B" then
       lcdout $fe,1                       'Clear LCD
       hpwm 1,0,5000                      'Turn off Pump
       hpwm 2,0,5000                      'Turn off Solenoid
    endif
    
       
    
       Goto main                          'Do it forever
    
       End

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: PLz Help! Serin and Serout Confusion

    Hi,
    Without looking at the code in detail it's possible that the two devices are getting out of sync due to the fact that sender is constantly sending either "A" or "B".
    What if you, boil the code down to a bare minimum and instead of comparing with "B" compare with anytning but "A"?
    Code:
    SERIN PortC.7, 2, Char
    IF Char = "A" THEN
      HPWM 1, 77, 5000
    ELSE
     HPWM 1, 0, 5000
    ENDIF
    /Henrik.

  3. #3
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: PLz Help! Serin and Serout Confusion

    Many of your elseif comparators could simply be else. The code also sets Hpwm then enters "if then" potentially changing the value again the first Hpwm is not needed. Just to give you some ideas here is an example of what could be done. I have included a space either side of the comparators, Vp < 300 not Vp<300. Just personal preference on my part? Probably but I have learned to be very strict with code syntax.

    Code:
      main:
       Serin portc.7,2,char           'Receive data sent from the transmitting pic 
    if (char = "A" ) then            'if data is character A
       
       pause 50
       Adcin 0, Temp                            ' Read channel 0 to Temp (0-1023)
       Temp = (Temp */ 500)>>2                  ' Equates to: (Temp * 500)/1024
       LCDOut ,$fe,$80,"Temp= ",DEC2 Temp       'Display Temp On The First Line 
       
       Adcin 1, Vp                                                     'Read channel 1 to Vp (0-1023)
       Vp = (Vp */ 500)>>2                                             'Equates to: (Vp * 500)/1024
       LCDOut $fe,$c0 ,"Pressure = ",DEC (Vp/100),".", DEC2 Vp," volt" 'Display Voltage out of pressure sensor on second line
       
       Adcin 2, Vl                                                     'Read channel 2 to Vl (0-1023)
       Vl = (Vl */ 500)>>2                                             'Equates to: (Vl * 500)/1024
       LCDOut $fe,$94 ,"Level= ",DEC (Vl/100),".", DEC2 Vl," volt"     'Display Voltage out of Level sensor on third line    
    
       if portd.0 = 0 then               'Soil Moisture is lower than preset value 
        if  Vp < 300 then                   'Voltage From Pressure Sensor is Less than preset value (3 volt)
           if Vl < 300 then                 'If voltage of Level sensor is Less than preset value (3 volt) 
           hpwm 1,179,5000              'Pump is on with 70% duty cycle at 5 khz
           else                             'If voltage of Level sensor is more than or equal preset value
           hpwm 1,77,5000               'Pump is on with 30% duty cycle at 5 kHz
           endif 'vl <
        endif 'vp<
        if Vp >= 300 then                'Voltage From Pressure Sensor is more than or equal preset value (3 volt)
           if Vl < 300 then                  'If voltage of Level sensor is less than preset value (3 volt)
           hpwm 1,179,5000               'Pump is on with 70% duty cycle at 5 khz
           else                             'If voltage of Level sensor is more than or equal preset value
           hpwm 1,77,5000                'Pump is on with 30% duty cycle at 5 khz
           endif 'vl<
        endif 'vp>
       endif '=
    
       if portd.0 != 0 then            'Soil Moisture is high or equal to preset value
           hpwm 1,0,5000                 'Turn off Pump
           hpwm 2,0,5000                 'Turn off Solenoid
       endif '!=
    endif 'A
    
    if (char = "B") then
       lcdout $fe,1                       'Clear LCD
       hpwm 1,0,5000                      'Turn off Pump
       hpwm 2,0,5000                      'Turn off Solenoid
    endif
    
       
    
       Goto main                          'Do it forever
    I am now thinking why sending "B" does not work and an answer eludes me. I need time for more thought.

Similar Threads

  1. Serout -->>> serin
    By larzazral in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 27th February 2010, 03:46
  2. SERIN and SEROUT
    By CumQuaT in forum mel PIC BASIC
    Replies: 10
    Last Post: - 3rd September 2009, 00:17
  3. Serin Serout
    By docaberle in forum Serial
    Replies: 3
    Last Post: - 29th September 2008, 14:24
  4. serin and serout
    By yeelooi in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th December 2007, 08:35
  5. serin,serout
    By andrewwaack in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th July 2006, 19:21

Members who have read this thread : 0

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