Menu

PulseIn issue

Help
2018-01-28
2018-01-29
  • JohanDenRidder

    JohanDenRidder - 2018-01-28

    I am trying to stabilize the speed of a motor by reading an attached Hall detector and then feeding the right PWM.
    It all works except for some strange behavior of the PulseIn command.
    I made a setup with a second pic to provide PortA.5 with a fixed pulse (21 mS, 24Hz).
    A logic analyzer on PortA.0 shows the content of the HSerprint commands.

    #chip 12F1840,16
    #define Sensor PORTA.5
    dir Sensor In
    dim SensorSpeed as Byte
    
    'USART settings
    #define USART_BAUD_RATE 2400
    #define USART_TX_BLOCKING
    SCKP = 1 ' Invert Tx Bit
    
    Do
          'wait for rising edge
          Do while Sensor=off
          Loop
    
          PulseIn Sensor, SensorSpeed, ms
          HSerPrint SensorSpeed
     Loop
    

    The LA shows that at the end of the positive pulse on channel 1 there is the serial print showing "21" on channel 0

     

    Last edit: JohanDenRidder 2018-01-28
  • JohanDenRidder

    JohanDenRidder - 2018-01-28

    After inserting a wait command the recorded pulse is much shorter:

    #chip 12F1840,16
    #define Sensor PORTA.5
    dir Sensor In
    dim SensorSpeed as Byte
    
    'USART settings
    #define USART_BAUD_RATE 2400
    #define USART_TX_BLOCKING
    SCKP = 1 ' Invert Tx Bit
    
    Do
          'wait for rising edge
          Do while Sensor=off
          Loop
    
          PulseIn Sensor, SensorSpeed, ms
          HSerPrint SensorSpeed
    
          Wait 75mS
     Loop
    

    Frequency is the same, pulse width is still 21mS,
    but GCB measures only 7mS pulse now, see attachment.

     

    Last edit: JohanDenRidder 2018-01-28
  • stan cartwright

    stan cartwright - 2018-01-28

    "I made a setup with a second pic to provide PortA.5 with a fixed pulse (21 mS, 24Hz)"
    24Hz is 41.666666666667 ms
    I'm vague about prob. pulsein is frequency independant in gcb..yes? are your pulsein values ok..like ms. just thoughts

     
  • JohanDenRidder

    JohanDenRidder - 2018-01-28

    Stan, please check the attached images. As you can see, the positive pulse is 21mS.
    One cycle is 42mS, so just a little over 24Hz.

     

    Last edit: JohanDenRidder 2018-01-28
  • mmotte

    mmotte - 2018-01-29

    Johan,
    Your code for waiting for the rising edge doesn't check that the signal is low before rising.

     'wait for rising edge
          Do while Sensor=off
          Loop
    

    So if the sensor is already on , it will go directly to pulseIn and only measure what is left of the pulse.

     'wait for pulse to be low
          Do while Sensor=on
          Loop
     'wait for rising edge
          Do while Sensor=off
          Loop
    
     
  • JohanDenRidder

    JohanDenRidder - 2018-01-29

    Oops, you are right.
    I took this snippet from the help page without thinking for myself...

    Thanks,
    Johan

     

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.