Counting led blinks..


Closed Thread
Results 1 to 40 of 93

Hybrid View

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

    Default Counting led blinks..

    Hi everyone, I have a coin acceptor that gives out pulse. Coins used are P1, P5 and P10. When I drop P1 it gives 1 blink to my led status, P5 it gives 5 blinks and P10 gives 10 blinks. I want to display the corresponding blinks on the LCD. I tried the following code and connected the .
    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
    Define OSC 20
    ADCON1 = 7 'Need this to turn off ADC   
    
    TRISB = 000011 'Pins A are in input mode
    PortB = 0
    W1 var byte
    Clear 
    
    
    pause 100
    Lcdout $fe, 1        ' Clear screen
    Lcdout "Blink Counter"       ' Display "Test Counter"
    Init:
    COUNT PORTB.1, 60, W1
    Lcdout $fe, 1        ' Clear screen
       Lcdout "Digital Designs"       
       Lcdout $fe, $c0, "Read: " , #W1
       pause 1000
    GOTO Init 'Do it always
    But it displays erratic values. Can anyone guide me on this?
    Diagram
    Name:  diagram1.gif
Views: 2226
Size:  4.9 KB

    regards,
    tacbanon
    Last edited by tacbanon; - 23rd September 2011 at 16:39.

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    Hi,
    How fast does the LED blink? You're only counting for 60ms so you need to insert the coin at exactly the right time - every time - for it be consistent.
    I'd probably do this by setting up one of the PICs timers as a counter and wire the pulse signal to its input.

    EDIT: Or something like:
    Code:
    Init:
      While PortB.1 = 0 : WEND
      Count PortB.1, 60, W1
      Count = Count + 1
      'LCDOUT......
      '.....
    Goto Init

    /Henrik.
    Last edited by HenrikOlsson; - 23rd September 2011 at 18:21.

  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..

    Timer/Counter... motion seconded
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default Re: Counting led blinks..

    @Henrik
    Hi sorry for the delay, I can not tell exactly how fast it is. Can you give me a very simple example or a link that will help me out, I'm new with timers/counter. I was thinking it will be easy if I can count it like it is a button pressed..then just increment a variable counter.

    regards,
    tacbanon

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    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.

  6. #6
    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.

  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..

    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.

  8. #8
    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.

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


    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.

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