Pulse-In function example

General discussion on mikroBasic.
Author
Message
DDJ
Posts: 15
Joined: 30 Jun 2008 07:10

Word

#16 Post by DDJ » 27 Aug 2008 20:03

Hi Dany
Thanks for your replies. I try and change it.
Last edited by DDJ on 28 Aug 2008 06:42, edited 1 time in total.

DDJ
Posts: 15
Joined: 30 Jun 2008 07:10

CCP

#17 Post by DDJ » 27 Aug 2008 20:09

Dany
Would it not be easier to use the CCP module here? I have never worked with it, but from a few posts it seems less complicated. What do you think ? The CD4093 puts out square wave pulses. Is it easy to measure these with CCP ?
Thanks

Charlie
Posts: 2744
Joined: 01 Dec 2004 22:29

#18 Post by Charlie » 27 Aug 2008 23:29

By saying I have to do it the hard way round, is that with a counter
No.The Counter command makes counting pulses easy.I think Dany has answered you question about your code.
Regards Charlie M.

DDJ
Posts: 15
Joined: 30 Jun 2008 07:10

It works !

#19 Post by DDJ » 28 Aug 2008 06:39

Thanks Dany, I changed the byte type to word and it works. Thanks again to everyone who helped me. This forum is really great.

Code: Select all

sub procedure LCD_BarGraph_Draw(dim Row as byte, dim Column_Start as byte, dim Column_End as byte, dim Value as word)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: CCP

#20 Post by Dany » 28 Aug 2008 09:34

DDJ wrote:Dany
Would it not be easier to use the CCP module here? I have never worked with it, but from a few posts it seems less complicated. What do you think ? The CD4093 puts out square wave pulses. Is it easy to measure these with CCP ?
Thanks
Well, I have never worked with the CCP either, it is on my list to try it out... :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

marcel n
Posts: 6
Joined: 26 Jul 2011 21:48

Re: Pulse-In function example

#21 Post by marcel n » 26 Jul 2011 22:03

Hi am just new here , i have a problem i have the easy pic 5 , and mikrobasic v5.1 and i want to use the example pulsein_demo but it wont work what do i wrong can someone help me ,and whit the other example of the lcd thats works
then i get some text , but whit the pulsein_demo i get no text , thanks

marcel n
Posts: 6
Joined: 26 Jul 2011 21:48

Re: Pulse-In function example

#22 Post by marcel n » 14 Aug 2011 15:11

hi i have try to make the pulsein_demo works on the pic16F887 and the easypic 5 in mikrobasic 5.1 and its works now continuously if you push the button D0 three times the display show 3 , and if you push him 5 times he shows 5 .
but how can i make this to also counts the pulses , if i connect my rc reciever and i go whit the stick up and down its dont change , what do i wrong , can you guys help me ,thanks

here mij code :

Code: Select all

'(*
' * Project name:
'
' * Copyright:
'     (c) mikroElektronika, 2006
' * Revision History:
'     20050914 (MJ):
'       - initial release.
' * Description:
'     This code demonstrates using Pulse-In Function for counting pulses in given
'     time period. This a blocking call. This demo waits for input signal
'     (high level) to start counting pulses. It counts pulses for 1000 miliseconds.
'     Then it prints results on LCD which is connected on PORTB.
' * Test configuration:
'     MCU:             P16F887
'     Dev.Board:       EasyPIC5
'     Oscillator:      HS, 8.000 MHz
'module pulsein_demo.mbas
'     SW:              mikroBasic v5.0 or higher
' * NOTES:
'     Prototype:
'       function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
'
'     PORT  - assigns port for pulse-in function
'     PIN   - assigns pin for pulse-in function
'     STATE - defines logical state for counting pulses (LOW = 0 or HIGH = 1)
'     DURATION - Time in ms during which the pulses are being count
'
'     This function returns number of pulses in given time (DURATION), on given
'       PORT and PIN.
'     Tested on EasyPIC3 with P16F877A. With few modification it can be used
'       with almost every PIC MCU.
' *)
program pulsein_demo

dim LCD_RS as sbit  at RB4_bit    ' Lcd module connections
    LCD_EN as sbit  at RB5_bit
    LCD_D4 as sbit  at RB0_bit
    LCD_D5 as sbit  at RB1_bit
    LCD_D6 as sbit  at RB2_bit
    LCD_D7 as sbit  at RB3_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit         ' End Lcd module connections

dim text as string [16]           ' Variable text is of string type

