1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Counting pulses at Timer0

Discussion in 'Homework Help' started by Shen Yi, Dec 29, 2015.

  1. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    Count the number of pulses fed into the T0CKI pin, display the count on PORTB, and set the
    RA3 bit when the count reaches the decimal value of 100. The count will be continuously displayed on PORTB after setting the RA3 bit.

    using ASSEMBLY language
    example:
    movlw counter
    movwf PORTB
    .......
     
  2. MaxHeadRoom78

    MaxHeadRoom78 Active Member

    Joined:
    Jun 9, 2015
    Messages:
    881
    Likes:
    70
    Location:
    Canada Geog Cen of N.A.
    ONLINE
    What PIC?
    You can adapt this.
    Max.

    Code (text):
     
    ;=======CCP_Tach.ASM====================================4/Sept/2015==
    ;
            list p=PIC18f23K22        ;list directive to define processor
            #include <p18f23K22.inc>    ;processor specific definitions


         ;Setup CONFIG1H
           CONFIG FOSC = HSHP, PLLCFG = OFF, PRICLKEN = OFF, FCMEN = OFF, IESO = OFF
         ;Setup CONFIG2L
         CONFIG PWRTEN = OFF, BOREN = OFF, BORV = 190
         ;Setup CONFIG2H
         CONFIG WDTEN = OFF, WDTPS = 1
         ;Setup CONFIG3H
         CONFIG MCLRE = EXTMCLR, CCP2MX = PORTB3, CCP3MX = PORTC6, HFOFST = OFF, T3CMX = PORTB5, P2BMX = PORTC0
         ;Setup CONFIG4L
         CONFIG STVREN = OFF, LVP = OFF, XINST = OFF
         ;Setup CONFIG5L
         CONFIG CP0 = OFF, CP1 = OFF             ;CP2 = OFF, CP3 = OFF
         ;Setup CONFIG5H
         CONFIG CPB = OFF, CPD = OFF
         ;Setup CONFIG6L
         CONFIG WRT0 = OFF, WRT1 = OFF            ;, WRT2 = OFF, WRT3 = OFF
         ;Setup CONFIG6H
         CONFIG WRTB = OFF, WRTC = OFF, WRTD = OFF
         ;Setup CONFIG7L
         CONFIG EBTR0 = OFF, EBTR1 = OFF        ;, EBTR2 = OFF, EBTR3 = OFF
         ;Setup CONFIG7H
         CONFIG EBTRB = OFF
           
    ;------------------------------------------------------------
    ; ------------------------------------------------------------
    ;------------------------------------------------------------
     ;This code executes when a reset occurs.

            ORG     0x0000        ;place code at reset vector

    ResetCode:
            bra    Start        ;go to beginning of program

    ;----------------------------------------------------------------------------
    ;This code executes when a high priority interrupt occurs.

            ORG    0x0008        ;place code at interrupt vector


    HighInt:
            bra    HighIntCode    ;go to high priority interrupt routine

    ;----------------------------------------------------------------------------
    ;This code executes when a low priority interrupt occurs.

            ORG    0x0018        ;place code at interrupt vector

    LoIntCode:  
                goto iserv ;do interrupts here

            reset            ;error if no valid interrupt so reset
    ;---------------------------------------------------------
    ;High priority interrupt routine.

    HighIntCode:
                retfie
    ;
    Start:
                clrf    ANSELA
                clrf    ANSELB
                clrf    ANSELC
            ;    movlw    b'00000110' ;turn off A/D, port A
            ;    movwf    ADCON1
                movlw   b'00000000' ;port B outputs
               movwf    TRISB
                bsf        TRISC, 0     ;port C, bit 0 timer 1 input
                bsf        TRISC, CCP1  ;CCP1 pin input
            ;    movlw   b'00000000' ;port B lines low
                clrf     PORTB
                bcf        INTCON, GIE    ;disable global interrupts
                bcf        INTCON, PEIE    ;disable peripheral interrupts
                bcf        PIE1,TMR1IE    ;disable TMR1 interrupt
                bcf        PIE1,  CCP1IE    ;disable CCP1 interrupt
                bcf        PIR1, CCP1IF    ;clear CCP1 interrupt flag
                clrf    CCP1CON       ;CCP1 module off, clear CCP1 prescaler
                movlw    b'10000000' ;TMR1 prescaler and TMR1 setup,
                movwf    T1CON       ;  and TMR1 off
                clrf    TMR1H    ;clear timer 1 high
                clrf    TMR1L    ;clear timer 1 low, clear prescaler
                clrf    CCPR1H    ;clear capture register high
                clrf    CCPR1L    ;clear capture register low
                movlw    b'00000101' ;CCP1 prescaler mode,
                movwf    CCP1CON       ; CCP1 capture mode, CCP1 on
                bsf        INTCON, GIE    ;enable global interrupts
                bsf        INTCON, PEIE    ;enable peripheral interrupts
                bsf        T1CON, 0    ;timer 1 on
    circle:
                goto    circle  ;done
    ;
    iserv:
                bcf        PIR1, CCP1IF    ;clear ccp1 interrupt flag,
                        ;   enable further interrupts
                movf    CCPR1L, W    ;read capture register low
                movwf    PORTB    ;display captured count at port B
                retfie        ;return from interrupt
        ;
            end
    ;------------------------------------------------------------
    ;at device programming time, select:
    ;       memory unprotected
    ;       watchdog timer disabled (default is enabled)
    ;       standard crystal XT (using 4 MHz osc for test)
    ;       power-up timer on
    ;    brown-out reset enabled
    ;    lvp disabled
    ;    debug mode disabled
    ;============================================================


     
     
  3. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,888
    Likes:
    500
    Location:
    Cleveland, OH, USA
    I don't believe the 16F84A has a CCP module. Edit: Oops, I connected Shen Li's two threads. He is using a 16F84A in the other thread. My assumption could be wrong.

    You (Shen Yi) need to read Section 5 of the datasheet for that processor. The way you get an interrupt at some set point is to load the counter with (256 - <desired count>). Thus, the counter will rollover and create an interrupt after only the desired number of counts (e.g., 100) instead of 256 counts.

    You will need to do the calculations for actual times, which may include considering a pre-scale, e.g, a prescale of 1:8 means that 8 signals only create one count on TMR0.

    John
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0

    using PIC16F84A sorry forgot to mention, n btw some part of the code you just gave me i dont understand such as the PIR1, CPP1IF, CCP1CON stuff like dat (and many more)
    but thx though
     
  6. MaxHeadRoom78

    MaxHeadRoom78 Active Member

    Joined:
    Jun 9, 2015
    Messages:
    881
    Likes:
    70
    Location:
    Canada Geog Cen of N.A.
    ONLINE
    Here for an older version.
    Max.
    Code (text):
     
    ;=======CAPT.ASM=====================================8/7/98==
            list    p=16c63
            radix   hex
    ;------------------------------------------------------------
    ;timer 1 and ccp1 module - capture mode demo
    ;------------------------------------------------------------
    ;       cpu equates (memory map)

    ;------------------------------------------------------------
    ;    bit equates
    rp0    equ    5
    ccp1    equ    2    ;ccp1 bit 2, port C
    ;------------------------------------------------------------
            org     0x000
        goto    start    ;skip over location pointed to by
                ;   interrupt vector
        org    0x004
        goto    iserv
    ;
    start:
        bsf    status,rp0  ;switch to bank 1
        movlw   b'00000000' ;port B outputs
            movwf    trisb
        bsf    trisc,0     ;port C, bit 0 timer 1 input
        bsf    trisc,ccp1  ;ccp1 pin input
        bcf    status,rp0  ;switch back to bank 0
        movlw   b'00000000' ;port B lines low
        movwf    portb
        bcf    intcon,7    ;disable global interrupts
        bcf    intcon,6    ;disable peripheral interrupts
        bsf    status,rp0  ;bank 1
        bcf    pie1,0    ;disable tmr1 interrupt
        bcf    pie1,2    ;disable ccp1 interrupt
        bcf    status,rp0  ;bank 0
        bcf    pir1,2    ;clear ccp1 interrupt flag
        clrf    ccp1con       ;ccp1 module off, clear ccp1 prescaler
        movlw    b'00000010' ;tmr1 prescaler and tmr1 setup,
        movwf    t1con       ;   tmr1 off
        clrf    tmr1h    ;clear timer 1 high
        clrf    tmr1l    ;clear timer 1 low, clear prescaler
        clrf    ccpr1h    ;clear capture register high
        clrf    ccpr1l    ;clear capture register low
        movlw    b'00000101' ;ccp1 prescaler mode,
        movwf    ccp1con       ;   ccp1 capture mode, ccp1 on
        bsf    intcon,7    ;enable global interrupts
        bsf    intcon,6    ;enable peripheral interrupts
        bsf    status,rp0  ;bank 1
        bsf    pie1,2    ;enable ccp1 interrupt
        bcf    status,rp0  ;bank 0
        bsf    t1con,0    ;timer 1 on
    circle:
      goto    circle  ;done
    ;
    iserv:
        bcf    pir1,2    ;clear ccp1 interrupt flag,
                ;   enable further interrupts
        movf    ccpr1l,w    ;read capture register low
        movwf    portb    ;display captured count at port B
        retfie        ;return from interrupt
    ;
            end
    ;------------------------------------------------------------

     
     
  7. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    Max sorry but it is PIC16F84A so i cant use your codes, would be great if you can help
     
  8. MaxHeadRoom78

    MaxHeadRoom78 Active Member

    Joined:
    Jun 9, 2015
    Messages:
    881
    Likes:
    70
    Location:
    Canada Geog Cen of N.A.
    ONLINE
    Learn to adapt it, very simple!
    Max.
     
  9. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    sorry max, thx for the effort but too many things are completely different so i dont understand most parts of your code, FYI i am a complete beginner in programming, 1st time learning this subject
     
  10. MaxHeadRoom78

    MaxHeadRoom78 Active Member

    Joined:
    Jun 9, 2015
    Messages:
    881
    Likes:
    70
    Location:
    Canada Geog Cen of N.A.
    ONLINE
    This is a very simple/easy program, this is the way you learn.
    If you just use off the shelf code then there is no learning achieved.
    Max.
     
  11. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,888
    Likes:
    500
    Location:
    Cleveland, OH, USA
    Then read the datasheet. Everything you need to know is in the section I mentioned. Of course, we haven't established whether you want to address your ignorance of writing code or are satisfied letting someone else write it for you.

    John
     
  12. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    sorry john but i desperately need the code as soon as possible and dont think i can complete my assignment even if i burn the midnight oil as i am doing right now
     
    • Informative Informative x 1
  13. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,888
    Likes:
    500
    Location:
    Cleveland, OH, USA
    I guess you have answered my question. Good luck.

    John
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    Ooops!!!

    upload_2015-12-29_18-42-9.png
     
  15. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    john i am desperate because i need the code to be complete in less than 12hours from now so am really sorry if i offended you for not doing my own work properly
     

Share This Page