hardware counting while software runs


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818

    Default hardware counting while software runs

    Hi All,
    Can someone tell me how to set up the PIC to count a signal or pulse in the background while a program is running? I am attempting to do the following:
    run a counter which counts down from some number - say 50, and at the same time,count an encoder pulse which will subtract from the count the software counter is counting, it is a time vs mechanical count. I have the counter set up to work properly but have no idea how to interface the hardware (encoder ?) into the counter loop, in fact I do not know how to interface the (Timers ? ) so as to allow PIC to work independantly of the Main Loop. RTFM has not been too useful so far. To complicate matters I have fallen in between ISPs so I have limited access to internet, at Mom's House, so untill I find another ISP, I have to drive over here Since good ol' % $ $ (name omitted) offered me Hi speed DSL for 17.95 which they couldn't deliver and lo speed for 35.99 and wouldn't discount.GRRRRR . . . . anyway anyone got Ideas how to do this?
    Thank You in advance,
    Joe

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Here I have a sample code for rotary encoder using two Timer modules in counter mode.

    Reference Signal of encoder is connected to Timer0, and one of pulse signals is connected to Timer1.


    Code:
    'Edit: the code is for 16F877-20Mhz.
    
    @ DEVICE PIC16F877, HS_OSC
    @ DEVICE PIC16F877, WDT_ON
    @ DEVICE PIC16F877, PROTECT_OFF
    @ DEVICE PIC16F877, CPD_OFF
    @ DEVICE PIC16F877, PWRT_ON
    @ DEVICE PIC16F877, BOD_ON
    
    
    DEFINE OSC 20
    
    '=======Add tris registers here... =========
    'I skipped the part where you need to set your registers.....
    '..
    '...
    '....
    '========Timer0 Settings - 8bit===================
    OPTION_REG = %00111000  
    ' Set 1:1 prescaler & assign to WDT
    ' Set TMR0 clock to external input on RA4/T0CKI. 
    ' Connect Rerefence signal of encoder to this pin.
    ' Increment on high-to-low transition on RA4/T0CKI pin
    
    '=====Timer1 settings  - 16Bit ====================
    T1CON = %00000111   ' 1:1 in counter mode. 
    'RC0 = Encoder input (RC0/T1OSO/T1CKI (on rising edge). 
    'Connect One of the pulse signals of encoder to this pin.)
    INTCON.7 = 0        ' Disable interrupts
    
    Encoder         var word    
    RefPoint        var word    
    RefBit          var word    
    TourNo          var word   
    
    Encoder     = 0
    TMR1L       = 0     ' Clear TMR counter
    TMR1H       = 0
    Refpoint    = 0
    Refbit      = 0
    TMR0        = 0
    Tourno      = 0
    
    
    Start:
        LCDOUT $fe,1,"Encoder:",DEC4 Encoder
        LCDOUT $fe,$c0,"Tour   :",DEC4 Tourno
        
        Encoder.highbyte  = TMR1H    'Load Lower byte of Timer1.
        Encoder.lowbyte = TMR1L      'Load Higher byte of Timer1.
     
        Refpoint = TMR0              'Load TMRO value.
    
        if Refbit + 1 = Refpoint then   ' Check: Do we have an increment?If yes, then 
            Tourno = Tourno + 1         ' Add up tour numbers.   
            TMR1H = 0                   ' Since the encoder completed one tour, clear timer1 Lower byte. 
            TMR1L = 0                   ' Since the encoder completed one tour, clear timer1 Higher byte.
            Encoder = 0                 ' Since the encoder completed one tour, clear timer1 holder, too.
        endif
    
        RefBit = Refpoint               ' Make eqaul before the next TMR0 assignment
        
        
        
    Goto start
    Last edited by sayzer; - 1st October 2006 at 14:06.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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


    Did you find this post helpful? Yes | No

    Default

    And depending wich PIC you're using you may have 1,2,3,... Timer/Counter who can work with external Clock. Be sure the PIC you select provide you at least 2 of these with external Clock Source.

    Or there's still this interrupt on something stuff.

    @Sayzer
    @ DEVICE HS_OSC,WDT_ON,PROTECT_OFF,CPD_ON,PWRT_ON,BOD_ON
    You know what i want to say now
    Steve

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

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Yeap, I forgot to say that the code above is for 16F877-20Mhz.

    Also,

    Quote Originally Posted by mister_e
    @Sayzer

    @ DEVICE HS_OSC,WDT_ON,PROTECT_OFF,CPD_ON,PWRT_ON,BOD_ON
    I know what you mean Steve! I will never forget.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default HMMMMMMMMmmmmmmm

    Thanks,
    I will take this code home and chew on it a while . . . . I am not manipulating data in an lcd, rather I am using it to control hardware.something like so:

    x var byte

    main:
    if porta.0 !=0 then
    pause50
    DoSomthing
    else
    endif
    goto main


    DoSomething:
    for x = 50 to 0 step - 1
    goto encoder
    portB.5 low
    next x
    return

    encoder:
    Portb.5 high ' to power up encoder
    some code here to count encoder subtracting code from var X
    return

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S.
    .....


    main:
    if porta.0 !=0 then
    pause50
    DoSomthing 'is a goto not a gosub
    else
    endif
    goto main


    DoSomething:
    for x = 50 to 0 step - 1
    goto encoder 'another one here
    portB.5 low
    next x
    return

    encoder:
    Portb.5 high ' to power up encoder
    some code here to count encoder subtracting code from var X
    return

    I guess this was a quick example.
    Check "goto" !
    Last edited by sayzer; - 2nd October 2006 at 03:29.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. Benefits of hardware usart over software?
    By sccoupe in forum Serial
    Replies: 2
    Last Post: - 19th March 2009, 01:16
  2. Hardware or Software
    By bmagistro in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th March 2009, 23:22
  3. Software Stack
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th December 2007, 10:04
  4. IR software & hardware basics
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 6th May 2007, 10:05
  5. Programming software for PicAll Hardware
    By bcd in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th July 2006, 08:15

Members who have read this thread : 1

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