CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How can I simulate "count" in pic basic pro with C

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

How can I simulate "count" in pic basic pro with C
PostPosted: Tue Aug 28, 2007 6:07 am     Reply with quote

Hi all

How can I simulate "count" in pic basic pro with CCS.
I need a source code for count the puls in the specific pin.
Do you get me a program with TMR0 (for example in 16f84a)?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 9:57 pm     Reply with quote

Tell us the purpose of your program. There may be a better way to
do it than duplicating the PBP Count function. Give some details about
the project and the input signal.
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 11:42 am     Reply with quote

My input signal is pulse (around 10KHz to 100KHz) and I need a program for measuring this frequency.This program must very exact.
for example:when the original pulse frequency is 15KHz I want this frequency counter represent exactly 15KH .(Not 14.9KH or Not15.1KH)
Do you help me?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 12:05 pm     Reply with quote

This thread shows how to use the CCP to make a tachometer.
http://www.ccsinfo.com/forum/viewtopic.php?t=29963

100 KHz has a 10 us period. That's too fast to use with interrupts
for most PIC oscillator frequencies. So, you would need to use a
divisor on the CCP input. Probably use CCP_CAPTURE_DIV_4,
or CCP_CAPTURE_DIV_16, which would increase the interrupt
interal to 40 us or to 160 us. It would depend upon your PIC oscillator
frequency. You may have to use a 24-bit timer (Timer1 with 8-bit
software extension). This is described in the link above.
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 2:24 pm     Reply with quote

Thanks for your help but this part, is a article of PBP HELP

Quote:
count function can be determined that the highest frequency of pulses that can be counted is 25KHz with a 4MHz oscillator and 125KHz with a 20MHz oscillator


we must pay attention to "count fonction" that used TMR0
Now what's your idea?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 2:32 pm     Reply with quote

If you want to use the Count function, then compile a small test program
in PBPro, and look at the .LST file in the PBPro project directory.
It will have commented listing of the MPASM code that's used to create
the Count function. I did this, and the code is posted below.

Next, learn how to read ASM code, and translate it to CCS C source code.
Here's an example where someone did this for the POT command:
http://www.ccsinfo.com/forum/viewtopic.php?t=27191

I don't want to do the translation. I don't have the time. I did the Button
comand and posted it in the Code Library. I don't have time to do any
more of them.
Code:

; PicBasicPro COUNT command list file   

        ORG 0                        ; Reset vector at 0
                LIST
        goto    INIT            ; Finish initialization
                LIST
COUNTT  movwf   RM1             ; Save bit mask
        call    INPUTT          ; Set pin to input
        bcf     FSR, 7          ; Point back to port
        movlw   COUNT_DELAY     ; Calc count time based on OSC
        movwf   R3
        movlw   (COUNT_DELAY) >> 8
        movwf   R3 + 1
        call    MUL        ; Time is R0, R2 + 1, R2 (hi to lo)
        incf    R0, F           ; Bump up high and mid for dec
        incf    R2 + 1, F
        clrf    R1              ; Zero counter
        clrf    R1 + 1
        movf    INDF, W         ; Read pin
        andwf   RM1, W          ; Isolate it
        movwf   R3              ; Save starting state as last

        countloop CLRWDT?NOP       ; 1 (20) Keep Watchdog happy
  M     ifndef NO_CLRWDT
  M         clrwdt
  M     else
  M         nop
  M     endif

        movf    INDF, W         ; 1 Read pin
        andwf   RM1, W          ; 1 Isolate it
        movwf   R3 + 1          ; 1 Save state
        xorwf   R3, W           ; 1 Compare with last time
        andwf   R3 + 1, W       ; 1 Only count high states
        xorwf   RM1, W          ; 1 Flip for next test
        btfsc   STATUS, Z       ; 1 / 2
        incf    R1, F           ; 1 / 0 Count pulse
        btfsc   STATUS, Z       ; 1 / 2
        incf    R1 + 1, F       ; 1 / 0
        movf    R3 + 1, W       ; 1 Set new last state
        movwf   R3              ; 1
        decf    R2, F           ; 1 Count time
        btfsc   STATUS, Z       ; 1 / 2
        decf    R2 + 1, F       ; 1 / 0
        btfsc   STATUS, Z       ; 1 / 2
        decfsz  R0, F           ; 1 / 2
        goto    countloop       ; 2 / 0
        movf    R1, W           ; Result to W
        goto    DONE

INPUTT  bsf     FSR, 7          ;Point to register bank 1
        iorwf   INDF, F         ; Set bit to input
        goto    DONE
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 11:49 pm     Reply with quote

Can you tell me :How many cycle(s) last each statement ?(1cycle ,2 cycle or...)
IF,FOR(),WHILE ,BREAK,CONTINUE,I++

Thanks for reply
anestho



Joined: 27 Dec 2006
Posts: 28

View user's profile Send private message

PostPosted: Thu Aug 30, 2007 6:46 pm     Reply with quote

I think that branching and goto's require 2 cycles, while everything else is 1 cycle.

Cycles can only apply to ASM not C. An "if" statement may be 3 ASM instructions or 10, depending on the conditional statement. If you want to know, compile a test code, and look at the .lst file to see how many instructions it takes in ASM.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group