dim counter,
    msec,
    num_impulse as word
    txt         as char[10]
dim tmp as word                               ' Temporary variable
    old as byte
sub procedure interrupt
   counter = counter + 1                               ' Increment value of counter on every interrupt
   if counter = 1 then                        ' Has 1ms elapsed?

     counter = 0

     msec = msec + 1                                ' Increment msec on every 1 ms
   end if
   TMR0 = 0
  INTCON = 0x20                    ' Clear the TMR0IF flag  Enable TMRO interrupt

end sub

sub function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
                              ' Previous pin state value

  tmp  = 0                                    ' Clear temporary variable
  msec = 0

  while TestBit(port, pin) = state            ' Wait for pulses

  wend                                        ' Note that the program will stop here
                                              '   if there are no pulses

                                   ' Note that the program will stop here
    while TestBit(port, pin) <> state           ' Wait for rising (falling) edge

    wend                                           '   if there are no pulses

                                ' Clear number of ms
  old  = TestBit(port, pin)                   ' Get current pin state

                                              '   if there are no pulses
  while duration > msec                       ' Check if duration is reached
     if (TestBit(port, pin)) <> old then     ' Check if pin state has changed

       tmp = tmp + 1

        old = TestBit(port, pin)              ' Get current pin state
     end if
  wend

    result = (tmp >> 1) + 1                   ' Assign result value
end sub

main:
  OPTION_REG = 2                           ' Assign prescaler to TMR0 (1:8)

  PORTB = 0xFF
  TRISB = %0000000
  ANSEL = 0
  ANSELH = 0
  PORTD = 0
  TRISD = %1000000


   Lcd_Init()                      ' LCD display initialization
  Lcd_Cmd(_LCD_CURSOR_OFF)        ' LCD command (cursor off)
  Lcd_Cmd(_LCD_CLEAR)             ' LCD command (clear LCD)

   text = "Puls meting"       ' Define the first message
  Lcd_Out(1,1,text)               ' Write the first message in the first line
   text = "Tijd : "       ' Define the first message
  Lcd_Out(2,1,text)               ' Write the first message in the first line


    while 1
  counter = 0
  TMR0  = 0
  INTCON = 0xA0                                ' Enable TMRO interrupt

 msec = 0                                    ' Clear msec
 num_impulse = 0                             ' Clear num_impulse


  CM1CON0.C1ON  = 0                                ' Comparators off
  CM2CON0.C2ON = 0



  num_impulse = Pulse_In(PORTD, 0, 1, 1000)   ' Count HI pulses on PORTD.0, for 1000ms

  WordToStr(num_impulse, txt)                 ' Convert results to string for LCD output
   'WordToStr(num_impulse, txt)                 ' Convert results to string for LCD output
  Lcd_Out(2,6, txt)               ' Write the first message in the first line Lcd_Out(2, 1, txt)

  wend                                        ' Wait forever (stop)
end.

marcel n
Posts: 6
Joined: 26 Jul 2011 21:48

Re: Pulse-In function example

#23 Post by marcel n » 14 Aug 2011 19:50

hi i have use you example and make it to use for the pic16F887 , whit the easypic5 and mb5.1 .
its not counting the time between the pulses because if i change my stick of my receiver nothing is gone change .
here is the code :

Code: Select all

'(*
' * Project name:
'
' * Copyright:
'     (c) mikroElektronika, 2006
' * Revision History:
'     20050914 (MJ):
'       - initial release.
' * Description:
'     This code demonstrates using Pulse-In Function for counting pulses in given
'     time period. This a blocking call. This demo waits for input signal
'     (high level) to start counting pulses. It counts pulses for 1000 miliseconds.
'     Then it prints results on LCD which is connected on PORTB.
' * Test configuration:
'     MCU:             P16F887
'     Dev.Board:       EasyPIC5
'     Oscillator:      HS, 8.000 MHz
'module pulsein_demo.mbas
'     SW:              mikroBasic v5.0 or higher
' * NOTES:
'     Prototype:
'       function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
'
'     PORT  - assigns port for pulse-in function
'     PIN   - assigns pin for pulse-in function
'     STATE - defines logical state for counting pulses (LOW = 0 or HIGH = 1)
'     DURATION - Time in ms during which the pulses are being count
'
'     This function returns number of pulses in given time (DURATION), on given
'       PORT and PIN.
'     Tested on EasyPIC3 with P16F877A. With few modification it can be used
'       with almost every PIC MCU.
' *)
program pulsein_demo

