Counting led blinks..


Closed Thread
Results 1 to 40 of 93

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi,
    You can count it like it's a button but the way you have it now means that the pulses have to come in exactly during the 60ms you're actually counting. If they start to come before you start to count you'll miss some of them or if the pusles starts 30ms after you've started to count you might stop counting before all pulses have arrived.

    Just look at the datasheet for the PIC you're using, TMR0 is probably the easiest to use. Read the section, look at the T0CON register and you'll see how to configure it as a counter. The pulses then goes to T0CKI-pin and you can access the count by reading the TMR0 register.

    Give it a try, if it doesn't work post the code (and what PIC you're using) and we'll take a look at it.

    /Henrik.

  2. #2
    Join Date
    Mar 2006
    Location
    China
    Posts
    266


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Interupt on change or even better a real INT pin. Then you can count all pulses without too much work. A good start is as always DT's instant interupts that makes it fast, fun and flexible.

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Why any real INT should be less work than any dedicated counter? No advanatage at all.

    Q: Sir We need to know when something chagend, a button press
    A: Load the counter to 255, on the next press it will generate an overflow interrupt
    Steve

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

  4. #4
    Join Date
    Mar 2006
    Location
    China
    Posts
    266


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Why? Since he already used PORTB.1 in his code an interrupt solution requires no HW change.

    And it was just a example to show an other possibility....

  5. #5
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi, Sorry for the delay. I got a working code that incorporate TMR0.
    Code:
    CLEAR DEFINE    OSC 48
    DEFINE LCD_DREG PORTB
    DEFINE LCD_DBIT 0
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 5
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 4
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50
    asm
        ;_PLLDIV_2_1L         EQU  H'F9'    ; Divide by 2 (8 MHz oscillator input)
        
        ;__CONFIG    _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L  ' 1
        __CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
        __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
        ;
        ;__CONFIG    _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L   
                                ;              ;                      ; USB clock source comes from the 96 MHz PLL divided by 2
                                ;              ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
                                ; No prescale (4 MHz oscillator input drives PLL directly)
    
    
    
    
        ;__CONFIG    _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H   '2
                                ;                  ;               ; Oscillator Switchover mode disabled
                                ;                  ; Fail-Safe Clock Monitor disabled
                                ; XT oscillator, PLL enabled, XT used by USB
        __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L  & _BORV_2_2L  & _VREGEN_ON_2L   
        __CONFIG    _CONFIG2H, _WDT_OFF_2H 
        __CONFIG    _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H 
        __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L  & _XINST_OFF_4L & _DEBUG_OFF_4L 
        endasm
    
    
    
    
    INTCON  =   100000       ' Enable global and TMR0 interrupts
    T0CON   =   000000       ' TMR0, CLK internal, prescaler 1:2, T0ON
                    
    
    
    ALPHA        VAR  WORD    ;this variable counts in the PauseUS loop
    BETA        VAR  BYTE    ;this variable counts interrupt ticks
    TRISD   = 110100    ;sets the 3 output pins in the D port
    PORTD = 000000    ;sets all pins low in the D port
                    
    ON INTERRUPT GOTO INTERUPTROUTINE    ;This line needs to be early in the program, before
                    ;the routine is called in any case.
                    
    MAINLOOP:        ;Main loop blinks D0 and D1 alternately
        IF PORTD.1 = 0 THEN    ;]
            PORTD.1 = 1    ;]
            PORTD.0 = 0    ;]  This part of the program blinks two LEDs in
        ELSE        ;]   the foreground
            PORTD.1 = 0    ;]
            PORTD.0 = 1    ;]
        ENDIF        ;]
                    
        FOR  ALPHA = 1 TO 300    ;The long pause is eliminated with this loop
            PAUSEUS 100    ;PAUSE command with short latency 
        NEXT ALPHA    ;
    GOTO MAINLOOP    ;end of loop
                    
    DISABLE    ;DISABLE and ENABLE must bracket the interrupt routine
    INTERUPTROUTINE:    ;this information is used by the compiler only.
       BETA = BETA + 1    ;
       Lcdout $fe, 128, "Digital Output"       ' Display 
       Lcdout $fe, $c0, "Counter: ", #BETA   
       IF BETA < 61 THEN ENDINTERRUPT    ;one second has not yet passed
       BETA = 0    ;    
       IF PORTD.3 = 1 THEN    ;Interrupt loop turns D3 on and off every
            PORTD.3 = 0    ;61 times through the interrupt routine.
       ELSE            ;That is about one second per full cycle
            PORTD.3 = 1    ;
       ENDIF    ;    
    ENDINTERRUPT:    ;
    INTCON.2 = 0        ;clears the interrupt flag.
    RESUME        ;resume the main program
    ENABLE        ;DISABLE and ENABLE must bracket the int routine
    END
    I used "000 = 1:2 Prescale value( I think this is the fastest)" is this the pin that should be reading the pulse input?
    I'm not really sure how to implement interrupt to the program yet, but I understand that its very important to learn this.

    regards
    tacbanon
    Last edited by tacbanon; - 30th September 2011 at 06:04.

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi,
    Not sure exactly what you're trying to do with that specific piece of code.... If you're trying to setup a timer interrupt you're on the right track but if you're trying to use TMR0 as a counter to count the pulses it's not what you want.

    When TMR0 is in timer-mode, like you have it, it counts internal instruction cycles "thru" the prescaler. If you clock the PIC at 4Mhz, one instruction cycle is 1us so the timer ticks along at 1Mhz/prescaler ratio. When it rolls over from 255 to 0 it sets the interrupt-flag.

    When TMR0 is in counter-mode it counts transitions on the T0CKI-pin instead of internal instruction cycles. Again it sets the interrupt flag when it rolls over from 255 to 0 but it may not be of interest here.

    Also, it looks like you're trying to set T0CON, INTCON, TRISD and PORTD "in binary" but there's no %-sign in front of the number. Also, try to always include all 8 bits. Like INTCON = %00100000 not INTCON=100000

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    FYI, the missing % is a new Code Box forum feature...
    Steve

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

  8. #8
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi,
    I searched in the forum and found a peice of code by Bruce...
    Code:
    DEFINE LCD_DREG PORTBDEFINE LCD_DBIT 0
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 5
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 4
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50
    PAUSE 100
    
    
    TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
    TRISB.0 = 0 ' RB0 = output for LED
    CMCON = 7   ' All digital
    
    
    i var byte
    i=0
    ' Assign prescaler to WDT for 1:1 prescale on TMR0
    ' TMR0 clock will be from your external input on RA4/T0CKI
    ' Increment on high-to-low transitions
     OPTION_REG = 111000
     
    ' If you prefer, then increment on low-to-high transitions
    ' OPTION_REG = 101000
    Lcdout $fe, 128, "Counter: ", #i 
    pause 100
    Main:
        TMR0 = 0     ' Clear TMR0 count before start
        
    Loop1:
        WHILE TMR0 = 0 ' Wait for high-to-low transition
        WEND           ' on RA4/T0CKI
        PORTB.0 = 1    ' LED on to indicate transition seen 
        i=i+1       ' Increment
        Lcdout $fe, 128, "Counter: ", #i   
        PAUSE 200
        PORTB.0 = 0    ' LED off
        GOTO Main      ' Start over
        END
    Everytime I press RA.4 it toggle RB0, I also put a variable that will display the value to the LCD, from the code I observed (please correct me if I wrong).
    a. Timer0 is selected
    b. The signal entry is RA.4
    c. It increments on low-to-high transitions
    d. Pre scale value is 1:2 (000 of Bits 2,1)
    d. The configuration setting used OPTION_REG = 111000

    So if connect the multi coin acceptor to the RA.4 this should count the number of pulse?

    thanks in advance,
    tacbanon
    Last edited by tacbanon; - 1st October 2011 at 06:01.

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi,
    Yes, that would work. I'd probably add a little pause directly after the WHILE-WEND loop to allow the coin acceptor to push out all the pulses before the code reads the timer.

    As for the prescaler, yes, the bits indicate a a 1:2 ratio but since bit 3 is set the prescaler isn't assigned to TMR0 but to the WDT so you get a 1:1 ratio which is what you want in this case.

    Steve,
    Thanks for the heads-up on the % character, great feature... Must try/verify:
    Code:
    This is a test, percent char %00110011
    This is a test, no percent char 00110011
    And here's how my post looks in preview:

    Name:  Preview.jpg
Views: 1912
Size:  28.2 KB
    Looks fine in the preview. (Appologies to tachbanon for trying this here).
    EDIT: And in the final post, I don't see a problem... Is it when there's a = infront?
    Code:
    OPTION_REG = %00110011
    Again, looks fine in the preview.
    EDIT: And in the final post.

    /Henrik.
    Last edited by HenrikOlsson; - 1st October 2011 at 07:13.

  10. #10
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi, now I'm trying to incorporate the coin acceptor, but I observered that when I connect PULSE line to RA4..it continues to count, even if the device is turned off or if I touched the wire. Please see the attachment, I'm using easypic6. What do you think causing it?


    regards,
    tacbanon
    Attached Images Attached Images  

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