Count RB0... on RB7


Closed Thread
Results 1 to 23 of 23

Hybrid View

  1. #1

    Smile Count RB0... on RB7

    Hello to all,

    I have a pic16f628 and I want to count 10 pulses in RB0 for when ends RB7 on led, they can give me some example.

    Thank you

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    here's one method using interrupt
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=%00000001     ' Set RB0 to input, others to output
        
        OPTION_REG=0        ' Enable internal pull-up on PORTB
                            ' Interrupt on falling edge of RB0
                             
        INTCON = %10010000  ' Enable global interrupt
                            ' Enable RB0 interrupt
        
        LED    VAR PORTB.7  ' connected between RB7 and GND via resistor
        ClkIn  var PORTB.0  ' connected between RB0 and GND
        
        INTF   var INTCON.1 ' RB0 Interrupt flag 
        Apulse VAR BYTE     ' store the amount of pulse
        
        on interrupt goto CountPulses
    
        led=0
        apulse=0
    
    start:
        if apulse<10 then start 
            '
            '   Here we got the 10 pulses
                    '                        
        INTCON = 0  ' disable all interrupt
        led=1       ' enable the LED
                    '
    Z:  goto z      ' stay here 'till next system reboot
            
    
    disable
        '   //////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                                     CountPulses:
        '   //////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
        '                                 
        '   Here is where the program jump every time the push-buton on
        '   RB0 is press.    
        '
        apulse=apulse+1       ' add a count
        while clkin=0 : wend  ' wait untill the push button is release
        pause 50              ' debounce time
        intf=0                ' reset interupt flag
        resume                ' Get out of here
    enable                    ' re-enable interrupt
    without interupts but a loop.
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=%00000001     ' Set RB0 to input, others to output
        
        OPTION_REG.7=0      ' Enable internal pull-up on PORTB
                               
        LED    VAR PORTB.7  ' connected between RB7 and GND via resistor
        ClkIn  var PORTB.0  ' connected between RB0 and GND
        
        Apulse VAR BYTE     ' store the amount of pulse
        
        led=0
        apulse=0
    
        repeat
            if clkin=0 then
                while clkin=0 : wend
                pause 50
                apulse=apulse+1
                endif
            until apulse=10
        led=1
    
    Here:
        goto here
    Using internal counter(TMR0) and polling TMR0 overflow flag and RA.4 as input instead of RB0
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=0              ' Set output to all i/o
        TRISA=255            ' set input to all
        
        OPTION_REG=%11111000 ' TMR0 clock source = RA4
                             ' increment counter on high to low transistion
                             ' prescaller assign to WDT
                             ' prescaller rate 1:1
                              
        CMCON=7              ' disable analog comparator
        
        INTCON = %00100000   ' enable TMR0 overflow interrupt 
                               
        LED    VAR PORTB.7   ' connected between RB7 and GND via resistor
        T0IF   var INTCON.2  ' TMR0 Interrupt flag   
    
        TMR0=246             ' pre-load TMR0 to have an Interrupt after 10 pulses
                             '     TMR0 is a 8 byte Register so 246+10=256=interrupt    
        led=0
        
        WHILE T0IF=0 : WEND
        LED=1
    Z:  GOTO Z
    Almost the same as above but polling only the TMR0 register
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=0              ' Set output to all i/o
        TRISA=255            ' set input to all
        
        OPTION_REG=%11111000 ' TMR0 clock source = RA4
                             ' increment counter on high to low transistion
                             ' prescaller assign to WDT
                             ' prescaller rate 1:1
                              
        CMCON=7              ' disable analog comparator
        
        INTCON = %00100000   ' enable TMR0 overflow interrupt 
                               
        LED    VAR PORTB.7   ' connected between RB7 and GND via resistor
    
        TMR0=0
        led=0
        
        WHILE TMR0<10 : WEND
        LED=1
    Z:  GOTO Z
    I prefer the last one, produce really tight code and easy to understand BUT if the pulse come from a PUSH-Button it can glow the LED before 10 human press as it don't provide any debounce solution. BUT if you add a 0.1-1uF in parrallel, it seems to be enough to do the job. Don't forget the pull-up resistor on the RA.4 pin. Let's say 10K as a standard value

    That should be more than enough to start
    Last edited by mister_e; - 10th September 2005 at 20:18.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3


    Did you find this post helpful? Yes | No

    Smile

    Thank you for your orientation






    Quote Originally Posted by mister_e
    here's one method using interrupt
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=%00000001     ' Set RB0 to input, others to output
        
        OPTION_REG=0        ' Enable internal pull-up on PORTB
                            ' Interrupt on falling edge of RB0
                             
        INTCON = %10010000  ' Enable global interrupt
                            ' Enable RB0 interrupt
        
        LED    VAR PORTB.7  ' connected between RB7 and GND via resistor
        ClkIn  var PORTB.0  ' connected between RB0 and GND
        
        INTF   var INTCON.1 ' RB0 Interrupt flag 
        Apulse VAR BYTE     ' store the amount of pulse
        
        on interrupt goto CountPulses
    
        led=0
        apulse=0
    
    start:
        if apulse<10 then start 
            '
            '   Here we got the 10 pulses
                    '                        
        INTCON = 0  ' disable all interrupt
        led=1       ' enable the LED
                    '
    Z:  goto z      ' stay here 'till next system reboot
            
    
    disable
        '   //////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                                     CountPulses:
        '   //////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
        '                                 
        '   Here is where the program jump every time the push-buton on
        '   RB0 is press.    
        '
        apulse=apulse+1       ' add a count
        while clkin=0 : wend  ' wait untill the push button is release
        pause 50              ' debounce time
        intf=0                ' reset interupt flag
        resume                ' Get out of here
    enable                    ' re-enable interrupt
    without interupts but a loop.
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=%00000001     ' Set RB0 to input, others to output
        
        OPTION_REG.7=0      ' Enable internal pull-up on PORTB
                               
        LED    VAR PORTB.7  ' connected between RB7 and GND via resistor
        ClkIn  var PORTB.0  ' connected between RB0 and GND
        
        Apulse VAR BYTE     ' store the amount of pulse
        
        led=0
        apulse=0
    
        repeat
            if clkin=0 then
                while clkin=0 : wend
                pause 50
                apulse=apulse+1
                endif
            until apulse=10
        led=1
    
    Here:
        goto here
    Using internal counter(TMR0) and polling TMR0 overflow flag and RA.4 as input instead of RB0
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=0              ' Set output to all i/o
        TRISA=255            ' set input to all
        
        OPTION_REG=%11111000 ' TMR0 clock source = RA4
                             ' increment counter on high to low transistion
                             ' prescaller assign to WDT
                             ' prescaller rate 1:1
                              
        CMCON=7              ' disable analog comparator
        
        INTCON = %00100000   ' enable TMR0 overflow interrupt 
                               
        LED    VAR PORTB.7   ' connected between RB7 and GND via resistor
        T0IF   var INTCON.2  ' TMR0 Interrupt flag   
    
        TMR0=246             ' pre-load TMR0 to have an Interrupt after 10 pulses
                             '     TMR0 is a 8 byte Register so 246+10=256=interrupt    
        led=0
        
        WHILE T0IF=0 : WEND
        LED=1
    Z:  GOTO Z
    Almost the same as above but polling only the TMR0 register
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=0              ' Set output to all i/o
        TRISA=255            ' set input to all
        
        OPTION_REG=%11111000 ' TMR0 clock source = RA4
                             ' increment counter on high to low transistion
                             ' prescaller assign to WDT
                             ' prescaller rate 1:1
                              
        CMCON=7              ' disable analog comparator
        
        INTCON = %00100000   ' enable TMR0 overflow interrupt 
                               
        LED    VAR PORTB.7   ' connected between RB7 and GND via resistor
    
        TMR0=0
        led=0
        
        WHILE TMR0<10 : WEND
        LED=1
    Z:  GOTO Z
    I prefer the last one, produce really tight code and easy to understand BUT if the pulse come from a PUSH-Button it can glow the LED before 10 human press as it don't provide any debounce solution. BUT if you add a 0.1-1uF in parrallel, it seems to be enough to do the job. Don't forget the pull-up resistor on the RA.4 pin. Let's say 10K as a standard value

    That should be more than enough to start

  4. #4


    Did you find this post helpful? Yes | No

    Smile

    Hello mister e

    As you it can store the I number of pulses in the eeprom to continue the count if the pic is off?.



    Quote Originally Posted by Leonardo
    Thank you for your orientation

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    to prevent adding any additional hardware to save count result when you remove the power, use the following
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=%00000001     ' Set RB0 to input, others to output
        
        OPTION_REG.7=0      ' Enable internal pull-up on PORTB
                               
        LED    VAR PORTB.7  ' connected between RB7 and GND via resistor
        ClkIn  var PORTB.0  ' connected between RB0 and GND
        
        Apulse VAR BYTE     ' store the amount of pulse
        Data @0,0 ' set count @ 0 at programming time
        
        led=0
        Read 0, Apulse ' Read count from internal eeprom
    
        repeat
            if clkin=0 then
                while clkin=0 : wend
                pause 50
                apulse=apulse+1
                WRITE 0,apulse ' save count in the internal EEPROM
                endif
            until apulse=10
        led=1
    
    Here:
        goto here
    this should work
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6


    Did you find this post helpful? Yes | No

    Smile

    Hello mister e,

    Excellent the code but like you reset the eeprom from some pin?.

    Thanks


    Quote Originally Posted by mister_e
    to prevent adding any additional hardware to save count result when you remove the power, use the following
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _LVP_OFF & _BODEN_ON 
    
        TRISB=%00000001     ' Set RB0 to input, others to output
        
        OPTION_REG.7=0      ' Enable internal pull-up on PORTB
                               
        LED    VAR PORTB.7  ' connected between RB7 and GND via resistor
        ClkIn  var PORTB.0  ' connected between RB0 and GND
        
        Apulse VAR BYTE     ' store the amount of pulse
        Data @0,0 ' set count @ 0 at programming time
        
        led=0
        Read 0, Apulse ' Read count from internal eeprom
    
        repeat
            if clkin=0 then
                while clkin=0 : wend
                pause 50
                apulse=apulse+1
                WRITE 0,apulse ' save count in the internal EEPROM
                endif
            until apulse=10
        led=1
    
    Here:
        goto here
    this should work

Similar Threads

  1. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  2. Can't get COUNT to count
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 23rd March 2009, 23:14
  3. Timer RB0...RB7 on RA1
    By Leonardo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th September 2005, 00:38
  4. Count command
    By hawk72501 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 6th September 2005, 19:04
  5. Count RB0... on RB7
    By Leonardo in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th August 2005, 23:13

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