dim LCD_RS as sbit  at RB4_bit    ' Lcd module connections
    LCD_EN as sbit  at RB5_bit
    LCD_D4 as sbit  at RB0_bit
    LCD_D5 as sbit  at RB1_bit
    LCD_D6 as sbit  at RB2_bit
    LCD_D7 as sbit  at RB3_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit         ' End Lcd module connections

dim counter,
    msec,
    num_impulse as word
    txt         as char[10]

sub procedure interrupt
   inc(counter)                               ' Increment value of counter on every interrupt
   if counter = 1 then                        ' Has 1ms elapsed?
     counter = 0                              ' Clear the counter
     inc(msec)                                ' Increment msec on every 1 ms
   end if
   TMR0 = 5
   ClearBit(INTCON, T0IF)                     ' Clear the TMR0IF flag
   SetBit(INTCON, T0IE)                       ' Enable TMR0 interrupt
end sub

sub function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
dim tmp as word                               ' Temporary variable
    old as byte                               ' Previous pin state value

  while TestBit(port, pin) = state            ' Wait for pulses
  wend                                        ' Note that the program will stop here
                                              '   if there are no pulses

  while TestBit(port, pin) <> state           ' Wait for rising (falling) edge
  wend                                        ' Note that the program will stop here
                                              '   if there are no pulses

  tmp  = 0                                    ' Clear temporary variable
  msec = 0                                    ' Clear number of ms
  old  = TestBit(port, pin)                   ' Get current pin state

  while duration > msec                       ' Check if duration is reached
     if (TestBit(port, pin)) <> old then      ' Check if pin state has changed
        inc(tmp)                              ' Increment number of pulses
        old = TestBit(port, pin)              ' Get current pin state
     end if
  wend

    result = (tmp >> 1) + 1                   ' Assign result value
end sub

main:
  OPTION_REG =  1                             ' Assign prescaler to TMR0 (1:8)
  TRISB  = $00                                ' Designate PORTB as output
  PORTB  = $FF                                ' Initialize PORTB

  TRISD  = $FF                                ' Designate PORTD as input (for signal)

  counter= $00                                ' Initialize the counter
  TMR0  = 5
  INTCON = $A0                                ' Enable TMRO interrupt
  'ANSEL = 0
  ANSELH = 0
  msec = 0                                    ' Clear msec
  num_impulse = 0                             ' Clear num_impulse

  ADCON1 = $FF                                ' PORTB all digital
  CM1CON0 = $06                                ' Comparators off
  CM2CON0 = $06
  CM2CON1 = $06
  
  Lcd_Init()                             ' Initialize LCD connected to PORTB
  Lcd_Cmd(_LCD_CURSOR_OFF)                          ' Send command to LCD "clear display"
  Lcd_Cmd(_LCD_CLEAR)                     ' Send command "cursor off"


  Lcd_Out(1, 1, "Waiting...      ")           ' Print txt to LCD
  num_impulse = Pulse_In(PORTD, 0, 1, 1000)   ' Count HI pulses on PORTD.0, for 1000ms
  WordToStr(num_impulse, txt)                 ' Convert results to string for LCD output
  Lcd_Out(1, 1, "Number of pulses")           ' Print txt to LCD
  Lcd_Out(2, 1, txt)                          ' Print number of pulses to LCD
  while 1 = 1
  wend                                        ' Wait forever (stop)
end.
its a good example but to measuring the time of the pulses of rc receivers the time of 1ms a time is to big because
1ms - 1.5ms - 2ms the max of the difference of the pulses is 1 ms that he max can display but not between them
how can this so be make that the counting of ms becomes of 100us thank you for your help

cybercarvalho
Posts: 5
Joined: 09 Apr 2011 01:35

Re: Pulse-In function example

#24 Post by cybercarvalho » 23 Nov 2017 13:12

Good morning, Marko.
I'm trying to download the PulseIn example, but it's giving error.
Can you help me?
I'll be very grateful!!
Thank you very much

FranzW
Posts: 1
Joined: 19 Mar 2018 00:05

Re: Pulse-In function example

#25 Post by FranzW » 19 Mar 2018 00:45

Hello,
The address to Download pulsein.zip doesn't exist any more. Would you please tell me, from where could I download pulsein.zip? Does a similar function for MikroC exist?
Regards,
Franz

Post Reply

Return to “mikroBasic General”